|
@@ -1,61 +1,26 @@
|
|
|
-import {createContext, useContext, useMemo} from 'react';
|
|
|
-
|
|
|
-import {createNavConfig} from 'sentry/components/nav/config';
|
|
|
-import type {
|
|
|
- NavConfig,
|
|
|
- NavItemLayout,
|
|
|
- NavSidebarItem,
|
|
|
- NavSubmenuItem,
|
|
|
-} from 'sentry/components/nav/utils';
|
|
|
-import {isNavItemActive, isSubmenuItemActive} from 'sentry/components/nav/utils';
|
|
|
-import {useLocation} from 'sentry/utils/useLocation';
|
|
|
-import useOrganization from 'sentry/utils/useOrganization';
|
|
|
+import {createContext, useContext, useMemo, useState} from 'react';
|
|
|
|
|
|
export interface NavContext {
|
|
|
- /** Raw config for entire nav items */
|
|
|
- config: Readonly<NavConfig>;
|
|
|
- /** Currently active submenu items, if any */
|
|
|
- submenu?: Readonly<NavItemLayout<NavSubmenuItem>>;
|
|
|
+ secondaryNavEl: HTMLElement | null;
|
|
|
+ setSecondaryNavEl: (el: HTMLElement | null) => void;
|
|
|
}
|
|
|
|
|
|
-const NavContext = createContext<NavContext>({config: {main: []}});
|
|
|
+const NavContext = createContext<NavContext>({
|
|
|
+ secondaryNavEl: null,
|
|
|
+ setSecondaryNavEl: () => {},
|
|
|
+});
|
|
|
|
|
|
export function useNavContext(): NavContext {
|
|
|
- const navContext = useContext(NavContext);
|
|
|
- return navContext;
|
|
|
+ return useContext(NavContext);
|
|
|
}
|
|
|
|
|
|
-export function NavContextProvider({children}: any) {
|
|
|
- const organization = useOrganization();
|
|
|
- const location = useLocation();
|
|
|
- /** Raw nav configuration values */
|
|
|
- const config = useMemo(() => createNavConfig({organization}), [organization]);
|
|
|
- /**
|
|
|
- * Active submenu items derived from the nav config and current `location`.
|
|
|
- * These are returned in a normalized layout format for ease of use.
|
|
|
- */
|
|
|
- const submenu = useMemo<NavContext['submenu']>(() => {
|
|
|
- for (const item of config.main) {
|
|
|
- if (isNavItemActive(item, location) || isSubmenuItemActive(item, location)) {
|
|
|
- return normalizeSubmenu(item.submenu);
|
|
|
- }
|
|
|
- }
|
|
|
- if (config.footer) {
|
|
|
- for (const item of config.footer) {
|
|
|
- if (isNavItemActive(item, location) || isSubmenuItemActive(item, location)) {
|
|
|
- return normalizeSubmenu(item.submenu);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return undefined;
|
|
|
- }, [config, location]);
|
|
|
+export function NavContextProvider({children}: {children: React.ReactNode}) {
|
|
|
+ const [secondaryNavEl, setSecondaryNavEl] = useState<HTMLElement | null>(null);
|
|
|
|
|
|
- return <NavContext.Provider value={{config, submenu}}>{children}</NavContext.Provider>;
|
|
|
-}
|
|
|
+ const value = useMemo(
|
|
|
+ () => ({secondaryNavEl, setSecondaryNavEl}),
|
|
|
+ [secondaryNavEl, setSecondaryNavEl]
|
|
|
+ );
|
|
|
|
|
|
-const normalizeSubmenu = (submenu: NavSidebarItem['submenu']): NavContext['submenu'] => {
|
|
|
- if (Array.isArray(submenu)) {
|
|
|
- return {main: submenu};
|
|
|
- }
|
|
|
- return submenu;
|
|
|
-};
|
|
|
+ return <NavContext.Provider value={value}>{children}</NavContext.Provider>;
|
|
|
+}
|