@@ -24,51 +24,79 @@ import {
2424 BreadcrumbSeparator ,
2525} from '@oasisprotocol/ui-library/src/components/ui/breadcrumb' ;
2626
27- const locationListMap : Record < string , string [ ] > = {
28- '/explore' : [ 'Explore' ] ,
29- '/dashboard/machines' : [ 'Dashboard ', 'Machines' ] ,
30- '/dashboard/apps' : [ 'Dashboard ', 'My Apps' ] ,
31- '/' : [ 'Dashboard' ] ,
27+ const navItems = {
28+ dashboard : { label : 'Dashboard' , path : '/dashboard' } ,
29+ myApps : { label : 'My Apps ', path : '/dashboard/apps' } ,
30+ machines : { label : 'Machines ', path : '/dashboard/machines' } ,
31+ explore : { label : 'Explore' , path : '/explore' } ,
3232} ;
3333
34+ const breadcrumbConfigs = [
35+ {
36+ pattern : navItems . explore . path ,
37+ breadcrumbs : [ navItems . explore ] ,
38+ matchType : 'startsWith' ,
39+ } ,
40+ {
41+ pattern : navItems . machines . path ,
42+ breadcrumbs : [ navItems . dashboard , navItems . machines ] ,
43+ matchType : 'startsWith' ,
44+ } ,
45+ {
46+ pattern : navItems . myApps . path ,
47+ breadcrumbs : [ navItems . dashboard , navItems . myApps ] ,
48+ matchType : 'startsWith' ,
49+ } ,
50+ {
51+ pattern : navItems . dashboard . path ,
52+ breadcrumbs : [ navItems . dashboard ] ,
53+ matchType : 'exact' ,
54+ } ,
55+ ] ;
56+
3457export const MainLayout : FC = ( ) => {
3558 const navigate = useNavigate ( ) ;
3659 const location = useLocation ( ) ;
37- const locationList = Object . entries ( locationListMap ) . find ( ( [ path ] ) =>
38- location . pathname . toLowerCase ( ) . startsWith ( path )
39- ) ?. [ 1 ] ;
4060
41- const getBreadcrumbPath = ( index : number ) => {
42- if ( ! locationList ) return '/' ;
43- if ( index === 0 && locationList [ 0 ] === 'Dashboard' ) {
44- return '/dashboard' ;
45- }
46- return location . pathname ;
61+ const getBreadcrumbConfig = ( ) => {
62+ const pathname = location . pathname . toLowerCase ( ) ;
63+
64+ return breadcrumbConfigs . find ( ( config ) => {
65+ if ( config . matchType === 'exact' ) {
66+ return pathname === config . pattern ;
67+ } else {
68+ return pathname . startsWith ( config . pattern ) ;
69+ }
70+ } ) ;
4771 } ;
72+
73+ const breadcrumbConfig = getBreadcrumbConfig ( ) ;
74+ const breadcrumbs = breadcrumbConfig ?. breadcrumbs || [ ] ;
75+
4876 return (
4977 < Layout
5078 headerContent = { < Header /> }
5179 headerBreadcrumbsContent = {
52- ! ! locationList ? .length && (
80+ ! ! breadcrumbs . length && (
5381 < Breadcrumb className = "flex px-2" >
5482 < BreadcrumbList >
55- { locationList . flatMap ( ( loc , i ) => {
83+ { breadcrumbs . flatMap ( ( breadcrumb , i ) => {
5684 const elements = [
57- < BreadcrumbItem key = { loc + i } >
85+ < BreadcrumbItem key = { breadcrumb . label + i } >
5886 < BreadcrumbLink asChild >
5987 < NavLink
60- to = { getBreadcrumbPath ( i ) }
88+ to = { breadcrumb . path }
6189 className = "text-foreground text-sm font-normal"
6290 >
63- { loc }
91+ { breadcrumb . label }
6492 </ NavLink >
6593 </ BreadcrumbLink >
6694 </ BreadcrumbItem > ,
6795 ] ;
6896
69- if ( i + 1 < locationList . length ) {
97+ if ( i + 1 < breadcrumbs . length ) {
7098 elements . push (
71- < BreadcrumbSeparator key = { `sep-${ loc } -${ i } ` } />
99+ < BreadcrumbSeparator key = { `sep-${ breadcrumb . label } -${ i } ` } />
72100 ) ;
73101 }
74102
0 commit comments