g1t/apps/web/app/components/ui.tsx
| 1 | import type { ComponentProps, ReactNode } from "react"; |
| 2 | |
| 3 | export function Field({ |
| 4 | label, |
| 5 | hint, |
| 6 | children, |
| 7 | }: { |
| 8 | label: string; |
| 9 | hint?: string; |
| 10 | children: ReactNode; |
| 11 | }) { |
| 12 | return ( |
| 13 | <label className="block"> |
| 14 | <span className="mb-1.5 block text-sm font-medium text-muted"> |
| 15 | {label} |
| 16 | </span> |
| 17 | {children} |
| 18 | {hint && <span className="mt-1.5 block text-xs text-muted">{hint}</span>} |
| 19 | </label> |
| 20 | ); |
| 21 | } |
| 22 | |
| 23 | const CONTROL = |
| 24 | "w-full rounded-md border border-line bg-bg px-3 py-2 text-sm outline-none focus:border-muted"; |
| 25 | |
| 26 | export function Input(props: ComponentProps<"input">) { |
| 27 | return <input {...props} className={CONTROL} />; |
| 28 | } |
| 29 | |
| 30 | export function Textarea(props: ComponentProps<"textarea">) { |
| 31 | return <textarea {...props} className={`${CONTROL} font-mono`} />; |
| 32 | } |
| 33 | |
| 34 | export function Button({ |
| 35 | variant = "primary", |
| 36 | ...props |
| 37 | }: ComponentProps<"button"> & { variant?: "primary" | "quiet" }) { |
| 38 | const styles = |
| 39 | variant === "primary" |
| 40 | ? "bg-fg text-bg hover:bg-white" |
| 41 | : "border border-line text-muted hover:text-fg"; |
| 42 | return ( |
| 43 | <button |
| 44 | {...props} |
| 45 | className={`rounded-md px-3 py-2 text-sm font-medium ${styles}`} |
| 46 | /> |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export function ErrorText({ children }: { children: ReactNode }) { |
| 51 | return children ? <p className="text-sm text-red-400">{children}</p> : null; |
| 52 | } |
| 53 | |
| 54 | const STATUS_STYLES: Record<string, string> = { |
| 55 | open: "border-accent/40 text-accent", |
| 56 | working: "border-accent/40 text-accent", |
| 57 | submitted: "border-sky-400/40 text-sky-300", |
| 58 | shipped: "border-violet-400/40 text-violet-300", |
| 59 | }; |
| 60 | |
| 61 | /** A status pill; unknown statuses render muted. */ |
| 62 | export function Status({ value }: { value: string }) { |
| 63 | const style = STATUS_STYLES[value] ?? "border-line text-muted"; |
| 64 | return ( |
| 65 | <span |
| 66 | className={`inline-flex items-center gap-1.5 rounded-full border px-2 py-0.5 text-xs ${style}`} |
| 67 | > |
| 68 | {value === "working" && ( |
| 69 | <span className="size-1.5 animate-pulse rounded-full bg-accent" /> |
| 70 | )} |
| 71 | {value} |
| 72 | </span> |
| 73 | ); |
| 74 | } |
| 75 | |
| 76 | const UNITS: [string, number][] = [ |
| 77 | ["d", 86_400_000], |
| 78 | ["h", 3_600_000], |
| 79 | ["m", 60_000], |
| 80 | ]; |
| 81 | |
| 82 | /** Compact relative time such as "5m ago". */ |
| 83 | export function TimeAgo({ at }: { at: number }) { |
| 84 | const elapsed = Date.now() - at; |
| 85 | const unit = UNITS.find(([, size]) => elapsed >= size); |
| 86 | const label = unit ? `${Math.floor(elapsed / unit[1])}${unit[0]} ago` : "just now"; |
| 87 | return ( |
| 88 | // The server and the browser render at slightly different times. |
| 89 | <time dateTime={new Date(at).toISOString()} suppressHydrationWarning> |
| 90 | {label} |
| 91 | </time> |
| 92 | ); |
| 93 | } |