pr_01m47d24b0e6n91zwymwxg0vpx/apps/sudo/app/components/ui.tsx
| 1 | /** |
| 2 | * sudo's building blocks, after apps/web/app/components/ui. None of them |
| 3 | * sets a `style` attribute: the content security policy allows no inline |
| 4 | * styles, so sizes and colours that vary are drawn as SVG attributes. |
| 5 | */ |
| 6 | import { AlertTriangle, CheckCircle2, Info } from "lucide-react"; |
| 7 | import type { ComponentProps, ReactNode } from "react"; |
| 8 | import { Link, type LinkProps } from "react-router"; |
| 9 | |
| 10 | import type { Limit, Terms, Trust } from "@g1t/contracts"; |
| 11 | |
| 12 | import { usd } from "~/lib/money"; |
| 13 | |
| 14 | export function Field({ |
| 15 | label, |
| 16 | hint, |
| 17 | children, |
| 18 | className = "", |
| 19 | }: { |
| 20 | label: string; |
| 21 | hint?: ReactNode; |
| 22 | children: ReactNode; |
| 23 | className?: string; |
| 24 | }) { |
| 25 | return ( |
| 26 | <label className={`block ${className}`}> |
| 27 | <span className="mb-1.5 block text-sm font-medium text-muted">{label}</span> |
| 28 | {children} |
| 29 | {hint && <span className="mt-1.5 block text-xs text-faint">{hint}</span>} |
| 30 | </label> |
| 31 | ); |
| 32 | } |
| 33 | |
| 34 | const CONTROL = |
| 35 | "w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none transition-colors placeholder:text-faint hover:border-line-strong focus:border-merged/60"; |
| 36 | |
| 37 | /** Tells password managers that a field is not a login. */ |
| 38 | const NOT_A_CREDENTIAL = { |
| 39 | autoComplete: "off", |
| 40 | "data-1p-ignore": true, |
| 41 | "data-lpignore": "true", |
| 42 | "data-bwignore": true, |
| 43 | "data-form-type": "other", |
| 44 | }; |
| 45 | |
| 46 | export function Input({ className = "", ...props }: ComponentProps<"input">) { |
| 47 | return <input {...NOT_A_CREDENTIAL} {...props} className={`${CONTROL} ${className}`} />; |
| 48 | } |
| 49 | |
| 50 | export function Textarea({ className = "", ...props }: ComponentProps<"textarea">) { |
| 51 | return <textarea {...NOT_A_CREDENTIAL} {...props} className={`${CONTROL} ${className}`} />; |
| 52 | } |
| 53 | |
| 54 | export function Select({ className = "", ...props }: ComponentProps<"select">) { |
| 55 | return <select {...props} className={`${CONTROL} ${className}`} />; |
| 56 | } |
| 57 | |
| 58 | type Variant = "primary" | "quiet" | "danger" | "lavender"; |
| 59 | |
| 60 | const BUTTON_BASE = |
| 61 | "inline-flex items-center justify-center gap-2 rounded-md px-3.5 py-2 text-sm font-medium whitespace-nowrap transition-colors disabled:opacity-50"; |
| 62 | |
| 63 | const BUTTON_VARIANTS: Record<Variant, string> = { |
| 64 | primary: "bg-fg text-bg hover:bg-white", |
| 65 | lavender: "bg-merged text-bg hover:bg-[#c8bdff]", |
| 66 | quiet: "border border-line text-fg/80 hover:border-line-strong hover:bg-surface hover:text-fg", |
| 67 | danger: "border border-danger/40 text-danger hover:border-danger/70 hover:bg-danger/10", |
| 68 | }; |
| 69 | |
| 70 | export function Button({ variant = "primary", className = "", ...props }: ComponentProps<"button"> & { variant?: Variant }) { |
| 71 | return <button {...props} className={`${BUTTON_BASE} ${BUTTON_VARIANTS[variant]} ${className}`} />; |
| 72 | } |
| 73 | |
| 74 | export function ButtonLink({ variant = "primary", className = "", ...props }: LinkProps & { variant?: Variant }) { |
| 75 | return <Link {...props} className={`${BUTTON_BASE} ${BUTTON_VARIANTS[variant]} ${className}`} />; |
| 76 | } |
| 77 | |
| 78 | /** A bordered panel with a heading. */ |
| 79 | export function Section({ |
| 80 | id, |
| 81 | title, |
| 82 | description, |
| 83 | actions, |
| 84 | children, |
| 85 | className = "", |
| 86 | }: { |
| 87 | id?: string; |
| 88 | title: string; |
| 89 | description?: ReactNode; |
| 90 | actions?: ReactNode; |
| 91 | children: ReactNode; |
| 92 | className?: string; |
| 93 | }) { |
| 94 | return ( |
| 95 | <section id={id} className={`scroll-mt-20 rounded-lg border border-line bg-surface ${className}`}> |
| 96 | <header className="flex flex-wrap items-start justify-between gap-3 border-b border-line px-4 py-3 sm:px-5"> |
| 97 | <div> |
| 98 | <h2 className="text-[0.9375rem] font-semibold tracking-tight">{title}</h2> |
| 99 | {description && <p className="mt-0.5 text-sm text-muted">{description}</p>} |
| 100 | </div> |
| 101 | {actions} |
| 102 | </header> |
| 103 | <div className="p-4 sm:p-5">{children}</div> |
| 104 | </section> |
| 105 | ); |
| 106 | } |
| 107 | |
| 108 | export function EmptyState({ title, children }: { title: string; children?: ReactNode }) { |
| 109 | return ( |
| 110 | <div className="rounded-lg border border-dashed border-line px-6 py-10 text-center"> |
| 111 | <p className="font-medium">{title}</p> |
| 112 | {children && <div className="mt-1.5 text-sm text-muted">{children}</div>} |
| 113 | </div> |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | const NOTICE = { |
| 118 | ok: { icon: CheckCircle2, className: "border-accent/30 bg-accent/8 text-accent" }, |
| 119 | error: { icon: AlertTriangle, className: "border-danger/30 bg-danger/8 text-danger" }, |
| 120 | warn: { icon: AlertTriangle, className: "border-warn/30 bg-warn/8 text-warn" }, |
| 121 | info: { icon: Info, className: "border-merged/30 bg-merged/8 text-merged" }, |
| 122 | } as const; |
| 123 | |
| 124 | export function Notice({ tone, children }: { tone: keyof typeof NOTICE; children: ReactNode }) { |
| 125 | const { icon: Icon, className } = NOTICE[tone]; |
| 126 | return ( |
| 127 | <div role={tone === "error" ? "alert" : "status"} className={`flex gap-2.5 rounded-md border px-3.5 py-2.5 text-sm ${className}`}> |
| 128 | <Icon size={16} className="mt-0.5 shrink-0" /> |
| 129 | <div className="min-w-0 text-fg-soft">{children}</div> |
| 130 | </div> |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | /** A small label; `tone` colours it. */ |
| 135 | export function Badge({ tone = "plain", children }: { tone?: "plain" | "lavender" | "mint" | "warn" | "danger" | "info"; children: ReactNode }) { |
| 136 | const tones = { |
| 137 | plain: "border-line text-muted", |
| 138 | lavender: "border-merged/35 bg-merged/10 text-merged", |
| 139 | mint: "border-accent/30 bg-accent/8 text-accent", |
| 140 | warn: "border-warn/35 bg-warn/10 text-warn", |
| 141 | danger: "border-danger/35 bg-danger/10 text-danger", |
| 142 | info: "border-info/35 bg-info/10 text-info", |
| 143 | }; |
| 144 | return ( |
| 145 | <span className={`inline-flex items-center rounded-full border px-2 py-px text-xs font-medium whitespace-nowrap ${tones[tone]}`}> |
| 146 | {children} |
| 147 | </span> |
| 148 | ); |
| 149 | } |
| 150 | |
| 151 | export function TermsBadge({ terms }: { terms: Terms }) { |
| 152 | if (terms.kind === "comped") return <Badge tone="mint">Comped</Badge>; |
| 153 | if (terms.kind === "custom") { |
| 154 | return <Badge tone="info">Custom{terms.discountPercent > 0 ? ` −${terms.discountPercent}%` : ""}</Badge>; |
| 155 | } |
| 156 | return <Badge>Standard</Badge>; |
| 157 | } |
| 158 | |
| 159 | const TRUST: Record<Trust, { label: string; tone: "plain" | "lavender" | "mint" | "info"; about: string }> = { |
| 160 | new: { label: "New", tone: "plain", about: "New: has not paid g1t yet, so the limit is small." }, |
| 161 | paid: { label: "Paid", tone: "info", about: "Paid: has paid g1t, and the limit grows with what it has paid." }, |
| 162 | established: { |
| 163 | label: "Established", |
| 164 | tone: "mint", |
| 165 | about: "Established: the limit follows their monthly spend, up to $10,000.", |
| 166 | }, |
| 167 | reviewed: { label: "Reviewed", tone: "mint", about: "Reviewed: g1t set the limit by hand, after talking to them." }, |
| 168 | // Comped accounts: g1t covers their usage, with no ceiling. |
| 169 | internal: { label: "Comped", tone: "lavender", about: "Comped: g1t covers their usage, with no limit." }, |
| 170 | }; |
| 171 | |
| 172 | export function TrustBadge({ trust }: { trust: Trust }) { |
| 173 | const { label, tone, about } = TRUST[trust] ?? { label: trust, tone: "plain", about: "" }; |
| 174 | return ( |
| 175 | <span title={about || undefined}> |
| 176 | <Badge tone={tone}>{label}</Badge> |
| 177 | </span> |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | /** What a trust level means for the limit, in a sentence. */ |
| 182 | export function trustAbout(trust: Trust): string { |
| 183 | return TRUST[trust]?.about ?? ""; |
| 184 | } |
| 185 | |
| 186 | /** A page's title, a line about it, and anything to its right. */ |
| 187 | export function PageHeader({ title, description, actions }: { title: ReactNode; description?: ReactNode; actions?: ReactNode }) { |
| 188 | return ( |
| 189 | <div className="flex flex-wrap items-start justify-between gap-4"> |
| 190 | <div className="min-w-0"> |
| 191 | <h1 className="text-2xl font-semibold tracking-tight">{title}</h1> |
| 192 | {description && <p className="mt-1 text-sm text-muted">{description}</p>} |
| 193 | </div> |
| 194 | {actions} |
| 195 | </div> |
| 196 | ); |
| 197 | } |
| 198 | |
| 199 | const STATE = { |
| 200 | ok: { label: "OK", fill: "var(--g1t-accent)", text: "text-accent" }, |
| 201 | warning: { label: "Warning", fill: "var(--g1t-warn)", text: "text-warn" }, |
| 202 | stopped: { label: "Stopped", fill: "var(--g1t-danger)", text: "text-danger" }, |
| 203 | } as const; |
| 204 | |
| 205 | export function StateBadge({ state }: { state: Limit["state"] }) { |
| 206 | const tone = state === "stopped" ? "danger" : state === "warning" ? "warn" : "mint"; |
| 207 | return <Badge tone={tone}>{STATE[state].label}</Badge>; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Usage this month that is not paid for yet, against the limit, as a bar |
| 212 | * coloured by where it stands. With no limit (comped) it shows the figure. |
| 213 | */ |
| 214 | export function ExposureBar({ limit, wide = false }: { limit: Limit; wide?: boolean }) { |
| 215 | const { exposureMicros, ceilingMicros, state } = limit; |
| 216 | const share = ceilingMicros && ceilingMicros > 0 ? Math.min(1, Math.max(0, exposureMicros / ceilingMicros)) : 0; |
| 217 | const percent = ceilingMicros && ceilingMicros > 0 ? Math.round((exposureMicros / ceilingMicros) * 100) : null; |
| 218 | return ( |
| 219 | <div className={wide ? "w-full" : "w-40 max-w-full"}> |
| 220 | <div className="flex items-baseline justify-between gap-2 text-xs"> |
| 221 | <span className="tabular font-medium text-fg-soft"> |
| 222 | {usd(exposureMicros)} |
| 223 | <span className="font-normal text-faint"> / {ceilingMicros == null ? "no limit" : usd(ceilingMicros)}</span> |
| 224 | </span> |
| 225 | {percent != null && <span className={`tabular ${STATE[state].text}`}>{percent}%</span>} |
| 226 | </div> |
| 227 | <svg viewBox="0 0 100 4" preserveAspectRatio="none" className="mt-1.5 block h-1.5 w-full" role="img" aria-label={`${STATE[state].label}: ${percent ?? 0}% of the limit`}> |
| 228 | <rect x="0" y="0" width="100" height="4" rx="2" fill="var(--g1t-raised)" /> |
| 229 | {ceilingMicros != null && share > 0 && ( |
| 230 | <rect x="0" y="0" width={Math.max(2, share * 100)} height="4" rx="2" fill={STATE[state].fill} /> |
| 231 | )} |
| 232 | </svg> |
| 233 | </div> |
| 234 | ); |
| 235 | } |
| 236 | |
| 237 | /** A label and a figure, for the strip of totals over a page. */ |
| 238 | export function Stat({ label, value, hint, tone }: { label: string; value: ReactNode; hint?: ReactNode; tone?: "danger" | "warn" | "mint" }) { |
| 239 | const color = tone === "danger" ? "text-danger" : tone === "warn" ? "text-warn" : tone === "mint" ? "text-accent" : "text-fg"; |
| 240 | return ( |
| 241 | <div className="rounded-lg border border-line bg-surface px-4 py-3"> |
| 242 | <p className="text-xs text-muted">{label}</p> |
| 243 | <p className={`tabular mt-1 text-lg font-semibold tracking-tight ${color}`}>{value}</p> |
| 244 | {hint && <p className="mt-0.5 text-xs text-faint">{hint}</p>} |
| 245 | </div> |
| 246 | ); |
| 247 | } |
| 248 | |
| 249 | const DATE = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "UTC" }); |
| 250 | const DATE_TIME = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short", timeZone: "UTC" }); |
| 251 | |
| 252 | /** A timestamp, in UTC, as staff compare notes across time zones. */ |
| 253 | export function When({ at, time = false }: { at: string | null | undefined; time?: boolean }) { |
| 254 | if (!at) return <span className="text-faint">—</span>; |
| 255 | const date = new Date(at); |
| 256 | if (Number.isNaN(date.getTime())) return <span>{at}</span>; |
| 257 | return ( |
| 258 | <time dateTime={date.toISOString()} title={date.toISOString()}> |
| 259 | {(time ? DATE_TIME : DATE).format(date)} |
| 260 | {time && <span className="text-faint"> UTC</span>} |
| 261 | </time> |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | /** A letter avatar, drawn as SVG so its colour needs no inline style. */ |
| 266 | export function Avatar({ name, size = 20, square = true }: { name: string; size?: number; square?: boolean }) { |
| 267 | const hues = [82, 200, 262, 28, 330, 160]; |
| 268 | let hash = 0; |
| 269 | for (const char of name) hash = (hash * 31 + char.charCodeAt(0)) | 0; |
| 270 | const hue = hues[Math.abs(hash) % hues.length]; |
| 271 | return ( |
| 272 | <svg width={size} height={size} viewBox="0 0 20 20" aria-hidden="true" className="shrink-0"> |
| 273 | <rect width="20" height="20" rx={square ? 5 : 10} fill={`oklch(0.4 0.09 ${hue})`} /> |
| 274 | <text x="10" y="14.2" textAnchor="middle" fontSize="11" fontWeight="600" fontFamily="var(--g1t-font-mono)" fill={`oklch(0.93 0.08 ${hue})`}> |
| 275 | {(name[0] ?? "?").toUpperCase()} |
| 276 | </text> |
| 277 | </svg> |
| 278 | ); |
| 279 | } |