Files
Termix/src/ui/auth/ElectronServerConfig.tsx
T

272 lines
8.1 KiB
TypeScript
Raw Normal View History

2025-09-12 14:42:00 -05:00
import React, { useState, useEffect } from "react";
2026-05-11 01:23:11 -05:00
import { Button } from "@/components/button.tsx";
import { Input } from "@/components/input.tsx";
import { Label } from "@/components/label.tsx";
import { Alert, AlertTitle, AlertDescription } from "@/components/alert.tsx";
2025-09-12 14:42:00 -05:00
import { useTranslation } from "react-i18next";
import {
getServerConfig,
saveServerConfig,
+5
2026-03-08 18:02:14 -05:00
getEmbeddedServerStatus,
setEmbeddedMode,
2025-09-12 14:42:00 -05:00
type ServerConfig,
2026-05-11 01:23:11 -05:00
} from "@/main-axios.ts";
+5
2026-03-08 18:02:14 -05:00
import { Server, Monitor, Loader2 } from "lucide-react";
2025-09-12 14:42:00 -05:00
interface ServerConfigProps {
onServerConfigured: (serverUrl: string) => void;
+5
2026-03-08 18:02:14 -05:00
onUseEmbedded?: () => void;
2025-09-12 14:42:00 -05:00
onCancel?: () => void;
isFirstTime?: boolean;
}
+7
2025-11-05 10:36:16 -06:00
export function ElectronServerConfig({
2025-09-12 14:42:00 -05:00
onServerConfigured,
+5
2026-03-08 18:02:14 -05:00
onUseEmbedded,
2025-09-12 14:42:00 -05:00
onCancel,
isFirstTime = false,
}: ServerConfigProps) {
const { t } = useTranslation();
const [serverUrl, setServerUrl] = useState("");
const [loading, setLoading] = useState(false);
+5
2026-03-08 18:02:14 -05:00
const [embeddedLoading, setEmbeddedLoading] = useState(false);
2025-09-12 14:42:00 -05:00
const [error, setError] = useState<string | null>(null);
+5
2026-03-08 18:02:14 -05:00
const [embeddedAvailable, setEmbeddedAvailable] = useState<boolean | null>(
null,
);
2025-09-12 14:42:00 -05:00
useEffect(() => {
loadServerConfig();
+5
2026-03-08 18:02:14 -05:00
checkEmbeddedBackend();
2025-09-12 14:42:00 -05:00
}, []);
const loadServerConfig = async () => {
try {
const config = await getServerConfig();
if (config?.serverUrl) {
setServerUrl(config.serverUrl);
}
2025-11-17 09:46:05 -06:00
} catch (error) {
console.error("Server config operation failed:", error);
}
2025-09-12 14:42:00 -05:00
};
+5
2026-03-08 18:02:14 -05:00
const checkEmbeddedBackend = async () => {
try {
const status = await getEmbeddedServerStatus();
setEmbeddedAvailable(!!status?.embedded);
} catch {
setEmbeddedAvailable(true);
}
};
const probeBackend = async (): Promise<boolean> => {
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 3000);
try {
const res = await fetch("http://localhost:30001/health", {
signal: controller.signal,
});
clearTimeout(timer);
return res.ok;
} catch {
clearTimeout(timer);
}
const controller2 = new AbortController();
const timer2 = setTimeout(() => controller2.abort(), 3000);
try {
await fetch("http://localhost:30001/version", {
signal: controller2.signal,
});
clearTimeout(timer2);
return true;
} catch {
clearTimeout(timer2);
return false;
}
};
const handleUseEmbedded = async () => {
setEmbeddedLoading(true);
setError(null);
try {
2026-05-28 22:29:20 -05:00
await new Promise((r) => setTimeout(r, 1500));
const maxRetries = 15;
+5
2026-03-08 18:02:14 -05:00
for (let i = 0; i < maxRetries; i++) {
if (await probeBackend()) {
setEmbeddedMode(true);
if (onUseEmbedded) {
onUseEmbedded();
} else {
onServerConfigured("http://localhost:30001");
}
return;
}
if (i < maxRetries - 1) {
await new Promise((r) => setTimeout(r, 2000));
}
}
setError(t("serverConfig.embeddedNotReady"));
} catch (err) {
setError(
err instanceof Error ? err.message : t("serverConfig.embeddedNotReady"),
);
} finally {
setEmbeddedLoading(false);
}
};
2025-09-12 14:42:00 -05:00
const handleSaveConfig = async () => {
if (!serverUrl.trim()) {
setError(t("serverConfig.enterServerUrl"));
return;
}
setLoading(true);
setError(null);
try {
+5
2026-03-08 18:02:14 -05:00
const normalizedUrl = serverUrl.trim();
+7
2025-11-05 10:36:16 -06:00
2025-09-12 14:42:00 -05:00
if (
!normalizedUrl.startsWith("http://") &&
!normalizedUrl.startsWith("https://")
) {
+7
2025-11-05 10:36:16 -06:00
setError(t("serverConfig.mustIncludeProtocol"));
setLoading(false);
return;
2025-09-12 14:42:00 -05:00
}
const config: ServerConfig = {
serverUrl: normalizedUrl,
lastUpdated: new Date().toISOString(),
};
const success = await saveServerConfig(config);
if (success) {
onServerConfigured(normalizedUrl);
} else {
setError(t("serverConfig.saveFailed"));
}
+7
2025-11-05 10:36:16 -06:00
} catch {
2025-09-12 14:42:00 -05:00
setError(t("serverConfig.saveError"));
} finally {
setLoading(false);
}
};
const handleUrlChange = (value: string) => {
setServerUrl(value);
setError(null);
};
return (
2026-05-28 22:29:20 -05:00
<div className="fixed inset-0 flex items-center justify-center bg-background p-6">
<div className="flex flex-col gap-5 p-6 border border-border bg-card max-w-md w-full">
<div className="flex flex-col gap-1">
<div className="flex items-center gap-2.5">
<Server className="size-4 text-accent-brand shrink-0" />
<p className="font-bold">{t("serverConfig.title")}</p>
+1
2025-12-31 22:20:12 -06:00
</div>
2026-05-28 22:29:20 -05:00
<p className="text-sm text-muted-foreground">
{t("serverConfig.description")}
</p>
</div>
+5
2026-03-08 18:02:14 -05:00
2026-05-28 22:29:20 -05:00
{embeddedAvailable !== false && (
<>
<Button
type="button"
variant="outline"
className="w-full"
onClick={handleUseEmbedded}
disabled={embeddedLoading || loading}
>
{embeddedLoading ? (
<span className="flex items-center gap-2">
<Loader2 className="size-4 animate-spin" />
{t("serverConfig.embeddedConnecting")}
</span>
) : (
<span className="flex items-center gap-2">
<Monitor className="size-4" />
{t("serverConfig.useEmbedded")}
<span className="px-1.5 py-0.5 text-[10px] font-bold bg-yellow-500/20 text-yellow-600 dark:text-yellow-400 border border-yellow-500/30">
BETA
</span>
</span>
)}
</Button>
<p className="text-xs text-muted-foreground -mt-3">
{t("serverConfig.embeddedDesc")}
</p>
+5
2026-03-08 18:02:14 -05:00
<div className="flex items-center gap-3">
<div className="h-px flex-1 bg-border" />
<span className="text-xs text-muted-foreground">
{t("common.or") || "OR"}
</span>
<div className="h-px flex-1 bg-border" />
</div>
2026-05-28 22:29:20 -05:00
</>
)}
<div className="flex flex-col gap-3">
<div className="flex flex-col gap-1.5">
<Label htmlFor="server-url">{t("serverConfig.serverUrl")}</Label>
<Input
id="server-url"
type="text"
placeholder="https://your-server.com"
value={serverUrl}
onChange={(e) => handleUrlChange(e.target.value)}
disabled={loading || embeddedLoading}
/>
</div>
{error && (
<Alert variant="destructive">
<AlertTitle>{t("common.error")}</AlertTitle>
<AlertDescription>{error}</AlertDescription>
</Alert>
+5
2026-03-08 18:02:14 -05:00
)}
2026-05-28 22:29:20 -05:00
<div className="flex gap-2">
{onCancel && !isFirstTime && (
+1
2025-12-31 22:20:12 -06:00
<Button
type="button"
+5
2026-03-08 18:02:14 -05:00
variant="outline"
2026-05-28 22:29:20 -05:00
className="flex-1"
onClick={onCancel}
disabled={loading || embeddedLoading}
+1
2025-12-31 22:20:12 -06:00
>
2026-05-28 22:29:20 -05:00
{t("common.cancel")}
+1
2025-12-31 22:20:12 -06:00
</Button>
2026-05-28 22:29:20 -05:00
)}
<Button
type="button"
className={`bg-accent-brand hover:bg-accent-brand/90 text-background font-bold ${onCancel && !isFirstTime ? "flex-1" : "w-full"}`}
onClick={handleSaveConfig}
disabled={loading || embeddedLoading || !serverUrl.trim()}
>
{loading ? (
<span className="flex items-center gap-2">
<div className="w-4 h-4 border-2 border-background border-t-transparent rounded-full animate-spin" />
{t("serverConfig.saving")}
</span>
) : (
t("serverConfig.saveConfig")
)}
</Button>
+1
2025-12-31 22:20:12 -06:00
</div>
2026-05-28 22:29:20 -05:00
<p className="text-xs text-muted-foreground text-center">
{t("serverConfig.helpText")}
</p>
2025-09-12 14:42:00 -05:00
</div>
</div>
</div>
);
}