|
1 |
| -import {createContext, useContext, useMemo} from 'react'; |
2 |
| - |
3 |
| -import {createNavConfig} from 'sentry/components/nav/config'; |
4 |
| -import type { |
5 |
| - NavConfig, |
6 |
| - NavItemLayout, |
7 |
| - NavSidebarItem, |
8 |
| - NavSubmenuItem, |
9 |
| -} from 'sentry/components/nav/utils'; |
10 |
| -import {isNavItemActive, isSubmenuItemActive} from 'sentry/components/nav/utils'; |
11 |
| -import {useLocation} from 'sentry/utils/useLocation'; |
12 |
| -import useOrganization from 'sentry/utils/useOrganization'; |
| 1 | +import {createContext, useContext, useMemo, useState} from 'react'; |
13 | 2 |
|
14 | 3 | export interface NavContext {
|
15 |
| - /** Raw config for entire nav items */ |
16 |
| - config: Readonly<NavConfig>; |
17 |
| - /** Currently active submenu items, if any */ |
18 |
| - submenu?: Readonly<NavItemLayout<NavSubmenuItem>>; |
| 4 | + secondaryNavEl: HTMLElement | null; |
| 5 | + setSecondaryNavEl: (el: HTMLElement | null) => void; |
19 | 6 | }
|
20 | 7 |
|
21 |
| -const NavContext = createContext<NavContext>({config: {main: []}}); |
| 8 | +const NavContext = createContext<NavContext>({ |
| 9 | + secondaryNavEl: null, |
| 10 | + setSecondaryNavEl: () => {}, |
| 11 | +}); |
22 | 12 |
|
23 | 13 | export function useNavContext(): NavContext {
|
24 |
| - const navContext = useContext(NavContext); |
25 |
| - return navContext; |
| 14 | + return useContext(NavContext); |
26 | 15 | }
|
27 | 16 |
|
28 |
| -export function NavContextProvider({children}: any) { |
29 |
| - const organization = useOrganization(); |
30 |
| - const location = useLocation(); |
31 |
| - /** Raw nav configuration values */ |
32 |
| - const config = useMemo(() => createNavConfig({organization}), [organization]); |
33 |
| - /** |
34 |
| - * Active submenu items derived from the nav config and current `location`. |
35 |
| - * These are returned in a normalized layout format for ease of use. |
36 |
| - */ |
37 |
| - const submenu = useMemo<NavContext['submenu']>(() => { |
38 |
| - for (const item of config.main) { |
39 |
| - if (isNavItemActive(item, location) || isSubmenuItemActive(item, location)) { |
40 |
| - return normalizeSubmenu(item.submenu); |
41 |
| - } |
42 |
| - } |
43 |
| - if (config.footer) { |
44 |
| - for (const item of config.footer) { |
45 |
| - if (isNavItemActive(item, location) || isSubmenuItemActive(item, location)) { |
46 |
| - return normalizeSubmenu(item.submenu); |
47 |
| - } |
48 |
| - } |
49 |
| - } |
50 |
| - return undefined; |
51 |
| - }, [config, location]); |
| 17 | +export function NavContextProvider({children}: {children: React.ReactNode}) { |
| 18 | + const [secondaryNavEl, setSecondaryNavEl] = useState<HTMLElement | null>(null); |
52 | 19 |
|
53 |
| - return <NavContext.Provider value={{config, submenu}}>{children}</NavContext.Provider>; |
54 |
| -} |
| 20 | + const value = useMemo( |
| 21 | + () => ({secondaryNavEl, setSecondaryNavEl}), |
| 22 | + [secondaryNavEl, setSecondaryNavEl] |
| 23 | + ); |
55 | 24 |
|
56 |
| -const normalizeSubmenu = (submenu: NavSidebarItem['submenu']): NavContext['submenu'] => { |
57 |
| - if (Array.isArray(submenu)) { |
58 |
| - return {main: submenu}; |
59 |
| - } |
60 |
| - return submenu; |
61 |
| -}; |
| 25 | + return <NavContext.Provider value={value}>{children}</NavContext.Provider>; |
| 26 | +} |
0 commit comments