import { RefreshCw } from "lucide-react"; import { useCallback, useEffect, useMemo, useState } from "react"; import type { DockerHostConfig } from "@fusion/core"; import { useDockerTargets } from "../hooks/useDockerTargets"; import { DockerTlsConfig } from "./DockerTlsConfig"; import "./DockerTargetSelector.css"; type TargetMode = "local" | "context" | "host"; interface DockerTargetSelectorProps { value?: DockerHostConfig; onChange: (config: DockerHostConfig) => void; onError?: (error: string) => void; } export function DockerTargetSelector({ value, onChange, onError }: DockerTargetSelectorProps) { const initialMode: TargetMode = value?.context ? "context" : value?.host ? "host" : "local"; const [mode, setMode] = useState(initialMode); const [selectedContext, setSelectedContext] = useState(value?.context ?? ""); const [host, setHost] = useState(value?.host ?? ""); const [localStatus, setLocalStatus] = useState(null); const { contexts, isLoadingContexts, contextsError, loadContexts, testConnection, isTestingConnection, lastTestResult, checkLocalDocker, isCheckingLocal } = useDockerTargets(); const tlsValue = useMemo( () => ({ tlsVerify: value?.tlsVerify, tlsCaPath: value?.tlsCaPath, tlsCertPath: value?.tlsCertPath, tlsKeyPath: value?.tlsKeyPath, }), [value], ); useEffect(() => { if (mode === "local") { onChange({}); return; } if (mode === "context") { void loadContexts().catch((error) => onError?.(error instanceof Error ? error.message : String(error))); onChange(selectedContext ? { context: selectedContext } : {}); return; } onChange({ host, ...tlsValue }); }, [mode]); const updateTls = useCallback( (tls: Pick) => { if (mode !== "host") return; onChange({ host, ...tls }); }, [host, mode, onChange], ); return (
{mode === "local" && localStatus &&
{localStatus}
} {mode === "context" && (
{contextsError &&
{contextsError}
}
)} {mode === "host" && (
{ const next = event.target.value; setHost(next); onChange({ host: next, ...tlsValue }); }} />
)} {lastTestResult && (
{lastTestResult.success ? `Connected${lastTestResult.dockerVersion ? ` (Docker ${lastTestResult.dockerVersion})` : ""}` : lastTestResult.error ?? "Connection failed"}
)}
); }