Skip to content

Commit b8bfc0b

Browse files
refactor(swingset): align live navigation with component sidebar (#10045)
1 parent a989859 commit b8bfc0b

6 files changed

Lines changed: 275 additions & 216 deletions

File tree

‎.changeset/crisp-rabbits-wave.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

‎packages/swingset/src/app/(clerk)/live-sidebar.tsx‎

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,41 @@ import Link from 'next/link';
55
import { usePathname } from 'next/navigation';
66
import type { ComponentProps } from 'react';
77

8+
import { type NavigationGroup, SidebarNavigation } from '@/components/sidebar-navigation';
89
import {
910
Sidebar,
1011
SidebarContent,
1112
SidebarGroup,
1213
SidebarGroupContent,
13-
SidebarGroupLabel,
1414
SidebarHeader,
1515
SidebarMenu,
1616
SidebarMenuButton,
1717
SidebarMenuItem,
1818
SidebarRail,
1919
} from '@/components/ui/sidebar';
20+
import { getLiveSidebarGroups } from '@/lib/live-navigation';
2021

21-
const flows = [
22-
{ title: 'Account section', href: '/live/account-section' },
23-
{ title: 'API keys', href: '/live/api-keys' },
24-
{ title: 'Password', href: '/live/password' },
25-
{ title: 'Connected accounts', href: '/live/connected-accounts' },
26-
{ title: 'Enterprise accounts', href: '/live/enterprise-accounts' },
27-
{ title: 'Delete account', href: '/live/delete-account' },
28-
{ title: 'Reverification', href: '/live/reverification' },
29-
];
22+
const groups = getLiveSidebarGroups();
3023

31-
function flowIsActive(pathname: string, href: string) {
24+
function pageIsActive(pathname: string, href: string) {
3225
return pathname === href || pathname.startsWith(`${href}/`);
3326
}
3427

3528
export function LiveSidebar(props: ComponentProps<typeof Sidebar>) {
3629
const pathname = usePathname();
30+
const navigationGroups: NavigationGroup[] = groups.map(({ group, categories }) => ({
31+
label: group,
32+
categories: categories.map(({ category, components }) => ({
33+
label: category,
34+
items: components.map(({ mod, href }) => ({
35+
href,
36+
usage: `<${mod.meta.title} />`,
37+
isActive: pageIsActive(pathname, href),
38+
status: mod.meta.status,
39+
substatus: mod.meta.substatus,
40+
})),
41+
})),
42+
}));
3743

3844
return (
3945
<Sidebar {...props}>
@@ -66,26 +72,7 @@ export function LiveSidebar(props: ComponentProps<typeof Sidebar>) {
6672
</SidebarMenu>
6773
</SidebarGroupContent>
6874
</SidebarGroup>
69-
<SidebarGroup className='py-1'>
70-
<SidebarGroupLabel className='text-sidebar-foreground/50 h-auto px-2 pb-1 pt-3 text-[10px] font-semibold uppercase tracking-wider'>
71-
Flows
72-
</SidebarGroupLabel>
73-
<SidebarGroupContent>
74-
<SidebarMenu>
75-
{flows.map(flow => (
76-
<SidebarMenuItem key={flow.href}>
77-
<SidebarMenuButton
78-
className='h-auto py-1 text-xs'
79-
isActive={flowIsActive(pathname, flow.href)}
80-
render={<Link href={flow.href} />}
81-
>
82-
{flow.title}
83-
</SidebarMenuButton>
84-
</SidebarMenuItem>
85-
))}
86-
</SidebarMenu>
87-
</SidebarGroupContent>
88-
</SidebarGroup>
75+
<SidebarNavigation groups={navigationGroups} />
8976
</SidebarContent>
9077
<SidebarRail />
9178
</Sidebar>

‎packages/swingset/src/components/app-sidebar.tsx‎

Lines changed: 22 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,37 @@
11
'use client';
22

3-
import { ChevronRightIcon, FlaskConicalIcon } from 'lucide-react';
3+
import { FlaskConicalIcon } from 'lucide-react';
44
import Link from 'next/link';
55
import { usePathname } from 'next/navigation';
66
import * as React from 'react';
77

8-
import { StatusDot } from '@/components/StatusDot';
9-
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from '@/components/ui/collapsible';
8+
import { type NavigationGroup, SidebarNavigation } from '@/components/sidebar-navigation';
109
import {
1110
Sidebar,
1211
SidebarContent,
1312
SidebarFooter,
1413
SidebarGroup,
1514
SidebarGroupContent,
16-
SidebarGroupLabel,
1715
SidebarHeader,
1816
SidebarMenu,
1917
SidebarMenuButton,
2018
SidebarMenuItem,
2119
SidebarRail,
2220
SidebarSeparator,
2321
} from '@/components/ui/sidebar';
24-
import { Tooltip, TooltipContent, TooltipTrigger } from '@/components/ui/tooltip';
2522
import { getSidebarGroups } from '@/lib/registry';
26-
import type { StoryStatus, WipSubstatus } from '@/lib/types';
23+
import { getSidebarCategories } from '@/lib/sidebar-navigation';
2724

2825
const groups = getSidebarGroups();
2926

30-
const COLLAPSED_BY_DEFAULT = new Set(['Blocks', 'Primitives', 'Components', 'Styles', 'Hooks', 'Localization']);
31-
32-
type SidebarEntry = ReturnType<typeof getSidebarGroups>[number]['components'][number];
33-
34-
// Partitions a group's entries by `meta.navigation.category` into subheaded runs. Category and
35-
// entry order both follow first appearance in the registry; uncategorized entries get no subheading.
36-
function byCategory(components: SidebarEntry[]) {
37-
const categories: { category: string; components: SidebarEntry[] }[] = [];
38-
for (const component of components) {
39-
const category = component.mod.meta.navigation?.category ?? '';
40-
const bucket = categories.find(c => c.category === category);
41-
if (bucket) {
42-
bucket.components.push(component);
43-
} else {
44-
categories.push({ category, components: [component] });
45-
}
46-
}
47-
return categories;
48-
}
49-
50-
function SidebarUsageItem({
51-
usage,
52-
href,
53-
isActive,
54-
status,
55-
substatus,
56-
}: {
57-
usage: string;
58-
href: string;
59-
isActive: boolean;
60-
status?: StoryStatus;
61-
substatus?: WipSubstatus;
62-
}) {
63-
const labelRef = React.useRef<HTMLSpanElement>(null);
64-
const [isTruncated, setIsTruncated] = React.useState(false);
65-
66-
React.useEffect(() => {
67-
const label = labelRef.current;
68-
if (!label) {
69-
return;
70-
}
71-
const check = () => setIsTruncated(label.scrollWidth > label.clientWidth);
72-
check();
73-
const observer = new ResizeObserver(check);
74-
observer.observe(label);
75-
return () => observer.disconnect();
76-
}, []);
77-
78-
return (
79-
<SidebarMenuItem>
80-
<Tooltip disabled={!isTruncated}>
81-
<TooltipTrigger
82-
delay={300}
83-
render={
84-
<SidebarMenuButton
85-
className='h-auto py-1 text-xs'
86-
isActive={isActive}
87-
render={<Link href={href} />}
88-
>
89-
{status ? (
90-
<StatusDot
91-
status={status}
92-
substatus={substatus}
93-
/>
94-
) : null}
95-
<span
96-
ref={labelRef}
97-
className='truncate font-mono text-[10px] leading-relaxed'
98-
>
99-
{usage}
100-
</span>
101-
</SidebarMenuButton>
102-
}
103-
/>
104-
<TooltipContent
105-
side='right'
106-
className='font-mono text-[10px]'
107-
>
108-
{usage}
109-
</TooltipContent>
110-
</Tooltip>
111-
</SidebarMenuItem>
112-
);
113-
}
114-
115-
function SidebarEntryMenu({
116-
components,
117-
groupSlug,
118-
pathname,
119-
}: {
120-
components: SidebarEntry[];
121-
groupSlug: string;
122-
pathname: string;
123-
}) {
124-
return (
125-
<SidebarMenu>
126-
{components.map(({ mod, componentSlug }) => {
27+
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
28+
const pathname = usePathname();
29+
const navigationGroups: NavigationGroup[] = groups.map(({ group, groupSlug, components }) => ({
30+
label: group,
31+
separatorBefore: group === 'Blocks',
32+
categories: getSidebarCategories(components).map(({ category, components }) => ({
33+
label: category,
34+
items: components.map(({ mod, componentSlug }) => {
12735
const href = `/${groupSlug}/${componentSlug}`;
12836
// How an entry is USED differs by layer, so the label follows the layer rather
12937
// than a guess at the title: hooks are called, atomic styles are a set of
@@ -135,23 +43,16 @@ function SidebarEntryMenu({
13543
: mod.meta.group === 'Styles' || mod.meta.group === 'Localization'
13644
? mod.meta.title
13745
: `<${mod.meta.title} />`;
138-
return (
139-
<SidebarUsageItem
140-
key={mod.meta.title}
141-
usage={usage}
142-
href={href}
143-
isActive={pathname === href}
144-
status={mod.meta.status}
145-
substatus={mod.meta.substatus}
146-
/>
147-
);
148-
})}
149-
</SidebarMenu>
150-
);
151-
}
152-
153-
export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
154-
const pathname = usePathname();
46+
return {
47+
href,
48+
usage,
49+
isActive: pathname === href,
50+
status: mod.meta.status,
51+
substatus: mod.meta.substatus,
52+
};
53+
}),
54+
})),
55+
}));
15556

15657
return (
15758
<Sidebar {...props}>
@@ -188,71 +89,7 @@ export function AppSidebar({ ...props }: React.ComponentProps<typeof Sidebar>) {
18889
<span className='text-sidebar-foreground/70 text-[10px] font-medium'>Mosaic - Swingset</span>
18990
</SidebarHeader>
19091
<SidebarContent className='gap-0'>
191-
{groups.map(({ group, groupSlug, components }) => (
192-
<React.Fragment key={group}>
193-
{group === 'Blocks' && <SidebarSeparator className='data-horizontal:w-auto my-1' />}
194-
<Collapsible
195-
defaultOpen={!COLLAPSED_BY_DEFAULT.has(group)}
196-
className='group/collapsible'
197-
>
198-
<SidebarGroup
199-
className='py-1'
200-
data-section={group}
201-
>
202-
<SidebarGroupLabel
203-
className='text-sidebar-foreground/50 hover:text-sidebar-foreground/80 h-auto w-full px-2 pb-1 pt-3 text-[10px] font-semibold uppercase tracking-wider'
204-
render={<CollapsibleTrigger />}
205-
>
206-
{group}
207-
<ChevronRightIcon className='size-3! ml-auto transition-transform group-data-[open]/collapsible:rotate-90' />
208-
</SidebarGroupLabel>
209-
<CollapsibleContent>
210-
<SidebarGroupContent>
211-
{byCategory(components).map(({ category, components }) =>
212-
category ? (
213-
<Collapsible
214-
key={category}
215-
// Collapsed by default, unless it holds the page being viewed.
216-
defaultOpen={components.some(
217-
({ componentSlug }) => pathname === `/${groupSlug}/${componentSlug}`,
218-
)}
219-
className='group/category'
220-
>
221-
<CollapsibleTrigger className='text-sidebar-foreground/40 hover:text-sidebar-foreground/70 flex w-full items-center gap-1 px-2 pb-0.5 pt-2 text-[9px] font-semibold uppercase tracking-wider'>
222-
<span
223-
aria-hidden='true'
224-
className='font-mono text-[10px] leading-none'
225-
>
226-
└
227-
</span>
228-
{category}
229-
<ChevronRightIcon className='size-2.5! ml-auto transition-transform group-data-[open]/category:rotate-90' />
230-
</CollapsibleTrigger>
231-
<CollapsibleContent>
232-
<div className='border-sidebar-border ml-3 border-l pl-1'>
233-
<SidebarEntryMenu
234-
components={components}
235-
groupSlug={groupSlug}
236-
pathname={pathname}
237-
/>
238-
</div>
239-
</CollapsibleContent>
240-
</Collapsible>
241-
) : (
242-
<SidebarEntryMenu
243-
key={group}
244-
components={components}
245-
groupSlug={groupSlug}
246-
pathname={pathname}
247-
/>
248-
),
249-
)}
250-
</SidebarGroupContent>
251-
</CollapsibleContent>
252-
</SidebarGroup>
253-
</Collapsible>
254-
</React.Fragment>
255-
))}
92+
<SidebarNavigation groups={navigationGroups} />
25693
</SidebarContent>
25794
<SidebarFooter className='gap-0 p-0'>
25895
<SidebarSeparator className='data-horizontal:w-auto my-1' />

0 commit comments

Comments
 (0)