The list was keyed on the active kind, so every switch remounted it and showed its skeleton before the same packages came back. The rows are already filtered from one read, so the keys only threw work away.
378 lines
19 KiB
Vue
378 lines
19 KiB
Vue
<script setup lang="ts">
|
|
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
|
|
import { ReadError } from "@/components/ui/read-error";
|
|
import { Item, ItemGroup } from "@/components/ui/item";
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { computed, onMounted, ref, watch } from "vue";
|
|
import { ChevronRight, Pencil, Plus, Settings2, Trash2 } from "@lucide/vue";
|
|
|
|
import PageHeader from "@/base/PageHeader.vue";
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@/components/ui/alert-dialog";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Collapsible, CollapsibleContent, CollapsibleTrigger } from "@/components/ui/collapsible";
|
|
import { Dialog, DialogContent, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog";
|
|
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
|
|
import { Empty, EmptyHeader, EmptyTitle } from "@/components/ui/empty";
|
|
import { Input } from "@/components/ui/input";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { MoreMenu } from "@/components/ui/more-menu";
|
|
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select";
|
|
import { SettingRow } from "@/components/ui/setting-row";
|
|
import { Switch } from "@/components/ui/switch";
|
|
import { Section } from "@/components/ui/section";
|
|
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
|
|
import { Textarea } from "@/components/ui/textarea";
|
|
import { grants, stage, useExtensionsStore, type CatalogueEntry } from "@/stores/extensions";
|
|
import { useOrgStore } from "@/stores/org";
|
|
import { api, type Extension, type ExtensionScope, type SettingField } from "@/lib/api";
|
|
import { useRead } from "@/lib/use-read";
|
|
import { fromLines, toLines } from "@/lib/lines";
|
|
import { stateTone } from "@/runs/state";
|
|
import InstallationDialog from "./InstallationDialog.vue";
|
|
import PackageList from "./PackageList.vue";
|
|
import ExtensionsMarketplace from "./ExtensionsMarketplace.vue";
|
|
import CatalogueInstallDialog from "./CatalogueInstallDialog.vue";
|
|
import PackageVersionsDialog from "./PackageVersionsDialog.vue";
|
|
|
|
const store = useExtensionsStore();
|
|
const org = useOrgStore();
|
|
const loading = ref(true);
|
|
onMounted(async () => {
|
|
await Promise.all([store.load(), org.loadOrg()]);
|
|
loading.value = false;
|
|
});
|
|
|
|
// A new installation may be owned by the instance when the caller administers
|
|
// it, by their organisation when they own it, and always by themselves.
|
|
const scopes = computed<ExtensionScope[]>(() => {
|
|
const out: ExtensionScope[] = [];
|
|
if (org.instanceAdmin) out.push("instance");
|
|
if (org.org?.role === "owner") out.push("org");
|
|
out.push("person");
|
|
return out;
|
|
});
|
|
const scopeLabels: Record<ExtensionScope, string> = { instance: "Instance", org: "Organisation", person: "Person" };
|
|
|
|
const sourceLocation = ref("");
|
|
const sourceKey = ref("");
|
|
watch(
|
|
() => store.source,
|
|
(view) => {
|
|
sourceLocation.value = view.source?.location ?? "";
|
|
sourceKey.value = view.source?.public_key ?? "";
|
|
},
|
|
{ immediate: true },
|
|
);
|
|
async function saveSource(): Promise<void> {
|
|
if (await store.saveSource(sourceLocation.value.trim(), sourceKey.value.trim())) {
|
|
sourceLocation.value = store.source.source?.location ?? "";
|
|
sourceKey.value = store.source.source?.public_key ?? "";
|
|
}
|
|
}
|
|
|
|
const installing = ref<CatalogueEntry | null>(null);
|
|
const installOpen = ref(false);
|
|
function openInstall(entry: CatalogueEntry): void {
|
|
installing.value = entry;
|
|
installOpen.value = true;
|
|
}
|
|
|
|
const versionsOf = ref("");
|
|
const versionsOpen = ref(false);
|
|
function openVersions(name: string): void {
|
|
versionsOf.value = name;
|
|
versionsOpen.value = true;
|
|
}
|
|
// Versions belong to the package, which only an instance administrator
|
|
// changes.
|
|
const managesVersions = (extension: Extension) => org.instanceAdmin && extension.managed === true;
|
|
|
|
const installation = ref<Extension | null>(null);
|
|
const installationOpen = ref(false);
|
|
function openInstallation(extension: Extension | null = null): void {
|
|
installation.value = extension;
|
|
installationOpen.value = true;
|
|
}
|
|
async function reload(): Promise<void> {
|
|
await store.load();
|
|
}
|
|
|
|
async function overrideInstallation(extension: Extension, scope: "org" | "person", off: boolean): Promise<void> {
|
|
await store.overrideInstallation(extension.id, scope, off);
|
|
}
|
|
|
|
function overrideScope(extension: Extension, scope: ExtensionScope, off: boolean): Promise<void> {
|
|
return scope === "org" || scope === "person" ? overrideInstallation(extension, scope, off) : Promise.resolve();
|
|
}
|
|
|
|
const editing = ref<Extension | null>(null);
|
|
const settingsOpen = ref(false);
|
|
const schema = ref<SettingField[]>([]);
|
|
const values = ref<Record<string, string>>({});
|
|
const other = ref("");
|
|
function openSettings(extension: Extension): void {
|
|
editing.value = extension;
|
|
schema.value = extension.settings_schema ?? [];
|
|
const known = new Set(schema.value.map((field) => field.key));
|
|
values.value = Object.fromEntries(Object.entries(extension.settings ?? {}).filter(([key]) => known.has(key)));
|
|
other.value = toLines(Object.fromEntries(Object.entries(extension.settings ?? {}).filter(([key]) => !known.has(key))));
|
|
settingsOpen.value = true;
|
|
}
|
|
async function saveSettings(): Promise<void> {
|
|
if (!editing.value) return;
|
|
const settings = {
|
|
...fromLines(other.value),
|
|
...Object.fromEntries(Object.entries(values.value).filter(([, value]) => value !== "")),
|
|
};
|
|
if (await store.saveSettings(editing.value, settings)) settingsOpen.value = false;
|
|
}
|
|
|
|
const removing = ref<Extension | null>(null);
|
|
const removePackage = ref(false);
|
|
function askRemove(extension: Extension): void {
|
|
removing.value = extension;
|
|
// An instance installation is what makes the package usable, so removing it
|
|
// removes the package too. A narrower installation is its own configuration.
|
|
removePackage.value = extension.scope === "instance";
|
|
}
|
|
async function remove(): Promise<void> {
|
|
if (!removing.value) return;
|
|
if (removePackage.value) await store.remove(removing.value.name);
|
|
else await store.removeInstallation(removing.value.id);
|
|
}
|
|
|
|
const tones = {
|
|
accept: { tone: "warn", label: "Authority not accepted" },
|
|
disabled: { tone: "idle", label: "Disabled" },
|
|
enabled: { tone: undefined, label: "" },
|
|
} as const;
|
|
|
|
// Packages are accepted per kind; the tab lists one kind at a time.
|
|
const kinds = useRead(() => api.extensionKinds());
|
|
onMounted(kinds.reload);
|
|
const pathGroups = computed(() => {
|
|
const byGroup = new Map<string, { id: string; label: string; kinds: { kind: string; label: string; singular: string; contract: string }[] }>();
|
|
for (const kind of kinds.data.value ?? []) {
|
|
const group = byGroup.get(kind.group) ?? { id: kind.group, label: kind.group[0]!.toUpperCase() + kind.group.slice(1), kinds: [] };
|
|
group.kinds.push(kind);
|
|
byGroup.set(kind.group, group);
|
|
}
|
|
return [...byGroup.values()];
|
|
});
|
|
const activeKind = ref("");
|
|
const active = computed(() => (kinds.data.value ?? []).find((kind) => kind.kind === activeKind.value) ?? kinds.data.value?.[0]);
|
|
</script>
|
|
|
|
<template>
|
|
<div>
|
|
<PageHeader title="Extensions" />
|
|
|
|
<Alert v-if="store.error" variant="destructive" class="mb-4"><AlertDescription>{{ store.error }}</AlertDescription></Alert>
|
|
|
|
<Tabs default-value="installations" class="space-y-4">
|
|
<TabsList>
|
|
<TabsTrigger value="installations">Installations</TabsTrigger>
|
|
<TabsTrigger value="packages">Packages</TabsTrigger>
|
|
<TabsTrigger value="marketplace">Marketplace</TabsTrigger>
|
|
</TabsList>
|
|
|
|
<TabsContent value="installations">
|
|
<Section>
|
|
<div class="mb-2 flex justify-end">
|
|
<Button size="icon-sm" variant="ghost" aria-label="New installation" title="New installation" @click="openInstallation()"><Plus /></Button>
|
|
</div>
|
|
<ItemGroup aria-label="Installations">
|
|
<Item v-for="extension in store.extensions" :key="extension.id" as="li" variant="outline">
|
|
<div class="min-w-0 w-full @3xl:w-auto @3xl:flex-1">
|
|
<p class="truncate text-label font-medium">
|
|
{{ extension.display_name || extension.name }}
|
|
<span class="font-mono text-caption text-fg-subtle">{{ extension.version }}</span>
|
|
<Badge v-if="extension.version_pinned" variant="outline" class="ml-1">Pinned</Badge>
|
|
</p>
|
|
<p v-if="extension.package !== extension.name" class="truncate font-mono text-caption text-fg-subtle">{{ extension.package }}</p>
|
|
<p v-if="extension.description" class="text-caption text-fg-subtle">{{ extension.description }}</p>
|
|
<p class="truncate font-mono text-caption text-fg-subtle">{{ extension.digest }}</p>
|
|
<Collapsible v-if="extension.declared">
|
|
<CollapsibleTrigger as-child>
|
|
<Button variant="ghost" size="sm" class="-ml-2 h-6 px-2 text-caption font-normal text-fg-subtle">
|
|
<ChevronRight class="transition-transform group-data-[state=open]/button:rotate-90" />
|
|
Declared authority
|
|
</Button>
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent>
|
|
<ul class="font-mono text-caption text-fg-subtle">
|
|
<li v-for="line in grants(extension.declared)" :key="line">{{ line }}</li>
|
|
</ul>
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
<Collapsible v-if="extension.accepted">
|
|
<CollapsibleTrigger as-child>
|
|
<Button variant="ghost" size="sm" class="-ml-2 h-6 px-2 text-caption font-normal text-fg-subtle">
|
|
<ChevronRight class="transition-transform group-data-[state=open]/button:rotate-90" />
|
|
Accepted authority
|
|
</Button>
|
|
</CollapsibleTrigger>
|
|
<CollapsibleContent>
|
|
<ul class="font-mono text-caption text-fg-subtle">
|
|
<li v-for="line in grants(extension.accepted)" :key="line">{{ line }}</li>
|
|
</ul>
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
</div>
|
|
<Badge variant="outline">{{ scopeLabels[extension.scope] }}</Badge>
|
|
<Badge v-if="extension.overridden" variant="idle">Switched off</Badge>
|
|
<Badge v-if="tones[stage(extension)].tone" :variant="tones[stage(extension)].tone">{{ tones[stage(extension)].label }}</Badge>
|
|
<Badge :variant="stateTone(extension.state === 'running' ? 'succeeded' : extension.state)" :title="extension.error || undefined">{{ extension.state }}</Badge>
|
|
<Switch
|
|
v-if="extension.editable"
|
|
:model-value="!!extension.enabled"
|
|
:disabled="!!store.busy || stage(extension) === 'accept'"
|
|
:aria-label="`Enable ${extension.name}`"
|
|
@update:model-value="(value: boolean) => store.setEnabled(extension, value)"
|
|
/>
|
|
<Tooltip v-if="extension.editable && extension.settings_schema.length">
|
|
<TooltipTrigger as-child>
|
|
<Button variant="ghost" size="icon" :aria-label="`Settings for ${extension.name}`" :disabled="!!store.busy" @click="openSettings(extension)">
|
|
<Settings2 />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Settings</TooltipContent>
|
|
</Tooltip>
|
|
<Tooltip v-if="extension.editable">
|
|
<TooltipTrigger as-child>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
:aria-label="`Edit ${extension.name}`"
|
|
:disabled="!!store.busy"
|
|
@click="openInstallation(extension)"
|
|
>
|
|
<Pencil />
|
|
</Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Edit</TooltipContent>
|
|
</Tooltip>
|
|
<MoreMenu
|
|
v-if="managesVersions(extension) || (!extension.editable && ((extension.overridden_by?.length ?? 0) > 0 || (extension.can_override?.length ?? 0) > 0))"
|
|
:label="`${extension.name} use`"
|
|
>
|
|
<DropdownMenuItem v-if="managesVersions(extension)" @select="openVersions(extension.package)">Versions</DropdownMenuItem>
|
|
<template v-for="scope in extension.editable ? [] : (extension.overridden_by ?? [])" :key="`restore-${scope}`">
|
|
<DropdownMenuItem @select="overrideScope(extension, scope, false)"
|
|
>Use again {{ scope === 'org' ? 'for the organisation' : 'for me' }}</DropdownMenuItem
|
|
>
|
|
</template>
|
|
<template v-for="scope in extension.editable ? [] : (extension.can_override ?? [])" :key="`off-${scope}`">
|
|
<DropdownMenuItem v-if="!(extension.overridden_by?.length)" @select="overrideScope(extension, scope, true)"
|
|
>Switch off {{ scope === 'org' ? 'for the organisation' : 'for me' }}</DropdownMenuItem
|
|
>
|
|
</template>
|
|
</MoreMenu>
|
|
<Tooltip v-if="extension.editable">
|
|
<TooltipTrigger as-child>
|
|
<Button
|
|
variant="ghost"
|
|
size="icon"
|
|
:aria-label="`Remove ${extension.name}`"
|
|
:disabled="!!store.busy"
|
|
@click="askRemove(extension)"
|
|
><Trash2
|
|
/></Button>
|
|
</TooltipTrigger>
|
|
<TooltipContent>Remove</TooltipContent>
|
|
</Tooltip>
|
|
</Item>
|
|
<li v-if="loading" class="py-3 text-label text-fg-muted" role="status">Loading installations…</li>
|
|
<li v-else-if="!store.extensions.length && !store.error">
|
|
<Empty class="py-6">
|
|
<EmptyHeader>
|
|
<EmptyTitle>Nothing installed</EmptyTitle>
|
|
</EmptyHeader>
|
|
</Empty>
|
|
</li>
|
|
</ItemGroup>
|
|
</Section>
|
|
</TabsContent>
|
|
|
|
<TabsContent value="packages">
|
|
<ReadError v-if="kinds.error.value" :message="kinds.error.value" retry-label="Retry" @retry="kinds.reload" />
|
|
<template v-else-if="active">
|
|
<Tabs :model-value="active.kind" class="mb-4" @update:model-value="(value) => (activeKind = String(value))">
|
|
<div class="flex flex-wrap gap-x-6 gap-y-3">
|
|
<div v-for="group in pathGroups" :key="group.id">
|
|
<h2 class="mb-1 text-caption text-fg-subtle">{{ group.label }}</h2>
|
|
<TabsList variant="line" class="justify-start">
|
|
<TabsTrigger v-for="kind in group.kinds" :key="kind.kind" :value="kind.kind" class="flex-none px-3">{{ kind.label }}</TabsTrigger>
|
|
</TabsList>
|
|
</div>
|
|
</div>
|
|
</Tabs>
|
|
<PackageList :kind="active.kind" :contract="active.contract" :noun="active.singular" />
|
|
</template>
|
|
</TabsContent>
|
|
|
|
<TabsContent value="marketplace">
|
|
<Collapsible class="mb-3" default-open>
|
|
<CollapsibleTrigger class="cursor-pointer text-caption text-fg-muted">Catalogue source</CollapsibleTrigger>
|
|
<CollapsibleContent>
|
|
<form class="mt-2 flex flex-wrap items-end gap-2" @submit.prevent="saveSource">
|
|
<Input v-model="sourceLocation" class="max-w-96 font-mono" placeholder="https://… or /path/to/catalogue.json" aria-label="Catalogue location" />
|
|
<Input v-model="sourceKey" class="max-w-96 font-mono" placeholder="base64 Ed25519 public key" aria-label="Catalogue public key" />
|
|
<Button type="submit" size="sm" :disabled="!!store.busy"><Plus data-icon="inline-start" />Save</Button>
|
|
</form>
|
|
</CollapsibleContent>
|
|
</Collapsible>
|
|
<ReadError v-if="store.catalogueError" :message="store.catalogueError" retry-label="Retry" @retry="store.load()" />
|
|
<ExtensionsMarketplace v-else @install="openInstall" @versions="openVersions" />
|
|
</TabsContent>
|
|
</Tabs>
|
|
|
|
<InstallationDialog v-model:open="installationOpen" :extension="installation" :packages="store.packages" :scopes="scopes" @saved="reload" />
|
|
|
|
<CatalogueInstallDialog v-model:open="installOpen" :entry="installing" :scopes="scopes" />
|
|
<PackageVersionsDialog v-model:open="versionsOpen" :name="versionsOf" />
|
|
|
|
<Dialog v-model:open="settingsOpen">
|
|
<DialogContent class="max-h-[90dvh] overflow-y-auto sm:max-w-2xl">
|
|
<DialogHeader><DialogTitle>Settings for {{ editing?.name }}</DialogTitle></DialogHeader>
|
|
<form id="extension-settings" @submit.prevent="saveSettings">
|
|
<SettingRow v-for="field in schema" :key="field.key" :label="field.label || field.key" :for="`ext-set-${field.key}`" :hint="field.description">
|
|
<Select v-if="field.options?.length" v-model="values[field.key]">
|
|
<SelectTrigger :id="`ext-set-${field.key}`" class="w-full"><SelectValue /></SelectTrigger>
|
|
<SelectContent><SelectItem v-for="option in field.options" :key="option" :value="option">{{ option }}</SelectItem></SelectContent>
|
|
</Select>
|
|
<Input v-else :id="`ext-set-${field.key}`" v-model="values[field.key]" :required="field.required" :placeholder="field.secret ? 'engine:key' : undefined" />
|
|
</SettingRow>
|
|
<SettingRow label="Other settings" for="ext-settings" hint="key=value, one per line. A key ending in _ref takes engine:key.">
|
|
<Textarea id="ext-settings" v-model="other" class="font-mono" rows="3" />
|
|
</SettingRow>
|
|
</form>
|
|
<DialogFooter><Button type="submit" form="extension-settings" :disabled="!!store.busy">Save and restart</Button></DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
|
|
<AlertDialog :open="!!removing" @update:open="(value) => { if (!value) removing = null; }">
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Remove {{ removing?.name }}?</AlertDialogTitle>
|
|
<AlertDialogDescription v-if="removePackage">Its process stops and the package is no longer accepted.</AlertDialogDescription>
|
|
<AlertDialogDescription v-else>Its process stops and its configuration is deleted. The package stays accepted.</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction @click="remove">Remove</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
</div>
|
|
</template>
|