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