| 1 | /** |
| 2 | * sudo's frame. On wide screens, a sidebar: Overview and Reach out as |
| 3 | * links, then sections that fold open to their pages, as GitLab and |
| 4 | * Vercel do theirs (apps/web's project sidebar is the same pattern). On a |
| 5 | * phone, a top bar whose menu holds the same. Every fold is a <details>, |
| 6 | * which opens and closes before the page's script has loaded, drawn open |
| 7 | * for the section holding the current page. All of it comes from |
| 8 | * lib/nav.ts. |
| 9 | */ |
| 10 | import { |
| 11 | Activity, |
| 12 | Building2, |
| 13 | Boxes, |
| 14 | ChevronRight, |
| 15 | CircleDollarSign, |
| 16 | Clock, |
| 17 | Cloud, |
| 18 | CreditCard, |
| 19 | Eye, |
| 20 | FileText, |
| 21 | Gauge, |
| 22 | HandCoins, |
| 23 | HandHelping, |
| 24 | Inbox, |
| 25 | LayoutDashboard, |
| 26 | LifeBuoy, |
| 27 | type LucideIcon, |
| 28 | Megaphone, |
| 29 | Menu, |
| 30 | Monitor, |
| 31 | Moon, |
| 32 | ScrollText, |
| 33 | Server, |
| 34 | ShieldAlert, |
| 35 | ShieldCheck, |
| 36 | Signpost, |
| 37 | Sun, |
| 38 | Siren, |
| 39 | Tags, |
| 40 | Ticket, |
| 41 | TrendingUp, |
| 42 | UserCog, |
| 43 | Users, |
| 44 | UsersRound, |
| 45 | X, |
| 46 | Zap, |
| 47 | } from "lucide-react"; |
| 48 | import { Link, useLocation, useRouteLoaderData } from "react-router"; |
| 49 | |
| 50 | import { Logo } from "~/components/logo"; |
| 51 | import { useZone } from "~/components/ui"; |
| 52 | import { NAV, type NavCounts, type NavGroup, type NavIcon, type NavItem, countFor, holdsCurrent, isCurrent } from "~/lib/nav"; |
| 53 | import { THEME_CHOICES, THEME_LABEL, type ThemeChoice, setTheme, useThemeChoice } from "~/lib/theme"; |
| 54 | import { zoneAbbr } from "~/lib/time"; |
| 55 | |
| 56 | const ICONS: Record<NavIcon, LucideIcon> = { |
| 57 | overview: LayoutDashboard, |
| 58 | workspaces: Boxes, |
| 59 | enterprises: Building2, |
| 60 | "reach-out": Zap, |
| 61 | people: Users, |
| 62 | invoices: FileText, |
| 63 | prices: Tags, |
| 64 | credits: HandCoins, |
| 65 | usage: Gauge, |
| 66 | stripe: CreditCard, |
| 67 | costs: Cloud, |
| 68 | abuse: ShieldAlert, |
| 69 | incidents: Siren, |
| 70 | inbox: Inbox, |
| 71 | "view-as": Eye, |
| 72 | announcements: Megaphone, |
| 73 | staff: UserCog, |
| 74 | audit: ScrollText, |
| 75 | requests: HandHelping, |
| 76 | overages: TrendingUp, |
| 77 | velocity: Activity, |
| 78 | invites: Ticket, |
| 79 | aliases: Signpost, |
| 80 | customers: UsersRound, |
| 81 | spend: Gauge, |
| 82 | revenue: CircleDollarSign, |
| 83 | platform: Server, |
| 84 | support: LifeBuoy, |
| 85 | team: UserCog, |
| 86 | }; |
| 87 | |
| 88 | /** A page's or section's icon, as the sidebar shows it. */ |
| 89 | export function NavGlyph({ icon, size = 15, className }: { icon: NavIcon; size?: number; className?: string }) { |
| 90 | const Icon = ICONS[icon]; |
| 91 | return <Icon size={size} className={className} />; |
| 92 | } |
| 93 | |
| 94 | /** How many are waiting on a page, such as access requests; nothing at zero. */ |
| 95 | function CountPill({ count, label }: { count: number; label: string }) { |
| 96 | if (count <= 0) return null; |
| 97 | return ( |
| 98 | <span |
| 99 | title={label} |
| 100 | className="shrink-0 rounded-full bg-accent/15 px-1.5 py-px text-[0.6875rem] font-semibold text-accent tabular-nums ring-1 ring-accent/30" |
| 101 | > |
| 102 | {count > 99 ? "99+" : count} |
| 103 | <span className="sr-only"> {label}</span> |
| 104 | </span> |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | const COUNT_LABEL = "waiting"; |
| 109 | |
| 110 | /** What an item's number counts, for screen readers: incidents are open, not waiting. */ |
| 111 | function countLabel(item: NavItem): string { |
| 112 | return item.count === "incidents" ? "open" : COUNT_LABEL; |
| 113 | } |
| 114 | |
| 115 | function SoonPill() { |
| 116 | return ( |
| 117 | <span className="shrink-0 rounded-full px-1.5 py-px text-[0.625rem] font-medium tracking-wide text-muted uppercase ring-1 ring-line"> |
| 118 | Soon |
| 119 | </span> |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | /** A top-level link, with its icon. */ |
| 124 | function TopLink({ item, pathname, counts }: { item: NavItem; pathname: string; counts: NavCounts }) { |
| 125 | const current = isCurrent(item, pathname); |
| 126 | return ( |
| 127 | <Link |
| 128 | to={item.to} |
| 129 | title={item.about} |
| 130 | aria-current={current ? "page" : undefined} |
| 131 | className={`group flex h-8 items-center gap-2.5 rounded-md px-2 text-[0.8125rem] transition-colors ${ |
| 132 | current ? "bg-raised font-medium text-fg" : "text-muted hover:bg-raised/60 hover:text-fg" |
| 133 | }`} |
| 134 | > |
| 135 | <NavGlyph icon={item.icon} className={`shrink-0 ${current ? "text-accent" : "text-faint group-hover:text-muted"}`} /> |
| 136 | <span className="min-w-0 grow truncate">{item.label}</span> |
| 137 | <CountPill count={countFor([item], counts)} label={countLabel(item)} /> |
| 138 | {item.soon && <SoonPill />} |
| 139 | </Link> |
| 140 | ); |
| 141 | } |
| 142 | |
| 143 | /** A page inside a section: indented, no icon, the current one marked in lavender. */ |
| 144 | function SubLink({ item, pathname, counts }: { item: NavItem; pathname: string; counts: NavCounts }) { |
| 145 | const current = isCurrent(item, pathname); |
| 146 | return ( |
| 147 | <Link |
| 148 | to={item.to} |
| 149 | title={item.about} |
| 150 | aria-current={current ? "page" : undefined} |
| 151 | className={`relative flex h-8 items-center gap-2 rounded-md pr-2 pl-9.5 text-[0.8125rem] transition-colors ${ |
| 152 | current |
| 153 | ? "bg-raised font-medium text-fg before:absolute before:top-1.5 before:bottom-1.5 before:left-4.75 before:z-10 before:w-px before:bg-accent" |
| 154 | : item.soon |
| 155 | ? "text-faint hover:bg-raised/60 hover:text-muted" |
| 156 | : "text-muted hover:bg-raised/60 hover:text-fg" |
| 157 | }`} |
| 158 | > |
| 159 | <span className="min-w-0 grow truncate">{item.label}</span> |
| 160 | <CountPill count={countFor([item], counts)} label={countLabel(item)} /> |
| 161 | {item.soon && <SoonPill />} |
| 162 | </Link> |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | /** A section: one row that folds open to its pages. Open when it holds the current page. */ |
| 167 | function Section({ group, pathname, counts }: { group: NavGroup & { title: string }; pathname: string; counts: NavCounts }) { |
| 168 | const current = holdsCurrent(group, pathname); |
| 169 | const waiting = countFor(group.items, counts); |
| 170 | const allSoon = group.items.every((item) => item.soon); |
| 171 | return ( |
| 172 | <details open={current} className="group/section"> |
| 173 | <summary |
| 174 | className={`group flex h-8 cursor-pointer list-none items-center gap-2.5 rounded-md px-2 text-[0.8125rem] transition-colors select-none hover:bg-raised/60 [&::-webkit-details-marker]:hidden ${ |
| 175 | current ? "text-fg" : allSoon ? "text-faint hover:text-muted" : "text-muted hover:text-fg" |
| 176 | }`} |
| 177 | > |
| 178 | {group.icon && <NavGlyph icon={group.icon} className={`shrink-0 ${current ? "text-muted" : "text-faint group-hover:text-muted"}`} />} |
| 179 | <span className={`min-w-0 grow truncate ${current ? "font-medium" : ""}`}>{group.title}</span> |
| 180 | {allSoon && ( |
| 181 | <span className="group-open/section:hidden"> |
| 182 | <SoonPill /> |
| 183 | </span> |
| 184 | )} |
| 185 | {/* Folded, the section carries its pages' counts; open, the pages do. */} |
| 186 | {waiting > 0 && ( |
| 187 | <span className="group-open/section:hidden"> |
| 188 | <CountPill count={waiting} label={COUNT_LABEL} /> |
| 189 | </span> |
| 190 | )} |
| 191 | <ChevronRight size={14} aria-hidden="true" className="shrink-0 text-faint transition-transform duration-150 group-open/section:rotate-90" /> |
| 192 | </summary> |
| 193 | <ul className="relative mt-px mb-1 space-y-px before:absolute before:top-1 before:bottom-1 before:left-4.75 before:w-px before:bg-line"> |
| 194 | {group.items.map((item) => ( |
| 195 | <li key={item.to}> |
| 196 | <SubLink item={item} pathname={pathname} counts={counts} /> |
| 197 | </li> |
| 198 | ))} |
| 199 | </ul> |
| 200 | </details> |
| 201 | ); |
| 202 | } |
| 203 | |
| 204 | function NavTree({ pathname, counts }: { pathname: string; counts: NavCounts }) { |
| 205 | return ( |
| 206 | <div className="space-y-px"> |
| 207 | {NAV.map((group, index) => |
| 208 | group.title ? ( |
| 209 | <Section key={group.title} group={group as NavGroup & { title: string }} pathname={pathname} counts={counts} /> |
| 210 | ) : ( |
| 211 | <ul key={index} className={`space-y-px ${index === 0 ? "" : "pt-2"} ${index < NAV.length - 1 ? "pb-2" : ""}`}> |
| 212 | {group.items.map((item) => ( |
| 213 | <li key={item.to}> |
| 214 | <TopLink item={item} pathname={pathname} counts={counts} /> |
| 215 | </li> |
| 216 | ))} |
| 217 | </ul> |
| 218 | ), |
| 219 | )} |
| 220 | </div> |
| 221 | ); |
| 222 | } |
| 223 | |
| 224 | /** Which zone the pages say times in, and the way to change it. */ |
| 225 | function TimesIn() { |
| 226 | const zone = useZone(); |
| 227 | const { pathname, search } = useLocation(); |
| 228 | const back = pathname === "/timezone" ? "/" : `${pathname}${search}`; |
| 229 | return ( |
| 230 | <p className="mt-1.5 flex items-center gap-1.5 text-[0.6875rem] leading-snug text-faint"> |
| 231 | <Clock size={12} aria-hidden="true" className="shrink-0" /> |
| 232 | <span className="min-w-0 truncate" title={`Times are shown in ${zone}`}> |
| 233 | Times in {zone === "UTC" ? "UTC" : `${zone.split("/").at(-1)!.replace(/_/g, " ")} (${zoneAbbr(new Date(), zone)})`} ·{" "} |
| 234 | <Link to={`/timezone?back=${encodeURIComponent(back)}`} className="underline-offset-2 hover:text-fg hover:underline"> |
| 235 | Change |
| 236 | </Link> |
| 237 | </span> |
| 238 | </p> |
| 239 | ); |
| 240 | } |
| 241 | |
| 242 | const THEME_ICON: Record<ThemeChoice, LucideIcon> = { auto: Monitor, light: Sun, dark: Moon }; |
| 243 | |
| 244 | /** Appearance: Auto, Light or Dark, switched at once and kept in a cookie (lib/theme.ts). */ |
| 245 | function ThemeSwitch() { |
| 246 | const root = useRouteLoaderData<{ theme?: ThemeChoice }>("root"); |
| 247 | const current = useThemeChoice(root?.theme); |
| 248 | return ( |
| 249 | <div role="radiogroup" aria-label="Appearance" className="mt-3 flex rounded-md border border-line p-0.5"> |
| 250 | {THEME_CHOICES.map((choice) => { |
| 251 | const Icon = THEME_ICON[choice]; |
| 252 | const on = choice === current; |
| 253 | return ( |
| 254 | <button |
| 255 | key={choice} |
| 256 | type="button" |
| 257 | role="radio" |
| 258 | aria-checked={on} |
| 259 | onClick={() => setTheme(choice)} |
| 260 | className={`flex h-7 flex-1 items-center justify-center gap-1.5 rounded text-xs transition-colors ${ |
| 261 | on ? "bg-raised font-medium text-fg" : "text-muted hover:text-fg" |
| 262 | }`} |
| 263 | > |
| 264 | <Icon size={13} aria-hidden /> |
| 265 | {THEME_LABEL[choice]} |
| 266 | </button> |
| 267 | ); |
| 268 | })} |
| 269 | </div> |
| 270 | ); |
| 271 | } |
| 272 | |
| 273 | /** Who is signed in: a panel of its own at the foot of the menu. */ |
| 274 | function SignedIn({ email }: { email: string | null | undefined }) { |
| 275 | return ( |
| 276 | <div className="border-t border-line bg-raised/40 px-4 py-3"> |
| 277 | {email && ( |
| 278 | <p title="Signed in through Cloudflare Access" className="flex items-center gap-2 text-[0.8125rem]"> |
| 279 | <ShieldCheck size={15} className="shrink-0 text-accent" /> |
| 280 | <span className="min-w-0 truncate font-mono text-xs text-fg-soft">{email}</span> |
| 281 | </p> |
| 282 | )} |
| 283 | <p className="mt-1.5 text-[0.6875rem] leading-snug text-faint">g1t staff only. Every change is recorded with who made it.</p> |
| 284 | <TimesIn /> |
| 285 | <ThemeSwitch /> |
| 286 | </div> |
| 287 | ); |
| 288 | } |
| 289 | |
| 290 | /** The sidebar, from `lg` up. */ |
| 291 | export function Sidebar({ email, counts = {} }: { email: string | null | undefined; counts?: NavCounts }) { |
| 292 | const { pathname } = useLocation(); |
| 293 | return ( |
| 294 | <aside className="fixed inset-y-0 left-0 z-30 hidden w-60 flex-col border-r border-line bg-surface/50 lg:flex"> |
| 295 | <div className="flex h-14 shrink-0 items-center px-4"> |
| 296 | <Link to="/" aria-label="sudo overview" className="flex"> |
| 297 | <Logo /> |
| 298 | </Link> |
| 299 | </div> |
| 300 | <nav aria-label="sudo" className="grow overflow-y-auto px-2 pt-2 pb-6"> |
| 301 | <NavTree pathname={pathname} counts={counts} /> |
| 302 | </nav> |
| 303 | <div className="shrink-0"> |
| 304 | <SignedIn email={email} /> |
| 305 | </div> |
| 306 | </aside> |
| 307 | ); |
| 308 | } |
| 309 | |
| 310 | /** The top bar and its menu, below `lg`. */ |
| 311 | export function MobileBar({ email, counts = {} }: { email: string | null | undefined; counts?: NavCounts }) { |
| 312 | const { pathname } = useLocation(); |
| 313 | return ( |
| 314 | <header className="sticky top-0 z-40 border-b border-line bg-surface/95 backdrop-blur lg:hidden"> |
| 315 | <div className="flex h-14 items-center justify-between gap-2 px-4"> |
| 316 | <Link to="/" aria-label="sudo overview" className="flex"> |
| 317 | <Logo /> |
| 318 | </Link> |
| 319 | {/* A details element: it opens and closes with no script. Keyed |
| 320 | by the page, so the next page arrives with the menu closed. */} |
| 321 | <details key={pathname} className="group/menu"> |
| 322 | <summary |
| 323 | aria-label="Menu" |
| 324 | className="flex h-9 cursor-pointer list-none items-center gap-1.5 rounded-md border border-line px-2.5 text-sm text-muted transition-colors select-none hover:border-line-strong hover:text-fg group-open/menu:border-line-strong group-open/menu:text-fg [&::-webkit-details-marker]:hidden" |
| 325 | > |
| 326 | <Menu size={16} className="group-open/menu:hidden" /> |
| 327 | <X size={16} className="hidden group-open/menu:block" /> |
| 328 | Menu |
| 329 | </summary> |
| 330 | <div className="absolute inset-x-0 top-14 max-h-[calc(100dvh-3.5rem)] overflow-y-auto border-b border-line bg-bg shadow-2xl shadow-black/50"> |
| 331 | <nav aria-label="sudo" className="px-2 pt-3 pb-4"> |
| 332 | <NavTree pathname={pathname} counts={counts} /> |
| 333 | </nav> |
| 334 | <SignedIn email={email} /> |
| 335 | </div> |
| 336 | </details> |
| 337 | </div> |
| 338 | </header> |
| 339 | ); |
| 340 | } |