|
1 | 1 | 'use client'; |
2 | 2 |
|
3 | | -import { useParams } from 'next/navigation'; |
| 3 | +import { useParams, usePathname } from 'next/navigation'; |
| 4 | +import { useMemo } from 'react'; |
4 | 5 | import { Breadcrumb } from '@/components/common/Breadcrumb'; |
5 | 6 | import { GuildIcon } from '@/components/common/GuildIcon'; |
6 | 7 | import { client } from '@/data/client'; |
7 | 8 | import { sortGuilds } from '@/utils/util'; |
8 | 9 |
|
9 | | -export interface DashboardCrumbSegment { |
10 | | - readonly href?: string; |
11 | | - readonly icon?: React.ReactNode; |
12 | | - readonly label: string; |
13 | | -} |
14 | | - |
15 | | -interface DashboardCrumbProps { |
16 | | - readonly segments: DashboardCrumbSegment[]; |
17 | | -} |
| 10 | +const SEGMENT_LABELS: Record<string, string> = { |
| 11 | + ama: 'AMA Bot', |
| 12 | + amas: 'AMA Sessions', |
| 13 | + new: 'New', |
| 14 | +} as const; |
18 | 15 |
|
19 | | -export function DashboardCrumbs({ segments }: DashboardCrumbProps) { |
| 16 | +export function DashboardCrumbs() { |
20 | 17 | const { data: me } = client.auth.useMe(); |
21 | 18 | const params = useParams<{ id?: string }>(); |
| 19 | + const pathname = usePathname(); |
| 20 | + |
| 21 | + const segments = useMemo(() => { |
| 22 | + if (!params.id || !pathname) { |
| 23 | + return []; |
| 24 | + } |
| 25 | + |
| 26 | + // Split the pathname and remove empty strings |
| 27 | + const pathParts = pathname.split('/').filter(Boolean); |
| 28 | + |
| 29 | + // Find where the guild ID is in the path |
| 30 | + const guildIdIndex = pathParts.indexOf(params.id); |
| 31 | + if (guildIdIndex === -1) { |
| 32 | + return []; |
| 33 | + } |
| 34 | + |
| 35 | + // Get all segments after the guild ID |
| 36 | + const relevantParts = pathParts.slice(guildIdIndex + 1); |
| 37 | + |
| 38 | + // Build segments with proper hrefs |
| 39 | + const result = []; |
| 40 | + for (let i = 0; i < relevantParts.length; i++) { |
| 41 | + const part = relevantParts[i]; |
| 42 | + if (!part) { |
| 43 | + continue; |
| 44 | + } |
| 45 | + |
| 46 | + const label = SEGMENT_LABELS[part] ?? part; |
| 47 | + |
| 48 | + // Don't create an href for the last segment (current page) |
| 49 | + const isLastSegment = i === relevantParts.length - 1; |
| 50 | + if (isLastSegment) { |
| 51 | + result.push({ label }); |
| 52 | + } else { |
| 53 | + // Build the href up to this segment |
| 54 | + const pathUpToHere = pathParts.slice(0, guildIdIndex + 2 + i).join('/'); |
| 55 | + result.push({ |
| 56 | + label, |
| 57 | + href: `/${pathUpToHere}`, |
| 58 | + }); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return result; |
| 63 | + }, [params.id, pathname]); |
22 | 64 |
|
23 | 65 | if (!params.id) { |
24 | 66 | throw new Error('id param not found, should not be rendering this component'); |
@@ -48,16 +90,7 @@ export function DashboardCrumbs({ segments }: DashboardCrumbProps) { |
48 | 90 | icon: <GuildIcon data={guild} disableLink hasBots />, |
49 | 91 | options: guildOptions, |
50 | 92 | }, |
51 | | - ...segments.map(({ href, ...rest }) => { |
52 | | - if (href) { |
53 | | - return { |
54 | | - href: href.replace('[id]', guild.id), |
55 | | - ...rest, |
56 | | - }; |
57 | | - } |
58 | | - |
59 | | - return rest; |
60 | | - }), |
| 93 | + ...segments, |
61 | 94 | ]} |
62 | 95 | /> |
63 | 96 | ); |
|
0 commit comments