refactor(layout): streamline layout components with RoutedSectionShell

- Introduced RoutedSectionShell to unify navigation structure across Playground, User Settings, and Workspace Settings layouts.
- Replaced individual navigation implementations with a more cohesive and reusable component.
- Updated navigation items to include href properties for better routing.
- Enhanced mobile navigation experience with drawer support and improved scroll handling.
This commit is contained in:
Anish Sarkar 2026-07-08 02:29:33 +05:30
parent 9109f0aa05
commit 0c2db7dfd5
6 changed files with 357 additions and 371 deletions

View file

@ -1,155 +1,26 @@
"use client";
import { ChevronRight, History, LayoutGrid } from "lucide-react";
import Link from "next/link";
import { History, LayoutGrid } from "lucide-react";
import { useSelectedLayoutSegments } from "next/navigation";
import type React from "react";
import { useEffect, useMemo, useState } from "react";
import { Button } from "@/components/ui/button";
import { useMemo } from "react";
import {
Drawer,
DrawerContent,
DrawerHandle,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
import { Separator } from "@/components/ui/separator";
import { PLAYGROUND_PLATFORMS, type PlaygroundPlatform, type PlaygroundVerb } from "@/lib/playground/catalog";
import { cn } from "@/lib/utils";
type RoutedSectionGroup,
type RoutedSectionItem,
RoutedSectionShell,
} from "@/components/layout";
import { PLAYGROUND_PLATFORMS } from "@/lib/playground/catalog";
interface PlaygroundLayoutShellProps {
workspaceId: string;
children: React.ReactNode;
}
interface TopLevelNavItem {
value: "overview" | "runs";
label: string;
href: string;
icon: React.ReactNode;
}
function TopLevelNavLink({
item,
activeValue,
onNavigate,
}: {
item: TopLevelNavItem;
activeValue: string;
onNavigate?: () => void;
}) {
const isActive = activeValue === item.value;
return (
<Link
href={item.href}
replace
scroll={false}
prefetch
onClick={onNavigate}
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
isActive
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
<span className="min-w-0 truncate">{item.label}</span>
</Link>
);
}
function ProviderNavGroup({
platform,
base,
activeValue,
isExpanded,
onToggle,
onNavigate,
}: {
platform: PlaygroundPlatform;
base: string;
activeValue: string;
isExpanded: boolean;
onToggle: () => void;
onNavigate?: () => void;
}) {
const Icon = platform.icon;
return (
<div className="flex flex-col gap-0.5">
<button
type="button"
aria-expanded={isExpanded}
onClick={onToggle}
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
isExpanded
? "text-accent-foreground"
: "text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
<Icon className="h-4 w-4 shrink-0" />
<span className="min-w-0 truncate">{platform.label}</span>
<ChevronRight
className={cn("ml-auto h-4 w-4 shrink-0 transition-transform", isExpanded && "rotate-90")}
/>
</button>
<div
className={cn(
"grid overflow-hidden transition-[grid-template-rows,opacity] duration-200 ease-out",
isExpanded ? "grid-rows-[1fr] opacity-100" : "grid-rows-[0fr] opacity-0"
)}
aria-hidden={!isExpanded}
>
<div className="min-h-0 overflow-hidden">
<div className="flex flex-col gap-0.5 pl-10">
{platform.verbs.map((verb) => {
const value = `${platform.id}/${verb.verb}`;
const isActive = activeValue === value;
return (
<Link
key={value}
href={`${base}/${platform.id}/${verb.verb}`}
replace
scroll={false}
prefetch
onClick={onNavigate}
className={cn(
"inline-flex h-auto items-center justify-start rounded-md px-3 py-2 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
isActive
? "bg-accent text-accent-foreground"
: "text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
<span className="min-w-0 truncate">{verb.label}</span>
</Link>
);
})}
</div>
</div>
</div>
</div>
);
}
export function PlaygroundLayoutShell({ workspaceId, children }: PlaygroundLayoutShellProps) {
const segments = useSelectedLayoutSegments();
const base = `/dashboard/${workspaceId}/playground`;
const activePlatform = PLAYGROUND_PLATFORMS.some((platform) => platform.id === segments[0])
? segments[0]
: null;
const [expandedProvider, setExpandedProvider] = useState<string | null>(activePlatform);
const [mobileNavOpen, setMobileNavOpen] = useState(false);
useEffect(() => {
if (activePlatform) {
setExpandedProvider(activePlatform);
}
}, [activePlatform]);
const topLevelItems = useMemo<TopLevelNavItem[]>(
const topLevelItems = useMemo<RoutedSectionItem[]>(
() => [
{
value: "overview",
@ -167,6 +38,24 @@ export function PlaygroundLayoutShell({ workspaceId, children }: PlaygroundLayou
[base]
);
const providerGroups = useMemo<RoutedSectionGroup[]>(
() =>
PLAYGROUND_PLATFORMS.map((platform) => {
const Icon = platform.icon;
return {
value: platform.id,
label: platform.label,
icon: <Icon className="h-4 w-4 shrink-0" />,
items: platform.verbs.map((verb) => ({
value: `${platform.id}/${verb.verb}`,
label: verb.label,
href: `${base}/${platform.id}/${verb.verb}`,
})),
};
}),
[base]
);
const activeValue =
segments.length >= 2
? `${segments[0]}/${segments[1]}`
@ -174,86 +63,33 @@ export function PlaygroundLayoutShell({ workspaceId, children }: PlaygroundLayou
? segments[0]
: "overview";
const selectedLabel = getSelectedLabel(activeValue, topLevelItems);
const renderNav = (onNavigate?: () => void) => (
<>
{topLevelItems.map((item) => (
<TopLevelNavLink
key={item.value}
item={item}
activeValue={activeValue}
onNavigate={onNavigate}
/>
))}
<Separator className="my-3 bg-border" />
<div className="flex flex-col gap-0.5">
{PLAYGROUND_PLATFORMS.map((platform) => (
<ProviderNavGroup
key={platform.id}
platform={platform}
base={base}
activeValue={activeValue}
isExpanded={expandedProvider === platform.id}
onToggle={() =>
setExpandedProvider((current) => (current === platform.id ? null : platform.id))
}
onNavigate={onNavigate}
/>
))}
</div>
</>
);
const selectedLabel = getSelectedLabel(activeValue, topLevelItems, providerGroups);
return (
<section className="flex h-full min-h-[min(680px,calc(100vh-5rem))] w-full select-none flex-col gap-6 md:flex-row">
<div className="md:w-[220px] md:shrink-0">
<h1 className="mb-4 px-1 text-xl font-semibold tracking-tight text-foreground md:text-2xl">
API Playground
</h1>
<nav className="hidden flex-col gap-0.5 md:flex">
{renderNav()}
</nav>
<Drawer open={mobileNavOpen} onOpenChange={setMobileNavOpen} shouldScaleBackground={false}>
<DrawerTrigger asChild>
<Button
type="button"
variant="outline"
className="flex h-10 w-full justify-between bg-transparent px-3 hover:bg-accent md:hidden"
>
<span className="truncate">{selectedLabel}</span>
<ChevronRight className="h-4 w-4 rotate-90 text-muted-foreground" />
</Button>
</DrawerTrigger>
<DrawerContent className="h-[88vh] overflow-hidden rounded-t-2xl border bg-popover text-popover-foreground">
<DrawerHandle className="mt-3 h-1.5 w-10" />
<DrawerTitle className="sr-only">API Playground navigation</DrawerTitle>
<nav className="flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto p-4">
{renderNav(() => setMobileNavOpen(false))}
</nav>
</DrawerContent>
</Drawer>
</div>
<div className="min-w-0 flex-1">
<div className="hidden md:block">
<h2 className="text-lg font-semibold">{selectedLabel}</h2>
<Separator className="mt-4 bg-border" />
</div>
<div className="min-w-0 pt-4">{children}</div>
</div>
</section>
<RoutedSectionShell
title="API Playground"
items={topLevelItems}
groups={providerGroups}
activeValue={activeValue}
selectedLabel={selectedLabel}
mobileNav="drawer"
>
{children}
</RoutedSectionShell>
);
}
function getSelectedLabel(activeValue: string, topLevelItems: TopLevelNavItem[]): string {
const topLevelItem = topLevelItems.find((item) => item.value === activeValue);
function getSelectedLabel(
activeValue: string,
items: RoutedSectionItem[],
groups: RoutedSectionGroup[]
): string {
const topLevelItem = items.find((item) => item.value === activeValue);
if (topLevelItem) return topLevelItem.label;
const [platformId, verbId] = activeValue.split("/");
const platform = PLAYGROUND_PLATFORMS.find((item) => item.id === platformId);
const verb: PlaygroundVerb | undefined = platform?.verbs.find((item) => item.verb === verbId);
const group = groups.find((item) => item.items.some((child) => child.value === activeValue));
const child = group?.items.find((item) => item.value === activeValue);
if (platform && verb) return `${platform.label} / ${verb.label}`;
if (group && child) return `${group.label} / ${child.label}`;
return "API Playground";
}

View file

@ -11,14 +11,12 @@ import {
ShieldCheck,
WandSparkles,
} from "lucide-react";
import Link from "next/link";
import { useSelectedLayoutSegment } from "next/navigation";
import { useTranslations } from "next-intl";
import type React from "react";
import { useCallback, useMemo, useState } from "react";
import { Separator } from "@/components/ui/separator";
import { useMemo } from "react";
import { type RoutedSectionItem, RoutedSectionShell } from "@/components/layout";
import { usePlatform } from "@/hooks/use-platform";
import { cn } from "@/lib/utils";
export type UserSettingsTab =
| "profile"
@ -42,50 +40,49 @@ export function UserSettingsLayoutShell({ workspaceId, children }: UserSettingsL
const t = useTranslations("userSettings");
const { isDesktop } = usePlatform();
const segment = useSelectedLayoutSegment();
const [tabScrollPos, setTabScrollPos] = useState<"start" | "middle" | "end">("start");
const handleTabScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
const el = e.currentTarget;
const atStart = el.scrollLeft <= 2;
const atEnd = el.scrollWidth - el.scrollLeft - el.clientWidth <= 2;
setTabScrollPos(atStart ? "start" : atEnd ? "end" : "middle");
}, []);
const navItems = useMemo(
const navItems = useMemo<RoutedSectionItem[]>(
() => [
{
value: "profile" as const,
label: t("profile_nav_label"),
href: `/dashboard/${workspaceId}/user-settings/profile`,
icon: <CircleUser className="h-4 w-4" />,
},
{
value: "api-key" as const,
label: t("api_key_nav_label"),
href: `/dashboard/${workspaceId}/user-settings/api-key`,
icon: <KeyRound className="h-4 w-4" />,
},
{
value: "prompts" as const,
label: "My Prompts",
href: `/dashboard/${workspaceId}/user-settings/prompts`,
icon: <WandSparkles className="h-4 w-4" />,
},
{
value: "community-prompts" as const,
label: "Community Prompts",
href: `/dashboard/${workspaceId}/user-settings/community-prompts`,
icon: <Library className="h-4 w-4" />,
},
{
value: "agent-permissions" as const,
label: "Agent Permissions",
href: `/dashboard/${workspaceId}/user-settings/agent-permissions`,
icon: <ShieldCheck className="h-4 w-4" />,
},
{
value: "messaging-channels" as const,
label: "Messaging Channels",
href: `/dashboard/${workspaceId}/user-settings/messaging-channels`,
icon: <MessageCircle className="h-4 w-4" />,
},
{
value: "purchases" as const,
label: "Purchase History",
href: `/dashboard/${workspaceId}/user-settings/purchases`,
icon: <ReceiptText className="h-4 w-4" />,
},
...(isDesktop
@ -93,17 +90,19 @@ export function UserSettingsLayoutShell({ workspaceId, children }: UserSettingsL
{
value: "desktop" as const,
label: "App Preferences",
href: `/dashboard/${workspaceId}/user-settings/desktop`,
icon: <Monitor className="h-4 w-4" />,
},
{
value: "hotkeys" as const,
label: "Hotkeys",
href: `/dashboard/${workspaceId}/user-settings/hotkeys`,
icon: <Keyboard className="h-4 w-4" />,
},
]
: []),
],
[t, isDesktop]
[t, isDesktop, workspaceId]
);
const activeTab: UserSettingsTab =
@ -112,72 +111,15 @@ export function UserSettingsLayoutShell({ workspaceId, children }: UserSettingsL
: DEFAULT_TAB;
const selectedLabel = navItems.find((item) => item.value === activeTab)?.label ?? t("title");
const hrefFor = (tab: UserSettingsTab) => `/dashboard/${workspaceId}/user-settings/${tab}`;
return (
<section className="flex h-full min-h-[min(680px,calc(100vh-5rem))] w-full select-none flex-col gap-6 md:flex-row">
<div className="md:w-[220px] md:shrink-0">
<h1 className="mb-4 px-1 text-xl font-semibold tracking-tight text-foreground md:text-2xl">
{t("title")}
</h1>
<nav className="hidden flex-col gap-0.5 md:flex">
{navItems.map((item) => (
<Link
key={item.value}
href={hrefFor(item.value)}
replace
scroll={false}
prefetch
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
activeTab === item.value
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
{item.label}
</Link>
))}
</nav>
<div
className="overflow-x-auto border-b border-border pb-3 md:hidden"
onScroll={handleTabScroll}
style={{
maskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
WebkitMaskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
}}
>
<div className="flex gap-1">
{navItems.map((item) => (
<Link
key={item.value}
href={hrefFor(item.value)}
replace
scroll={false}
prefetch
className={cn(
"inline-flex h-auto shrink-0 items-center gap-2 rounded-md px-3 py-1.5 text-xs font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
activeTab === item.value
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
{item.label}
</Link>
))}
</div>
</div>
</div>
<div className="min-w-0 flex-1">
<div className="hidden md:block">
<h2 className="text-lg font-semibold">{selectedLabel}</h2>
<Separator className="mt-4 bg-border" />
</div>
<div className="min-w-0 pt-4 md:max-w-3xl">{children}</div>
</div>
</section>
<RoutedSectionShell
title={t("title")}
items={navItems}
activeValue={activeTab}
selectedLabel={selectedLabel}
contentClassName="md:max-w-3xl"
>
{children}
</RoutedSectionShell>
);
}

View file

@ -1,13 +1,11 @@
"use client";
import { BookText, Cpu, Earth, Settings, UserKey } from "lucide-react";
import Link from "next/link";
import { useSelectedLayoutSegment } from "next/navigation";
import { useTranslations } from "next-intl";
import type React from "react";
import { useCallback, useMemo, useState } from "react";
import { Separator } from "@/components/ui/separator";
import { cn } from "@/lib/utils";
import { useMemo } from "react";
import { type RoutedSectionItem, RoutedSectionShell } from "@/components/layout";
export type WorkspaceSettingsTab = "general" | "models" | "team-roles" | "prompts" | "public-links";
@ -24,44 +22,41 @@ export function WorkspaceSettingsLayoutShell({
}: WorkspaceSettingsLayoutShellProps) {
const t = useTranslations("workspaceSettings");
const segment = useSelectedLayoutSegment();
const [tabScrollPos, setTabScrollPos] = useState<"start" | "middle" | "end">("start");
const handleTabScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
const el = e.currentTarget;
const atStart = el.scrollLeft <= 2;
const atEnd = el.scrollWidth - el.scrollLeft - el.clientWidth <= 2;
setTabScrollPos(atStart ? "start" : atEnd ? "end" : "middle");
}, []);
const navItems = useMemo(
const navItems = useMemo<RoutedSectionItem[]>(
() => [
{
value: "general" as const,
label: t("nav_general"),
href: `/dashboard/${workspaceId}/workspace-settings/general`,
icon: <Settings className="h-4 w-4" />,
},
{
value: "models" as const,
label: t("nav_models"),
href: `/dashboard/${workspaceId}/workspace-settings/models`,
icon: <Cpu className="h-4 w-4" />,
},
{
value: "team-roles" as const,
label: t("nav_team_roles"),
href: `/dashboard/${workspaceId}/workspace-settings/team-roles`,
icon: <UserKey className="h-4 w-4" />,
},
{
value: "prompts" as const,
label: t("nav_system_instructions"),
href: `/dashboard/${workspaceId}/workspace-settings/prompts`,
icon: <BookText className="h-4 w-4" />,
},
{
value: "public-links" as const,
label: t("nav_public_links"),
href: `/dashboard/${workspaceId}/workspace-settings/public-links`,
icon: <Earth className="h-4 w-4" />,
},
],
[t]
[t, workspaceId]
);
const activeTab: WorkspaceSettingsTab =
@ -70,73 +65,15 @@ export function WorkspaceSettingsLayoutShell({
: DEFAULT_TAB;
const selectedLabel = navItems.find((item) => item.value === activeTab)?.label ?? t("title");
const hrefFor = (tab: WorkspaceSettingsTab) =>
`/dashboard/${workspaceId}/workspace-settings/${tab}`;
return (
<section className="flex h-full min-h-[min(680px,calc(100vh-5rem))] w-full select-none flex-col gap-6 md:flex-row">
<div className="md:w-[220px] md:shrink-0">
<h1 className="mb-4 px-1 text-xl font-semibold tracking-tight text-foreground md:text-2xl">
{t("title")}
</h1>
<nav className="hidden flex-col gap-0.5 md:flex">
{navItems.map((item) => (
<Link
key={item.value}
href={hrefFor(item.value)}
replace
scroll={false}
prefetch
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
activeTab === item.value
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
{item.label}
</Link>
))}
</nav>
<div
className="overflow-x-auto border-b border-border pb-3 md:hidden"
onScroll={handleTabScroll}
style={{
maskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
WebkitMaskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
}}
>
<div className="flex gap-1">
{navItems.map((item) => (
<Link
key={item.value}
href={hrefFor(item.value)}
replace
scroll={false}
prefetch
className={cn(
"inline-flex h-auto shrink-0 items-center gap-2 rounded-md px-3 py-1.5 text-xs font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
activeTab === item.value
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
{item.label}
</Link>
))}
</div>
</div>
</div>
<div className="min-w-0 flex-1">
<div className="hidden md:block">
<h2 className="text-lg font-semibold">{selectedLabel}</h2>
<Separator className="mt-4" />
</div>
<div className="min-w-0 pt-4 md:max-w-3xl">{children}</div>
</div>
</section>
<RoutedSectionShell
title={t("title")}
items={navItems}
activeValue={activeTab}
selectedLabel={selectedLabel}
contentClassName="md:max-w-3xl"
>
{children}
</RoutedSectionShell>
);
}

View file

@ -9,6 +9,7 @@ export type {
User,
Workspace,
} from "./types/layout.types";
export type { RoutedSectionGroup, RoutedSectionItem } from "./ui";
export {
ChatListItem,
CreateWorkspaceDialog,
@ -20,6 +21,7 @@ export {
MobileSidebarTrigger,
NavIcon,
NavSection,
RoutedSectionShell,
Sidebar,
SidebarCollapseButton,
SidebarHeader,

View file

@ -0,0 +1,267 @@
"use client";
import { ChevronRight } from "lucide-react";
import Link from "next/link";
import type React from "react";
import { useCallback, useEffect, useState } from "react";
import { Button } from "@/components/ui/button";
import {
Drawer,
DrawerContent,
DrawerHandle,
DrawerTitle,
DrawerTrigger,
} from "@/components/ui/drawer";
import { Separator } from "@/components/ui/separator";
import { cn } from "@/lib/utils";
export interface RoutedSectionItem {
value: string;
label: string;
href: string;
icon?: React.ReactNode;
}
export interface RoutedSectionGroup {
value: string;
label: string;
icon?: React.ReactNode;
items: RoutedSectionItem[];
}
interface RoutedSectionShellProps {
title: string;
items: RoutedSectionItem[];
activeValue: string;
selectedLabel: string;
children: React.ReactNode;
groups?: RoutedSectionGroup[];
mobileNav?: "scroll" | "drawer";
contentClassName?: string;
}
function findActiveGroupValue(groups: RoutedSectionGroup[], activeValue: string): string | null {
return (
groups.find((group) => group.items.some((item) => item.value === activeValue))?.value ?? null
);
}
function SectionNavLink({
item,
activeValue,
onNavigate,
}: {
item: RoutedSectionItem;
activeValue: string;
onNavigate?: () => void;
}) {
const isActive = activeValue === item.value;
return (
<Link
href={item.href}
replace
scroll={false}
prefetch
onClick={onNavigate}
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
isActive
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
<span className="min-w-0 truncate">{item.label}</span>
</Link>
);
}
function SectionNavGroup({
group,
activeValue,
isExpanded,
onToggle,
onNavigate,
}: {
group: RoutedSectionGroup;
activeValue: string;
isExpanded: boolean;
onToggle: () => void;
onNavigate?: () => void;
}) {
return (
<div className="flex flex-col gap-0.5">
<button
type="button"
aria-expanded={isExpanded}
onClick={onToggle}
className={cn(
"inline-flex h-auto items-center justify-start gap-3 rounded-md px-3 py-2.5 text-left text-sm font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
isExpanded
? "text-accent-foreground"
: "text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{group.icon}
<span className="min-w-0 truncate">{group.label}</span>
<ChevronRight
className={cn("ml-auto h-4 w-4 shrink-0 transition-transform", isExpanded && "rotate-90")}
/>
</button>
<div
className={cn(
"grid overflow-hidden transition-[grid-template-rows,opacity] duration-200 ease-out",
isExpanded ? "grid-rows-[1fr] opacity-100" : "grid-rows-[0fr] opacity-0"
)}
aria-hidden={!isExpanded}
>
<div className="min-h-0 overflow-hidden">
<div className="flex flex-col gap-0.5 pl-10">
{group.items.map((item) => (
<SectionNavLink
key={item.value}
item={item}
activeValue={activeValue}
onNavigate={onNavigate}
/>
))}
</div>
</div>
</div>
</div>
);
}
export function RoutedSectionShell({
title,
items,
activeValue,
selectedLabel,
children,
groups = [],
mobileNav = "scroll",
contentClassName,
}: RoutedSectionShellProps) {
const [tabScrollPos, setTabScrollPos] = useState<"start" | "middle" | "end">("start");
const [drawerOpen, setDrawerOpen] = useState(false);
const [expandedGroup, setExpandedGroup] = useState<string | null>(() =>
findActiveGroupValue(groups, activeValue)
);
useEffect(() => {
const activeGroup = findActiveGroupValue(groups, activeValue);
if (activeGroup) {
setExpandedGroup(activeGroup);
}
}, [activeValue, groups]);
const handleTabScroll = useCallback((e: React.UIEvent<HTMLDivElement>) => {
const el = e.currentTarget;
const atStart = el.scrollLeft <= 2;
const atEnd = el.scrollWidth - el.scrollLeft - el.clientWidth <= 2;
setTabScrollPos(atStart ? "start" : atEnd ? "end" : "middle");
}, []);
const renderNav = (onNavigate?: () => void) => (
<>
{items.map((item) => (
<SectionNavLink
key={item.value}
item={item}
activeValue={activeValue}
onNavigate={onNavigate}
/>
))}
{groups.length > 0 && (
<>
<Separator className="my-3 bg-border" />
<div className="flex flex-col gap-0.5">
{groups.map((group) => (
<SectionNavGroup
key={group.value}
group={group}
activeValue={activeValue}
isExpanded={expandedGroup === group.value}
onToggle={() =>
setExpandedGroup((current) => (current === group.value ? null : group.value))
}
onNavigate={onNavigate}
/>
))}
</div>
</>
)}
</>
);
return (
<section className="flex h-full min-h-[min(680px,calc(100vh-5rem))] w-full select-none flex-col gap-6 md:flex-row">
<div className="md:w-[220px] md:shrink-0">
<h1 className="mb-4 px-1 text-xl font-semibold tracking-tight text-foreground md:text-2xl">
{title}
</h1>
<nav className="hidden flex-col gap-0.5 md:flex">{renderNav()}</nav>
{mobileNav === "drawer" ? (
<Drawer open={drawerOpen} onOpenChange={setDrawerOpen} shouldScaleBackground={false}>
<DrawerTrigger asChild>
<Button
type="button"
variant="outline"
className="flex h-10 w-full justify-between bg-transparent px-3 hover:bg-accent md:hidden"
>
<span className="truncate">{selectedLabel}</span>
<ChevronRight className="h-4 w-4 rotate-90 text-muted-foreground" />
</Button>
</DrawerTrigger>
<DrawerContent className="h-[88vh] overflow-hidden rounded-t-2xl border bg-popover text-popover-foreground">
<DrawerHandle className="mt-3 h-1.5 w-10" />
<DrawerTitle className="sr-only">{title} navigation</DrawerTitle>
<nav className="flex min-h-0 flex-1 flex-col gap-0.5 overflow-y-auto p-4">
{renderNav(() => setDrawerOpen(false))}
</nav>
</DrawerContent>
</Drawer>
) : (
<div
className="overflow-x-auto border-b border-border pb-3 md:hidden"
onScroll={handleTabScroll}
style={{
maskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
WebkitMaskImage: `linear-gradient(to right, ${tabScrollPos === "start" ? "black" : "transparent"}, black 24px, black calc(100% - 24px), ${tabScrollPos === "end" ? "black" : "transparent"})`,
}}
>
<div className="flex gap-1">
{items.map((item) => (
<Link
key={item.value}
href={item.href}
replace
scroll={false}
prefetch
className={cn(
"inline-flex h-auto shrink-0 items-center gap-2 rounded-md px-3 py-1.5 text-xs font-medium transition-colors duration-150 focus:outline-none focus-visible:outline-none",
activeValue === item.value
? "bg-accent text-accent-foreground"
: "bg-transparent text-muted-foreground hover:bg-accent hover:text-accent-foreground"
)}
>
{item.icon}
{item.label}
</Link>
))}
</div>
</div>
)}
</div>
<div className="min-w-0 flex-1">
<div className="hidden md:block">
<h2 className="text-lg font-semibold">{selectedLabel}</h2>
<Separator className="mt-4 bg-border" />
</div>
<div className={cn("min-w-0 pt-4", contentClassName)}>{children}</div>
</div>
</section>
);
}

View file

@ -1,6 +1,8 @@
export { CreateWorkspaceDialog } from "./dialogs";
export { Header } from "./header";
export { IconRail, NavIcon, WorkspaceAvatar } from "./icon-rail";
export type { RoutedSectionGroup, RoutedSectionItem } from "./RoutedSectionShell";
export { RoutedSectionShell } from "./RoutedSectionShell";
export { LayoutShell } from "./shell";
export {
ChatListItem,