183 lines
7.1 KiB
Vue
183 lines
7.1 KiB
Vue
<script setup lang="ts">
|
|
import { onMounted, ref } from "vue";
|
|
import { Link, Plus, Trash2, X } from "@lucide/vue";
|
|
|
|
import { useAccountsStore } from "@/accounts/accounts";
|
|
import PageHeader from "@/base/PageHeader.vue";
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Section } from "@/components/ui/section";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table";
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { errors } from "@/copy/errors";
|
|
import { api, describeError, type Person } from "@/lib/api";
|
|
import { useOrgStore } from "@/stores/org";
|
|
|
|
/** The people who answer approvals. Saving replaces the whole list, so each
|
|
* change sends the list with that one change and shows what the server kept. */
|
|
const people = ref<Person[]>([]);
|
|
const error = ref("");
|
|
const loading = ref(true);
|
|
const busy = ref(false);
|
|
const name = ref("");
|
|
const identity = ref<Record<string, string>>({});
|
|
|
|
const accounts = useAccountsStore();
|
|
const orgStore = useOrgStore();
|
|
orgStore.watchIdentities();
|
|
|
|
const accountPrefix = "account:";
|
|
const mine = () => (accounts.status?.user ? accountPrefix + accounts.status.user.id : "");
|
|
|
|
/** An account identity reads as the account's name; a channel identity as written. */
|
|
function label(value: string): string {
|
|
return value.startsWith(accountPrefix) ? orgStore.identityName(value.slice(accountPrefix.length)) : value;
|
|
}
|
|
|
|
async function load(): Promise<void> {
|
|
try {
|
|
people.value = await api.people();
|
|
error.value = "";
|
|
} catch (cause) {
|
|
error.value = describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
if (!accounts.status) await accounts.load();
|
|
await load();
|
|
});
|
|
|
|
async function save(next: Person[]): Promise<void> {
|
|
busy.value = true;
|
|
try {
|
|
people.value = await api.putPeople(next);
|
|
error.value = "";
|
|
} catch (cause) {
|
|
error.value = describeError(cause, errors.common.unknown);
|
|
} finally {
|
|
busy.value = false;
|
|
}
|
|
}
|
|
|
|
const change = (person: Person, edit: Partial<Person>) =>
|
|
save(people.value.map((p) => (p === person ? { ...p, ...edit } : p)));
|
|
|
|
async function add(): Promise<void> {
|
|
const value = name.value.trim();
|
|
if (!value) return;
|
|
await save([...people.value, { id: "", name: value, authority: false, identities: [] }]);
|
|
if (!error.value) name.value = "";
|
|
}
|
|
|
|
const rename = (person: Person, event: Event) => {
|
|
const next = (event.target as HTMLInputElement).value.trim();
|
|
if (next && next !== person.name) void change(person, { name: next });
|
|
};
|
|
|
|
async function addIdentity(person: Person, value: string): Promise<void> {
|
|
const next = value.trim();
|
|
if (!next || person.identities.includes(next)) return;
|
|
await change(person, { identities: [...person.identities, next] });
|
|
if (!error.value) identity.value[person.id] = "";
|
|
}
|
|
|
|
const removeIdentity = (person: Person, value: string) =>
|
|
change(person, { identities: person.identities.filter((i) => i !== value) });
|
|
|
|
const remove = (person: Person) => save(people.value.filter((p) => p !== person));
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<PageHeader title="People" />
|
|
|
|
<Alert v-if="error" variant="destructive" class="mb-4"><AlertDescription>{{ error }}</AlertDescription></Alert>
|
|
|
|
<form class="mb-4 flex flex-wrap items-center gap-2" @submit.prevent="add">
|
|
<Input v-model="name" class="max-w-64" placeholder="Name" aria-label="Name" required />
|
|
<Button type="submit" size="sm" :disabled="!name.trim() || busy"><Plus data-icon="inline-start" />Add</Button>
|
|
</form>
|
|
|
|
<Section>
|
|
<Table>
|
|
<TableHeader>
|
|
<TableRow>
|
|
<TableHead class="w-64">Name</TableHead>
|
|
<TableHead class="w-28">Authority</TableHead>
|
|
<TableHead>Identities</TableHead>
|
|
<TableHead class="w-16 text-right">Actions</TableHead>
|
|
</TableRow>
|
|
</TableHeader>
|
|
<TableBody>
|
|
<TableRow v-for="person in people" :key="person.id">
|
|
<TableCell>
|
|
<Input :default-value="person.name" :aria-label="`Name of ${person.name}`" :disabled="busy" @change="rename(person, $event)" />
|
|
</TableCell>
|
|
<TableCell>
|
|
<Switch
|
|
:model-value="person.authority"
|
|
:aria-label="`Authority for ${person.name}`"
|
|
:disabled="busy"
|
|
@update:model-value="(on: boolean) => change(person, { authority: on })"
|
|
/>
|
|
</TableCell>
|
|
<TableCell>
|
|
<div class="flex flex-wrap items-center gap-2">
|
|
<Badge v-for="value in person.identities" :key="value" variant="outline" class="gap-1">
|
|
{{ label(value) }}
|
|
<button
|
|
type="button"
|
|
class="rounded-sm focus-visible:outline focus-visible:outline-2"
|
|
:aria-label="`Remove ${label(value)} from ${person.name}`"
|
|
:disabled="busy"
|
|
@click="removeIdentity(person, value)"
|
|
><X class="size-3" /></button>
|
|
</Badge>
|
|
<form class="flex items-center gap-1" @submit.prevent="addIdentity(person, identity[person.id] ?? '')">
|
|
<Input
|
|
v-model="identity[person.id]"
|
|
class="h-8 w-44"
|
|
placeholder="telegram:1001"
|
|
:aria-label="`Add an identity to ${person.name}`"
|
|
/>
|
|
<Button type="submit" size="icon" variant="ghost" :aria-label="`Add identity to ${person.name}`" :disabled="busy || !identity[person.id]?.trim()"><Plus /></Button>
|
|
</form>
|
|
<Button
|
|
v-if="mine() && !person.identities.includes(mine())"
|
|
size="sm"
|
|
variant="outline"
|
|
:disabled="busy"
|
|
@click="addIdentity(person, mine())"
|
|
><Link data-icon="inline-start" />Link my account</Button>
|
|
</div>
|
|
</TableCell>
|
|
<TableCell class="text-right">
|
|
<Tooltip>
|
|
<TooltipTrigger as-child>
|
|
<Button variant="ghost" size="icon" :aria-label="`Remove ${person.name}`" :disabled="busy" @click="remove(person)"><Trash2 /></Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Remove</TooltipContent>
|
|
</Tooltip>
|
|
</TableCell>
|
|
</TableRow>
|
|
<TableRow v-if="loading">
|
|
<TableCell colspan="4" class="text-label text-fg-muted" role="status">Loading people…</TableCell>
|
|
</TableRow>
|
|
<TableRow v-else-if="!people.length && !error">
|
|
<TableCell colspan="4">
|
|
<Empty class="py-6"><EmptyHeader><EmptyTitle>No people yet</EmptyTitle></EmptyHeader></Empty>
|
|
</TableCell>
|
|
</TableRow>
|
|
</TableBody>
|
|
</Table>
|
|
</Section>
|
|
</div>
|
|
</template>
|