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

106 lines
4.1 KiB
Vue

<script setup lang="ts">
import { Item, ItemGroup } from "@/components/ui/item";
import { onMounted, ref } from "vue";
import { Copy, Plus, X } from "@lucide/vue";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { NativeSelect, NativeSelectOption } from "@/components/ui/native-select";
import { api } from "@/lib/api";
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 = String((cause as Error).message || cause);
} 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);
const revoke = (id: string) =>
run(async () => {
await api.revokeToken(id);
await load();
});
onMounted(load);
</script>
<template>
<Section title="API tokens">
<template v-if="available" #action>
<form class="flex items-center gap-2" @submit.prevent="create">
<Input v-model="name" class="w-40" maxlength="40" required placeholder="Name" aria-label="Token name" />
<NativeSelect v-model="days" size="sm" aria-label="Lifetime">
<NativeSelectOption v-for="option in lifetimes" :key="option.days" :value="option.days">{{ option.label }}</NativeSelectOption>
</NativeSelect>
<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>
<p v-if="error" role="alert" class="mb-3 text-danger">{{ error }}</p>
<div v-if="created" class="mb-3 rounded-lg border border-border p-3">
<p class="mb-2 text-xs">Copy this token now. It cannot be shown again.</p>
<div class="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>
</div>
<p v-if="available && !tokens.length && !busy && !error" class="text-fg-subtle">No tokens yet.</p>
<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-sm">{{ token.name || token.id.slice(0, 12) }}</span>
<span class="text-xs text-fg-subtle">Created <Timestamp :at="token.created_at" /></span>
<span class="text-xs text-fg-subtle">Expires <Timestamp v-if="token.expires_at" :at="token.expires_at" /><template v-else>Never</template></span>
<span class="text-xs 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="revoke(token.id)">Revoke</Button>
</Item>
</ItemGroup>
</Section>
</template>