Files
archie/ui/src/accounts/PersonalTokensCard.vue
T

133 lines
5.7 KiB
Vue

<script setup lang="ts">
import { Item, ItemGroup } from "@/components/ui/item";
import { ReadError } from "@/components/ui/read-error";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { onMounted, ref } from "vue";
import { Copy, Plus, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Card } from "@/components/ui/card";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
import { Input } from "@/components/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
import { api, describeError } from "@/lib/api";
import { errors } from "@/copy/errors";
import { Section } from "@/components/ui/section";
import { Timestamp } from "@/components/ui/timestamp";
interface PersonalToken {
id: string;
name: string;
created_at: string;
expires_at: string | null;
last_used_at: string | null;
}
const lifetimes = [
{ days: 7, label: "7 days" },
{ days: 30, label: "30 days" },
{ days: 90, label: "90 days" },
{ days: 0, label: "No expiry" },
];
const available = ref(false);
const tokens = ref<PersonalToken[]>([]);
const created = ref("");
const error = ref("");
const busy = ref(false);
const name = ref("");
const days = ref(30);
async function run(action: () => Promise<void>): Promise<void> {
busy.value = true;
try {
await action();
error.value = "";
} catch (cause) {
error.value = describeError(cause, errors.common.unknown);
} finally {
busy.value = false;
}
}
const load = () =>
run(async () => {
const result = await api.tokens<{ tokens: PersonalToken[]; available: boolean }>();
available.value = result?.available ?? false;
tokens.value = result?.tokens ?? [];
});
const create = () =>
run(async () => {
created.value = (await api.createToken(name.value.trim(), Number(days.value)))?.token ?? "";
name.value = "";
await load();
});
const copy = () => navigator.clipboard.writeText(created.value);
// The token is kept until the next Revoke: Reka closes the dialog before the
// action's click runs, so clearing it on close would drop the revoke.
const pending = ref<PersonalToken | null>(null);
const dialogOpen = ref(false);
const revoke = (id: string) =>
run(async () => {
await api.revokeToken(id);
await load();
});
const confirmRevoke = () => {
if (pending.value) revoke(pending.value.id);
};
onMounted(load);
</script>
<template>
<Section title="API tokens">
<template v-if="available" #action>
<form class="flex flex-wrap items-center gap-2" @submit.prevent="create">
<Input v-model="name" class="w-40 min-w-0 flex-1 sm:flex-none" maxlength="40" required placeholder="Name" aria-label="Token name" />
<Select :model-value="String(days)" @update:model-value="(v) => (days = Number(v))">
<SelectTrigger size="sm" class="w-28" aria-label="Lifetime"><SelectValue /></SelectTrigger>
<SelectContent>
<SelectItem v-for="option in lifetimes" :key="option.days" :value="String(option.days)">{{ option.label }}</SelectItem>
</SelectContent>
</Select>
<Button type="submit" size="sm" :disabled="busy || !name.trim()"><Plus data-icon="inline-start" />New token</Button>
</form>
</template>
<p v-if="!available && !busy && !error" class="text-fg-subtle">Personal tokens need a signed-in person.</p>
<ReadError v-if="error" :message="error" retry-label="Retry" class="mb-3" @retry="load" />
<Card v-if="created" size="sm" class="mb-3">
<p class="text-caption text-fg-subtle">Copy this token now. It cannot be shown again.</p>
<div class="mt-2 flex gap-2">
<Input :model-value="created" readonly aria-label="New token" @focus="($event.target as HTMLInputElement).select()" />
<Button variant="outline" size="sm" aria-label="Copy token" @click="copy"><Copy /></Button>
<Button variant="ghost" size="sm" aria-label="Dismiss" @click="created = ''"><X /></Button>
</div>
</Card>
<Empty v-if="available && !tokens.length && !busy && !error"><EmptyHeader><EmptyTitle>No tokens yet.</EmptyTitle></EmptyHeader></Empty>
<ItemGroup v-if="tokens.length" aria-label="API tokens">
<Item v-for="token in tokens" :key="token.id" as="li" variant="outline" size="xs">
<span class="flex-1 truncate text-label">{{ token.name || token.id.slice(0, 12) }}</span>
<span class="text-caption text-fg-subtle">Created <Timestamp :at="token.created_at" /></span>
<span class="text-caption text-fg-subtle">Expires <Timestamp v-if="token.expires_at" :at="token.expires_at" /><template v-else>Never</template></span>
<span class="text-caption text-fg-subtle">Last used <Timestamp v-if="token.last_used_at" :at="token.last_used_at" /><template v-else>Never</template></span>
<Button variant="ghost" size="sm" class="text-danger" :disabled="busy" @click="pending = token; dialogOpen = true">Revoke</Button>
</Item>
</ItemGroup>
</Section>
<AlertDialog :open="dialogOpen" @update:open="(v: boolean) => { dialogOpen = v; }">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Revoke token?</AlertDialogTitle>
<AlertDialogDescription>Revoke "{{ pending?.name || pending?.id.slice(0, 12) }}"? Anything using it stops working immediately.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction @click="confirmRevoke">Revoke token</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>