|
| 1 | +/* eslint-disable require-atomic-updates */ |
| 2 | +/* eslint-disable @typescript-eslint/no-floating-promises */ |
| 3 | +import * as React from 'react'; |
| 4 | + |
| 5 | +import * as strings from 'ControlStrings'; |
| 6 | +import { useAtom } from 'jotai'; |
| 7 | + |
| 8 | +import { |
| 9 | + INav, |
| 10 | + INavLink, |
| 11 | + INavLinkGroup, |
| 12 | + Nav, |
| 13 | +} from '@fluentui/react/lib/Nav'; |
| 14 | +import { Separator } from '@fluentui/react/lib/Separator'; |
| 15 | +import { Spinner } from '@fluentui/react/lib/Spinner'; |
| 16 | +import { Stack } from '@fluentui/react/lib/Stack'; |
| 17 | +import { Text } from '@fluentui/react/lib/text'; |
| 18 | +import { IRenderFunction } from '@fluentui/react/lib/Utilities'; |
| 19 | +import { TermStore } from '@microsoft/microsoft-graph-types'; |
| 20 | + |
| 21 | +import { globalState } from './atoms/globalState'; |
| 22 | +import { ErrorMessage } from './ErrorMessage/ErrorMessage'; |
| 23 | +import { useGraphTaxonomyAPI } from './hooks/useGraphTaxonomyAPI'; |
| 24 | +import { useSessionStorage } from './hooks/useSessionStorage'; |
| 25 | +import { INavigationProps } from './INavigationProps'; |
| 26 | +import { RenderLink } from './RenderLink'; |
| 27 | +import { RenderNoOptions } from './RenderNoOptions'; |
| 28 | +import { useNavigationStyles } from './useNavigationStyles'; |
| 29 | +import { useTaxonomyUtils } from './utils/useTaxonomyUtils'; |
| 30 | +import { useUtils } from './utils/useUtils'; |
| 31 | + |
| 32 | +export const Navigation: React.FunctionComponent<INavigationProps> = ( |
| 33 | + props: React.PropsWithChildren<INavigationProps> |
| 34 | +) => { |
| 35 | + const { context, termSetId } = props; |
| 36 | + const [appGlobalState, setAppGlobalState] = useAtom(globalState); |
| 37 | + const { isLoadingNavitionTree, refreshNavigationTree, showContextMenu, onSelected } = appGlobalState || {}; |
| 38 | + const { navStyles } = useNavigationStyles(); |
| 39 | + const { pageContext } = context || {}; |
| 40 | + const { site } = pageContext || {}; |
| 41 | + const { getTermSetChildren, getTermSet } = useGraphTaxonomyAPI(context); |
| 42 | + const isLoadedTermSetsRef = React.useRef<boolean>(false); |
| 43 | + const { createItems } = useTaxonomyUtils(context); |
| 44 | + const [navLinksGroup, setNavLinksGroup] = React.useState<INavLinkGroup[]>([]); |
| 45 | + const { selectedItem } = appGlobalState; |
| 46 | + const navRef = React.useRef<INav>(null); |
| 47 | + const { getCacheKey } = useUtils(); |
| 48 | + |
| 49 | + const [getSessionStorageItem] = useSessionStorage(); |
| 50 | + const [termSetInfo, setTermSetInfo] = React.useState<TermStore.Set>(null); |
| 51 | + const [error, setError] = React.useState<Error>(null); |
| 52 | + |
| 53 | + const loadNavLinks = React.useCallback( |
| 54 | + async (refresh: boolean) => { |
| 55 | + if (!site) return; |
| 56 | + |
| 57 | + setError(null); |
| 58 | + setAppGlobalState((state) => ({ ...state, isLoadingNavitionTree: true })); |
| 59 | + isLoadedTermSetsRef.current = true; |
| 60 | + const termSetChildren = await getTermSetChildren(site?.id.toString(), termSetId, refresh); |
| 61 | + const navItems = await createItems(site?.id.toString(), termSetId, termSetChildren, 0, refresh); |
| 62 | + const navGroup = { groupData: { termSet: termSetId }, links: navItems }; |
| 63 | + setNavLinksGroup((state) => [...state, navGroup]); |
| 64 | + setAppGlobalState((state) => ({ |
| 65 | + ...state, |
| 66 | + selectedItem: selectedItem ?? navItems[0], |
| 67 | + isLoadingNavitionTree: false, |
| 68 | + refreshNavigationTree: false, |
| 69 | + })); |
| 70 | + }, |
| 71 | + [ |
| 72 | + selectedItem, |
| 73 | + site, |
| 74 | + getSessionStorageItem, |
| 75 | + getCacheKey, |
| 76 | + getTermSetChildren, |
| 77 | + createItems, |
| 78 | + setAppGlobalState, |
| 79 | + termSetId, |
| 80 | + ] |
| 81 | + ); |
| 82 | + |
| 83 | + React.useEffect(() => { |
| 84 | + (async () => { |
| 85 | + try { |
| 86 | + if (!isLoadedTermSetsRef.current) { |
| 87 | + const termSetInfo = await getTermSet(site?.id.toString(), termSetId); |
| 88 | + setTermSetInfo(termSetInfo); |
| 89 | + await loadNavLinks(false); |
| 90 | + isLoadedTermSetsRef.current = true; |
| 91 | + } |
| 92 | + } catch (error) { |
| 93 | + setError(error); |
| 94 | + } finally { |
| 95 | + setAppGlobalState((state) => ({ ...state, isLoadingNavitionTree: false })); |
| 96 | + } |
| 97 | + })(); |
| 98 | + }, []); |
| 99 | + |
| 100 | + React.useEffect(() => { |
| 101 | + (async () => { |
| 102 | + if (refreshNavigationTree) { |
| 103 | + setNavLinksGroup([]); |
| 104 | + await loadNavLinks(refreshNavigationTree); |
| 105 | + } |
| 106 | + })(); |
| 107 | + }, [refreshNavigationTree]); |
| 108 | + |
| 109 | + const onRenderLink: IRenderFunction<INavLink> = React.useCallback( |
| 110 | + (link: INavLink): JSX.Element => { |
| 111 | + return <RenderLink link={link} showContextMenu={showContextMenu} />; |
| 112 | + }, |
| 113 | + [showContextMenu] |
| 114 | + ); |
| 115 | + |
| 116 | + const onLinkClick = React.useCallback( |
| 117 | + (ev: React.MouseEvent<HTMLElement, MouseEvent>, item: INavLink) => { |
| 118 | + ev.preventDefault(); |
| 119 | + setAppGlobalState((state) => ({ ...state, selectedItem: item })); |
| 120 | + if (onSelected) { |
| 121 | + onSelected(item.data); |
| 122 | + } |
| 123 | + }, |
| 124 | + [selectedItem, setAppGlobalState] |
| 125 | + ); |
| 126 | + |
| 127 | + const hasLinks = React.useMemo(() => { |
| 128 | + return navLinksGroup[0]?.links?.length > 0; |
| 129 | + }, [navLinksGroup]); |
| 130 | + |
| 131 | + const hasError = React.useMemo(() => { |
| 132 | + return error !== null || termSetInfo === null; |
| 133 | + }, [error, termSetInfo]); |
| 134 | + |
| 135 | + if (isLoadingNavitionTree) return <Spinner ariaLive="assertive" />; |
| 136 | + |
| 137 | + if (hasError) |
| 138 | + return ( |
| 139 | + <ErrorMessage showError={hasError} errorMessage={error?.message ?? strings.TermSertNaviagtionErrorMessage} /> |
| 140 | + ); |
| 141 | + |
| 142 | + return ( |
| 143 | + <> |
| 144 | + <Stack horizontalAlign="stretch" tokens={{ childrenGap: 0 }}> |
| 145 | + <Text variant="large">{termSetInfo?.localizedNames[0].name ?? ""}</Text> |
| 146 | + <Separator /> |
| 147 | + {hasLinks ? ( |
| 148 | + <Nav |
| 149 | + componentRef={navRef} |
| 150 | + ariaLabel="navigation" |
| 151 | + styles={navStyles} |
| 152 | + groups={navLinksGroup} |
| 153 | + onRenderLink={onRenderLink} |
| 154 | + selectedKey={selectedItem?.key} |
| 155 | + onLinkClick={onLinkClick} |
| 156 | + /> |
| 157 | + ) : ( |
| 158 | + <RenderNoOptions /> |
| 159 | + )} |
| 160 | + </Stack> |
| 161 | + </> |
| 162 | + ); |
| 163 | +}; |
0 commit comments