Skip to content

Commit

Permalink
buttons header value based on url instead of state
Browse files Browse the repository at this point in the history
  • Loading branch information
Yooooomi committed Nov 24, 2024
1 parent f23caa1 commit 77474db
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions apps/client/src/components/ButtonsHeader/ButtonsHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tab, Tabs } from "@mui/material";
import { useCallback, useState, useEffect } from "react";
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,15 +15,17 @@ interface ButtonsHeaderProps {

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

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

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

0 comments on commit 77474db

Please sign in to comment.