diff --git a/client/src/components/Files/docsync/DocSyncConnectModal.tsx b/client/src/components/Files/docsync/DocSyncConnectModal.tsx index e5cd07836..e2a8f8c96 100644 --- a/client/src/components/Files/docsync/DocSyncConnectModal.tsx +++ b/client/src/components/Files/docsync/DocSyncConnectModal.tsx @@ -3,6 +3,7 @@ import { AlertTriangle, Check, Loader2, ShieldAlert } from 'lucide-react' import Modal from '../../shared/Modal' import ToggleSwitch from '../../Settings/ToggleSwitch' import { useTranslation } from '../../../i18n/TranslationContext' +import { useConnectForm } from './useConnectForm' import { Badge } from './DocSyncBits' import { DOCUMENT_PROVIDER_ICONS } from '../../shared/DocumentProviderIcons' import type { DocSyncProvider, useDocSync } from './useDocSync' @@ -30,35 +31,13 @@ export default function DocSyncConnectModal({ const existing = sync.connectionFor(provider.id) const Icon = DOCUMENT_PROVIDER_ICONS[provider.id] - const [values, setValues] = useState>({}) - const [insecure, setInsecure] = useState(null) + const form = useConnectForm(provider, existing, sync) const [verdict, setVerdict] = useState<{ connected: boolean; account?: string; error?: string } | null>(null) - // Null until touched, so a connection that loads after the dialog opened is - // still reflected. Seeding the state at mount showed a stored "allow - // self-signed" as off and wrote that back on the next save — a security - // switch silently turning itself off is the worst version of this bug. - const insecureOn = insecure ?? existing?.allowInsecureTls ?? false - - const baseUrl = values.base_url ?? existing?.baseUrl ?? '' - // The address is a column of its own, not a settings entry, so it has to be - // read from there — otherwise the field sits empty while the form submits the - // stored value behind it. - const shown = (key: string) => - values[key] ?? (key === 'base_url' ? existing?.baseUrl : existing?.settings?.[key]) ?? '' - const required = provider.fields.filter(f => f.required) - const canSubmit = required.every(f => - f.field_key === 'base_url' - ? baseUrl.trim().length > 0 - : f.secret - ? (values[f.field_key] ?? '').length > 0 || !!existing?.secrets?.[f.field_key] - : shown(f.field_key).trim().length > 0, - ) - - const test = async () => setVerdict(await sync.testConnection(provider.id, baseUrl, values, insecureOn)) + const test = async () => setVerdict(await form.probe()) const save = async () => { - if (await sync.saveConnection(provider.id, baseUrl, values, insecureOn)) onConnected(provider.id) + if (await form.save()) onConnected(provider.id) } return ( @@ -79,7 +58,7 @@ export default function DocSyncConnectModal({