-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: breadcrumb comply with submenu nav
- Loading branch information
Showing
9 changed files
with
176 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'web': minor | ||
--- | ||
|
||
Refactored breadcrumb component to comply with new navigation structure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
'use client'; | ||
|
||
import { Container } from '@/components/container'; | ||
import { getNavbarNodeByUrl } from '@/services/cms/get-navbar-node-by-url'; | ||
import { NavbarNode } from '@/services/cms/get-navbar-tree'; | ||
import { IconChevronRight } from '@tabler/icons-react'; | ||
import Link from 'next/link'; | ||
import { usePathname } from 'next/navigation'; | ||
import React, { | ||
Dispatch, | ||
Fragment, | ||
PropsWithChildren, | ||
SetStateAction, | ||
createContext, | ||
useContext, | ||
useEffect, | ||
useState, | ||
} from 'react'; | ||
|
||
interface CrumbProps { | ||
name: string; | ||
href?: string; | ||
} | ||
|
||
const Crumb: React.FC<CrumbProps> = ({ name, href }) => { | ||
if (name === 'root') return <Link href="/">Home</Link>; | ||
|
||
if (!href) return <span>{name}</span>; | ||
|
||
return <Link href={href.startsWith('/') ? href : '/' + href}>{name}</Link>; | ||
}; | ||
|
||
interface MenuBreadcrumbsProps { | ||
navbar: NavbarNode; | ||
} | ||
|
||
export const MenuBreadcrumbs: React.FC<MenuBreadcrumbsProps> = ({ navbar }) => { | ||
const { additionalCrumbs } = useMenuBreadcrumbs(); | ||
const pathName = usePathname(); | ||
|
||
const pathSegments = pathName.split('/'); | ||
|
||
const targetNode = pathSegments[1] || '/'; | ||
|
||
const node = getNavbarNodeByUrl(navbar, targetNode); | ||
|
||
if (!node) return null; | ||
|
||
const nodes = [node]; | ||
let current = node; | ||
|
||
while (current.parent) { | ||
current = current.parent; | ||
nodes.push(current); | ||
} | ||
|
||
return ( | ||
<div className="bg-primary-lighter"> | ||
<Container component="nav" className="flex h-14 items-center gap-x-4 text-base font-bold text-primary-dark"> | ||
{nodes.toReversed().map((node, i) => { | ||
return ( | ||
<Fragment key={node.id}> | ||
{i > 0 && <IconChevronRight size={24} />} | ||
<Crumb name={node.name} href={node.url} /> | ||
</Fragment> | ||
); | ||
})} | ||
{additionalCrumbs.map((name, i) => { | ||
return ( | ||
<Fragment key={name}> | ||
<IconChevronRight size={24} /> | ||
<Crumb name={name} /> | ||
</Fragment> | ||
); | ||
})} | ||
</Container> | ||
</div> | ||
); | ||
}; | ||
|
||
interface Value { | ||
additionalCrumbs: string[]; | ||
setAdditionalCrumbs: Dispatch<SetStateAction<string[]>>; | ||
} | ||
|
||
const context = createContext<Value>({} as Value); | ||
|
||
interface MenuBreadcrumbsProvider extends PropsWithChildren {} | ||
|
||
export const MenuBreadcrumbsProvider: React.FC<MenuBreadcrumbsProvider> = ({ children }) => { | ||
const [additionalCrumbs, setAdditionalCrumbs] = useState<string[]>([]); | ||
|
||
console.log({ additionalCrumbs }); | ||
|
||
return <context.Provider value={{ additionalCrumbs, setAdditionalCrumbs }}>{children}</context.Provider>; | ||
}; | ||
|
||
const useMenuBreadcrumbs = () => useContext(context); | ||
|
||
interface EnhanceMenuBreadcrumbsProps { | ||
append: string; | ||
} | ||
|
||
export const EnhanceMenuBreadcrumbs: React.FC<EnhanceMenuBreadcrumbsProps> = ({ append }) => { | ||
const { setAdditionalCrumbs } = useMenuBreadcrumbs(); | ||
|
||
useEffect(() => { | ||
setAdditionalCrumbs([append]); | ||
|
||
return () => { | ||
console.log('cleanup'); | ||
setAdditionalCrumbs([]); | ||
}; | ||
}, [append, setAdditionalCrumbs]); | ||
|
||
return null; | ||
}; |
This file was deleted.
Oops, something went wrong.