fix(extensions): list settings a version no longer declares and save only once they are removed
This commit is contained in:
2 files changed
+26
-23
No files matched your search
@@ -399,7 +399,6 @@ func (a *extensionsAPI) handleExtensionSettings(w http.ResponseWriter, r *http.R
|
||||
writeControlPlaneError(w, err)
|
||||
return
|
||||
}
|
||||
keepUndeclared(values, doc, name, installed.Descriptor.Settings)
|
||||
if err := a.putExtensionSettings(r.Context(), withSettings(doc, name, values), version); err != nil {
|
||||
writeControlPlaneError(w, err)
|
||||
return
|
||||
@@ -435,22 +434,6 @@ func declaredSettings(schema []storepkg.Setting, values map[string]string) (map[
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// keepUndeclared copies into values the stored settings the package no longer
|
||||
// declares, so a version that drops a key and a later one that brings it back
|
||||
// do not lose what the operator entered.
|
||||
func keepUndeclared(values map[string]string, doc controlplanerpc.ExtensionSettings, name string, schema []storepkg.Setting) {
|
||||
for _, setting := range doc.Extensions {
|
||||
if setting.Name != name {
|
||||
continue
|
||||
}
|
||||
for key, value := range setting.Settings {
|
||||
if !slices.ContainsFunc(schema, func(s storepkg.Setting) bool { return s.Key == key }) {
|
||||
values[key] = value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// withSettings returns doc with the named extension's settings replaced,
|
||||
// keeping whether it is enabled.
|
||||
func withSettings(doc controlplanerpc.ExtensionSettings, name string, values map[string]string) controlplanerpc.ExtensionSettings {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<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";
|
||||
@@ -20,10 +21,20 @@ 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;
|
||||
return props.extension.settings_schema.some((spec) => (draft.value[spec.key] ?? "").trim() !== (saved[spec.key] ?? ""));
|
||||
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),
|
||||
);
|
||||
@@ -35,11 +46,8 @@ function secretRef(key: string): { engine: string; key: string } {
|
||||
function setSecretRef(key: string, value: { engine: string; key: string }): void {
|
||||
draft.value = { ...draft.value, [key]: value.engine || value.key ? `${value.engine}:${value.key}` : "" };
|
||||
}
|
||||
// Only declared keys are sent: the server refuses any other, and keeps the
|
||||
// values an earlier version declared on its own.
|
||||
function save(): void {
|
||||
const declared = Object.fromEntries(props.extension.settings_schema.map((spec) => [spec.key, draft.value[spec.key] ?? ""]));
|
||||
void store.saveSettings(props.extension.name, declared);
|
||||
void store.saveSettings(props.extension.name, draft.value);
|
||||
}
|
||||
function setValue(key: string, value: string | number): void {
|
||||
draft.value = { ...draft.value, [key]: String(value) };
|
||||
@@ -64,6 +72,18 @@ const problem = computed(() => (props.extension.enabled && props.status?.state !
|
||||
<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>
|
||||
@@ -86,7 +106,7 @@ const problem = computed(() => (props.extension.enabled && props.status?.state !
|
||||
</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 || store.busy === extension.name" @click="save">Save</Button>
|
||||
<Button size="sm" :disabled="!dirty || undeclared.length > 0 || store.busy === extension.name" @click="save">Save</Button>
|
||||
</div>
|
||||
</Section>
|
||||
</template>
|
||||
Reference in new issue
Block a user