-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuse-breadcrumbs.tsx
46 lines (39 loc) · 1.21 KB
/
use-breadcrumbs.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'use client';
import { usePathname } from 'next/navigation';
import { useMemo } from 'react';
type BreadcrumbItem = {
title: string;
link: string;
};
// This allows to add custom title as well
const routeMapping: Record<string, BreadcrumbItem[]> = {
'/app': [{ title: 'Dashboard', link: '/app' }],
'/app/employee': [
{ title: 'Dashboard', link: '/app' },
{ title: 'Employee', link: '/app/employee' }
],
'/app/product': [
{ title: 'Dashboard', link: '/app' },
{ title: 'Product', link: '/app/product' }
]
// Add more custom mappings as needed
};
export function useBreadcrumbs() {
const pathname = usePathname();
const breadcrumbs = useMemo(() => {
// Check if we have a custom mapping for this exact path
if (routeMapping[pathname]) {
return routeMapping[pathname];
}
// If no exact match, fall back to generating breadcrumbs from the path
const segments = pathname.split('/').filter(Boolean);
return segments.map((segment, index) => {
const path = `/${segments.slice(0, index + 1).join('/')}`;
return {
title: segment.charAt(0).toUpperCase() + segment.slice(1),
link: path
};
});
}, [pathname]);
return breadcrumbs;
}