1- import { ReactElement , ReactNode , useMemo , useState } from "react" ;
1+ import { ReactElement , ReactNode , useEffect } from "react" ;
22import type { NextPage } from "next" ;
33import type { AppProps } from "next/app" ;
44import Head from "next/head" ;
5- import { DxcApplicationLayout , DxcTextInput , DxcToastsQueue } from "@dxc-technology/halstack-react" ;
6- import SidenavLogo from "@/common/sidenav/SidenavLogo" ;
5+ import { DxcApplicationLayout , DxcToastsQueue } from "@dxc-technology/halstack-react" ;
76import MainContent from "@/common/MainContent" ;
87import { useRouter } from "next/router" ;
98import { LinksSectionDetails , LinksSections } from "@/common/pagesList" ;
10- import Link from "next/link" ;
119import StatusBadge from "@/common/StatusBadge" ;
1210import "../global-styles.css" ;
1311import createCache , { EmotionCache } from "@emotion/cache" ;
1412import { CacheProvider } from "@emotion/react" ;
13+ import { usePathname } from "next/navigation" ;
1514
1615type NextPageWithLayout = NextPage & {
1716 getLayout ?: ( _page : ReactElement ) => ReactNode ;
@@ -26,54 +25,69 @@ const clientSideEmotionCache = createCache({ key: "css", prepend: true });
2625export default function App ( { Component, pageProps, emotionCache = clientSideEmotionCache } : AppPropsWithLayout ) {
2726 const getLayout = Component . getLayout || ( ( page ) => page ) ;
2827 const componentWithLayout = getLayout ( < Component { ...pageProps } /> ) ;
29- const [ filter , setFilter ] = useState ( "" ) ;
30- const { asPath : currentPath } = useRouter ( ) ;
31- const filteredLinks = useMemo ( ( ) => {
32- const filtered : LinksSectionDetails [ ] = [ ] ;
33- LinksSections . map ( ( section ) => {
34- const sectionFilteredLinks = section ?. links . filter ( ( link ) =>
35- link . label . toLowerCase ( ) . includes ( filter . toLowerCase ( ) )
36- ) ;
37- if ( sectionFilteredLinks . length ) {
38- filtered . push ( { label : section . label , links : sectionFilteredLinks } ) ;
39- }
40- } ) ;
41- return filtered ;
42- } , [ filter ] ) ;
28+ const router = useRouter ( ) ;
29+ const pathname = usePathname ( ) ;
30+ // const [filter, setFilter] = useState("");
31+ // const filteredLinks = useMemo(() => {
32+ // const filtered: LinksSectionDetails[] = [];
33+ // LinksSections.map((section) => {
34+ // const sectionFilteredLinks = section?.links.filter((link) =>
35+ // link.label.toLowerCase().includes(filter.toLowerCase())
36+ // );
37+ // if (sectionFilteredLinks.length) {
38+ // filtered.push({ label: section.label, links: sectionFilteredLinks });
39+ // }
40+ // });
41+ // return filtered;
42+ // }, [filter]);
43+
44+ const mapLinksToGroupItems = ( sections : LinksSectionDetails [ ] ) => {
45+ const matchPaths = ( linkPath : string ) => {
46+ const desiredPaths = [ linkPath , `${ linkPath } /code` ] ;
47+ const pathToBeMatched = pathname ?. split ( "#" ) [ 0 ] ?. slice ( 0 , - 1 ) ;
48+ return pathToBeMatched ? desiredPaths . includes ( pathToBeMatched ) : false ;
49+ } ;
4350
44- const matchPaths = ( linkPath : string ) => {
45- const desiredPaths = [ linkPath , `${ linkPath } /code` ] ;
46- const pathToBeMatched = currentPath ?. split ( "#" ) [ 0 ] ?. slice ( 0 , - 1 ) ;
47- return pathToBeMatched ? desiredPaths . includes ( pathToBeMatched ) : false ;
51+ return sections . map ( ( section ) => ( {
52+ title : section . label ,
53+ items : section . links . map ( ( link ) => ( {
54+ label : link . label ,
55+ onSelect : ( ) => router . push ( link . path ) ,
56+ selected : matchPaths ( link . path ) ,
57+ ...( link . status && {
58+ badge : link . status !== "stable" ? < StatusBadge hasTitle status = { link . status } /> : undefined ,
59+ } ) ,
60+ } ) ) ,
61+ } ) ) ;
4862 } ;
4963
64+ useEffect ( ( ) => {
65+ const paths = [ ...new Set ( LinksSections . flatMap ( ( s ) => s . links . map ( ( l ) => l . path ) ) ) ] ;
66+ const prefetchPaths = async ( ) => {
67+ for ( const path of paths ) {
68+ await router . prefetch ( path ) ;
69+ }
70+ } ;
71+ void prefetchPaths ( ) ;
72+ } , [ ] ) ;
73+
74+ // TODO: ADD FILTERING
75+ // TODO: ADD CATEGORIZATION
76+
77+ const sections = mapLinksToGroupItems ( LinksSections ) ;
78+
5079 return (
5180 < CacheProvider value = { emotionCache } >
5281 < Head >
5382 < link rel = "icon" type = "image/png" sizes = "32x32" href = "/favicon.png" />
5483 </ Head >
5584 < DxcApplicationLayout
56- visibilityToggleLabel = "Menu"
5785 sidenav = {
58- < DxcApplicationLayout . SideNav title = { < SidenavLogo /> } >
59- < DxcApplicationLayout . SideNav . Section >
60- < DxcTextInput
61- placeholder = "Search docs"
62- value = { filter }
63- onChange = { ( { value } : { value : string } ) => {
64- setFilter ( value ) ;
65- } }
66- size = "fillParent"
67- clearable
68- margin = { {
69- top : "large" ,
70- bottom : "large" ,
71- right : "medium" ,
72- left : "medium" ,
73- } }
74- />
75- </ DxcApplicationLayout . SideNav . Section >
76- { filteredLinks ?. map ( ( { label, links } ) => (
86+ < DxcApplicationLayout . SideNav
87+ items = { sections }
88+ // title={<SidenavLogo />}
89+ >
90+ { /* {filteredLinks?.map(({ label, links }) => (
7791 <DxcApplicationLayout.SideNav.Section key={label}>
7892 <DxcApplicationLayout.SideNav.Group title={label}>
7993 {links.map(({ label, path, status }) => (
@@ -91,7 +105,7 @@ export default function App({ Component, pageProps, emotionCache = clientSideEmo
91105 <DxcApplicationLayout.SideNav.Link href="https://github.com/dxc-technology/halstack-react" newWindow>
92106 GitHub
93107 </DxcApplicationLayout.SideNav.Link>
94- </ DxcApplicationLayout . SideNav . Section >
108+ </DxcApplicationLayout.SideNav.Section> */ }
95109 </ DxcApplicationLayout . SideNav >
96110 }
97111 >
0 commit comments