Skip to content

Commit

Permalink
Merge pull request #436 from M2K3K5/fix-settings-navigation
Browse files Browse the repository at this point in the history
fix: update selected tab on the settings page based on URL path on page load
  • Loading branch information
Yooooomi authored Nov 24, 2024
2 parents 7b62414 + 77474db commit 7620569
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions apps/client/src/components/ButtonsHeader/ButtonsHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Tab, Tabs } from "@mui/material";
import { useCallback, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useCallback, useMemo } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import Text from "../Text";
import s from "./index.module.css";

Expand All @@ -15,7 +15,17 @@ interface ButtonsHeaderProps {

export default function ButtonsHeader({ items }: ButtonsHeaderProps) {
const navigate = useNavigate();
const [tab, setTab] = useState(0);
const location = useLocation();

const tab = useMemo(() => {
const currentTab = items.findIndex(item =>
location.pathname.startsWith(item.url),
);
if (currentTab !== -1) {
return currentTab;
}
return items[0];
}, [items, location.pathname]);

const goto = useCallback(
(value: number) => {
Expand All @@ -24,7 +34,6 @@ export default function ButtonsHeader({ items }: ButtonsHeaderProps) {
return;
}
navigate(url.url);
setTab(value);
},
[items, navigate],
);
Expand Down

0 comments on commit 7620569

Please sign in to comment.