Files
archie/ui/src/sources/SourcesPanel.vue
T

251 lines
9.2 KiB
Vue

<script setup lang="ts">
import { Check, Copy, KeyRound, Lock, LockOpen, Plus, Trash2 } from "@lucide/vue";
import { onMounted, ref } from "vue";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from "@/components/ui/alert-dialog";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { Section } from "@/components/ui/section";
import { Input } from "@/components/ui/input";
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
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 { useLiveResource } from "@/stores/session";
import {
signingKind,
signingLabel,
sourceURL,
type Source,
} from "./source-signing";
import SourceSecret from "./SourceSecret.vue";
import { useSources } from "./use-sources";
/**
* Capture sources: the endpoint each sender posts to and its signing setting
* (docs/prds/event-automation.md "Sources"). Turning signing off is a request
* that takes effect only once approved.
*/
const {
sources,
loadError,
failure,
revealed,
busy,
load,
create,
newSecret,
requestUnsigned,
requireSigning,
approveUnsigned,
rename,
setDeliveryHeader,
remove,
refusals,
} = useSources();
const pending = ref<Source | null>(null);
const customPath = ref("");
const copied = ref<string | null>(null);
useLiveResource(null, () => void load());
onMounted(load);
async function copy(value: string): Promise<void> {
await navigator.clipboard.writeText(value);
copied.value = value;
setTimeout(() => (copied.value = null), 1500);
}
async function submit(): Promise<void> {
await create(customPath.value);
if (!failure.value) customPath.value = "";
}
function url(source: Source): string {
return sourceURL(window.location.origin, source.path);
}
</script>
<template>
<section class="mb-6 flex flex-col gap-3">
<form
class="flex flex-wrap items-center justify-end gap-2"
@submit.prevent="submit"
>
<Input
v-model="customPath"
class="w-full font-mono sm:w-64"
placeholder="Custom path (optional)"
aria-label="Custom path"
/>
<Button type="submit" variant="outline" :disabled="busy">
<Plus data-icon="inline-start" />
New source</Button>
</form>
<Alert v-if="failure" variant="destructive">
<AlertTitle>{{ errors.sources.panel.writeFailed }}</AlertTitle>
<AlertDescription>{{ failure }}</AlertDescription>
</Alert>
<Alert v-if="loadError" variant="destructive">
<AlertTitle>{{ errors.sources.panel.loadFailed }}</AlertTitle>
<AlertDescription>{{ loadError }} <Button variant="link" size="xs" @click="load">Retry</Button></AlertDescription>
</Alert>
<SourceSecret v-if="revealed" :source="revealed" @dismiss="revealed = null" />
<Section v-if="sources && sources.length">
<Table>
<TableHeader>
<TableRow>
<TableHead>Source</TableHead>
<TableHead>Signing</TableHead>
<TableHead />
</TableRow>
</TableHeader>
<TableBody>
<TableRow v-for="source in sources" :key="source.path">
<TableCell class="font-mono text-fg-muted">
<div class="flex w-64 max-w-full min-w-0 flex-col gap-1">
<Input
class="h-7 border-transparent px-1 font-sans shadow-none hover:border-input dark:bg-transparent"
:model-value="source.name ?? ''"
placeholder="Name this source"
:aria-label="`Name of source ${source.path}`"
:disabled="busy"
@change="rename(source, ($event.target as HTMLInputElement).value)"
/>
<Tooltip><TooltipTrigger as-child><span tabindex="0" class="truncate">{{ source.path }}</span></TooltipTrigger><TooltipContent>{{ url(source) }}</TooltipContent></Tooltip>
<Input
class="h-6 border-transparent px-1 font-mono text-caption text-fg-muted shadow-none hover:border-input md:text-caption dark:bg-transparent"
:model-value="source.delivery_header ?? ''"
placeholder="Delivery ID header"
title="The header the sender repeats on every retry of one delivery; a retry then starts no second run"
:aria-label="`Delivery ID header of source ${source.path}`"
:disabled="busy"
@change="setDeliveryHeader(source, ($event.target as HTMLInputElement).value)"
/>
<div
v-if="refusals[source.path]"
class="text-caption font-sans text-danger"
:title="refusals[source.path].addrs.join(', ')"
>
{{ refusals[source.path].refused }} refused in 24h from
{{ refusals[source.path].addrs.slice(0, 3).join(", ") }}
</div>
</div></TableCell
>
<TableCell>
<Badge :variant="signingKind(source.signing)">{{
signingLabel(source.signing)
}}</Badge>
</TableCell>
<TableCell>
<div class="flex flex-wrap items-center justify-end gap-1">
<Tooltip>
<TooltipTrigger as-child>
<Button
variant="ghost"
size="icon-sm"
aria-label="Copy URL"
@click="copy(url(source))"
>
<Check v-if="copied === url(source)" />
<Copy v-else />
</Button>
</TooltipTrigger>
<TooltipContent>Copy URL</TooltipContent>
</Tooltip>
<Tooltip>
<TooltipTrigger as-child>
<Button
variant="ghost"
size="icon-sm"
aria-label="New secret"
:disabled="busy"
@click="newSecret(source)"
>
<KeyRound />
</Button>
</TooltipTrigger>
<TooltipContent>New secret</TooltipContent>
</Tooltip>
<Button
v-if="source.signing === 'signed'"
variant="outline"
size="sm"
:disabled="busy"
@click="requestUnsigned(source)"
>
<LockOpen data-icon="inline-start" />
Turn off signing</Button>
<Button
v-if="source.signing === 'unsigned_pending_approval'"
size="sm"
:disabled="busy"
@click="approveUnsigned(source)"
>
<Check data-icon="inline-start" />
Approve unsigned</Button>
<Button
v-if="source.signing !== 'signed'"
variant="outline"
size="sm"
:disabled="busy"
@click="requireSigning(source)"
>
<Lock data-icon="inline-start" />
Require signing</Button>
<Tooltip>
<TooltipTrigger as-child>
<Button
variant="ghost"
size="icon-sm"
aria-label="Delete source"
:disabled="busy"
@click="pending = source"
>
<Trash2 />
</Button>
</TooltipTrigger>
<TooltipContent>Delete source</TooltipContent>
</Tooltip>
</div>
</TableCell>
</TableRow>
</TableBody>
</Table>
</Section>
<Empty v-else-if="sources && !loadError">
<EmptyHeader><EmptyTitle>No sources yet</EmptyTitle></EmptyHeader>
</Empty>
</section>
<AlertDialog :open="pending !== null" @update:open="(v: boolean) => { if (!v) pending = null; }">
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle>Delete source?</AlertDialogTitle>
<AlertDialogDescription>Delete the source "{{ pending?.path }}"? Its webhook endpoint stops accepting deliveries.</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel>Cancel</AlertDialogCancel>
<AlertDialogAction @click="if (pending) { const src = pending; pending = null; remove(src); }">Delete source</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</template>