2026-05-22 20:26:11 -04:00
|
|
|
import { useEffect, useMemo, useState } from "react";
|
|
|
|
|
|
|
|
|
|
import type {
|
|
|
|
|
ConnectionCheckResponse,
|
2026-06-09 13:00:48 -04:00
|
|
|
PluginAdminForm,
|
2026-05-22 20:26:11 -04:00
|
|
|
PluginAdminFormField,
|
|
|
|
|
PluginConfigSchema,
|
|
|
|
|
} from "@/api/types";
|
|
|
|
|
import { ConnectionCheckAction } from "@/components/admin/ConnectionCheckAction";
|
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
2026-06-09 13:00:48 -04:00
|
|
|
|
2026-08-06 13:12:39 -04:00
|
|
|
import { adminFormForConfigSchema, humanizeConfigKey } from "./configSchemaAdminForm";
|
2026-06-09 13:00:48 -04:00
|
|
|
import { SchemaForm } from "./SchemaForm";
|
|
|
|
|
import { buildSchemaValues } from "./schemaFormUtils";
|
2026-05-22 20:26:11 -04:00
|
|
|
|
|
|
|
|
type PluginConfigValue = Record<string, unknown>;
|
|
|
|
|
|
2026-08-06 13:12:39 -04:00
|
|
|
const EMPTY_FIELDS: PluginAdminFormField[] = [];
|
|
|
|
|
|
2026-05-22 20:26:11 -04:00
|
|
|
type Props = {
|
|
|
|
|
schema: PluginConfigSchema;
|
|
|
|
|
value?: PluginConfigValue;
|
2026-07-23 11:24:04 -04:00
|
|
|
configuredSecrets?: string[];
|
|
|
|
|
onSave: (key: string, value: PluginConfigValue, clearSecrets: string[]) => void;
|
|
|
|
|
onTest?: (
|
|
|
|
|
key: string,
|
|
|
|
|
value: PluginConfigValue,
|
|
|
|
|
clearSecrets: string[],
|
|
|
|
|
) => Promise<ConnectionCheckResponse>;
|
2026-05-22 20:26:11 -04:00
|
|
|
isSaving?: boolean;
|
|
|
|
|
isTesting?: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2026-08-06 13:12:39 -04:00
|
|
|
function defaultValueForField(field: PluginAdminFormField): string | boolean {
|
2026-05-22 20:26:11 -04:00
|
|
|
if (field.default_value !== undefined) {
|
|
|
|
|
if (typeof field.default_value === "boolean") {
|
|
|
|
|
return field.default_value;
|
|
|
|
|
}
|
|
|
|
|
if (typeof field.default_value === "number") {
|
|
|
|
|
return String(field.default_value);
|
|
|
|
|
}
|
|
|
|
|
if (typeof field.default_value === "string") {
|
|
|
|
|
return field.default_value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (field.control === "SWITCH") {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-06 13:12:39 -04:00
|
|
|
function valueForField(
|
|
|
|
|
field: PluginAdminFormField,
|
|
|
|
|
configValue?: PluginConfigValue,
|
|
|
|
|
): string | boolean {
|
2026-05-22 20:26:11 -04:00
|
|
|
const raw = configValue?.[field.key];
|
|
|
|
|
if (typeof raw === "boolean") {
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
if (typeof raw === "number") {
|
|
|
|
|
return String(raw);
|
|
|
|
|
}
|
|
|
|
|
if (typeof raw === "string") {
|
|
|
|
|
return raw;
|
|
|
|
|
}
|
|
|
|
|
return defaultValueForField(field);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function PluginConfigForm({
|
|
|
|
|
schema,
|
|
|
|
|
value,
|
2026-07-23 11:24:04 -04:00
|
|
|
configuredSecrets = [],
|
2026-05-22 20:26:11 -04:00
|
|
|
onSave,
|
|
|
|
|
onTest,
|
|
|
|
|
isSaving = false,
|
|
|
|
|
isTesting = false,
|
|
|
|
|
}: Props) {
|
2026-08-06 13:12:39 -04:00
|
|
|
const inferredDescriptor = useMemo(() => adminFormForConfigSchema(schema), [schema]);
|
|
|
|
|
const fields = inferredDescriptor?.fields ?? EMPTY_FIELDS;
|
|
|
|
|
const supported = inferredDescriptor != null;
|
2026-05-22 20:26:11 -04:00
|
|
|
|
2026-07-23 11:24:04 -04:00
|
|
|
const descriptor = useMemo<PluginAdminForm>(() => {
|
2026-08-06 13:12:39 -04:00
|
|
|
const base = inferredDescriptor ?? { fields };
|
2026-07-23 11:24:04 -04:00
|
|
|
const configured = new Set(configuredSecrets);
|
|
|
|
|
return {
|
|
|
|
|
...base,
|
|
|
|
|
fields: base.fields.map((field) =>
|
|
|
|
|
configured.has(field.key) && (field.secret || field.control === "PASSWORD")
|
|
|
|
|
? { ...field, placeholder: "Saved secret — leave blank to keep" }
|
|
|
|
|
: field,
|
|
|
|
|
),
|
|
|
|
|
};
|
2026-08-06 13:12:39 -04:00
|
|
|
}, [configuredSecrets, fields, inferredDescriptor]);
|
2026-06-09 13:00:48 -04:00
|
|
|
|
|
|
|
|
const [values, setValues] = useState<PluginConfigValue>(() =>
|
2026-05-22 20:26:11 -04:00
|
|
|
Object.fromEntries(fields.map((field) => [field.key, valueForField(field, value)])),
|
|
|
|
|
);
|
|
|
|
|
const [testResult, setTestResult] = useState<ConnectionCheckResponse | null>(null);
|
2026-07-23 11:24:04 -04:00
|
|
|
const [clearSecrets, setClearSecrets] = useState<Set<string>>(new Set());
|
2026-05-22 20:26:11 -04:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-06-09 13:00:48 -04:00
|
|
|
setValues(Object.fromEntries(fields.map((field) => [field.key, valueForField(field, value)])));
|
2026-07-23 11:24:04 -04:00
|
|
|
setClearSecrets(new Set());
|
2026-05-22 20:26:11 -04:00
|
|
|
}, [fields, value]);
|
|
|
|
|
|
2026-06-09 13:00:48 -04:00
|
|
|
function handleChange(next: PluginConfigValue) {
|
2026-05-22 20:26:11 -04:00
|
|
|
setTestResult(null);
|
2026-06-09 13:00:48 -04:00
|
|
|
setValues(next);
|
2026-07-23 11:24:04 -04:00
|
|
|
setClearSecrets((current) => {
|
|
|
|
|
const updated = new Set(current);
|
|
|
|
|
for (const key of configuredSecrets) {
|
|
|
|
|
const replacement = next[key];
|
|
|
|
|
if (typeof replacement === "string" && replacement.trim() !== "") {
|
|
|
|
|
updated.delete(key);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return updated;
|
|
|
|
|
});
|
2026-05-22 20:26:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function handleTest() {
|
|
|
|
|
if (!onTest) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-07-23 11:24:04 -04:00
|
|
|
setTestResult(
|
|
|
|
|
await onTest(schema.key, buildSchemaValues(descriptor, values), Array.from(clearSecrets)),
|
|
|
|
|
);
|
2026-05-22 20:26:11 -04:00
|
|
|
} catch (error) {
|
|
|
|
|
setTestResult({
|
|
|
|
|
success: false,
|
|
|
|
|
message: error instanceof Error ? error.message : "Connection check failed.",
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!supported) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-2 rounded-md border border-amber-500/30 bg-amber-500/5 p-3">
|
|
|
|
|
<Label>{schema.title || schema.key}</Label>
|
|
|
|
|
<p className="text-muted-foreground text-sm">
|
|
|
|
|
This plugin uses a configuration schema shape that the admin form does not support yet.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-23 11:24:04 -04:00
|
|
|
<fieldset disabled={isSaving || isTesting} className="space-y-3 rounded-md border p-3">
|
2026-05-22 20:26:11 -04:00
|
|
|
<div className="space-y-1">
|
|
|
|
|
<Label>{schema.title || schema.key}</Label>
|
|
|
|
|
{schema.description ? (
|
|
|
|
|
<p className="text-muted-foreground text-xs">{schema.description}</p>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-06-09 13:00:48 -04:00
|
|
|
<SchemaForm
|
|
|
|
|
descriptor={descriptor}
|
|
|
|
|
values={values}
|
|
|
|
|
onChange={handleChange}
|
|
|
|
|
idPrefix={schema.key}
|
|
|
|
|
/>
|
2026-05-22 20:26:11 -04:00
|
|
|
|
2026-07-23 11:24:04 -04:00
|
|
|
{configuredSecrets.length > 0 ? (
|
|
|
|
|
<div className="space-y-2 rounded-md border border-dashed p-2.5">
|
|
|
|
|
{configuredSecrets.map((key) => {
|
|
|
|
|
const field = fields.find((candidate) => candidate.key === key);
|
|
|
|
|
const clearing = clearSecrets.has(key);
|
|
|
|
|
const required = field?.required === true;
|
|
|
|
|
return (
|
|
|
|
|
<div key={key} className="flex items-center justify-between gap-3 text-xs">
|
|
|
|
|
<span className={clearing ? "text-destructive" : "text-muted-foreground"}>
|
2026-08-06 13:12:39 -04:00
|
|
|
{field?.label || humanizeConfigKey(key)}: {clearing ? "will be cleared" : "saved"}
|
2026-07-23 11:24:04 -04:00
|
|
|
{required ? " (required)" : ""}
|
|
|
|
|
</span>
|
|
|
|
|
{!required ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="xs"
|
|
|
|
|
variant="ghost"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setClearSecrets((current) => {
|
|
|
|
|
const updated = new Set(current);
|
|
|
|
|
if (updated.has(key)) updated.delete(key);
|
|
|
|
|
else updated.add(key);
|
|
|
|
|
return updated;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{clearing ? "Keep saved secret" : "Clear saved secret"}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2026-05-22 20:26:11 -04:00
|
|
|
<div className="flex flex-wrap items-center gap-3">
|
|
|
|
|
{onTest ? (
|
|
|
|
|
<ConnectionCheckAction
|
|
|
|
|
onClick={handleTest}
|
|
|
|
|
result={testResult}
|
|
|
|
|
isPending={isTesting}
|
|
|
|
|
disabled={isSaving}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
<Button
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="outline"
|
|
|
|
|
disabled={isSaving || isTesting}
|
2026-07-23 11:24:04 -04:00
|
|
|
onClick={() =>
|
|
|
|
|
onSave(schema.key, buildSchemaValues(descriptor, values), Array.from(clearSecrets))
|
|
|
|
|
}
|
2026-05-22 20:26:11 -04:00
|
|
|
>
|
|
|
|
|
{schema.admin_form?.submit_label || "Save config"}
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
2026-07-23 11:24:04 -04:00
|
|
|
</fieldset>
|
2026-05-22 20:26:11 -04:00
|
|
|
);
|
|
|
|
|
}
|