Spaces:
Running
Running
Download web/src/components/shell/app-shell.tsx from Aynkhn/WorkPilot-AI: direct link, hf CLI and curl.
- Browser
- Download file 11.8 kB
-
https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/shell/app-shell.tsx
- Command line
-
hf download hf://spaces/Aynkhn/WorkPilot-AI/web/src/components/shell/app-shell.tsx
-
curl -L -o app-shell.tsx https://huggingface.co/spaces/Aynkhn/WorkPilot-AI/resolve/main/web/src/components/shell/app-shell.tsx
11.8 kB
| "use client"; | |
| import { | |
| ActivityIcon, | |
| BanknoteIcon, | |
| BarChart3Icon, | |
| BookOpenIcon, | |
| BrainIcon, | |
| Building2Icon, | |
| CheckIcon, | |
| ChevronsUpDownIcon, | |
| InboxIcon, | |
| LanguagesIcon, | |
| LogOutIcon, | |
| MenuIcon, | |
| MessageSquareIcon, | |
| MonitorIcon, | |
| MoonIcon, | |
| PackageIcon, | |
| PlugIcon, | |
| ScaleIcon, | |
| SettingsIcon, | |
| SunIcon, | |
| WorkflowIcon, | |
| type LucideIcon, | |
| } from "lucide-react"; | |
| import Link from "next/link"; | |
| import { usePathname, useRouter } from "next/navigation"; | |
| import { useTheme } from "next-themes"; | |
| import { useState, type ReactNode } from "react"; | |
| import { toast } from "sonner"; | |
| import { Button } from "@/components/ui/button"; | |
| import { | |
| DropdownMenu, | |
| DropdownMenuContent, | |
| DropdownMenuItem, | |
| DropdownMenuLabel, | |
| DropdownMenuRadioGroup, | |
| DropdownMenuRadioItem, | |
| DropdownMenuSeparator, | |
| DropdownMenuTrigger, | |
| } from "@/components/ui/dropdown-menu"; | |
| import { Sheet, SheetContent, SheetTitle, SheetTrigger } from "@/components/ui/sheet"; | |
| import { useAuth } from "@/lib/auth"; | |
| import { useI18n, type MessageKey } from "@/lib/i18n"; | |
| import type { Locale } from "@/lib/i18n/messages"; | |
| import type { ApprovalItem } from "@/lib/types"; | |
| import { useApi } from "@/lib/use-api"; | |
| import { cn } from "@/lib/utils"; | |
| interface NavItem { | |
| href: string; | |
| label: MessageKey; | |
| icon: LucideIcon; | |
| } | |
| const NAV: { group: MessageKey; items: NavItem[] }[] = [ | |
| { | |
| group: "nav.operate", | |
| items: [ | |
| { href: "/inbox", label: "nav.inbox", icon: InboxIcon }, | |
| { href: "/runs", label: "nav.runs", icon: ActivityIcon }, | |
| { href: "/workflows", label: "nav.workflows", icon: WorkflowIcon }, | |
| { href: "/chat", label: "nav.chat", icon: MessageSquareIcon }, | |
| ], | |
| }, | |
| { | |
| group: "nav.commerce", | |
| items: [ | |
| { href: "/orders", label: "nav.orders", icon: PackageIcon }, | |
| { href: "/cod", label: "nav.cod", icon: BanknoteIcon }, | |
| ], | |
| }, | |
| { | |
| group: "nav.insight", | |
| items: [ | |
| { href: "/analytics", label: "nav.analytics", icon: BarChart3Icon }, | |
| { href: "/knowledge", label: "nav.knowledge", icon: BookOpenIcon }, | |
| { href: "/memory", label: "nav.memory", icon: BrainIcon }, | |
| { href: "/integrations", label: "nav.integrations", icon: PlugIcon }, | |
| { href: "/rules", label: "nav.rules", icon: ScaleIcon }, | |
| { href: "/settings", label: "nav.settings", icon: SettingsIcon }, | |
| ], | |
| }, | |
| ]; | |
| function Logo() { | |
| const { t } = useI18n(); | |
| return ( | |
| <Link href="/inbox" className="flex items-center gap-2 px-2 font-semibold tracking-tight"> | |
| <span | |
| className="grid size-7 place-items-center rounded-md bg-primary text-primary-foreground" | |
| aria-hidden | |
| > | |
| <svg viewBox="0 0 24 24" className="size-4" fill="none" stroke="currentColor" strokeWidth={2.2}> | |
| <path d="M4 7l4 10 4-7 4 7 4-10" strokeLinecap="round" strokeLinejoin="round" /> | |
| </svg> | |
| </span> | |
| <span>{t("app.name")}</span> | |
| </Link> | |
| ); | |
| } | |
| function NavLinks({ onNavigate }: { onNavigate?: () => void }) { | |
| const { t } = useI18n(); | |
| const pathname = usePathname(); | |
| const pending = useApi<ApprovalItem[]>("/approvals", { pollMs: 15_000 }); | |
| const count = pending.data?.length ?? 0; | |
| return ( | |
| <nav className="space-y-5" aria-label={t("common.mainNav")}> | |
| {NAV.map((g) => ( | |
| <div key={g.group} className="space-y-1"> | |
| <p className="px-2 text-[11px] font-medium tracking-wide text-muted-foreground uppercase"> | |
| {t(g.group)} | |
| </p> | |
| {g.items.map((item) => { | |
| const active = pathname === item.href || pathname.startsWith(`${item.href}/`); | |
| return ( | |
| <Link | |
| key={item.href} | |
| href={item.href} | |
| onClick={onNavigate} | |
| aria-current={active ? "page" : undefined} | |
| className={cn( | |
| "flex items-center gap-2.5 rounded-md px-2 py-1.5 text-sm transition-colors", | |
| active | |
| ? "bg-sidebar-accent font-medium text-sidebar-accent-foreground" | |
| : "text-sidebar-foreground/80 hover:bg-sidebar-accent/60 hover:text-sidebar-accent-foreground", | |
| )} | |
| > | |
| <item.icon className="size-4 shrink-0" aria-hidden /> | |
| <span className="flex-1">{t(item.label)}</span> | |
| {item.href === "/inbox" && count > 0 && ( | |
| <span | |
| className="tabular min-w-5 rounded-full bg-warning-soft px-1.5 text-center text-[11px] font-semibold text-warning" | |
| data-testid="inbox-count" | |
| > | |
| {count} | |
| </span> | |
| )} | |
| </Link> | |
| ); | |
| })} | |
| </div> | |
| ))} | |
| </nav> | |
| ); | |
| } | |
| function TenantSwitcher() { | |
| const { me, switchTenant } = useAuth(); | |
| const { t, m } = useI18n(); | |
| const router = useRouter(); | |
| if (!me) return null; | |
| return ( | |
| <DropdownMenu> | |
| <DropdownMenuTrigger asChild> | |
| <Button | |
| variant="ghost" | |
| className="h-auto w-full justify-between gap-2 px-2 py-1.5 text-start" | |
| data-testid="tenant-switcher" | |
| > | |
| <span className="flex min-w-0 items-center gap-2"> | |
| <Building2Icon className="size-4 shrink-0 text-muted-foreground" aria-hidden /> | |
| <span className="min-w-0"> | |
| <span className="block truncate text-sm font-medium">{me.tenant.name}</span> | |
| <span className="block text-[11px] text-muted-foreground"> | |
| {m.settings.roles[me.role]} | |
| </span> | |
| </span> | |
| </span> | |
| <ChevronsUpDownIcon className="size-3.5 text-muted-foreground" aria-hidden /> | |
| </Button> | |
| </DropdownMenuTrigger> | |
| <DropdownMenuContent align="start" className="w-60"> | |
| <DropdownMenuLabel>{t("common.workspace")}</DropdownMenuLabel> | |
| {me.tenants.map((tn) => ( | |
| <DropdownMenuItem | |
| key={tn.id} | |
| onSelect={async () => { | |
| if (tn.id === me.tenant.id) return; | |
| try { | |
| await switchTenant(tn.id); | |
| router.push("/inbox"); | |
| } catch { | |
| toast.error(t("common.error")); | |
| } | |
| }} | |
| > | |
| <span className="flex-1 truncate">{tn.name}</span> | |
| <span className="text-[11px] text-muted-foreground">{m.settings.roles[tn.role]}</span> | |
| {tn.id === me.tenant.id && <CheckIcon className="size-3.5" aria-hidden />} | |
| </DropdownMenuItem> | |
| ))} | |
| </DropdownMenuContent> | |
| </DropdownMenu> | |
| ); | |
| } | |
| function Preferences() { | |
| const { t, locale, setLocale } = useI18n(); | |
| const { theme, setTheme } = useTheme(); | |
| const { me, logout } = useAuth(); | |
| const router = useRouter(); | |
| return ( | |
| <div className="flex items-center gap-1"> | |
| <Button | |
| variant="ghost" | |
| size="sm" | |
| onClick={() => setLocale(locale === "en" ? "ur" : "en")} | |
| aria-label={t("common.language")} | |
| data-testid="locale-toggle" | |
| className={locale === "en" ? "font-urdu" : ""} | |
| > | |
| <LanguagesIcon aria-hidden /> | |
| {locale === "en" ? "اردو" : "English"} | |
| </Button> | |
| <DropdownMenu> | |
| <DropdownMenuTrigger asChild> | |
| <Button variant="ghost" size="icon-sm" aria-label={t("common.theme")} data-testid="theme-toggle"> | |
| <SunIcon className="dark:hidden" aria-hidden /> | |
| <MoonIcon className="hidden dark:block" aria-hidden /> | |
| </Button> | |
| </DropdownMenuTrigger> | |
| <DropdownMenuContent align="end"> | |
| <DropdownMenuLabel>{t("common.theme")}</DropdownMenuLabel> | |
| <DropdownMenuRadioGroup value={theme ?? "system"} onValueChange={setTheme}> | |
| <DropdownMenuRadioItem value="light"> | |
| <SunIcon aria-hidden /> {t("common.light")} | |
| </DropdownMenuRadioItem> | |
| <DropdownMenuRadioItem value="dark"> | |
| <MoonIcon aria-hidden /> {t("common.dark")} | |
| </DropdownMenuRadioItem> | |
| <DropdownMenuRadioItem value="system"> | |
| <MonitorIcon aria-hidden /> {t("common.system")} | |
| </DropdownMenuRadioItem> | |
| </DropdownMenuRadioGroup> | |
| <DropdownMenuSeparator /> | |
| <DropdownMenuLabel>{t("common.language")}</DropdownMenuLabel> | |
| <DropdownMenuRadioGroup value={locale} onValueChange={(v) => setLocale(v as Locale)}> | |
| <DropdownMenuRadioItem value="en">English</DropdownMenuRadioItem> | |
| <DropdownMenuRadioItem value="ur" className="font-urdu"> | |
| اردو | |
| </DropdownMenuRadioItem> | |
| </DropdownMenuRadioGroup> | |
| </DropdownMenuContent> | |
| </DropdownMenu> | |
| {me && ( | |
| <DropdownMenu> | |
| <DropdownMenuTrigger asChild> | |
| <Button | |
| variant="ghost" | |
| size="icon-sm" | |
| className="rounded-full" | |
| aria-label={me.user.email} | |
| data-testid="user-menu" | |
| > | |
| <span className="grid size-7 place-items-center rounded-full bg-muted text-xs font-semibold uppercase"> | |
| {me.user.email.slice(0, 1)} | |
| </span> | |
| </Button> | |
| </DropdownMenuTrigger> | |
| <DropdownMenuContent align="end" className="w-56"> | |
| <DropdownMenuLabel className="truncate font-normal text-muted-foreground" dir="ltr"> | |
| {me.user.email} | |
| </DropdownMenuLabel> | |
| <DropdownMenuSeparator /> | |
| <DropdownMenuItem | |
| onSelect={async () => { | |
| await logout(); | |
| router.replace("/login"); | |
| }} | |
| > | |
| <LogOutIcon aria-hidden /> | |
| {t("common.signOut")} | |
| </DropdownMenuItem> | |
| </DropdownMenuContent> | |
| </DropdownMenu> | |
| )} | |
| </div> | |
| ); | |
| } | |
| export function AppShell({ children }: { children: ReactNode }) { | |
| const [open, setOpen] = useState(false); | |
| const { dir, t } = useI18n(); | |
| return ( | |
| <div className="flex min-h-dvh"> | |
| <aside className="sticky top-0 hidden h-dvh w-60 shrink-0 flex-col gap-4 border-e bg-sidebar px-3 py-4 md:flex"> | |
| <Logo /> | |
| <TenantSwitcher /> | |
| <div className="flex-1 overflow-y-auto"> | |
| <NavLinks /> | |
| </div> | |
| </aside> | |
| <div className="flex min-w-0 flex-1 flex-col"> | |
| <header className="sticky top-0 z-20 flex h-14 items-center gap-2 border-b bg-background/90 px-4 backdrop-blur"> | |
| <Sheet open={open} onOpenChange={setOpen}> | |
| <SheetTrigger asChild> | |
| <Button variant="ghost" size="icon-sm" className="md:hidden" aria-label={t("common.menu")}> | |
| <MenuIcon aria-hidden /> | |
| </Button> | |
| </SheetTrigger> | |
| <SheetContent side={dir === "rtl" ? "right" : "left"} className="w-64 gap-4 px-3 py-4"> | |
| <SheetTitle className="sr-only">{t("common.menu")}</SheetTitle> | |
| <Logo /> | |
| <TenantSwitcher /> | |
| <NavLinks onNavigate={() => setOpen(false)} /> | |
| </SheetContent> | |
| </Sheet> | |
| <div className="md:hidden"> | |
| <Logo /> | |
| </div> | |
| <div className="ms-auto"> | |
| <Preferences /> | |
| </div> | |
| </header> | |
| <main id="main" className="mx-auto w-full max-w-6xl flex-1 px-4 py-6 md:px-8"> | |
| {children} | |
| </main> | |
| </div> | |
| </div> | |
| ); | |
| } | |