|
| 1 | +import { Link, Outlet, useRouterState } from "@tanstack/react-router"; |
| 2 | +import { |
| 3 | + Activity, |
| 4 | + FileVideo, |
| 5 | + FlaskConical, |
| 6 | + Gauge, |
| 7 | + Inbox, |
| 8 | + LogOut, |
| 9 | + Moon, |
| 10 | + Search, |
| 11 | + Settings, |
| 12 | + Sun, |
| 13 | + UploadCloud, |
| 14 | +} from "lucide-react"; |
| 15 | +import { motion } from "motion/react"; |
| 16 | +import { useEffect, useState } from "react"; |
| 17 | + |
| 18 | +import { CommandPalette } from "@/components/CommandPalette"; |
| 19 | +import { Login } from "@/components/Login"; |
| 20 | +import { SESSION_CHANGED, getSession, logout } from "@/lib/auth"; |
| 21 | +import { asset, cn } from "@/lib/utils"; |
| 22 | + |
| 23 | +/* Classic-site menu names so migrating devs feel at home. */ |
| 24 | +const sections = [ |
| 25 | + { |
| 26 | + id: "main", |
| 27 | + label: null, |
| 28 | + items: [ |
| 29 | + { to: "/", label: "Home", icon: Inbox }, |
| 30 | + { to: "/runs", label: "Test results", icon: Activity }, |
| 31 | + ], |
| 32 | + }, |
| 33 | + { |
| 34 | + id: "suite", |
| 35 | + label: "Suite", |
| 36 | + items: [ |
| 37 | + { to: "/tests", label: "Regression tests", icon: FlaskConical }, |
| 38 | + { to: "/samples", label: "Samples", icon: FileVideo }, |
| 39 | + { to: "/upload", label: "Sample upload", icon: UploadCloud }, |
| 40 | + ], |
| 41 | + }, |
| 42 | + { |
| 43 | + id: "platform", |
| 44 | + label: "Platform", |
| 45 | + items: [ |
| 46 | + { to: "/status", label: "Platform status", icon: Gauge }, |
| 47 | + { to: "/admin", label: "Administration", icon: Settings }, |
| 48 | + ], |
| 49 | + }, |
| 50 | +] as const; |
| 51 | + |
| 52 | +function useTheme() { |
| 53 | + const [dark, setDark] = useState(() => localStorage.getItem("sp-theme") === "dark"); |
| 54 | + useEffect(() => { |
| 55 | + document.documentElement.classList.toggle("dark", dark); |
| 56 | + localStorage.setItem("sp-theme", dark ? "dark" : "light"); |
| 57 | + }, [dark]); |
| 58 | + return { dark, toggle: () => setDark((d) => !d) }; |
| 59 | +} |
| 60 | + |
| 61 | +const COLLAPSED = 52; |
| 62 | +const EXPANDED = 224; |
| 63 | + |
| 64 | +export function AppShell() { |
| 65 | + const { dark, toggle } = useTheme(); |
| 66 | + const pathname = useRouterState({ select: (s) => s.location.pathname }); |
| 67 | + const [session, setSession] = useState(getSession); |
| 68 | + const [open, setOpen] = useState(false); |
| 69 | + |
| 70 | + // The account page can change the email shown here, so pick the stored |
| 71 | + // session back up instead of leaving the corner reading the old one. |
| 72 | + useEffect(() => { |
| 73 | + const sync = () => setSession(getSession()); |
| 74 | + window.addEventListener(SESSION_CHANGED, sync); |
| 75 | + return () => window.removeEventListener(SESSION_CHANGED, sync); |
| 76 | + }, []); |
| 77 | + |
| 78 | + // The reset screen arrives from an email with nobody signed in, so it |
| 79 | + // renders on its own rather than behind the sign-in gate. |
| 80 | + if (pathname === "/reset") return <Outlet />; |
| 81 | + if (!session) return <Login onDone={() => setSession(getSession())} />; |
| 82 | + |
| 83 | + return ( |
| 84 | + <div className="relative flex h-full overflow-hidden p-2"> |
| 85 | + {/* Rail reserves collapsed width; sidebar overlays on hover so main never reflows. */} |
| 86 | + <div style={{ width: COLLAPSED }} className="shrink-0" /> |
| 87 | + |
| 88 | + <motion.aside |
| 89 | + onMouseEnter={() => setOpen(true)} |
| 90 | + onMouseLeave={() => setOpen(false)} |
| 91 | + initial={false} |
| 92 | + animate={{ width: open ? EXPANDED : COLLAPSED }} |
| 93 | + transition={{ type: "spring", stiffness: 420, damping: 38 }} |
| 94 | + className={cn( |
| 95 | + "absolute inset-y-2 left-2 z-30 flex flex-col overflow-hidden rounded-xl", |
| 96 | + open && "border bg-card shadow-pop", |
| 97 | + )} |
| 98 | + > |
| 99 | + <div className="flex items-center gap-2.5 px-2.5 py-2.5"> |
| 100 | + <img src={asset("ccx.svg")} alt="CCExtractor" className="size-7 shrink-0" /> |
| 101 | + <span |
| 102 | + className={cn( |
| 103 | + "whitespace-nowrap text-[13px] font-semibold tracking-tight transition-opacity", |
| 104 | + open ? "opacity-100" : "opacity-0", |
| 105 | + )} |
| 106 | + > |
| 107 | + Sample Platform |
| 108 | + </span> |
| 109 | + {open && ( |
| 110 | + <button |
| 111 | + onClick={toggle} |
| 112 | + aria-label="Toggle theme" |
| 113 | + className="ml-auto cursor-pointer rounded-md p-1.5 text-faint transition-colors hover:bg-muted hover:text-foreground" |
| 114 | + > |
| 115 | + {dark ? <Sun className="size-3.5" /> : <Moon className="size-3.5" />} |
| 116 | + </button> |
| 117 | + )} |
| 118 | + </div> |
| 119 | + |
| 120 | + <div className="mb-2 px-1.5"> |
| 121 | + {open ? ( |
| 122 | + <CommandPalette /> |
| 123 | + ) : ( |
| 124 | + <div className="flex h-8 items-center rounded-lg px-2 text-faint"> |
| 125 | + <Search className="size-4 shrink-0" /> |
| 126 | + </div> |
| 127 | + )} |
| 128 | + </div> |
| 129 | + |
| 130 | + <nav className="flex flex-1 flex-col gap-3 overflow-y-auto overflow-x-hidden px-1.5"> |
| 131 | + {sections.map((section) => ( |
| 132 | + <div key={section.id} className="flex flex-col gap-px"> |
| 133 | + {/* Always reserve the header row so icons don't shift on expand. */} |
| 134 | + <div className="h-4 px-2 pb-1 text-[11px] font-medium text-faint"> |
| 135 | + <span className={cn("transition-opacity", open ? "opacity-100" : "opacity-0")}> |
| 136 | + {section.label ?? ""} |
| 137 | + </span> |
| 138 | + </div> |
| 139 | + {section.items.map(({ to, label, icon: Icon }) => { |
| 140 | + const active = to === "/" ? pathname === "/" : pathname.startsWith(to); |
| 141 | + return ( |
| 142 | + <Link |
| 143 | + key={to} |
| 144 | + to={to} |
| 145 | + title={label} |
| 146 | + className={cn( |
| 147 | + "relative flex h-8 items-center gap-2.5 rounded-md px-2 text-[13px] font-medium transition-colors", |
| 148 | + active |
| 149 | + ? "text-foreground" |
| 150 | + : "text-muted-foreground hover:bg-muted/70 hover:text-foreground", |
| 151 | + )} |
| 152 | + > |
| 153 | + {active && ( |
| 154 | + <motion.span |
| 155 | + layoutId="nav-active" |
| 156 | + className="absolute inset-0 rounded-md bg-muted shadow-card" |
| 157 | + transition={{ type: "spring", stiffness: 500, damping: 40 }} |
| 158 | + /> |
| 159 | + )} |
| 160 | + <Icon className={cn("relative size-4 shrink-0", active ? "text-primary" : "text-faint")} /> |
| 161 | + <span |
| 162 | + className={cn( |
| 163 | + "relative whitespace-nowrap transition-opacity", |
| 164 | + open ? "opacity-100" : "opacity-0", |
| 165 | + )} |
| 166 | + > |
| 167 | + {label} |
| 168 | + </span> |
| 169 | + </Link> |
| 170 | + ); |
| 171 | + })} |
| 172 | + </div> |
| 173 | + ))} |
| 174 | + </nav> |
| 175 | + |
| 176 | + <div className="flex items-center gap-2 px-2.5 py-2"> |
| 177 | + {/* The whole identity block is the way to the account page — the |
| 178 | + classic site puts it behind the same name in the corner. */} |
| 179 | + <Link |
| 180 | + to="/account" |
| 181 | + title="Your account" |
| 182 | + className="flex min-w-0 flex-1 items-center gap-2 transition-opacity hover:opacity-75" |
| 183 | + > |
| 184 | + <div className="flex size-6 shrink-0 items-center justify-center rounded-full bg-accent text-[10px] font-semibold uppercase text-accent-foreground"> |
| 185 | + {session.email.slice(0, 2)} |
| 186 | + </div> |
| 187 | + {open && ( |
| 188 | + <div className="min-w-0 leading-tight"> |
| 189 | + <div className="truncate text-xs font-medium">{session.email}</div> |
| 190 | + <div className="text-[10px] capitalize text-faint">{session.role}</div> |
| 191 | + </div> |
| 192 | + )} |
| 193 | + </Link> |
| 194 | + {open && ( |
| 195 | + <button |
| 196 | + onClick={logout} |
| 197 | + title="Sign out" |
| 198 | + className="cursor-pointer rounded-md p-1.5 text-faint transition-colors hover:bg-muted hover:text-foreground" |
| 199 | + > |
| 200 | + <LogOut className="size-3.5" /> |
| 201 | + </button> |
| 202 | + )} |
| 203 | + </div> |
| 204 | + </motion.aside> |
| 205 | + |
| 206 | + <main className="flex min-w-0 flex-1 flex-col overflow-hidden rounded-xl border bg-card shadow-card"> |
| 207 | + <motion.div |
| 208 | + key={pathname} |
| 209 | + className="min-h-0 flex-1 overflow-y-auto" |
| 210 | + initial={{ opacity: 0, y: 6 }} |
| 211 | + animate={{ opacity: 1, y: 0 }} |
| 212 | + transition={{ duration: 0.22, ease: [0.25, 0.1, 0.25, 1] }} |
| 213 | + > |
| 214 | + <Outlet /> |
| 215 | + </motion.div> |
| 216 | + </main> |
| 217 | + </div> |
| 218 | + ); |
| 219 | +} |
0 commit comments