useRegisterOrgMenu.tsx27 lines · main
| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from 'next/router' |
| 4 | import { useLayoutEffect } from 'react' |
| 5 | |
| 6 | import { useMobileSheet } from '../Navigation/NavigationBar/MobileSheetContext' |
| 7 | import { OrgMenuContent } from '../ProjectLayout/LayoutHeader/MobileMenuContent/OrgMenuContent' |
| 8 | import { getPathnameWithoutQuery, isOrgMenuScope } from './OrganizationLayout.utils' |
| 9 | |
| 10 | /** |
| 11 | * Registers the org menu with the mobile sheet when in org scope (/org/...). |
| 12 | * Unregisters when navigating away. Call from OrganizationLayout. |
| 13 | */ |
| 14 | export function useRegisterOrgMenu() { |
| 15 | const router = useRouter() |
| 16 | const { setContent: setMobileSheetContent, registerOpenMenu } = useMobileSheet() |
| 17 | |
| 18 | useLayoutEffect(() => { |
| 19 | const pathname = getPathnameWithoutQuery(router.asPath, router.pathname) |
| 20 | if (!isOrgMenuScope(pathname)) return |
| 21 | |
| 22 | const unregister = registerOpenMenu(() => { |
| 23 | setMobileSheetContent(<OrgMenuContent onCloseSheet={() => setMobileSheetContent(null)} />) |
| 24 | }) |
| 25 | return unregister |
| 26 | }, [router.asPath, router.pathname, registerOpenMenu, setMobileSheetContent]) |
| 27 | } |