import React, { useState, useEffect } from "react"; import { Button } from "@/components/ui/button.tsx"; import { Input } from "@/components/ui/input.tsx"; import { Label } from "@/components/ui/label.tsx"; import { Alert, AlertTitle, AlertDescription } from "@/components/ui/alert.tsx"; import { useTranslation } from "react-i18next"; import { getServerConfig, saveServerConfig, type ServerConfig, } from "@/ui/main-axios.ts"; import { Server } from "lucide-react"; interface ServerConfigProps { onServerConfigured: (serverUrl: string) => void; onCancel?: () => void; isFirstTime?: boolean; } export function ElectronServerConfig({ onServerConfigured, onCancel, isFirstTime = false, }: ServerConfigProps) { const { t } = useTranslation(); const [serverUrl, setServerUrl] = useState(""); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); useEffect(() => { loadServerConfig(); }, []); const loadServerConfig = async () => { try { const config = await getServerConfig(); if (config?.serverUrl) { setServerUrl(config.serverUrl); } } catch {} }; const handleSaveConfig = async () => { if (!serverUrl.trim()) { setError(t("serverConfig.enterServerUrl")); return; } setLoading(true); setError(null); try { let normalizedUrl = serverUrl.trim(); if ( !normalizedUrl.startsWith("http://") && !normalizedUrl.startsWith("https://") ) { setError(t("serverConfig.mustIncludeProtocol")); setLoading(false); return; } const config: ServerConfig = { serverUrl: normalizedUrl, lastUpdated: new Date().toISOString(), }; const success = await saveServerConfig(config); if (success) { onServerConfigured(normalizedUrl); } else { setError(t("serverConfig.saveFailed")); } } catch { setError(t("serverConfig.saveError")); } finally { setLoading(false); } }; const handleUrlChange = (value: string) => { setServerUrl(value); setError(null); }; return (

{t("serverConfig.title")}

{t("serverConfig.description")}

handleUrlChange(e.target.value)} className="w-full h-10" disabled={loading} />
{error && ( {t("common.error")} {error} )}
{onCancel && !isFirstTime && ( )}
{t("serverConfig.helpText")}
); }