101 lines
4.5 KiB
Vue
101 lines
4.5 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, watch } from "vue";
|
|
import { X } from "@lucide/vue";
|
|
|
|
import { Alert, AlertDescription } from "@/components/ui/alert";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
|
|
import { Section } from "@/components/ui/section";
|
|
import { useExtensionsStore, type Extension } from "@/stores/extensions";
|
|
import ExtensionSettingField from "./ExtensionSettingField.vue";
|
|
|
|
const props = defineProps<{
|
|
extension: Extension;
|
|
/** The messaging service's report for this channel, when it has one. */
|
|
status?: { state?: string; detail?: string };
|
|
}>();
|
|
|
|
const store = useExtensionsStore();
|
|
const draft = ref<Record<string, string>>({});
|
|
watch(() => props.extension.settings, (saved) => { draft.value = { ...saved }; }, { immediate: true });
|
|
|
|
// Stored keys this version no longer declares. The server refuses them, so
|
|
// they are listed and must be removed before the form saves.
|
|
const undeclared = computed(() =>
|
|
Object.keys(draft.value).filter((key) => !props.extension.settings_schema.some((spec) => spec.key === key)),
|
|
);
|
|
const dirty = computed(() => {
|
|
const saved = props.extension.settings;
|
|
const keys = new Set([...Object.keys(saved), ...Object.keys(draft.value)]);
|
|
return [...keys].some((key) => (draft.value[key] ?? "").trim() !== (saved[key] ?? ""));
|
|
});
|
|
function removeKey(key: string): void {
|
|
const { [key]: _removed, ...rest } = draft.value;
|
|
draft.value = rest;
|
|
}
|
|
const missing = computed(() =>
|
|
props.extension.settings_schema.filter((spec) => spec.required && !(draft.value[spec.key] ?? "").trim()).map((spec) => spec.label),
|
|
);
|
|
|
|
function save(): void {
|
|
void store.saveSettings(props.extension.name, draft.value);
|
|
}
|
|
function setValue(key: string, value: string): void {
|
|
draft.value = { ...draft.value, [key]: value };
|
|
}
|
|
|
|
const badge = computed(() => {
|
|
if (!props.extension.enabled) return { label: "Off", tone: "idle" } as const;
|
|
const state = props.status?.state;
|
|
if (state === "failed") return { label: "Failed", tone: "danger" } as const;
|
|
if (state === "degraded") return { label: "Degraded", tone: "warn" } as const;
|
|
if (state === "stopped") return { label: "Stopped", tone: "idle" } as const;
|
|
return undefined;
|
|
});
|
|
// The service reports why an enabled channel did not start: missing settings,
|
|
// or the error its last run ended with.
|
|
const problem = computed(() => (props.extension.enabled && props.status?.state !== "running" ? props.status?.detail : ""));
|
|
</script>
|
|
|
|
<template>
|
|
<Section :title="extension.display_name || extension.name">
|
|
<template #action>
|
|
<Badge v-if="badge" :variant="badge.tone">{{ badge.label }}</Badge>
|
|
</template>
|
|
<Alert v-if="problem" variant="destructive"><AlertDescription>{{ problem }}</AlertDescription></Alert>
|
|
<Alert v-if="undeclared.length" variant="warn">
|
|
<AlertDescription>
|
|
<p>No longer used by this version. Remove to save:</p>
|
|
<ul class="mt-1 flex flex-wrap gap-2">
|
|
<li v-for="key in undeclared" :key="key">
|
|
<Badge variant="outline" class="gap-1 font-mono">{{ key }}
|
|
<button type="button" class="rounded-sm hover:text-fg focus-visible:outline-2" :aria-label="`Remove ${key}`" @click="removeKey(key)"><X class="size-3" /></button>
|
|
</Badge>
|
|
</li>
|
|
</ul>
|
|
</AlertDescription>
|
|
</Alert>
|
|
<FieldGroup class="@container/field-group">
|
|
<Field v-for="spec in extension.settings_schema" :key="spec.key" orientation="responsive">
|
|
<FieldContent>
|
|
<FieldLabel :for="`${extension.name}-${spec.key}`">{{ spec.label }}</FieldLabel>
|
|
<FieldDescription v-if="spec.description">{{ spec.description }}</FieldDescription>
|
|
</FieldContent>
|
|
<div class="flex min-w-0 flex-wrap items-center gap-3 @md/field-group:flex-1">
|
|
<ExtensionSettingField
|
|
:id="`${extension.name}-${spec.key}`"
|
|
:spec="spec"
|
|
:model-value="draft[spec.key] ?? ''"
|
|
@update:model-value="(value) => setValue(spec.key, value)"
|
|
/>
|
|
</div>
|
|
</Field>
|
|
</FieldGroup>
|
|
<div class="flex flex-wrap items-center justify-end gap-3">
|
|
<span v-if="dirty && missing.length" class="text-caption text-fg-muted">Missing {{ missing.join(", ") }}</span>
|
|
<Button size="sm" :disabled="!dirty || undeclared.length > 0 || store.busy === extension.name" @click="save">Save</Button>
|
|
</div>
|
|
</Section>
|
|
</template>
|