"use client"; import { useTranslations } from "next-intl"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { Switch } from "@/components/ui/switch"; import type { WorkflowSetting, WorkflowSettingValue } from "@/lib/workflows"; /** What the creator changed, per setting. A cleared number is absent, not zero. */ export type AdvancedValues = Record; function isValid(setting: WorkflowSetting, value: unknown): value is WorkflowSettingValue { if (setting.type === "bool") return typeof value === "boolean"; if (setting.type === "enum") return typeof value === "string" && (setting.choices ?? []).includes(value); return ( typeof value === "number" && Number.isInteger(value) && value >= (setting.min ?? value) && value <= (setting.max ?? value) ); } /** Only what differs from the defaults and is valid: an empty result leaves the request as it always was. */ export function changedSettings(schema: readonly WorkflowSetting[], values: AdvancedValues): AdvancedValues { const changed: AdvancedValues = {}; for (const setting of schema) { const value = values[setting.key]; if (value !== undefined && value !== setting.default && isValid(setting, value)) changed[setting.key] = value; } return changed; } /** A number typed but outside its range or not whole, which the server would reject. */ export function invalidSetting(schema: readonly WorkflowSetting[], values: AdvancedValues): WorkflowSetting | null { return schema.find((setting) => values[setting.key] !== undefined && !isValid(setting, values[setting.key])) ?? null; } /** * The settings most people never touch, rendered from the schema * `GET /tasks/workflows` publishes for the chosen workflow. */ export function WorkflowAdvancedSettings({ schema, values, onChange, disabled }: { schema: readonly WorkflowSetting[]; values: AdvancedValues; onChange: (values: AdvancedValues) => void; disabled?: boolean; }) { const t = useTranslations("home.advanced"); const changedCount = Object.keys(changedSettings(schema, values)).length; const set = (key: string, value: WorkflowSettingValue | undefined) => { const next = { ...values }; if (value === undefined) delete next[key]; else next[key] = value; onChange(next); }; return (

{t("title")}

{t("body")}

{schema.map((setting) => { const id = `workflow-setting-${setting.key}`; const label = t(`settings.${setting.key}.label`); const hint = t(`settings.${setting.key}.hint`); const value = values[setting.key]; if (setting.type === "bool") { return (

{hint}

set(setting.key, checked)} />
); } if (setting.type === "enum") { return (

{hint}

); } const problem = value !== undefined && !isValid(setting, value); return (
set(setting.key, event.target.value === "" ? undefined : Number(event.target.value))} />

{problem ? t("outOfRange", { min: setting.min ?? 0, max: setting.max ?? 0 }) : hint}

); })}
); }