g1t/apps/sudo/app/routes/user.tsx
| 1 | import { ArrowLeft } from "lucide-react"; |
| 2 | import { Form, Link, data, redirect } from "react-router"; |
| 3 | |
| 4 | import { securityEventLabel } from "@g1t/contracts"; |
| 5 | |
| 6 | import type { Route } from "./+types/user"; |
| 7 | import { Badge, Button, EmptyState, Field, Input, Notice, PageHeader, Section, When } from "~/components/ui"; |
| 8 | import { accountsAdmin } from "~/lib/services.server"; |
| 9 | import { settle } from "~/lib/settle"; |
| 10 | import { requireStaff } from "~/lib/staff"; |
| 11 | |
| 12 | export const meta: Route.MetaFunction = ({ params }) => [ |
| 13 | { title: `${params.username} · sudo` }, |
| 14 | { name: "robots", content: "noindex, nofollow" }, |
| 15 | ]; |
| 16 | |
| 17 | export async function loader({ params, request, context }: Route.LoaderArgs) { |
| 18 | requireStaff(context); |
| 19 | const result = await settle(accountsAdmin.user(params.username)); |
| 20 | if (result.ok && !result.value) throw data("No such account.", { status: 404 }); |
| 21 | return { |
| 22 | user: result.ok ? result.value : null, |
| 23 | error: result.ok ? null : result.error, |
| 24 | removed: new URL(request.url).searchParams.get("removed"), |
| 25 | }; |
| 26 | } |
| 27 | |
| 28 | /** Removes an address: the reason is required, recorded and shown to the person. */ |
| 29 | export async function action({ params, request, context }: Route.ActionArgs) { |
| 30 | const staff = requireStaff(context); |
| 31 | const form = await request.formData(); |
| 32 | const email = String(form.get("email") ?? "").trim(); |
| 33 | const reason = String(form.get("reason") ?? "").trim(); |
| 34 | if (!reason) return data({ error: "Say why. The person sees the reason in their security log.", email }, { status: 422 }); |
| 35 | const result = await accountsAdmin.removeEmail(params.username, email, reason, staff.email); |
| 36 | if (!result.ok) return data({ error: result.error.message, email }, { status: 422 }); |
| 37 | throw redirect(`/users/${encodeURIComponent(params.username)}?removed=${encodeURIComponent(email)}`); |
| 38 | } |
| 39 | |
| 40 | export default function User({ loaderData, actionData }: Route.ComponentProps) { |
| 41 | const { user, error, removed } = loaderData; |
| 42 | if (!user) { |
| 43 | return ( |
| 44 | <main className="mx-auto max-w-4xl px-4 py-8 sm:py-10"> |
| 45 | <Notice tone="error">Could not load the account: {error}</Notice> |
| 46 | </main> |
| 47 | ); |
| 48 | } |
| 49 | return ( |
| 50 | <main className="mx-auto max-w-4xl px-4 py-8 sm:py-10"> |
| 51 | <Link to="/workspaces" className="inline-flex items-center gap-1.5 text-sm text-muted hover:text-fg"> |
| 52 | <ArrowLeft size={14} /> Workspaces |
| 53 | </Link> |
| 54 | <PageHeader |
| 55 | title={user.username} |
| 56 | description={ |
| 57 | <> |
| 58 | Account <span className="font-mono">{user.id}</span>, made <When at={user.createdAt} />.{" "} |
| 59 | {user.privateEmail ? "Keeps its address private on commits." : "Shows its primary address on commits."} |
| 60 | </> |
| 61 | } |
| 62 | /> |
| 63 | {removed && ( |
| 64 | <div className="mt-5"> |
| 65 | <Notice tone="ok">Removed {removed}. The person was told, with the reason.</Notice> |
| 66 | </div> |
| 67 | )} |
| 68 | |
| 69 | <Section |
| 70 | id="emails" |
| 71 | title="Email addresses" |
| 72 | description="Remove an address someone else needs, or one that is compromised. Never the last confirmed one; removing the primary makes the oldest other confirmed address primary." |
| 73 | className="mt-6" |
| 74 | > |
| 75 | <ul className="divide-y divide-line rounded-md border border-line"> |
| 76 | {user.emails.map((email) => ( |
| 77 | <li key={email.email} className="space-y-3 px-4 py-3"> |
| 78 | <div className="flex flex-wrap items-center gap-2 text-sm"> |
| 79 | <span className="font-mono break-all">{email.email}</span> |
| 80 | {email.primary && <Badge tone="lavender">Primary</Badge>} |
| 81 | {email.backup && <Badge>Backup</Badge>} |
| 82 | {email.verified ? <Badge tone="mint">Confirmed</Badge> : <Badge tone="warn">Unconfirmed</Badge>} |
| 83 | <span className="text-xs text-faint"> |
| 84 | added <When at={email.createdAt} /> |
| 85 | </span> |
| 86 | </div> |
| 87 | <Form method="post" className="flex flex-col gap-2 sm:flex-row sm:items-end"> |
| 88 | <input type="hidden" name="email" value={email.email} /> |
| 89 | <div className="grow"> |
| 90 | <Field label="Reason (the person sees it)"> |
| 91 | <Input |
| 92 | name="reason" |
| 93 | required |
| 94 | maxLength={200} |
| 95 | placeholder="Another account needs this address" |
| 96 | /> |
| 97 | </Field> |
| 98 | </div> |
| 99 | <Button variant="danger" type="submit"> |
| 100 | Remove |
| 101 | </Button> |
| 102 | </Form> |
| 103 | {actionData?.email === email.email && actionData.error && <Notice tone="error">{actionData.error}</Notice>} |
| 104 | </li> |
| 105 | ))} |
| 106 | </ul> |
| 107 | </Section> |
| 108 | |
| 109 | <Section id="log" title="Security log" description="What happened to the account's addresses and password, newest first." className="mt-6"> |
| 110 | {user.log.length === 0 ? ( |
| 111 | <EmptyState title="Nothing yet" /> |
| 112 | ) : ( |
| 113 | <ul className="divide-y divide-line text-sm"> |
| 114 | {user.log.map((event, at) => ( |
| 115 | <li key={`${event.createdAt}:${at}`} className="flex flex-col gap-1 py-2 sm:flex-row sm:justify-between sm:gap-4"> |
| 116 | <span className="min-w-0 break-words"> |
| 117 | {securityEventLabel(event)} |
| 118 | {event.staff && ( |
| 119 | <span className="text-muted"> |
| 120 | {" "} |
| 121 | · by {event.staff} |
| 122 | {event.reason ? `: ${event.reason}` : ""} |
| 123 | </span> |
| 124 | )} |
| 125 | </span> |
| 126 | <span className="shrink-0 text-xs text-faint"> |
| 127 | <When at={event.createdAt} time /> |
| 128 | </span> |
| 129 | </li> |
| 130 | ))} |
| 131 | </ul> |
| 132 | )} |
| 133 | </Section> |
| 134 | </main> |
| 135 | ); |
| 136 | } |