69 lines · 3.7 KBRaw
| 1 | import type { CiSecret } from "@refs/backend/contract"; |
| 2 | import { Button, Empty, Input, InputArea, LayerCard, Select, Table, formatDate } from "@refs/design-system"; |
| 3 | import { KeyIcon } from "@refs/design-system/icons"; |
| 4 | import { useState } from "react"; |
| 5 | import { Form, useNavigation } from "react-router"; |
| 6 | |
| 7 | /** The secrets of one owner, and a form to add one. Values are write-only: the table shows names and nothing else. */ |
| 8 | export function SecretTable({ secrets, environments, empty }: { secrets: readonly CiSecret[]; environments?: readonly string[]; empty: string }) { |
| 9 | const navigation = useNavigation(); |
| 10 | const saving = navigation.state === "submitting" && navigation.formData?.get("intent") === "secret-set"; |
| 11 | const [name, setName] = useState(""); |
| 12 | const [value, setValue] = useState(""); |
| 13 | return ( |
| 14 | <div className="flex flex-col gap-3"> |
| 15 | <LayerCard className="p-4"> |
| 16 | <Form key={navigation.state === "idle" ? secrets.length : "saving"} method="post" className="flex flex-col gap-4" autoComplete="off"> |
| 17 | <input type="hidden" name="intent" value="secret-set" /> |
| 18 | <div className="flex flex-wrap items-start gap-3"> |
| 19 | <Input label="Name" name="name" placeholder="NPM_TOKEN" value={name} onChange={(event) => setName(event.target.value.toUpperCase())} className="font-mono" required /> |
| 20 | {environments && <Select label="Scope" name="environment" defaultValue="" items={{ "": "Repository", ...Object.fromEntries(environments.map((environment) => [environment, `Environment: ${environment}`])) }} className="w-56" />} |
| 21 | </div> |
| 22 | <InputArea label="Value" name="value" rows={3} value={value} onValueChange={setValue} className="font-mono" description="refs stores it sealed. Nobody can read it back, and it is masked in logs." required /> |
| 23 | <div> |
| 24 | <Button type="submit" variant="primary" loading={saving} disabled={!name || !value}> |
| 25 | Save secret |
| 26 | </Button> |
| 27 | </div> |
| 28 | </Form> |
| 29 | </LayerCard> |
| 30 | <LayerCard> |
| 31 | {secrets.length === 0 ? ( |
| 32 | <Empty size="sm" icon={<KeyIcon size={32} />} title="No secrets" description={empty} /> |
| 33 | ) : ( |
| 34 | <Table> |
| 35 | <Table.Header> |
| 36 | <Table.Row> |
| 37 | <Table.Head>Name</Table.Head> |
| 38 | {environments && <Table.Head>Scope</Table.Head>} |
| 39 | <Table.Head>Updated</Table.Head> |
| 40 | <Table.Head /> |
| 41 | </Table.Row> |
| 42 | </Table.Header> |
| 43 | <Table.Body> |
| 44 | {secrets.map((secret) => ( |
| 45 | <Table.Row key={`${secret.environment ?? ""}/${secret.name}`}> |
| 46 | <Table.Cell className="font-mono text-xs">{secret.name}</Table.Cell> |
| 47 | {environments && <Table.Cell className="text-muted">{secret.environment ?? "Repository"}</Table.Cell>} |
| 48 | <Table.Cell className="text-muted"> |
| 49 | {formatDate(secret.updatedAt)} by {secret.updatedBy} |
| 50 | </Table.Cell> |
| 51 | <Table.Cell className="text-right"> |
| 52 | <Form method="post" className="inline-flex"> |
| 53 | <input type="hidden" name="intent" value="secret-delete" /> |
| 54 | <input type="hidden" name="name" value={secret.name} /> |
| 55 | <input type="hidden" name="environment" value={secret.environment ?? ""} /> |
| 56 | <Button type="submit" variant="secondary-destructive" size="sm"> |
| 57 | Delete |
| 58 | </Button> |
| 59 | </Form> |
| 60 | </Table.Cell> |
| 61 | </Table.Row> |
| 62 | ))} |
| 63 | </Table.Body> |
| 64 | </Table> |
| 65 | )} |
| 66 | </LayerCard> |
| 67 | </div> |
| 68 | ); |
| 69 | } |