2026-09-11 15:22:14 +08:00
|
|
|
import { channelValidationMessage } from "./validationMessages";
|
2026-08-12 16:22:34 +08:00
|
|
|
import {
|
|
|
|
|
Suspense,
|
2026-09-11 15:22:14 +08:00
|
|
|
useCallback,
|
2026-08-12 16:22:34 +08:00
|
|
|
useEffect,
|
2026-09-11 15:22:14 +08:00
|
|
|
useId,
|
2026-08-12 16:22:34 +08:00
|
|
|
useMemo,
|
2026-09-11 15:22:14 +08:00
|
|
|
useRef,
|
2026-08-12 16:22:34 +08:00
|
|
|
useState,
|
|
|
|
|
type ComponentType,
|
2026-09-11 15:22:14 +08:00
|
|
|
type ReactNode,
|
2026-08-12 16:22:34 +08:00
|
|
|
} from "react";
|
2026-07-13 13:11:46 +08:00
|
|
|
import {
|
|
|
|
|
Check,
|
|
|
|
|
ChevronDown,
|
|
|
|
|
Clipboard,
|
2026-09-11 15:22:14 +08:00
|
|
|
Download,
|
2026-07-13 13:11:46 +08:00
|
|
|
Loader2,
|
|
|
|
|
Plus,
|
|
|
|
|
} from "lucide-react";
|
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
|
|
2026-07-19 23:30:49 +08:00
|
|
|
import { channelUiContribution } from "@/channel-plugins/registry";
|
2026-09-11 15:22:14 +08:00
|
|
|
import type {
|
|
|
|
|
ChannelFeatureAction,
|
|
|
|
|
ChannelPluginConnectFlowProps,
|
|
|
|
|
} from "@/channel-plugins/types";
|
2026-07-13 13:11:46 +08:00
|
|
|
import { ToggleButton } from "@/components/settings/ToggleButton";
|
2026-09-11 15:22:14 +08:00
|
|
|
import { useAutoSave } from "@/components/settings/shared/useAutoSave";
|
2026-07-13 13:11:46 +08:00
|
|
|
import {
|
2026-09-11 15:22:14 +08:00
|
|
|
type ChannelConfigField,
|
|
|
|
|
type ChannelFieldSection,
|
2026-07-13 13:11:46 +08:00
|
|
|
type ChannelProviderPreset,
|
|
|
|
|
type ChannelSetupPresentation,
|
2026-09-11 15:22:14 +08:00
|
|
|
type ChannelSetupRequirement,
|
2026-07-13 13:11:46 +08:00
|
|
|
} from "@/components/settings/channels/catalog";
|
|
|
|
|
import {
|
|
|
|
|
CredentialForm,
|
|
|
|
|
channelValuesForSubmit,
|
|
|
|
|
defaultChannelFieldValues,
|
|
|
|
|
} from "@/components/settings/channels/CredentialForm";
|
|
|
|
|
import {
|
|
|
|
|
ChannelLogo,
|
2026-07-19 23:30:49 +08:00
|
|
|
ChannelRuntimeError,
|
2026-09-11 15:22:14 +08:00
|
|
|
CHANNEL_SETUP_PANEL_CLASS_NAME,
|
2026-07-13 13:11:46 +08:00
|
|
|
ChannelStatusBadge,
|
|
|
|
|
channelSetup,
|
|
|
|
|
channelStatusLabel,
|
2026-07-19 23:30:49 +08:00
|
|
|
channelToggleChecked,
|
|
|
|
|
localizedChannelDisplayName,
|
2026-07-13 13:11:46 +08:00
|
|
|
} from "@/components/settings/channels/ChannelIdentity";
|
|
|
|
|
import {
|
|
|
|
|
ChannelProviderPresets,
|
|
|
|
|
ChannelSetupActions,
|
2026-09-11 15:22:14 +08:00
|
|
|
ChannelValidationProgress,
|
2026-07-13 13:11:46 +08:00
|
|
|
} from "@/components/settings/channels/ChannelSetupParts";
|
2026-07-19 23:30:49 +08:00
|
|
|
import { ChannelInstancesPanel } from "@/components/settings/channels/ChannelInstancesPanel";
|
2026-07-13 13:11:46 +08:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import {
|
|
|
|
|
configureChannel,
|
2026-09-11 15:22:14 +08:00
|
|
|
disableNanobotFeature,
|
2026-07-13 13:11:46 +08:00
|
|
|
validateChannel,
|
|
|
|
|
} from "@/lib/api";
|
|
|
|
|
import { copyTextToClipboard } from "@/lib/clipboard";
|
|
|
|
|
import type {
|
|
|
|
|
ChannelValidationPayload,
|
|
|
|
|
NanobotFeatureInfo,
|
|
|
|
|
NanobotFeaturesPayload,
|
|
|
|
|
} from "@/lib/types";
|
|
|
|
|
import { cn } from "@/lib/utils";
|
2026-08-10 15:43:54 +08:00
|
|
|
import { useClient } from "@/providers/ClientProvider";
|
2026-07-13 13:11:46 +08:00
|
|
|
|
|
|
|
|
export function ChannelCatalogRow({
|
|
|
|
|
feature,
|
|
|
|
|
showBrandLogos,
|
|
|
|
|
onSelect,
|
2026-09-10 11:10:51 +08:00
|
|
|
actionKey,
|
2026-09-11 15:22:14 +08:00
|
|
|
actionsDisabled = false,
|
2026-09-10 11:10:51 +08:00
|
|
|
onAction,
|
2026-07-13 13:11:46 +08:00
|
|
|
}: {
|
|
|
|
|
feature: NanobotFeatureInfo;
|
|
|
|
|
showBrandLogos: boolean;
|
2026-09-10 11:10:51 +08:00
|
|
|
onSelect: (connect?: boolean) => void;
|
|
|
|
|
actionKey: string | null;
|
2026-09-11 15:22:14 +08:00
|
|
|
actionsDisabled?: boolean;
|
|
|
|
|
onAction: ChannelFeatureAction;
|
2026-07-13 13:11:46 +08:00
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
2026-07-19 23:30:49 +08:00
|
|
|
const displayName = localizedChannelDisplayName(feature, t);
|
2026-09-10 11:10:51 +08:00
|
|
|
const alwaysEnabled = feature.capabilities?.includes("always_enabled") ?? false;
|
2026-09-11 15:22:14 +08:00
|
|
|
const pendingChecked = actionKey === `enable:${feature.name}` ? true
|
|
|
|
|
: actionKey === `disable:${feature.name}` ? false : null;
|
|
|
|
|
const ownActionBusy = pendingChecked !== null || actionKey === `install:${feature.name}`;
|
|
|
|
|
const checked = alwaysEnabled || (pendingChecked ?? channelToggleChecked(feature));
|
|
|
|
|
const anyActionBusy = Boolean(actionKey);
|
|
|
|
|
const installButtonRef = useRef<HTMLButtonElement | null>(null);
|
|
|
|
|
const [installHint, setInstallHint] = useState(false);
|
2026-07-13 13:11:46 +08:00
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!installHint) return;
|
|
|
|
|
const timeout = window.setTimeout(() => setInstallHint(false), 1400);
|
|
|
|
|
return () => window.clearTimeout(timeout);
|
|
|
|
|
}, [installHint]);
|
|
|
|
|
|
|
|
|
|
const pointToInstall = () => {
|
|
|
|
|
setInstallHint(true);
|
|
|
|
|
installButtonRef.current?.focus();
|
|
|
|
|
};
|
|
|
|
|
const channelIdentity = (
|
|
|
|
|
<>
|
2026-07-13 13:11:46 +08:00
|
|
|
<ChannelLogo feature={feature} showBrandLogos={showBrandLogos} />
|
|
|
|
|
<div className="min-w-0 flex-1">
|
|
|
|
|
<h3 className="truncate text-[14px] font-semibold leading-5 text-foreground">
|
2026-07-19 23:30:49 +08:00
|
|
|
{displayName}
|
2026-07-13 13:11:46 +08:00
|
|
|
</h3>
|
|
|
|
|
</div>
|
2026-09-11 15:22:14 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn(
|
|
|
|
|
"settings-list-row flex min-w-0 items-center gap-3 py-2.5 transition-colors",
|
|
|
|
|
!alwaysEnabled && "settings-hover",
|
|
|
|
|
)}>
|
|
|
|
|
{alwaysEnabled ? (
|
|
|
|
|
<div className="flex min-w-0 flex-1 select-none items-center gap-3">
|
|
|
|
|
{channelIdentity}
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
aria-label={t("settings.channels.selectChannel", {
|
|
|
|
|
name: displayName,
|
|
|
|
|
defaultValue: "View {{name}} settings",
|
|
|
|
|
})}
|
|
|
|
|
aria-haspopup="dialog"
|
|
|
|
|
disabled={actionsDisabled}
|
|
|
|
|
onClick={() => feature.installed ? onSelect() : pointToInstall()}
|
|
|
|
|
className="group flex min-w-0 flex-1 select-none items-center gap-3 rounded-control text-left transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-border/80"
|
|
|
|
|
>
|
|
|
|
|
{channelIdentity}
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
{feature.runtime_status === "failed" ? (
|
|
|
|
|
<div className="min-w-0 shrink truncate">
|
2026-07-19 23:30:49 +08:00
|
|
|
<ChannelStatusBadge status={feature.runtime_status}>
|
|
|
|
|
{channelStatusLabel(feature, tx)}
|
|
|
|
|
</ChannelStatusBadge>
|
2026-09-11 15:22:14 +08:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
<div className="flex w-16 shrink-0 items-center justify-center">
|
|
|
|
|
{!feature.installed ? (
|
|
|
|
|
<Button ref={installButtonRef} type="button" variant="outline" size="icon"
|
|
|
|
|
className={cn(
|
|
|
|
|
"h-[22px] w-[38px] min-w-0 shrink-0 rounded-full border-border/70 bg-background p-0 shadow-sm settings-hover active:scale-[0.96]",
|
|
|
|
|
installHint && "border-[#2997FF]/60 bg-[#2997FF]/10 text-[#087FE7] ring-2 ring-[#2997FF]/25 ring-offset-2",
|
|
|
|
|
)}
|
|
|
|
|
disabled={actionsDisabled || anyActionBusy || !feature.install_supported}
|
|
|
|
|
aria-label={t("settings.channels.installChannel", { name: displayName })}
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setInstallHint(false);
|
|
|
|
|
onAction("enable", feature.name, { installOnly: true, confirmed: true });
|
|
|
|
|
}}>
|
|
|
|
|
{ownActionBusy
|
|
|
|
|
? <Loader2 className="h-3.5 w-3.5 animate-spin" aria-hidden />
|
|
|
|
|
: <Download className="h-3.5 w-3.5" aria-hidden />}
|
|
|
|
|
<span className="sr-only">{tx("settings.channels.install", "Install")}</span>
|
|
|
|
|
</Button>
|
|
|
|
|
) : (
|
|
|
|
|
<ToggleButton checked={checked}
|
|
|
|
|
label={checked ? tx("settings.values.on", "On") : tx("settings.values.off", "Off")}
|
|
|
|
|
disabled={alwaysEnabled || actionsDisabled || anyActionBusy}
|
|
|
|
|
ariaLabel={t("settings.channels.toggleChannel", { name: displayName, defaultValue: "{{name}} channel" })}
|
|
|
|
|
onChange={(enabled) => {
|
|
|
|
|
if (enabled && feature.configured === false) onSelect(true);
|
|
|
|
|
else onAction(enabled ? "enable" : "disable", feature.name);
|
|
|
|
|
}} />
|
|
|
|
|
)}
|
2026-07-13 13:11:46 +08:00
|
|
|
</div>
|
2026-09-10 11:10:51 +08:00
|
|
|
</div>
|
2026-07-13 13:11:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ChannelSetupPanel({
|
|
|
|
|
token,
|
|
|
|
|
feature,
|
|
|
|
|
actionKey,
|
|
|
|
|
showBrandLogos,
|
|
|
|
|
onAction,
|
|
|
|
|
onFeaturesUpdate,
|
2026-09-10 11:10:51 +08:00
|
|
|
connectRequestId = 0,
|
2026-09-11 15:22:14 +08:00
|
|
|
onBeforeCloseChange,
|
2026-07-13 13:11:46 +08:00
|
|
|
}: {
|
|
|
|
|
token: string;
|
|
|
|
|
feature: NanobotFeatureInfo;
|
|
|
|
|
actionKey: string | null;
|
|
|
|
|
showBrandLogos: boolean;
|
2026-09-11 15:22:14 +08:00
|
|
|
onAction: ChannelFeatureAction;
|
2026-07-13 13:11:46 +08:00
|
|
|
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
|
2026-09-10 11:10:51 +08:00
|
|
|
connectRequestId?: number;
|
2026-09-11 15:22:14 +08:00
|
|
|
onBeforeCloseChange?: (handler: (() => Promise<boolean>) | null) => void;
|
2026-07-13 13:11:46 +08:00
|
|
|
}) {
|
2026-07-19 23:30:49 +08:00
|
|
|
const { t, i18n } = useTranslation();
|
2026-07-13 13:11:46 +08:00
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
2026-07-19 23:30:49 +08:00
|
|
|
const displayName = localizedChannelDisplayName(feature, t);
|
|
|
|
|
const uiContribution = channelUiContribution(feature.name, feature.webui);
|
|
|
|
|
const PluginPanel = uiContribution?.Panel;
|
2026-09-11 15:22:14 +08:00
|
|
|
const setup = channelSetup(feature, i18n.resolvedLanguage ?? i18n.language);
|
|
|
|
|
const missingSupport = !feature.installed;
|
|
|
|
|
if (PluginPanel && !missingSupport) {
|
2026-07-13 13:11:46 +08:00
|
|
|
return (
|
2026-08-12 16:22:34 +08:00
|
|
|
<Suspense fallback={<ChannelPluginLoading />}>
|
|
|
|
|
<PluginPanel
|
2026-09-10 11:10:51 +08:00
|
|
|
connectRequestId={connectRequestId}
|
2026-08-12 16:22:34 +08:00
|
|
|
token={token}
|
|
|
|
|
feature={feature}
|
|
|
|
|
actionKey={actionKey}
|
|
|
|
|
showBrandLogos={showBrandLogos}
|
|
|
|
|
onAction={onAction}
|
|
|
|
|
onFeaturesUpdate={onFeaturesUpdate}
|
|
|
|
|
/>
|
|
|
|
|
</Suspense>
|
2026-07-19 23:30:49 +08:00
|
|
|
);
|
|
|
|
|
}
|
2026-09-11 15:22:14 +08:00
|
|
|
if (feature.instances !== undefined && !missingSupport) {
|
2026-07-19 23:30:49 +08:00
|
|
|
return (
|
|
|
|
|
<ChannelInstancesPanel
|
2026-07-13 13:11:46 +08:00
|
|
|
feature={feature}
|
|
|
|
|
showBrandLogos={showBrandLogos}
|
|
|
|
|
onFeaturesUpdate={onFeaturesUpdate}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-09-11 15:22:14 +08:00
|
|
|
const enableBusy = actionKey === `enable:${feature.name}` || actionKey === `install:${feature.name}`;
|
|
|
|
|
const anyActionBusy = Boolean(actionKey);
|
|
|
|
|
const installSupportLabel = tx("settings.channels.install", "Install");
|
|
|
|
|
|
|
|
|
|
const header = (
|
|
|
|
|
<div className="flex min-w-0 items-center gap-3">
|
|
|
|
|
<ChannelLogo feature={feature} showBrandLogos={showBrandLogos} />
|
|
|
|
|
<h3 className="sr-only">{displayName}</h3>
|
|
|
|
|
{missingSupport && feature.install_supported ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
disabled={anyActionBusy}
|
|
|
|
|
onClick={() => onAction("enable", feature.name, { installOnly: true, confirmed: true })}
|
|
|
|
|
className="h-8 rounded-full px-3 text-[12px] font-semibold"
|
|
|
|
|
>
|
|
|
|
|
{enableBusy ? (
|
|
|
|
|
<Loader2 className="mr-1.5 h-3.5 w-3.5 animate-spin" aria-hidden />
|
|
|
|
|
) : (
|
|
|
|
|
<Plus className="mr-1.5 h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
)}
|
|
|
|
|
{installSupportLabel}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-07-13 13:11:46 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-09-11 15:22:14 +08:00
|
|
|
<aside className={CHANNEL_SETUP_PANEL_CLASS_NAME}>
|
|
|
|
|
{missingSupport ? <>
|
|
|
|
|
<div className="pe-20">{header}</div>
|
|
|
|
|
<ChannelRuntimeError message={feature.runtime_error} className="mt-4" />
|
|
|
|
|
</> : <ChannelSetupSurface
|
|
|
|
|
key={feature.name}
|
|
|
|
|
header={header}
|
2026-07-13 13:11:46 +08:00
|
|
|
token={token}
|
|
|
|
|
feature={feature}
|
|
|
|
|
setup={setup}
|
|
|
|
|
connectRequestId={connectRequestId}
|
2026-09-11 15:22:14 +08:00
|
|
|
ConnectFlow={feature.installed ? uiContribution?.ConnectFlow : undefined}
|
2026-07-13 13:11:46 +08:00
|
|
|
onFeaturesUpdate={onFeaturesUpdate}
|
2026-09-11 15:22:14 +08:00
|
|
|
onBeforeCloseChange={onBeforeCloseChange}
|
|
|
|
|
/>}
|
2026-07-13 13:11:46 +08:00
|
|
|
</aside>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ChannelSetupSurface({
|
2026-09-11 15:22:14 +08:00
|
|
|
header,
|
2026-07-13 13:11:46 +08:00
|
|
|
token,
|
|
|
|
|
feature,
|
|
|
|
|
setup,
|
|
|
|
|
connectRequestId,
|
2026-07-19 23:30:49 +08:00
|
|
|
ConnectFlow,
|
2026-07-13 13:11:46 +08:00
|
|
|
onFeaturesUpdate,
|
2026-09-11 15:22:14 +08:00
|
|
|
onBeforeCloseChange,
|
2026-07-13 13:11:46 +08:00
|
|
|
}: {
|
2026-09-11 15:22:14 +08:00
|
|
|
header: ReactNode;
|
2026-07-13 13:11:46 +08:00
|
|
|
token: string;
|
|
|
|
|
feature: NanobotFeatureInfo;
|
|
|
|
|
setup: ChannelSetupPresentation;
|
|
|
|
|
connectRequestId: number;
|
2026-07-19 23:30:49 +08:00
|
|
|
ConnectFlow?: ComponentType<ChannelPluginConnectFlowProps>;
|
2026-07-13 13:11:46 +08:00
|
|
|
onFeaturesUpdate: (payload: NanobotFeaturesPayload) => void;
|
2026-09-11 15:22:14 +08:00
|
|
|
onBeforeCloseChange?: (handler: (() => Promise<boolean>) | null) => void;
|
2026-07-13 13:11:46 +08:00
|
|
|
}) {
|
2026-08-10 15:43:54 +08:00
|
|
|
const { client } = useClient();
|
2026-07-13 13:11:46 +08:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
|
|
|
|
const [notice, setNotice] = useState<string | null>(null);
|
|
|
|
|
const [saving, setSaving] = useState(false);
|
2026-09-11 15:22:14 +08:00
|
|
|
const [autoSaving, setAutoSaving] = useState(false);
|
|
|
|
|
const [autoSaveState, setAutoSaveState] = useState<"idle" | "saved">("idle");
|
|
|
|
|
const autoSavePromiseRef = useRef<Promise<boolean> | null>(null);
|
2026-07-13 13:11:46 +08:00
|
|
|
const [validating, setValidating] = useState(false);
|
2026-09-11 15:22:14 +08:00
|
|
|
const [pendingEnabled, setPendingEnabled] = useState<boolean | null>(null);
|
|
|
|
|
const actionPending = saving || autoSaving || validating || pendingEnabled !== null;
|
2026-07-13 13:11:46 +08:00
|
|
|
const [validation, setValidation] = useState<ChannelValidationPayload | null>(null);
|
|
|
|
|
const [visibleSecrets, setVisibleSecrets] = useState<Record<string, boolean>>({});
|
|
|
|
|
const [touchedFields, setTouchedFields] = useState<Set<string>>(() => new Set());
|
2026-09-11 15:22:14 +08:00
|
|
|
const [clearedSecrets, setClearedSecrets] = useState<Set<string>>(() => new Set());
|
|
|
|
|
const [fieldErrors, setFieldErrors] = useState<Record<string, string>>({});
|
|
|
|
|
const [advancedOpen, setAdvancedOpen] = useState(false);
|
|
|
|
|
const advancedPanelId = useId();
|
2026-07-13 13:11:46 +08:00
|
|
|
const configValuesKey = JSON.stringify(feature.config_values ?? {});
|
|
|
|
|
const configuredFields = useMemo(
|
|
|
|
|
() => new Set(feature.configured_fields ?? []),
|
|
|
|
|
[feature.configured_fields],
|
|
|
|
|
);
|
|
|
|
|
const mode = setup.mode ?? "credentials";
|
|
|
|
|
const fields = setup.fields ?? [];
|
2026-09-11 15:22:14 +08:00
|
|
|
const requirementKeys = new Set(
|
|
|
|
|
(setup.requirements ?? []).flatMap((requirement) => requirement.alternatives.flat()),
|
2026-07-19 23:30:49 +08:00
|
|
|
);
|
2026-09-11 15:22:14 +08:00
|
|
|
const primaryFields = fields.filter(
|
|
|
|
|
(field) => !field.optional || requirementKeys.has(field.key),
|
|
|
|
|
);
|
|
|
|
|
const manualFields = setup.manualFields ?? [];
|
|
|
|
|
const advancedFields = mode === "connect"
|
|
|
|
|
? manualFields
|
|
|
|
|
: fields.filter((field) => !primaryFields.includes(field));
|
|
|
|
|
const editableFields = mode === "credentials" ? fields : mode === "connect" ? manualFields : [];
|
|
|
|
|
const savedSecretFields = editableFields.filter(
|
|
|
|
|
(field) => field.secret && configuredFields.has(field.key),
|
|
|
|
|
);
|
|
|
|
|
const availablePresets = feature.configured ? [] : setup.presets ?? [];
|
|
|
|
|
const hasAdvanced = advancedFields.length > 0
|
|
|
|
|
|| savedSecretFields.length > 0
|
|
|
|
|
|| availablePresets.length > 0;
|
2026-07-13 13:11:46 +08:00
|
|
|
const [fieldValues, setFieldValues] = useState<Record<string, string>>(() =>
|
|
|
|
|
defaultChannelFieldValues(editableFields, feature.config_values),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setNotice(null);
|
|
|
|
|
setVisibleSecrets({});
|
|
|
|
|
setSaving(false);
|
|
|
|
|
setValidating(false);
|
|
|
|
|
setValidation(null);
|
|
|
|
|
setTouchedFields(new Set());
|
2026-09-11 15:22:14 +08:00
|
|
|
setClearedSecrets(new Set());
|
|
|
|
|
setFieldErrors({});
|
2026-07-13 13:11:46 +08:00
|
|
|
setFieldValues(defaultChannelFieldValues(editableFields, feature.config_values));
|
|
|
|
|
}, [configValuesKey, feature.name]);
|
|
|
|
|
|
|
|
|
|
const toggleSecret = (key: string) => {
|
|
|
|
|
setVisibleSecrets((current) => ({ ...current, [key]: !current[key] }));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setFieldValue = (key: string, value: string) => {
|
|
|
|
|
setFieldValues((current) => ({ ...current, [key]: value }));
|
|
|
|
|
setTouchedFields((current) => new Set(current).add(key));
|
2026-09-11 15:22:14 +08:00
|
|
|
setClearedSecrets((current) => {
|
|
|
|
|
if (!current.has(key)) return current;
|
|
|
|
|
const next = new Set(current);
|
|
|
|
|
next.delete(key);
|
|
|
|
|
return next;
|
|
|
|
|
});
|
|
|
|
|
setFieldErrors((current) => {
|
|
|
|
|
if (!current[key]) return current;
|
|
|
|
|
const next = { ...current };
|
|
|
|
|
delete next[key];
|
|
|
|
|
return next;
|
|
|
|
|
});
|
|
|
|
|
setAutoSaveState("idle");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const saveConnectionSettings = async () => {
|
|
|
|
|
if (mode !== "connect" || saving || !touchedFields.size) return;
|
|
|
|
|
setSaving(true);
|
|
|
|
|
try {
|
|
|
|
|
const payload = await configureChannel(client, feature.name,
|
|
|
|
|
channelValuesForSubmit(editableFields, fieldValues, touchedFields, clearedSecrets));
|
|
|
|
|
setTouchedFields(new Set());
|
|
|
|
|
if (payload.nanobot_features) onFeaturesUpdate(payload.nanobot_features);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setNotice((err as Error).message);
|
|
|
|
|
} finally {
|
|
|
|
|
setSaving(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setSecretCleared = (key: string, clear: boolean) => {
|
|
|
|
|
setClearedSecrets((current) => {
|
|
|
|
|
const next = new Set(current);
|
|
|
|
|
if (clear) next.add(key);
|
|
|
|
|
else next.delete(key);
|
|
|
|
|
return next;
|
|
|
|
|
});
|
|
|
|
|
setAutoSaveState("idle");
|
2026-07-13 13:11:46 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const applyPreset = (preset: ChannelProviderPreset) => {
|
2026-09-11 15:22:14 +08:00
|
|
|
const values = Object.fromEntries(
|
|
|
|
|
Object.entries(preset.values).filter(([key]) => !(fieldValues[key] ?? "").trim()),
|
|
|
|
|
);
|
|
|
|
|
if (!Object.keys(values).length) return;
|
|
|
|
|
setFieldValues((current) => ({ ...current, ...values }));
|
2026-07-13 13:11:46 +08:00
|
|
|
setTouchedFields((current) => {
|
|
|
|
|
const next = new Set(current);
|
2026-09-11 15:22:14 +08:00
|
|
|
for (const key of Object.keys(values)) next.add(key);
|
2026-07-13 13:11:46 +08:00
|
|
|
return next;
|
|
|
|
|
});
|
2026-09-11 15:22:14 +08:00
|
|
|
setAutoSaveState("idle");
|
2026-07-13 13:11:46 +08:00
|
|
|
};
|
|
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
const credentialDirty = touchedFields.size > 0 || clearedSecrets.size > 0;
|
|
|
|
|
const touchedSecret = fields.some(
|
|
|
|
|
(field) => field.secret && touchedFields.has(field.key),
|
|
|
|
|
);
|
|
|
|
|
const saveCredentialDraft = useCallback(async (): Promise<boolean> => {
|
|
|
|
|
const activeSave = autoSavePromiseRef.current;
|
|
|
|
|
if (activeSave) return activeSave;
|
|
|
|
|
if (mode !== "credentials" || !credentialDirty) return true;
|
|
|
|
|
if (saving || validating || pendingEnabled !== null) return false;
|
|
|
|
|
|
|
|
|
|
const values = channelValuesForSubmit(fields, fieldValues, touchedFields, clearedSecrets);
|
|
|
|
|
const save = (async () => {
|
|
|
|
|
setAutoSaving(true);
|
|
|
|
|
setAutoSaveState("idle");
|
|
|
|
|
setNotice(null);
|
|
|
|
|
try {
|
|
|
|
|
const payload = await configureChannel(client, feature.name, values);
|
|
|
|
|
setTouchedFields(new Set());
|
|
|
|
|
setClearedSecrets(new Set());
|
|
|
|
|
setAutoSaveState("saved");
|
|
|
|
|
if (payload.nanobot_features) onFeaturesUpdate(payload.nanobot_features);
|
|
|
|
|
return true;
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setNotice((err as Error).message);
|
|
|
|
|
return false;
|
|
|
|
|
} finally {
|
|
|
|
|
setAutoSaving(false);
|
|
|
|
|
}
|
|
|
|
|
})();
|
|
|
|
|
autoSavePromiseRef.current = save;
|
|
|
|
|
const saved = await save;
|
|
|
|
|
if (autoSavePromiseRef.current === save) autoSavePromiseRef.current = null;
|
|
|
|
|
return saved;
|
|
|
|
|
}, [
|
|
|
|
|
clearedSecrets,
|
|
|
|
|
client,
|
|
|
|
|
credentialDirty,
|
|
|
|
|
feature.name,
|
|
|
|
|
fieldValues,
|
|
|
|
|
fields,
|
|
|
|
|
mode,
|
|
|
|
|
onFeaturesUpdate,
|
|
|
|
|
pendingEnabled,
|
|
|
|
|
saving,
|
|
|
|
|
touchedFields,
|
|
|
|
|
validating,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
useAutoSave(
|
|
|
|
|
{ fieldValues, touchedFields: [...touchedFields], clearedSecrets: [...clearedSecrets] },
|
|
|
|
|
mode === "credentials" && credentialDirty,
|
|
|
|
|
actionPending,
|
|
|
|
|
() => void saveCredentialDraft(),
|
|
|
|
|
!touchedSecret,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (autoSaveState !== "saved") return;
|
|
|
|
|
const timeout = window.setTimeout(() => setAutoSaveState("idle"), 1500);
|
|
|
|
|
return () => window.clearTimeout(timeout);
|
|
|
|
|
}, [autoSaveState]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!onBeforeCloseChange) return;
|
|
|
|
|
onBeforeCloseChange(credentialDirty ? saveCredentialDraft : null);
|
|
|
|
|
return () => onBeforeCloseChange(null);
|
|
|
|
|
}, [credentialDirty, onBeforeCloseChange, saveCredentialDraft]);
|
|
|
|
|
|
2026-07-13 13:11:46 +08:00
|
|
|
const copyCommand = () => {
|
|
|
|
|
if (!setup.command) return;
|
|
|
|
|
void copyTextToClipboard(setup.command).then((ok) => {
|
|
|
|
|
setNotice(
|
|
|
|
|
ok
|
|
|
|
|
? tx("settings.channels.commandCopied", "Command copied.")
|
|
|
|
|
: tx("settings.channels.commandCopyFailed", "Could not copy command."),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const saveCredentialSettings = async () => {
|
2026-09-11 15:22:14 +08:00
|
|
|
if (actionPending) return;
|
|
|
|
|
const errors = channelRequirementErrors(
|
|
|
|
|
fields,
|
|
|
|
|
setup.requirements ?? [],
|
|
|
|
|
fieldValues,
|
|
|
|
|
configuredFields,
|
|
|
|
|
clearedSecrets,
|
|
|
|
|
tx("settings.channels.fieldRequired", "Required to complete setup."),
|
|
|
|
|
);
|
|
|
|
|
if (Object.keys(errors).length) {
|
|
|
|
|
setFieldErrors(errors);
|
|
|
|
|
setNotice(tx("settings.channels.validationFailed", "Check the required setup before enabling."));
|
|
|
|
|
focusFirstChannelFieldError(errors);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setPendingEnabled(true);
|
2026-07-13 13:11:46 +08:00
|
|
|
setSaving(true);
|
|
|
|
|
setValidating(true);
|
|
|
|
|
setNotice(null);
|
2026-09-11 15:22:14 +08:00
|
|
|
const values = channelValuesForSubmit(fields, fieldValues, touchedFields, clearedSecrets);
|
2026-07-13 13:11:46 +08:00
|
|
|
try {
|
2026-08-10 15:43:54 +08:00
|
|
|
const validationPayload = await validateChannel(client, feature.name, values);
|
2026-07-13 13:11:46 +08:00
|
|
|
setValidation(validationPayload);
|
|
|
|
|
if (!validationPayload.can_enable) {
|
2026-09-11 15:22:14 +08:00
|
|
|
const errors = channelServerValidationErrors(
|
|
|
|
|
fields,
|
|
|
|
|
validationPayload.missing_fields,
|
|
|
|
|
tx("settings.channels.fieldRequired", "Required to complete setup."),
|
|
|
|
|
);
|
|
|
|
|
setFieldErrors(errors);
|
|
|
|
|
if (advancedFields.some((field) => errors[field.key])) setAdvancedOpen(true);
|
|
|
|
|
focusFirstChannelFieldError(errors);
|
2026-07-13 13:11:46 +08:00
|
|
|
setNotice(
|
2026-09-11 15:22:14 +08:00
|
|
|
(validationPayload.message ? channelValidationMessage(validationPayload.message, t) : undefined)
|
2026-07-13 13:11:46 +08:00
|
|
|
?? tx("settings.channels.validationFailed", "Check the required setup before enabling."),
|
|
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const payload = await configureChannel(
|
2026-08-10 15:43:54 +08:00
|
|
|
client,
|
2026-07-13 13:11:46 +08:00
|
|
|
feature.name,
|
|
|
|
|
values,
|
|
|
|
|
{ enable: true },
|
|
|
|
|
);
|
|
|
|
|
if (payload.nanobot_features) {
|
|
|
|
|
onFeaturesUpdate(payload.nanobot_features);
|
|
|
|
|
}
|
|
|
|
|
setNotice(tx("settings.channels.checkedAndEnabled", "Checked and enabled."));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setNotice((err as Error).message);
|
|
|
|
|
} finally {
|
|
|
|
|
setSaving(false);
|
|
|
|
|
setValidating(false);
|
2026-09-11 15:22:14 +08:00
|
|
|
setPendingEnabled(null);
|
2026-07-13 13:11:46 +08:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const checkCurrentSettings = async () => {
|
2026-09-11 15:22:14 +08:00
|
|
|
if (actionPending) return;
|
2026-07-13 13:11:46 +08:00
|
|
|
setValidating(true);
|
2026-09-11 15:22:14 +08:00
|
|
|
setValidation(null);
|
2026-07-13 13:11:46 +08:00
|
|
|
setNotice(null);
|
|
|
|
|
try {
|
|
|
|
|
const payload = await validateChannel(
|
2026-08-10 15:43:54 +08:00
|
|
|
client,
|
2026-07-13 13:11:46 +08:00
|
|
|
feature.name,
|
2026-09-11 15:22:14 +08:00
|
|
|
channelValuesForSubmit(fields, fieldValues, touchedFields, clearedSecrets),
|
2026-07-13 13:11:46 +08:00
|
|
|
);
|
|
|
|
|
setValidation(payload);
|
2026-09-11 15:22:14 +08:00
|
|
|
const failedCheck = payload.checks.find((check) => check.status === "fail");
|
|
|
|
|
const resultMessage = failedCheck?.message ?? payload.message;
|
|
|
|
|
const localizedResult = resultMessage ? channelValidationMessage(resultMessage, t) : null;
|
|
|
|
|
const localizedRuntimeError = feature.runtime_error
|
|
|
|
|
? channelValidationMessage(feature.runtime_error, t)
|
|
|
|
|
: null;
|
|
|
|
|
setNotice(
|
|
|
|
|
payload.status !== "connected"
|
|
|
|
|
&& localizedResult
|
|
|
|
|
&& localizedResult !== localizedRuntimeError
|
|
|
|
|
? localizedResult
|
|
|
|
|
: null,
|
|
|
|
|
);
|
2026-07-13 13:11:46 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
setNotice((err as Error).message);
|
|
|
|
|
} finally {
|
|
|
|
|
setValidating(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
const enabled = pendingEnabled ?? channelToggleChecked(feature);
|
|
|
|
|
const toggleEnabled = async (next: boolean) => {
|
|
|
|
|
if (actionPending) return;
|
|
|
|
|
if (next) {
|
|
|
|
|
await saveCredentialSettings();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setPendingEnabled(false);
|
|
|
|
|
setSaving(true);
|
|
|
|
|
setNotice(null);
|
|
|
|
|
try {
|
|
|
|
|
onFeaturesUpdate(await disableNanobotFeature(client, feature.name));
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setNotice((err as Error).message);
|
|
|
|
|
} finally {
|
|
|
|
|
setSaving(false);
|
|
|
|
|
setPendingEnabled(null);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const inlineActions = mode === "credentials" && primaryFields.length === 1
|
|
|
|
|
&& !advancedOpen && !setup.presets?.length && !setup.actions?.length;
|
|
|
|
|
const credentialActions = mode === "credentials" ? (
|
|
|
|
|
<div className="ms-auto flex min-h-10 flex-wrap items-center justify-end gap-x-3 gap-y-2 sm:min-h-9">
|
|
|
|
|
{feature.setup?.verifies_connection ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
className="h-10 rounded-full px-3 text-[12px] font-semibold sm:h-9"
|
|
|
|
|
onClick={() => void checkCurrentSettings()}
|
|
|
|
|
disabled={actionPending}
|
|
|
|
|
>
|
|
|
|
|
{tx("settings.channels.checkConnection", "Check connection")}
|
|
|
|
|
</Button>
|
|
|
|
|
) : null}
|
|
|
|
|
<ToggleButton checked={enabled} disabled={actionPending}
|
|
|
|
|
label={tx("settings.channels.enable", "Enable")}
|
|
|
|
|
onChange={(next) => void toggleEnabled(next)} />
|
|
|
|
|
</div>
|
|
|
|
|
) : null;
|
2026-07-13 13:11:46 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-07-19 23:30:49 +08:00
|
|
|
<form
|
2026-09-11 15:22:14 +08:00
|
|
|
className="flex flex-col gap-4"
|
2026-07-19 23:30:49 +08:00
|
|
|
onSubmit={(event) => {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
if (mode === "credentials") void saveCredentialSettings();
|
|
|
|
|
}}
|
|
|
|
|
>
|
2026-09-11 15:22:14 +08:00
|
|
|
<div className="flex flex-wrap items-center justify-between gap-x-4 gap-y-2 pe-20">
|
|
|
|
|
{header}
|
|
|
|
|
<div className="ms-auto flex min-h-8 items-center gap-2">
|
|
|
|
|
<span
|
|
|
|
|
role="status"
|
|
|
|
|
aria-live="polite"
|
|
|
|
|
aria-atomic="true"
|
|
|
|
|
className={cn(
|
|
|
|
|
"inline-flex items-center gap-1.5 text-[11px] leading-4 text-muted-foreground",
|
|
|
|
|
!autoSaving && autoSaveState !== "saved" && "sr-only",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{autoSaving ? (
|
|
|
|
|
<>
|
|
|
|
|
<Loader2 className="h-3 w-3 animate-spin" aria-hidden />
|
|
|
|
|
{tx("settings.actions.saving", "Saving")}
|
|
|
|
|
</>
|
|
|
|
|
) : autoSaveState === "saved" ? (
|
|
|
|
|
<>
|
|
|
|
|
<Check className="h-3 w-3" aria-hidden />
|
|
|
|
|
{tx("settings.channels.savedSettings", "Settings saved.")}
|
|
|
|
|
</>
|
2026-07-13 13:11:46 +08:00
|
|
|
) : null}
|
2026-09-11 15:22:14 +08:00
|
|
|
</span>
|
|
|
|
|
{hasAdvanced ? (
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
|
|
|
|
className="inline-flex min-h-8 items-center gap-1.5 rounded px-1 text-[12px] text-muted-foreground transition-colors hover:text-foreground focus-visible:outline-offset-2"
|
|
|
|
|
aria-expanded={advancedOpen}
|
|
|
|
|
aria-controls={advancedPanelId}
|
|
|
|
|
onClick={() => setAdvancedOpen((current) => !current)}
|
|
|
|
|
>
|
|
|
|
|
{tx("settings.channels.advanced", "Advanced")}
|
|
|
|
|
<ChevronDown className={cn(
|
|
|
|
|
"h-3.5 w-3.5 transition-transform motion-reduce:transition-none",
|
|
|
|
|
advancedOpen && "rotate-180",
|
|
|
|
|
)} aria-hidden />
|
|
|
|
|
</button>
|
|
|
|
|
) : null}
|
2026-07-13 13:11:46 +08:00
|
|
|
</div>
|
2026-09-11 15:22:14 +08:00
|
|
|
</div>
|
|
|
|
|
<ChannelRuntimeError message={feature.runtime_error} />
|
|
|
|
|
<div className="flex flex-wrap items-center gap-4">
|
|
|
|
|
<section className={cn("min-w-0", inlineActions ? "flex-[1_1_20rem]" : "w-full")}>
|
|
|
|
|
<div className="flex flex-wrap items-center justify-between gap-2">
|
|
|
|
|
<div className="flex max-w-full flex-wrap justify-end gap-2">
|
|
|
|
|
{mode === "webui" ? (
|
|
|
|
|
<span className="inline-flex items-center gap-1 rounded-full bg-emerald-500/10 px-2.5 py-1 text-[11.5px] font-medium text-emerald-700 dark:text-emerald-200">
|
|
|
|
|
<Check className="h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
{tx("settings.channels.managedByWebui", "Managed by WebUI")}
|
|
|
|
|
</span>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<ChannelSetupActions feature={feature} setup={setup} onNotice={setNotice} />
|
2026-07-13 13:11:46 +08:00
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
{mode === "connect" && !feature.installed ? null : mode === "connect" && ConnectFlow ? (
|
|
|
|
|
<Suspense fallback={<ChannelPluginLoading compact />}>
|
|
|
|
|
<ConnectFlow
|
|
|
|
|
token={token}
|
|
|
|
|
feature={feature}
|
|
|
|
|
idleLabel={setup.primaryActionLabel ?? tx("settings.channels.connect", "Connect")}
|
|
|
|
|
connectRequestId={connectRequestId}
|
|
|
|
|
onFeaturesUpdate={onFeaturesUpdate}
|
|
|
|
|
/>
|
|
|
|
|
</Suspense>
|
|
|
|
|
) : mode === "connect" ? (
|
|
|
|
|
<>
|
|
|
|
|
<div className="mt-3 flex flex-wrap justify-end gap-2">
|
2026-07-13 13:11:46 +08:00
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="sm"
|
2026-08-05 13:24:45 +08:00
|
|
|
variant="secondary"
|
2026-09-11 15:22:14 +08:00
|
|
|
className="h-8 rounded-full bg-background/80 px-3 text-[12px] font-semibold settings-hover"
|
|
|
|
|
onClick={() =>
|
|
|
|
|
setNotice(
|
|
|
|
|
tx(
|
|
|
|
|
"settings.channels.connectPreview",
|
|
|
|
|
"Run the command below in your terminal to connect.",
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
}
|
2026-07-13 13:11:46 +08:00
|
|
|
>
|
2026-09-11 15:22:14 +08:00
|
|
|
{setup.primaryActionLabel ?? tx("settings.channels.connect", "Connect")}
|
2026-07-13 13:11:46 +08:00
|
|
|
</Button>
|
2026-09-11 15:22:14 +08:00
|
|
|
{setup.command ? (
|
|
|
|
|
<Button
|
|
|
|
|
type="button"
|
|
|
|
|
size="sm"
|
|
|
|
|
variant="secondary"
|
|
|
|
|
className="h-8 rounded-full px-3 text-[12px] font-semibold"
|
|
|
|
|
onClick={copyCommand}
|
|
|
|
|
>
|
|
|
|
|
<Clipboard className="mr-1.5 h-3.5 w-3.5" aria-hidden />
|
|
|
|
|
{tx("settings.channels.copyCommand", "Copy command")}
|
|
|
|
|
</Button>
|
2026-07-13 13:11:46 +08:00
|
|
|
) : null}
|
2026-09-11 15:22:14 +08:00
|
|
|
</div>
|
|
|
|
|
{setup.command ? (
|
|
|
|
|
<code className="mt-3 block rounded-control border border-border/50 bg-muted/45 px-2.5 py-2 font-mono text-[11px] leading-5 text-foreground">
|
|
|
|
|
{setup.command}
|
|
|
|
|
</code>
|
2026-07-13 13:11:46 +08:00
|
|
|
) : null}
|
2026-09-11 15:22:14 +08:00
|
|
|
</>
|
|
|
|
|
) : mode === "credentials" ? (
|
|
|
|
|
<>
|
|
|
|
|
{primaryFields.length ? (
|
|
|
|
|
<ChannelFieldGroups
|
|
|
|
|
fields={primaryFields}
|
|
|
|
|
values={fieldValues}
|
|
|
|
|
configuredFields={configuredFields}
|
|
|
|
|
visibleSecrets={visibleSecrets}
|
|
|
|
|
onChange={setFieldValue}
|
|
|
|
|
onFieldBlur={(key) => {
|
|
|
|
|
if (fields.some((field) => field.key === key && field.secret)) {
|
|
|
|
|
void saveCredentialDraft();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onToggleSecret={toggleSecret}
|
|
|
|
|
errors={fieldErrors}
|
|
|
|
|
clearedSecrets={clearedSecrets}
|
|
|
|
|
onClearSecret={setSecretCleared}
|
|
|
|
|
requirements={setup.requirements ?? []}
|
|
|
|
|
sectionLabels={setup.sectionLabels}
|
|
|
|
|
disabled={actionPending}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
</>
|
|
|
|
|
) : null}
|
|
|
|
|
</section>
|
2026-07-13 13:11:46 +08:00
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
{hasAdvanced ? (
|
|
|
|
|
<div id={advancedPanelId} hidden={!advancedOpen} className="min-w-0 w-full space-y-3 text-[12px] leading-5 text-muted-foreground">
|
|
|
|
|
{availablePresets.length ? (
|
|
|
|
|
<ChannelProviderPresets
|
|
|
|
|
presets={availablePresets}
|
|
|
|
|
onApply={applyPreset}
|
|
|
|
|
label={setup.presetLabel}
|
|
|
|
|
disabled={actionPending}
|
|
|
|
|
/>
|
|
|
|
|
) : null}
|
|
|
|
|
<div className="space-y-2">
|
|
|
|
|
{savedSecretFields.filter((field) => !(fieldValues[field.key] ?? "").trim()).map((field) => (
|
|
|
|
|
<div key={field.key} className="flex min-h-9 flex-wrap items-center justify-between gap-x-4 gap-y-1">
|
|
|
|
|
<span>{field.label}</span>
|
|
|
|
|
<button type="button" className="min-h-8 text-[12px] hover:text-foreground"
|
|
|
|
|
onClick={() => setSecretCleared(field.key, !clearedSecrets.has(field.key))}>
|
|
|
|
|
{clearedSecrets.has(field.key)
|
|
|
|
|
? tx("settings.channels.keepSavedSecret", "Keep saved credential")
|
|
|
|
|
: tx("settings.channels.removeSavedSecret", "Remove saved credential")}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
{advancedFields.length ? (
|
|
|
|
|
<div onBlur={() => void saveConnectionSettings()}>
|
|
|
|
|
<CredentialForm
|
|
|
|
|
fields={advancedFields}
|
|
|
|
|
values={fieldValues}
|
|
|
|
|
configuredFields={configuredFields}
|
|
|
|
|
visibleSecrets={visibleSecrets}
|
|
|
|
|
onChange={setFieldValue}
|
|
|
|
|
onFieldBlur={(key) => {
|
|
|
|
|
if (fields.some((field) => field.key === key && field.secret)) {
|
|
|
|
|
void saveCredentialDraft();
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
onToggleSecret={toggleSecret}
|
|
|
|
|
errors={fieldErrors}
|
|
|
|
|
clearedSecrets={clearedSecrets}
|
|
|
|
|
onClearSecret={setSecretCleared}
|
|
|
|
|
compact
|
|
|
|
|
disabled={actionPending}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
{credentialActions}
|
|
|
|
|
</div>
|
|
|
|
|
{mode === "credentials" && feature.setup?.verifies_connection
|
|
|
|
|
&& (validation || validating) ? (
|
|
|
|
|
<div>
|
|
|
|
|
<ChannelValidationProgress
|
|
|
|
|
validation={validation}
|
|
|
|
|
validating={validating}
|
|
|
|
|
feature={feature}
|
|
|
|
|
/>
|
2026-07-13 13:11:46 +08:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-09-11 15:22:14 +08:00
|
|
|
<div
|
|
|
|
|
role="status"
|
|
|
|
|
aria-live="polite"
|
|
|
|
|
className={cn(
|
|
|
|
|
"rounded-control bg-muted/55 px-3 py-2.5 text-[12px] leading-5 text-muted-foreground",
|
|
|
|
|
!notice && "sr-only",
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{notice ?? ""}
|
|
|
|
|
</div>
|
2026-07-19 23:30:49 +08:00
|
|
|
</form>
|
2026-07-13 13:11:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
2026-08-12 16:22:34 +08:00
|
|
|
|
2026-09-11 15:22:14 +08:00
|
|
|
const CHANNEL_FIELD_SECTION_ORDER: ChannelFieldSection[] = [
|
|
|
|
|
"account",
|
|
|
|
|
"credentials",
|
|
|
|
|
"connection",
|
|
|
|
|
"access",
|
|
|
|
|
"behavior",
|
|
|
|
|
"security",
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
function ChannelFieldGroups({
|
|
|
|
|
fields,
|
|
|
|
|
requirements,
|
|
|
|
|
sectionLabels,
|
|
|
|
|
...formProps
|
|
|
|
|
}: {
|
|
|
|
|
fields: ChannelConfigField[];
|
|
|
|
|
requirements: ChannelSetupRequirement[];
|
|
|
|
|
sectionLabels?: Record<string, string>;
|
|
|
|
|
values: Record<string, string>;
|
|
|
|
|
configuredFields: Set<string>;
|
|
|
|
|
visibleSecrets: Record<string, boolean>;
|
|
|
|
|
onChange: (key: string, value: string) => void;
|
|
|
|
|
onFieldBlur?: (key: string) => void;
|
|
|
|
|
onToggleSecret: (key: string) => void;
|
|
|
|
|
errors: Record<string, string>;
|
|
|
|
|
clearedSecrets: Set<string>;
|
|
|
|
|
onClearSecret: (key: string, clear: boolean) => void;
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback });
|
|
|
|
|
const labels = new Map(fields.map((field) => [field.key, field.label]));
|
|
|
|
|
const compositeRequirements = requirements.filter(
|
|
|
|
|
(requirement) => requirement.alternatives.length > 1,
|
|
|
|
|
);
|
|
|
|
|
const groups = new Map<string, ChannelConfigField[]>();
|
|
|
|
|
for (const field of fields) {
|
|
|
|
|
const section = field.section ?? "credentials";
|
|
|
|
|
const current = groups.get(section) ?? [];
|
|
|
|
|
current.push(field);
|
|
|
|
|
groups.set(section, current);
|
|
|
|
|
}
|
|
|
|
|
const orderedSections = [
|
|
|
|
|
...CHANNEL_FIELD_SECTION_ORDER,
|
|
|
|
|
...[...groups.keys()].filter(
|
|
|
|
|
(section) => !CHANNEL_FIELD_SECTION_ORDER.includes(section as ChannelFieldSection),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="space-y-5">
|
|
|
|
|
{compositeRequirements.map((requirement, index) => (
|
|
|
|
|
<div
|
|
|
|
|
key={index}
|
|
|
|
|
className="rounded-control border border-border/60 bg-background/55 px-3 py-2.5"
|
|
|
|
|
>
|
|
|
|
|
<div className="text-[11px] font-semibold text-foreground">
|
|
|
|
|
{tx("settings.channels.chooseCredentialMethod", "Choose one credential method")}
|
|
|
|
|
</div>
|
|
|
|
|
<div className="mt-1 flex flex-wrap items-center gap-1.5 text-[11px] text-muted-foreground">
|
|
|
|
|
{requirement.alternatives.map((alternative, alternativeIndex) => (
|
|
|
|
|
<span key={alternative.join("|")} className="contents">
|
|
|
|
|
{alternativeIndex ? <span aria-hidden>{tx("settings.channels.or", "or")}</span> : null}
|
|
|
|
|
<span className="rounded-full bg-muted px-2 py-0.5 text-foreground/85">
|
|
|
|
|
{alternative.map((key) => labels.get(key) ?? key.split(".").at(-1)).join(" + ")}
|
|
|
|
|
</span>
|
|
|
|
|
</span>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
))}
|
|
|
|
|
{orderedSections.map((section) => {
|
|
|
|
|
const sectionFields = groups.get(section);
|
|
|
|
|
if (!sectionFields?.length) return null;
|
|
|
|
|
return (
|
|
|
|
|
<fieldset key={section} className="min-w-0">
|
|
|
|
|
<legend className={groups.size === 1 ? "sr-only" : "mb-2 text-[12px] font-medium text-muted-foreground"}>
|
|
|
|
|
{channelFieldSectionLabel(section, tx, sectionLabels)}
|
|
|
|
|
</legend>
|
|
|
|
|
<div>
|
|
|
|
|
<CredentialForm fields={sectionFields} {...formProps} compact />
|
|
|
|
|
</div>
|
|
|
|
|
</fieldset>
|
|
|
|
|
);
|
|
|
|
|
})}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function channelFieldSectionLabel(
|
|
|
|
|
section: string,
|
|
|
|
|
tx: (key: string, fallback: string) => string,
|
|
|
|
|
sectionLabels?: Record<string, string>,
|
|
|
|
|
): string {
|
|
|
|
|
const customLabel = sectionLabels?.[section];
|
|
|
|
|
if (customLabel) return customLabel;
|
|
|
|
|
|
|
|
|
|
const fallbacks: Record<ChannelFieldSection, string> = {
|
|
|
|
|
account: "Account",
|
|
|
|
|
credentials: "Credentials",
|
|
|
|
|
connection: "Connection",
|
|
|
|
|
access: "Access",
|
|
|
|
|
behavior: "Behavior",
|
|
|
|
|
security: "Security",
|
|
|
|
|
advanced: "Advanced",
|
|
|
|
|
};
|
|
|
|
|
const fallback = fallbacks[section as ChannelFieldSection];
|
|
|
|
|
if (fallback) return tx(`settings.channels.sections.${section}`, fallback);
|
|
|
|
|
return section
|
|
|
|
|
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
|
|
|
.replace(/[_-]+/g, " ")
|
|
|
|
|
.replace(/\b\w/g, (letter) => letter.toUpperCase());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function channelRequirementErrors(
|
|
|
|
|
fields: ChannelConfigField[],
|
|
|
|
|
requirements: ChannelSetupRequirement[],
|
|
|
|
|
values: Record<string, string>,
|
|
|
|
|
configuredFields: Set<string>,
|
|
|
|
|
clearedSecrets: Set<string>,
|
|
|
|
|
message: string,
|
|
|
|
|
): Record<string, string> {
|
|
|
|
|
const fieldByKey = new Map(fields.map((field) => [field.key, field]));
|
|
|
|
|
const present = (key: string) => {
|
|
|
|
|
const field = fieldByKey.get(key);
|
|
|
|
|
if (!field || clearedSecrets.has(key)) return false;
|
|
|
|
|
const value = (values[key] ?? "").trim();
|
|
|
|
|
if (field.kind === "bool") return value === "true";
|
|
|
|
|
if (value) return true;
|
|
|
|
|
return Boolean(field.secret && configuredFields.has(key));
|
|
|
|
|
};
|
|
|
|
|
const errors: Record<string, string> = {};
|
|
|
|
|
for (const requirement of requirements) {
|
|
|
|
|
if (requirement.alternatives.some((alternative) => alternative.every(present))) continue;
|
|
|
|
|
const closest = [...requirement.alternatives].sort(
|
|
|
|
|
(left, right) => left.filter((key) => !present(key)).length - right.filter((key) => !present(key)).length,
|
|
|
|
|
)[0] ?? [];
|
|
|
|
|
for (const key of closest) {
|
|
|
|
|
if (!present(key) && fieldByKey.has(key)) errors[key] = message;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function channelServerValidationErrors(
|
|
|
|
|
fields: ChannelConfigField[],
|
|
|
|
|
missingFields: string[],
|
|
|
|
|
message: string,
|
|
|
|
|
): Record<string, string> {
|
|
|
|
|
const missing = new Set(missingFields);
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
|
fields
|
|
|
|
|
.filter((field) => missing.has(field.key) || missing.has(field.key.split(".").at(-1) ?? ""))
|
|
|
|
|
.map((field) => [field.key, message]),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function focusFirstChannelFieldError(errors: Record<string, string>) {
|
|
|
|
|
const key = Object.keys(errors)[0];
|
|
|
|
|
if (!key) return;
|
|
|
|
|
window.requestAnimationFrame(() => {
|
|
|
|
|
document.getElementById(`channel-field-${key.replace(/[^a-zA-Z0-9_-]/g, "-")}`)?.focus();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-12 16:22:34 +08:00
|
|
|
function ChannelPluginLoading({ compact = false }: { compact?: boolean }) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
return (
|
|
|
|
|
<div
|
|
|
|
|
role="status"
|
|
|
|
|
className={cn(
|
|
|
|
|
"flex items-center justify-center gap-2 text-sm text-muted-foreground",
|
2026-09-11 15:22:14 +08:00
|
|
|
compact
|
|
|
|
|
? "min-h-12"
|
|
|
|
|
: `${CHANNEL_SETUP_PANEL_CLASS_NAME} min-h-48`,
|
2026-08-12 16:22:34 +08:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<Loader2 className="h-4 w-4 animate-spin motion-reduce:animate-none" aria-hidden />
|
|
|
|
|
{t("settings.status.loading")}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|