From 56f988e8ca5ad7ce4f75f1a2161f4e5c0b0c1577 Mon Sep 17 00:00:00 2001 From: Jules Belveze <32683010+JulesBelveze@users.noreply.github.com> Date: Tue, 12 Nov 2024 13:02:54 +0100 Subject: [PATCH] [front] - fix(navigation): tabs switch glitch (#8579) * [front] - refactor: streamline state management in NavigationSidebar - Remove useState and useEffect for activePath and currentTab in favor of useMemo - Simplify tab and navigation item rendering logic with direct mapping from navs - Ensure a default value for the Tabs component when no currentTab is identified - Remove redundant conditionals and nested fragment structures for navigation rendering * [front] - refactor: clean up import statements and fix JSX format in NavigationSidebar - Simplify import statement for SidebarNavigation type - Ensure proper formatting with space in self-closing Logo JSX tag * [front] - docs: add TODO for fixing AppLayout issue - Added a comment to address the AppLayout changing issue between pages at a future date --- .../navigation/NavigationSidebar.tsx | 141 ++++++++---------- 1 file changed, 59 insertions(+), 82 deletions(-) diff --git a/front/components/navigation/NavigationSidebar.tsx b/front/components/navigation/NavigationSidebar.tsx index 115f490a6d89..7e5e42765658 100644 --- a/front/components/navigation/NavigationSidebar.tsx +++ b/front/components/navigation/NavigationSidebar.tsx @@ -5,6 +5,7 @@ import { NavigationListItem, NavigationListLabel, Tabs, + TabsContent, TabsList, TabsTrigger, } from "@dust-tt/sparkle"; @@ -13,11 +14,7 @@ import Link from "next/link"; import { useRouter } from "next/router"; import React, { useCallback, useEffect, useMemo, useState } from "react"; -import type { - AppLayoutNavigation, - SidebarNavigation, - TabAppLayoutNavigation, -} from "@app/components/navigation/config"; +import type { SidebarNavigation } from "@app/components/navigation/config"; import { getTopNavigationTabs } from "@app/components/navigation/config"; import { UserMenu } from "@app/components/UserMenu"; import WorkspacePicker from "@app/components/WorkspacePicker"; @@ -42,9 +39,6 @@ export const NavigationSidebar = React.forwardRef< const router = useRouter(); const { user } = useUser(); const [activePath, setActivePath] = useState(""); - const [currentTab, setCurrentTab] = useState< - TabAppLayoutNavigation | undefined - >(undefined); useEffect(() => { if (router.isReady && router.route) { @@ -52,29 +46,12 @@ export const NavigationSidebar = React.forwardRef< } }, [router.route, router.isReady]); - const nav = useMemo(() => getTopNavigationTabs(owner), [owner]); - - const [navs, setNavs] = useState([]); - - // TODO(2024-06-19 flav): Fix issue with AppLayout changing between pages - useEffect(() => { - setNavs((prevNavs) => { - const newNavs = nav.map((n) => { - const current = n.isCurrent(activePath); - return { ...n, current }; - }); - - const isSameCurrent = - prevNavs.length === newNavs.length && - prevNavs.every((prevNav, index) => { - return prevNav.current === newNavs[index].current; - }); - - const activeTab = nav.filter((n) => n.isCurrent(activePath))[0]; - setCurrentTab(activeTab); - return isSameCurrent ? prevNavs : newNavs; - }); - }, [nav, activePath]); + // TODO(2024-06-19 flav): Fix issue with AppLayout changing between pagesg + const navs = useMemo(() => getTopNavigationTabs(owner), [owner]); + const currentTab = useMemo( + () => navs.find((n) => n.isCurrent(activePath)), + [navs, activePath] + ); return (
)} {subscription.paymentFailingSince && } - {nav.length > 1 && ( + {navs.length > 1 && (
- + {navs.map((tab) => ( ))} - -
- )} - {subNavigation && ( - <> - {subNavigation && ( - <> - {subNavigation.map((nav) => ( -
- - {nav.label && ( - - )} - {nav.menus.map((menu) => ( - - - {menu.subMenuLabel && ( -
- {menu.subMenuLabel} -
- )} - {menu.subMenu && ( -
- {menu.subMenu.map((nav) => ( + {navs.map((tab) => ( + + {subNavigation && tab.isCurrent(activePath) && ( + <> + {subNavigation.map((nav) => ( +
+ + {nav.label && ( + + )} + {nav.menus.map((menu) => ( + - ))} -
- )} - + {menu.subMenuLabel && ( +
+ {menu.subMenuLabel} +
+ )} + {menu.subMenu && ( +
+ {menu.subMenu.map((nav) => ( + + ))} +
+ )} + + ))} + +
))} -
-
- ))} - - )} - + + )} + + ))} + +
)}
{children}