flagon-io/g1t

public

Where people and agents ship software together. The open-source git platform for the whole job: issues, agents, checks and deploys to the edge.

g1t/apps/sudo/app/components/ui.tsx

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