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

662 lines29,139 bytesCodeBlame
1/**
2 * The billing sections shared by a workspace's page and an enterprise's:
3 * confirmations, terms, plan, pools and caps, credit, bank transfers, the
4 * Stripe billing link, the ledger and the audit log. Plain forms; sudo ships no JavaScript.
5 */
6import { CreditCard, Gift, Landmark, ScrollText, UserRound } from "lucide-react";
7import type { ReactNode } from "react";
8import { Link } from "react-router";
9
10import type { AdminAction, AdminOwner, Allowances, BillingLink, LedgerEntry, Terms } from "@g1t/contracts";
11
12import { Avatar, Badge, Button, EmptyState, Field, Input, Notice, Section, Select, Textarea, When } from "~/components/ui";
13import { actionLabel } from "~/lib/ledgers";
14import { dollarsField, usd } from "~/lib/money";
15import { givenParts } from "~/lib/pricing";
16import type { Review, SectionError } from "~/lib/review";
17
18export function Hidden({ values }: { values: Record<string, string> }) {
19 return (
20 <>
21 {Object.entries(values).map(([name, value]) => (
22 <input key={name} type="hidden" name={name} value={value} />
23 ))}
24 </>
25 );
26}
27
28export function Figure({ label, value, hint }: { label: string; value: string; hint?: ReactNode }) {
29 return (
30 <div className="rounded-lg border border-line bg-surface px-4 py-3">
31 <p className="text-xs text-muted">{label}</p>
32 <p className="tabular mt-1 text-lg font-semibold tracking-tight">{value}</p>
33 {hint && <p className="mt-0.5 text-xs text-faint">{hint}</p>}
34 </div>
35 );
36}
37
38/** Owners' usernames, each with their email beneath (and on hover). */
39export function Owners({ owners, compact = false }: { owners: AdminOwner[]; compact?: boolean }) {
40 if (owners.length === 0) return <span className="text-faint">No owner</span>;
41 if (compact) {
42 return (
43 <span className="flex flex-col gap-0.5">
44 {owners.map((owner) => (
45 <span key={owner.username} title={owner.email ?? "No email"} className="min-w-0 truncate">
46 <span className="font-mono text-fg-soft">{owner.username}</span>
47 {owner.email && <span className="block truncate text-xs text-faint">{owner.email}</span>}
48 </span>
49 ))}
50 </span>
51 );
52 }
53 return (
54 <ul className="flex flex-wrap gap-2">
55 {owners.map((owner) => (
56 <li key={owner.username} title={owner.email ?? "No email"} className="inline-flex items-center gap-1.5 rounded-md border border-line bg-bg px-2 py-1 text-xs">
57 <Avatar name={owner.username} size={14} square={false} />
58 <span className="font-mono">{owner.username}</span>
59 {owner.email && <span className="text-faint">{owner.email}</span>}
60 </li>
61 ))}
62 </ul>
63 );
64}
65
66// --- Confirmation ------------------------------------------------------------
67
68function describeTerms(terms: Terms): [string, string][] {
69 return [
70 ["Terms", terms.kind === "custom" ? "Custom" : terms.kind === "comped" ? "Comped" : "Standard"],
71 ["Discount", terms.kind === "custom" ? `${terms.discountPercent}%` : "—"],
72 [
73 "Limit",
74 terms.kind === "comped"
75 ? terms.ceilingMicros == null
76 ? "The default monthly budget, at cost"
77 : `${usd(terms.ceilingMicros)} a month, at cost`
78 : terms.ceilingMicros == null
79 ? "By trust"
80 : usd(terms.ceilingMicros),
81 ],
82 ["Until", terms.until ? terms.until.slice(0, 10) : "No end"],
83 ["Note", terms.note || "—"],
84 ];
85}
86
87/** Where a confirmed change lands, when its result is shown in place. */
88const RESULT_ANCHOR: Partial<Record<Review["intent"], string>> = { "billing-link": "billing-link", invoice: "invoices" };
89
90export function ReviewPanel({ review, pathname }: { review: Review; pathname: string }) {
91 let title: string;
92 let body: ReactNode;
93 let danger = false;
94 let confirm = "Confirm";
95 if (review.intent === "terms") {
96 const before = describeTerms(review.before);
97 const after = describeTerms(review.after);
98 title = "Confirm the new terms";
99 danger = review.after.kind === "comped";
100 body = (
101 <>
102 <div className="overflow-x-auto">
103 <table className="w-full text-sm">
104 <thead>
105 <tr className="text-left text-xs text-muted">
106 <th className="py-1.5 pr-4 font-medium" />
107 <th className="py-1.5 pr-4 font-medium">Now</th>
108 <th className="py-1.5 font-medium">After</th>
109 </tr>
110 </thead>
111 <tbody>
112 {after.map(([label, value], index) => (
113 <tr key={label} className="border-t border-line align-top">
114 <td className="py-1.5 pr-4 text-muted">{label}</td>
115 <td className="py-1.5 pr-4 break-words text-faint">{before[index][1]}</td>
116 <td className={`py-1.5 break-words ${value !== before[index][1] ? "font-medium text-fg" : "text-muted"}`}>{value}</td>
117 </tr>
118 ))}
119 </tbody>
120 </table>
121 </div>
122 {review.after.kind === "comped" && (
123 <p className="mt-3 text-sm text-warn">
124 Comped: nothing will be charged{review.after.until ? ` until ${review.after.until.slice(0, 10)}` : ""}. Usage is still recorded
125 at cost.
126 </p>
127 )}
128 </>
129 );
130 } else if (review.intent === "attach") {
131 title = `Bill ${review.workspace} to ${review.targetName}?`;
132 body = (
133 <p className="text-sm text-muted">
134 From now on <span className="font-mono text-fg">{review.workspace}</span>'s usage is billed to{" "}
135 <span className="text-fg">{review.targetName}</span> and counts against its limit and terms, not its own.
136 </p>
137 );
138 } else if (review.intent === "detach") {
139 title = `Move ${review.workspace} off ${review.from}?`;
140 danger = true;
141 body = (
142 <p className="text-sm text-muted">
143 <span className="font-mono text-fg">{review.workspace}</span> goes back to paying for itself, on its own terms and the limit
144 its trust gives it. It may stop at once if its own limit is lower than what it owes.
145 </p>
146 );
147 } else if (review.intent === "billing-email") {
148 title = `Send ${review.name}'s invoices to ${review.after}?`;
149 body = (
150 <p className="text-sm text-muted">
151 Stripe emails its invoices to <span className="font-mono text-fg">{review.after}</span>
152 {review.before ? (
153 <>
154 {" "}
155 instead of <span className="font-mono">{review.before}</span>
156 </>
157 ) : null}
158 , from the next invoice on.{!review.before && " This also sets the enterprise up as a customer on Stripe."}
159 </p>
160 );
161 } else if (review.intent === "invoice") {
162 title = `Invoice ${review.name} now?`;
163 confirm = "Send the invoice";
164 body = (
165 <ul className="list-disc space-y-1 pl-5 text-sm text-muted">
166 <li>Makes one Stripe invoice, with a line for each of its {review.workspaces} workspace{review.workspaces === 1 ? "" : "s"} for what it owes now.</li>
167 <li>Net 30: due in 30 days.</li>
168 <li>
169 Stripe emails it to <span className="font-mono text-fg">{review.email ?? "the invoice email (none set yet)"}</span>, with a link to
170 pay on Stripe's page.
171 </li>
172 </ul>
173 );
174 } else {
175 title = `Make a Stripe billing link for ${review.workspace}?`;
176 confirm = "Make the link";
177 body = (
178 <p className="text-sm text-muted">
179 Opens a one-time session on Stripe's billing page for <span className="font-mono text-fg">{review.workspace}</span>'s
180 customer, where they update their card and see their invoices. Anyone with the link can use it until it expires, so send it
181 only to the workspace's owner. Making it is recorded with your email.
182 </p>
183 );
184 }
185 return (
186 <section id="review" className={`scroll-mt-20 rounded-lg border p-4 sm:p-5 ${danger ? "border-warn/40 bg-warn/5" : "border-merged/40 bg-merged/5"}`}>
187 <h2 className="font-semibold tracking-tight">{title}</h2>
188 <div className="mt-3">{body}</div>
189 <form method="post" action={`${pathname}#${RESULT_ANCHOR[review.intent] ?? "top"}`} className="mt-4 flex flex-wrap items-center gap-2">
190 <Hidden values={review.fields} />
191 <input type="hidden" name="intent" value={review.intent} />
192 <input type="hidden" name="confirm" value="yes" />
193 <Button type="submit" variant={danger ? "danger" : "lavender"}>
194 {confirm}
195 </Button>
196 <Link to={pathname} className="px-2 text-sm text-muted hover:text-fg">
197 Cancel
198 </Link>
199 </form>
200 </section>
201 );
202}
203
204// --- Terms -------------------------------------------------------------------
205
206const KINDS: { value: Terms["kind"]; title: string; text: string }[] = [
207 { value: "standard", title: "Standard", text: "Published prices; the limit comes from trust." },
208 { value: "comped", title: "Comped", text: "Nothing charged. Usage still recorded at cost." },
209 { value: "custom", title: "Custom", text: "A discount, a custom limit, or both." },
210];
211
212export function TermsForm({ terms, pathname, error }: { terms: Terms; pathname: string; error: SectionError }) {
213 const values = error?.values;
214 const kind = values?.kind ?? terms.kind;
215 return (
216 <Section
217 id="terms"
218 title="Terms"
219 description={
220 terms.setBy ? (
221 <>
222 Set by <span className="font-mono">{terms.setBy}</span> on <When at={terms.setAt} />
223 {terms.note && <> · “{terms.note}”</>}
224 </>
225 ) : (
226 "Standard, as everyone starts."
227 )
228 }
229 >
230 <form method="post" action={`${pathname}#review`} className="space-y-4">
231 <input type="hidden" name="intent" value="terms" />
232 {error && <Notice tone="error">{error.error}</Notice>}
233 <fieldset>
234 <legend className="mb-1.5 text-sm font-medium text-muted">Kind</legend>
235 <div className="grid gap-2 sm:grid-cols-3">
236 {KINDS.map((option) => (
237 <label
238 key={option.value}
239 className="flex cursor-pointer gap-2.5 rounded-md border border-line bg-bg p-3 transition-colors hover:border-line-strong has-checked:border-merged/60 has-checked:bg-merged/8"
240 >
241 <input type="radio" name="kind" value={option.value} defaultChecked={kind === option.value} className="mt-0.5" required />
242 <span>
243 <span className="block text-sm font-medium">{option.title}</span>
244 <span className="mt-0.5 block text-xs text-muted">{option.text}</span>
245 </span>
246 </label>
247 ))}
248 </div>
249 </fieldset>
250 <div className="grid gap-4 sm:grid-cols-3">
251 <Field label="Discount %" hint="Custom only.">
252 <Input name="discount" inputMode="numeric" pattern="\d{1,3}" placeholder="0" defaultValue={values?.discount ?? (terms.discountPercent ? String(terms.discountPercent) : "")} />
253 </Field>
254 <Field label="Limit $" hint="Unpaid usage allowed; blank: trust decides. Comped: the monthly budget at cost; blank: the default ($150).">
255 <Input name="ceiling" inputMode="decimal" placeholder="By trust" defaultValue={values?.ceiling ?? dollarsField(terms.ceilingMicros)} />
256 </Field>
257 <Field label="Until" hint="Blank: no end. UTC.">
258 <Input type="date" name="until" defaultValue={values?.until ?? (terms.until ? terms.until.slice(0, 10) : "")} />
259 </Field>
260 </div>
261 <Field label="Note" hint="Required. Why, for whoever looks next.">
262 <Textarea name="note" rows={2} required maxLength={500} defaultValue={values?.note ?? ""} placeholder="e.g. Design partner through launch" />
263 </Field>
264 <div className="flex justify-end">
265 <Button type="submit">Review terms</Button>
266 </div>
267 </form>
268 </Section>
269 );
270}
271
272// --- Plan, pools and caps -------------------------------------------------------
273
274/** What staff have set beyond the terms, one line each; defaults left out. */
275export function allowanceLines(allowances: Allowances | undefined, comped: boolean): string[] {
276 const lines: string[] = [];
277 if (comped) lines.push("The g1t plan, without its price (comped)");
278 else if (allowances?.plan) lines.push("The g1t plan, without its price");
279 if (!allowances) return lines;
280 if (allowances.ossRepoMicros != null) lines.push(`Open-source pool: ${usd(allowances.ossRepoMicros)} a month for each public repository`);
281 if (allowances.trialMicros != null) lines.push(`Trial: ${usd(allowances.trialMicros)}, outside the monthly pool`);
282 if (allowances.maxConcurrentAgents != null) {
283 lines.push(`${allowances.maxConcurrentAgents} agent${allowances.maxConcurrentAgents === 1 ? "" : "s"} at once`);
284 }
285 if (allowances.runCapMicros != null) lines.push(`Run cap: ${usd(allowances.runCapMicros)} a run`);
286 if (allowances.issueCapMicros != null) lines.push(`Issue cap: ${usd(allowances.issueCapMicros)} an issue`);
287 if (allowances.hold) lines.push(`Held: ${allowances.hold}`);
288 return lines;
289}
290
291/**
292 * The g1t plan without its price, the account's share of g1t's pools, and
293 * staff's overrides of what owners set: agents at once, the run and issue
294 * caps, and a hold on new compute. Comped accounts have the plan anyway.
295 */
296export function AllowancesForm({
297 allowances,
298 comped,
299 pathname,
300 error,
301}: {
302 allowances: Allowances | undefined;
303 comped: boolean;
304 pathname: string;
305 error: SectionError;
306}) {
307 const values = error?.values;
308 const current: Allowances = allowances ?? { plan: false, ossRepoMicros: null, trialMicros: null };
309 const lines = allowanceLines(allowances, comped);
310 return (
311 <Section
312 id="allowances"
313 title="Plan, pools and caps"
314 description={
315 comped
316 ? "Comped: the g1t plan is on without its price whatever is set here. The pools and caps still apply."
317 : "The g1t plan without its price, this account's share of g1t's pools, and overrides of what owners set."
318 }
319 >
320 <div className="mb-4 rounded-md border border-line bg-bg px-3.5 py-2.5 text-sm">
321 <p className="text-xs text-faint">Set now</p>
322 {lines.length === 0 ? (
323 <p className="mt-0.5 text-muted">Nothing: the defaults.</p>
324 ) : (
325 <ul className="mt-1 space-y-0.5">
326 {lines.map((line) => (
327 <li key={line} className={line.startsWith("Held:") ? "text-danger" : "text-fg-soft"}>
328 {line}
329 </li>
330 ))}
331 </ul>
332 )}
333 </div>
334 <form method="post" action={`${pathname}#allowances`} className="space-y-4">
335 <input type="hidden" name="intent" value="allowances" />
336 {error && <Notice tone="error">{error.error}</Notice>}
337 <label className="flex cursor-pointer items-start gap-2.5 text-sm">
338 <input type="checkbox" name="plan" defaultChecked={values ? values.plan === "on" : current.plan} className="mt-0.5" />
339 <span>
340 <span className="block font-medium">The g1t plan, without its price</span>
341 <span className="block text-xs text-muted">
342 Its included usage, storage and caps, with no $20 a month. Usage past it is charged as usual.
343 </span>
344 </span>
345 </label>
346 <div className="grid gap-4 sm:grid-cols-2">
347 <Field label="Open-source share $" hint="Each public repository, a month. Blank: the default ($2).">
348 <Input name="oss" inputMode="decimal" placeholder="Default" defaultValue={values?.oss ?? dollarsField(current.ossRepoMicros)} />
349 </Field>
350 <Field label="Trial $" hint="Each workspace, outside the monthly pool. Blank: the default ($5).">
351 <Input name="trial" inputMode="decimal" placeholder="Default" defaultValue={values?.trial ?? dollarsField(current.trialMicros)} />
352 </Field>
353 <Field label="Agents at once" hint="Blank: the plan's (2 in the first month or on the trial, then 10).">
354 <Input
355 name="agents"
356 inputMode="numeric"
357 pattern="\d{1,3}"
358 placeholder="Default"
359 defaultValue={values?.agents ?? (current.maxConcurrentAgents != null ? String(current.maxConcurrentAgents) : "")}
360 />
361 </Field>
362 <Field label="Run cap $" hint="One run's spend, over the owners'. Blank: theirs, or $2.">
363 <Input name="runCap" inputMode="decimal" placeholder="Owners'" defaultValue={values?.runCap ?? dollarsField(current.runCapMicros)} />
364 </Field>
365 <Field label="Issue cap $" hint="One issue's agents in all, over the owners'. Blank: theirs, or $10.">
366 <Input name="issueCap" inputMode="decimal" placeholder="Owners'" defaultValue={values?.issueCap ?? dollarsField(current.issueCapMicros)} />
367 </Field>
368 <Field label="Hold" hint="Pauses new compute and tells the owners why. Blank: no hold.">
369 <Input name="hold" maxLength={200} placeholder="No hold" defaultValue={values?.hold ?? current.hold ?? ""} />
370 </Field>
371 </div>
372 <Field label="Note" hint="Required. Why, for whoever looks next.">
373 <Textarea
374 name="note"
375 rows={2}
376 required
377 maxLength={500}
378 defaultValue={values?.note ?? ""}
379 placeholder="e.g. Open-source foundation, larger share through 2027"
380 />
381 </Field>
382 <div className="flex justify-end">
383 <Button type="submit">Save plan, pools and caps</Button>
384 </div>
385 </form>
386 </Section>
387 );
388}
389
390// --- Bank transfer ---------------------------------------------------------------
391
392/**
393 * Money that reached g1t outside Stripe's page, such as a bank transfer
394 * for a $1,000+ prepayment, entered as a payment.
395 */
396export function PaymentForm({ workspace, pathname, error }: { workspace: string; pathname: string; error: SectionError }) {
397 const values = error?.values;
398 return (
399 <Section
400 id="payment"
401 title="Record a bank transfer"
402 description="Money that reached g1t's bank without Stripe's page, such as a $1,000+ prepayment. It goes on the statement as a payment and counts toward the limit at once."
403 >
404 <form method="post" action={`${pathname}#payment`} className="space-y-4">
405 <input type="hidden" name="intent" value="payment" />
406 {error && <Notice tone="error">{error.error}</Notice>}
407 <div className="grid gap-4 sm:grid-cols-2">
408 <Field label="Amount $" hint="As it arrived, up to $100,000.">
409 <Input name="amount" inputMode="decimal" required placeholder="1,500.00" defaultValue={values?.amount ?? ""} />
410 </Field>
411 <Field label="Reference" hint="As the bank shows it. Each is recorded once.">
412 <Input name="reference" required maxLength={100} className="font-mono" defaultValue={values?.reference ?? ""} />
413 </Field>
414 </div>
415 <Field label="Note" hint="Required. Shown on the statement after “Paid by bank transfer”.">
416 <Textarea
417 name="note"
418 rows={2}
419 required
420 maxLength={500}
421 placeholder="e.g. Prepayment for October, invoice INV-0042"
422 defaultValue={values?.note ?? ""}
423 />
424 </Field>
425 <Field
426 label="Confirm"
427 hint={
428 <>
429 Type the workspace's slug (<span className="font-mono text-muted">{workspace}</span>) to record it.
430 </>
431 }
432 >
433 <Input name="confirmation" required placeholder={workspace} className="font-mono" />
434 </Field>
435 <div className="flex justify-end">
436 <Button type="submit" variant="lavender">
437 <Landmark size={14} />
438 Record the payment
439 </Button>
440 </div>
441 </form>
442 </Section>
443 );
444}
445
446// --- Credit -------------------------------------------------------------------
447
448export function CreditForm({ workspaces, pathname, error }: { workspaces: string[]; pathname: string; error: SectionError }) {
449 const values = error?.values;
450 const single = workspaces.length === 1 ? workspaces[0] : null;
451 return (
452 <Section id="credit" title="Issue credit" description="A refund or goodwill. Added to the workspace's balance at once.">
453 {workspaces.length === 0 ? (
454 <p className="text-sm text-muted">Add a workspace first: credit goes to a workspace.</p>
455 ) : (
456 <form method="post" action={`${pathname}#credit`} className="space-y-4">
457 <input type="hidden" name="intent" value="credit" />
458 {error && <Notice tone="error">{error.error}</Notice>}
459 {single ? (
460 <input type="hidden" name="workspace" value={single} />
461 ) : (
462 <Field label="Workspace">
463 <Select name="workspace" required defaultValue={values?.workspace ?? ""}>
464 <option value="" disabled>
465 Choose a workspace
466 </option>
467 {workspaces.map((slug) => (
468 <option key={slug} value={slug}>
469 {slug}
470 </option>
471 ))}
472 </Select>
473 </Field>
474 )}
475 <Field label="Amount $" hint="Up to $10,000 at a time.">
476 <Input name="amount" inputMode="decimal" required placeholder="25.00" defaultValue={values?.amount ?? ""} />
477 </Field>
478 <Field label="Note" hint="Required. Shown on the workspace's statement.">
479 <Textarea name="note" rows={2} required maxLength={500} placeholder="e.g. Refund for the failed runs on Oct 2" defaultValue={values?.note ?? ""} />
480 </Field>
481 <Field
482 label="Confirm"
483 hint={
484 <>
485 Type the workspace's slug{single && <> (<span className="font-mono text-muted">{single}</span>)</>} to issue it.
486 </>
487 }
488 >
489 <Input name="confirmation" required placeholder={single ?? "workspace-slug"} className="font-mono" />
490 </Field>
491 <div className="flex justify-end">
492 <Button type="submit" variant="lavender">
493 <Gift size={14} />
494 Issue credit
495 </Button>
496 </div>
497 </form>
498 )}
499 </Section>
500 );
501}
502
503// --- Stripe billing link -----------------------------------------------------
504
505/**
506 * Card details, invoices and receipts live on Stripe's own billing page.
507 * sudo never shows a card field: staff send the customer a link instead.
508 */
509export function BillingLinkSection({
510 link,
511 pathname,
512 error,
513}: {
514 link: BillingLink | null;
515 pathname: string;
516 error: SectionError;
517}) {
518 return (
519 <Section
520 id="billing-link"
521 title="Stripe billing page"
522 description="Where the customer updates their card and sees invoices. g1t never takes card numbers."
523 >
524 {error && (
525 <div className="mb-4">
526 <Notice tone="error">{error.error}</Notice>
527 </div>
528 )}
529 {link ? (
530 <div className="space-y-4">
531 <Notice tone="info">Send this to the customer. g1t never takes card numbers; they enter them on Stripe.</Notice>
532 <Field label="One-time link" hint={link.expiresNote}>
533 <Input readOnly value={link.portalUrl} className="font-mono text-xs" aria-label="One-time Stripe billing link" />
534 </Field>
535 {link.loginUrl && (
536 <Field label="Permanent sign-in page" hint={`The customer signs in with ${link.customerEmail ?? "the email Stripe has for them"}.`}>
537 <Input readOnly value={link.loginUrl} className="font-mono text-xs" aria-label="Stripe billing sign-in page" />
538 </Field>
539 )}
540 {!link.loginUrl && link.customerEmail && <p className="text-xs text-faint">Stripe has {link.customerEmail} for this customer.</p>}
541 </div>
542 ) : (
543 <form method="post" action={`${pathname}#review`} className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
544 <input type="hidden" name="intent" value="billing-link" />
545 <p className="text-sm text-muted">
546 Never take card details over the phone or by email. Make a link to Stripe's billing page and send it to the owner.
547 </p>
548 <Button type="submit" variant="quiet" className="shrink-0">
549 <CreditCard size={14} />
550 Make a link
551 </Button>
552 </form>
553 )}
554 </Section>
555 );
556}
557
558// --- Ledger and audit ---------------------------------------------------------
559
560const ENTRY_KIND: Record<string, string> = { top_up: "Top-up", usage: "Usage", credit: "Credit" };
561
562export function LedgerSection({
563 ledger,
564 showWorkspace = false,
565 description = "Recent lines of the statement, newest first.",
566}: {
567 ledger: LedgerEntry[];
568 showWorkspace?: boolean;
569 description?: ReactNode;
570}) {
571 return (
572 <Section title="Ledger" description={description}>
573 {ledger.length === 0 ? (
574 <EmptyState title="Nothing yet" />
575 ) : (
576 <div className="-mx-4 -my-4 overflow-x-auto sm:-mx-5 sm:-my-5">
577 <table className="w-full min-w-[36rem] text-sm">
578 <thead>
579 <tr className="border-b border-line text-left text-xs text-muted">
580 <th className="px-4 py-2 font-medium sm:pl-5">When</th>
581 <th className="px-4 py-2 font-medium">What</th>
582 <th className="px-4 py-2 text-right font-medium sm:pr-5">Amount</th>
583 </tr>
584 </thead>
585 <tbody>
586 {ledger.map((entry) => (
587 <tr key={entry.id} className="border-b border-line align-top last:border-0">
588 <td className="px-4 py-2.5 text-xs whitespace-nowrap text-muted sm:pl-5">
589 <When at={entry.createdAt} time />
590 </td>
591 <td className="px-4 py-2.5">
592 <div className="flex flex-wrap items-center gap-1.5">
593 <Badge tone={entry.amountMicros > 0 ? "mint" : "plain"}>{ENTRY_KIND[entry.kind] ?? entry.kind}</Badge>
594 {showWorkspace && entry.workspace && (
595 <Link to={`/workspaces/${encodeURIComponent(entry.workspace)}`} className="font-mono text-xs text-merged hover:underline">
596 {entry.workspace}
597 </Link>
598 )}
599 <span className="break-words">{entry.description}</span>
600 {givenParts(entry).map((part) => (
601 <Badge key={part.label} tone="lavender">
602 Given · {part.label} {usd(part.micros)}
603 </Badge>
604 ))}
605 </div>
606 <p className="mt-0.5 font-mono text-xs text-faint">
607 {[
608 entry.repo && (entry.number != null ? `${entry.repo}#${entry.number}` : entry.repo),
609 entry.task,
610 entry.model,
611 entry.billedTo === "workspace" ? "own provider" : null,
612 entry.createdBy && `by ${entry.createdBy}`,
613 ]
614 .filter(Boolean)
615 .join(" · ")}
616 </p>
617 </td>
618 <td className={`tabular px-4 py-2.5 text-right whitespace-nowrap sm:pr-5 ${entry.amountMicros > 0 ? "text-accent" : "text-fg-soft"}`}>
619 {usd(entry.amountMicros, { signed: true })}
620 </td>
621 </tr>
622 ))}
623 </tbody>
624 </table>
625 </div>
626 )}
627 </Section>
628 );
629}
630
631
632
633export function AuditSection({ audit, description = "Every change made in sudo, and by whom." }: { audit: AdminAction[]; description?: ReactNode }) {
634 return (
635 <Section title="Audit log" description={description}>
636 {audit.length === 0 ? (
637 <p className="flex items-center gap-2 text-sm text-muted">
638 <ScrollText size={14} />
639 No changes yet.
640 </p>
641 ) : (
642 <ol className="space-y-3">
643 {audit.map((entry) => (
644 <li key={entry.id} className="border-l-2 border-merged/40 pl-3">
645 <p className="flex flex-wrap items-center gap-x-2 text-sm">
646 <span className="font-medium text-merged">{actionLabel(entry.action)}</span>
647 <span className="text-xs text-faint">
648 <When at={entry.createdAt} time />
649 </span>
650 </p>
651 {entry.detail && <p className="mt-0.5 text-sm break-words text-fg-soft">{entry.detail}</p>}
652 <p className="mt-0.5 flex items-center gap-1 font-mono text-xs text-faint">
653 <UserRound size={11} />
654 {entry.by}
655 </p>
656 </li>
657 ))}
658 </ol>
659 )}
660 </Section>
661 );
662}