import { useRef, type ReactNode } from "react"; import { Check, ChevronDown, Clipboard, Loader2 } from "lucide-react"; import { useTranslation } from "react-i18next"; import { ToggleButton } from "@/components/ui/toggle-button"; import type { ChannelConfigField, ChannelProviderPreset, ChannelSetupPresentation } from "@/channel-plugins/catalog"; import { ChannelFieldGroups } from "./ChannelCredentialFields"; import { CredentialForm } from "./CredentialForm"; import { ChannelRuntimeError } from "./ChannelIdentity"; import { ChannelProviderPresets, ChannelSetupActions } from "./ChannelSetupParts"; import { ChannelValidationProgress } from "./ChannelValidationProgress"; import { Button } from "@/components/ui/button"; import type { ChannelValidationPayload, NanobotFeatureInfo } from "@/lib/types"; import { cn } from "@/lib/utils"; export interface ChannelSetupSurfaceViewProps { header: ReactNode; feature: NanobotFeatureInfo; setup: ChannelSetupPresentation; primaryFields: ChannelConfigField[]; advancedFields: ChannelConfigField[]; savedSecretFields: ChannelConfigField[]; availablePresets: ChannelProviderPreset[]; configuredFields: Set; fieldValues: Record; visibleSecrets: Record; clearedSecrets: Set; fieldErrors: Record; actionPending: boolean; enabled: boolean; autoSaving: boolean; autoSaveState: "idle" | "saved"; advancedOpen: boolean; advancedPanelId: string; validation: ChannelValidationPayload | null; validating: boolean; notice: string | null; connectFlow?: ReactNode; onAdvancedOpenChange: (open: boolean) => void; onChangeField: (key: string, value: string) => void; onToggleSecret: (key: string) => void; onClearSecret: (key: string, clear: boolean) => void; onApplyPreset: (preset: ChannelProviderPreset) => void; onSubmit: () => void; onCheckConnection: () => void; onToggleEnabled: (enabled: boolean) => void; onCopyCommand: () => void; onNotice: (notice: string | null) => void; } export function ChannelSetupSurfaceView({ header, feature, setup, primaryFields, advancedFields, savedSecretFields, availablePresets, configuredFields, fieldValues, visibleSecrets, clearedSecrets, fieldErrors, actionPending, enabled, autoSaving, autoSaveState, advancedOpen, advancedPanelId, validation, validating, notice, connectFlow, onAdvancedOpenChange, onChangeField: setFieldValue, onToggleSecret: toggleSecret, onClearSecret: setSecretCleared, onApplyPreset: applyPreset, onSubmit: saveCredentialSettings, onCheckConnection: checkCurrentSettings, onToggleEnabled: toggleEnabled, onCopyCommand: copyCommand, onNotice: setNotice, }: ChannelSetupSurfaceViewProps) { const { t } = useTranslation(); const tx = (key: string, fallback: string) => t(key, { defaultValue: fallback }); const mode = setup.mode ?? "credentials"; // Keep feedback's footprint so blur-save updates cannot move the close target mid-click. const lastNotice = useRef(notice); if (notice) lastNotice.current = notice; const hasAdvanced = advancedFields.length > 0 || savedSecretFields.length > 0 || availablePresets.length > 0; const advancedFormProps = { fields: advancedFields, values: fieldValues, configuredFields, visibleSecrets, onChange: setFieldValue, onToggleSecret: toggleSecret, errors: fieldErrors, clearedSecrets, onClearSecret: setSecretCleared, disabled: actionPending, }; const inlineActions = mode === "credentials" && primaryFields.length === 1 && !advancedOpen && !setup.presets?.length && !setup.actions?.length; const credentialActions = mode === "credentials" ? (
{feature.setup?.verifies_connection ? ( ) : null} void toggleEnabled(next)} />
) : null; return (
{ event.preventDefault(); if (mode === "credentials") void saveCredentialSettings(); }} >
{header}
{autoSaving ? ( <> {tx("settings.actions.saving", "Saving")} ) : autoSaveState === "saved" ? ( <> {tx("settings.channels.savedSettings", "Settings saved.")} ) : null} {hasAdvanced ? ( ) : null}
{setup.privacyNotice ? (

{setup.privacyNotice}

) : null}
{mode === "webui" ? ( {tx("settings.channels.managedByWebui", "Managed by WebUI")} ) : null}
{mode === "connect" && connectFlow ? ( connectFlow ) : mode === "connect" ? ( <>
{setup.command ? ( ) : null}
{setup.command ? ( {setup.command} ) : null} ) : mode === "credentials" ? ( <> {primaryFields.length ? ( ) : null} ) : null}
{hasAdvanced ? ( ) : null} {credentialActions}
{mode === "credentials" && feature.setup?.verifies_connection && (validation || validating) ? (
) : null}
{notice ?? lastNotice.current ?? ""}
); }