fix: preserve widget drafts and connection groups
This commit is contained in:
1 parent
48042a08ff
commit
29c94be821
12 files changed
+258
-79
No files matched your search
@@ -1,4 +1,8 @@
|
||||
import { getWidgetConnectionBindingNames, isWidgetConnectionCompatible } from "@homarr/custom-widgets/package";
|
||||
import {
|
||||
getWidgetConnectionBindingNames,
|
||||
isWidgetConnectionCompatible,
|
||||
mergeWidgetConnectionBindings,
|
||||
} from "@homarr/custom-widgets/package";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { parse, stringify } from "superjson";
|
||||
import { z } from "zod/v4";
|
||||
@@ -58,7 +62,11 @@ export async function prepareWidgetMigration(
|
||||
const issues = validateCustomWidgetOptions(source.options, configuration);
|
||||
if (issues.length)
|
||||
throw new TRPCError({ code: "PRECONDITION_FAILED", message: `Placement ${placement.id}: ${issues[0]?.message}` });
|
||||
const mergedBindings = { ...bindings, ...placement.packageOptions.connectionBindings };
|
||||
const mergedBindings = mergeWidgetConnectionBindings(
|
||||
source.connections,
|
||||
bindings,
|
||||
placement.packageOptions.connectionBindings,
|
||||
);
|
||||
for (const [name, requirement] of Object.entries(source.connections)) {
|
||||
const names = getWidgetConnectionBindingNames(name, requirement, mergedBindings);
|
||||
if (!names.length && !requirement.optional)
|
||||
|
||||
@@ -6,7 +6,11 @@ import { isRecord } from "@homarr/common";
|
||||
import { eq } from "@homarr/db";
|
||||
import { boards, customWidgetArtifacts, customWidgetInstallations, items } from "@homarr/db/schema";
|
||||
import { getCustomWidgetDefaultOptions, validateCustomWidgetOptions } from "@homarr/custom-widgets/core";
|
||||
import { customWidgetArtifactSchema, customWidgetPackageSchema } from "@homarr/custom-widgets/package";
|
||||
import {
|
||||
customWidgetArtifactSchema,
|
||||
customWidgetPackageSchema,
|
||||
mergeWidgetConnectionBindings,
|
||||
} from "@homarr/custom-widgets/package";
|
||||
import type { CustomWidgetArtifact, CustomWidgetPackage } from "@homarr/custom-widgets/package";
|
||||
|
||||
import { throwIfActionForbiddenAsync } from "../../board/board-access";
|
||||
@@ -105,7 +109,11 @@ export async function resolvePackagePlacement(ctx: PackageContext, itemId: strin
|
||||
const issues = validateCustomWidgetOptions(source.options, configuration);
|
||||
if (issues.length > 0)
|
||||
throw new TRPCError({ code: "PRECONDITION_FAILED", message: "Widget configuration needs repair" });
|
||||
const bindings = { ...parseBindings(installation.bindings), ...options.connectionBindings };
|
||||
const bindings = mergeWidgetConnectionBindings(
|
||||
source.connections,
|
||||
parseBindings(installation.bindings),
|
||||
options.connectionBindings,
|
||||
);
|
||||
return { item, installation, artifact, source, configuration, bindings };
|
||||
}
|
||||
|
||||
|
||||
@@ -115,6 +115,24 @@ export function getWidgetConnectionBindingNames(
|
||||
);
|
||||
}
|
||||
|
||||
/** A placement selects an entire group; inherited members must not leak into its selection. */
|
||||
export function mergeWidgetConnectionBindings(
|
||||
requirements: Record<string, { multiple?: boolean }>,
|
||||
defaults: Record<string, string>,
|
||||
overrides: Record<string, string>,
|
||||
) {
|
||||
const bindings = { ...defaults };
|
||||
for (const [name, requirement] of Object.entries(requirements)) {
|
||||
if (!requirement.multiple) continue;
|
||||
const belongsToGroup = (key: string) => key === name || key.startsWith(`${name}:`);
|
||||
if (!Object.keys(overrides).some(belongsToGroup)) continue;
|
||||
for (const key of Object.keys(bindings).filter(belongsToGroup)) delete bindings[key];
|
||||
}
|
||||
const merged = { ...bindings, ...overrides };
|
||||
// An explicit empty group is stored as an empty base binding to distinguish it from inheritance.
|
||||
return Object.fromEntries(Object.entries(merged).filter(([, id]) => Boolean(id)));
|
||||
}
|
||||
|
||||
export function getWidgetConnectionRequirement<T extends { multiple?: boolean }>(
|
||||
requirements: Record<string, T>,
|
||||
name: string,
|
||||
|
||||
@@ -5415,6 +5415,7 @@
|
||||
"advancedOptions": "Advanced options",
|
||||
"invalidJson": "Invalid JSON",
|
||||
"connectionDefault": "Use installation connection ({name})",
|
||||
"useInstallationConnections": "Use installation connections",
|
||||
"connectionUnbound": "Choose a connection",
|
||||
"connectionMissing": "This required connection is not bound.",
|
||||
"connectionUnavailable": "Saved connection is unavailable",
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
"use client";
|
||||
|
||||
import { Alert, Select, Stack } from "@mantine/core";
|
||||
import { Alert, Button, MultiSelect, Select, Stack } from "@mantine/core";
|
||||
|
||||
import { clientApi } from "@homarr/api/client";
|
||||
import { useSession } from "@homarr/auth/client";
|
||||
import { isRecord } from "@homarr/common";
|
||||
import { isWidgetConnectionCompatible } from "@homarr/custom-widgets/package";
|
||||
import {
|
||||
getWidgetConnectionBindingNames,
|
||||
isWidgetConnectionCompatible,
|
||||
mergeWidgetConnectionBindings,
|
||||
} from "@homarr/custom-widgets/package";
|
||||
import type { CustomWidgetPackage } from "@homarr/custom-widgets/package";
|
||||
import { useI18n } from "@homarr/translation/client";
|
||||
|
||||
@@ -27,6 +31,7 @@ export function PackageConnectionBindings({
|
||||
enabled: isAdmin && Object.keys(connections).length > 0,
|
||||
});
|
||||
if (!isAdmin) return null;
|
||||
const effectiveBindings = mergeWidgetConnectionBindings(connections, installationBindings, value);
|
||||
return (
|
||||
<Stack gap="sm">
|
||||
{local.isError && <Alert color="red">{t("connectionsError")}</Alert>}
|
||||
@@ -34,6 +39,52 @@ export function PackageConnectionBindings({
|
||||
const candidates = (local.data ?? []).filter(
|
||||
(row) => isRecord(row.configuration) && isWidgetConnectionCompatible(connection, row.configuration),
|
||||
);
|
||||
if (connection.multiple) {
|
||||
const names = getWidgetConnectionBindingNames(name, connection, effectiveBindings);
|
||||
const selected = [...new Set(names.map((key) => effectiveBindings[key]!))];
|
||||
const choices = candidates.map((row) => ({ value: row.id, label: row.name }));
|
||||
for (const id of selected) {
|
||||
if (!choices.some((choice) => choice.value === id))
|
||||
choices.push({ value: id, label: t("connectionUnavailable") });
|
||||
}
|
||||
const overrideKeys = Object.keys(value).filter((key) => key === name || key.startsWith(`${name}:`));
|
||||
const withoutOverride = () => {
|
||||
const bindings = { ...value };
|
||||
for (const key of overrideKeys) delete bindings[key];
|
||||
return bindings;
|
||||
};
|
||||
let error: string | undefined;
|
||||
if (local.data && !connection.optional && !selected.length) error = t("connectionMissing");
|
||||
if (local.data && selected.some((id) => !candidates.some((row) => row.id === id)))
|
||||
error = t("connectionUnavailable");
|
||||
return (
|
||||
<Stack key={name} gap="xs">
|
||||
<MultiSelect
|
||||
label={connection.label}
|
||||
description={connection.description}
|
||||
required={!connection.optional}
|
||||
placeholder={t("connectionUnbound")}
|
||||
clearable
|
||||
searchable
|
||||
data={choices}
|
||||
value={selected}
|
||||
error={error}
|
||||
disabled={local.isLoading}
|
||||
onChange={(ids) => {
|
||||
const bindings = withoutOverride();
|
||||
bindings[name] = ids[0] ?? "";
|
||||
for (const id of ids.slice(1)) bindings[`${name}:${id}`] = id;
|
||||
onChange(bindings);
|
||||
}}
|
||||
/>
|
||||
{overrideKeys.length > 0 && (
|
||||
<Button variant="subtle" size="compact-xs" onClick={() => onChange(withoutOverride())}>
|
||||
{t("useInstallationConnections")}
|
||||
</Button>
|
||||
)}
|
||||
</Stack>
|
||||
);
|
||||
}
|
||||
const defaults = candidates.find((row) => row.id === installationBindings[name]);
|
||||
const selected = value[name];
|
||||
const choices = candidates.map((row) => ({ value: row.id, label: row.name }));
|
||||
|
||||
Reference in new issue
Block a user