2026-04-24 21:30:48 -05:00
|
|
|
import { useState } from "react";
|
|
|
|
|
import { motion, AnimatePresence } from "framer-motion";
|
|
|
|
|
import { Wifi, WifiOff, Loader, CheckCircle, AlertCircle, Lock, LockOpen } from "lucide-react";
|
|
|
|
|
import {
|
|
|
|
|
configSchema,
|
|
|
|
|
loadConfig,
|
|
|
|
|
saveConfig,
|
|
|
|
|
checkConnection,
|
|
|
|
|
type Config,
|
|
|
|
|
type HealthStatus,
|
|
|
|
|
} from "@/lib/config";
|
2026-04-24 13:56:13 -05:00
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
|
import { Input, Textarea } from "@/components/ui/input";
|
|
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
|
import { Muted } from "@/components/ui/typography";
|
|
|
|
|
import { COLOR } from "@/lib/constants";
|
2026-04-24 21:30:48 -05:00
|
|
|
|
|
|
|
|
interface SettingsFormProps {
|
|
|
|
|
onSaved?: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const statusConfig = {
|
2026-04-24 13:56:13 -05:00
|
|
|
ok: { icon: CheckCircle, color: COLOR.success, label: "Connected" },
|
|
|
|
|
"auth-required": { icon: AlertCircle, color: COLOR.warning, label: "Auth required" },
|
|
|
|
|
unreachable: { icon: WifiOff, color: COLOR.destructive, label: "Unreachable" },
|
|
|
|
|
checking: { icon: Loader, color: COLOR.accentText, label: "Checking..." },
|
2026-04-24 21:30:48 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function SettingsForm({ onSaved }: SettingsFormProps) {
|
|
|
|
|
const existing = loadConfig();
|
|
|
|
|
const [baseUrl, setBaseUrl] = useState(existing?.baseUrl ?? "http://localhost:8000");
|
|
|
|
|
const [token, setToken] = useState(existing?.token ?? "");
|
|
|
|
|
const [errors, setErrors] = useState<Partial<Record<keyof Config, string>>>({});
|
|
|
|
|
const [saved, setSaved] = useState(false);
|
|
|
|
|
const [health, setHealth] = useState<{ status: HealthStatus; message: string } | null>(null);
|
|
|
|
|
const [checking, setChecking] = useState(false);
|
|
|
|
|
|
|
|
|
|
async function handleTest() {
|
|
|
|
|
setChecking(true);
|
|
|
|
|
setHealth({ status: "checking", message: "Connecting..." });
|
|
|
|
|
const result = await checkConnection(baseUrl, token || undefined);
|
|
|
|
|
setHealth(result);
|
|
|
|
|
setChecking(false);
|
|
|
|
|
|
|
|
|
|
if (result.status === "auth-required" && !token) {
|
|
|
|
|
document.getElementById("honcho-token")?.focus();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleSubmit(e: React.SyntheticEvent<HTMLFormElement>) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const result = configSchema.safeParse({ baseUrl, token });
|
|
|
|
|
if (!result.success) {
|
|
|
|
|
const fieldErrors: typeof errors = {};
|
|
|
|
|
for (const issue of result.error.issues) {
|
|
|
|
|
const key = issue.path[0] as keyof Config;
|
|
|
|
|
fieldErrors[key] = issue.message;
|
|
|
|
|
}
|
|
|
|
|
setErrors(fieldErrors);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
setErrors({});
|
|
|
|
|
saveConfig(result.data);
|
|
|
|
|
setSaved(true);
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
setSaved(false);
|
|
|
|
|
onSaved?.();
|
|
|
|
|
}, 600);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const StatusIcon = health ? statusConfig[health.status].icon : null;
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
onSubmit={handleSubmit}
|
|
|
|
|
className="rounded-2xl p-6 space-y-5"
|
|
|
|
|
style={{
|
|
|
|
|
background: "var(--bg-2)",
|
|
|
|
|
border: "1px solid var(--border)",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{/* Base URL */}
|
|
|
|
|
<div>
|
2026-04-24 13:56:13 -05:00
|
|
|
<Label className="mb-1.5 text-sm">
|
2026-04-24 21:30:48 -05:00
|
|
|
Honcho Base URL
|
2026-04-24 13:56:13 -05:00
|
|
|
</Label>
|
2026-04-24 21:30:48 -05:00
|
|
|
<div className="flex gap-2">
|
2026-04-24 13:56:13 -05:00
|
|
|
<Input
|
2026-04-24 21:30:48 -05:00
|
|
|
type="url"
|
|
|
|
|
value={baseUrl}
|
|
|
|
|
onChange={(e) => { setBaseUrl(e.target.value); setHealth(null); }}
|
|
|
|
|
placeholder="http://localhost:8000"
|
2026-04-24 13:56:13 -05:00
|
|
|
className="flex-1 font-mono rounded-xl"
|
2026-04-24 21:30:48 -05:00
|
|
|
/>
|
2026-04-24 13:56:13 -05:00
|
|
|
<Button
|
2026-04-24 21:30:48 -05:00
|
|
|
type="button"
|
2026-04-24 13:56:13 -05:00
|
|
|
variant="accent"
|
2026-04-24 21:30:48 -05:00
|
|
|
onClick={handleTest}
|
|
|
|
|
disabled={checking || !baseUrl}
|
2026-04-24 13:56:13 -05:00
|
|
|
className="rounded-xl"
|
2026-04-24 21:30:48 -05:00
|
|
|
>
|
|
|
|
|
{checking ? (
|
|
|
|
|
<motion.div animate={{ rotate: 360 }} transition={{ duration: 1, repeat: Infinity, ease: "linear" }}>
|
|
|
|
|
<Loader className="w-4 h-4" strokeWidth={1.5} />
|
|
|
|
|
</motion.div>
|
|
|
|
|
) : (
|
|
|
|
|
<Wifi className="w-4 h-4" strokeWidth={1.5} />
|
|
|
|
|
)}
|
|
|
|
|
<span className="hidden sm:block">Test</span>
|
2026-04-24 13:56:13 -05:00
|
|
|
</Button>
|
2026-04-24 21:30:48 -05:00
|
|
|
</div>
|
|
|
|
|
{errors.baseUrl && (
|
2026-04-24 13:56:13 -05:00
|
|
|
<p className="text-xs mt-1" style={{ color: COLOR.destructive }}>{errors.baseUrl}</p>
|
2026-04-24 21:30:48 -05:00
|
|
|
)}
|
2026-04-24 13:56:13 -05:00
|
|
|
<Muted className="text-xs mt-1.5">
|
2026-04-24 21:30:48 -05:00
|
|
|
URL of your self-hosted Honcho instance
|
2026-04-24 13:56:13 -05:00
|
|
|
</Muted>
|
2026-04-24 21:30:48 -05:00
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Health status */}
|
|
|
|
|
<AnimatePresence>
|
|
|
|
|
{health && (
|
|
|
|
|
<motion.div
|
|
|
|
|
initial={{ opacity: 0, height: 0 }}
|
|
|
|
|
animate={{ opacity: 1, height: "auto" }}
|
|
|
|
|
exit={{ opacity: 0, height: 0 }}
|
|
|
|
|
className="overflow-hidden"
|
|
|
|
|
>
|
|
|
|
|
<div
|
|
|
|
|
className="rounded-xl px-4 py-3 flex items-center gap-2.5"
|
|
|
|
|
style={{
|
|
|
|
|
background: "var(--surface)",
|
|
|
|
|
border: `1px solid ${statusConfig[health.status].color}33`,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{StatusIcon && (
|
|
|
|
|
<StatusIcon
|
|
|
|
|
className="w-4 h-4 shrink-0"
|
|
|
|
|
style={{ color: statusConfig[health.status].color }}
|
|
|
|
|
strokeWidth={1.5}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
<div>
|
|
|
|
|
<p className="text-sm font-medium" style={{ color: statusConfig[health.status].color }}>
|
|
|
|
|
{statusConfig[health.status].label}
|
|
|
|
|
</p>
|
2026-04-24 13:56:13 -05:00
|
|
|
<Muted className="text-xs mt-0.5">
|
2026-04-24 21:30:48 -05:00
|
|
|
{health.message}
|
2026-04-24 13:56:13 -05:00
|
|
|
</Muted>
|
2026-04-24 21:30:48 -05:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</motion.div>
|
|
|
|
|
)}
|
|
|
|
|
</AnimatePresence>
|
|
|
|
|
|
|
|
|
|
{/* Token */}
|
|
|
|
|
<div>
|
2026-04-24 13:56:13 -05:00
|
|
|
<Label
|
2026-04-24 21:30:48 -05:00
|
|
|
htmlFor="honcho-token"
|
2026-04-24 13:56:13 -05:00
|
|
|
className="flex items-center gap-1.5 mb-1.5 text-sm"
|
2026-04-24 21:30:48 -05:00
|
|
|
>
|
|
|
|
|
{token ? (
|
|
|
|
|
<Lock className="w-3.5 h-3.5" style={{ color: "var(--accent)" }} strokeWidth={1.5} />
|
|
|
|
|
) : (
|
|
|
|
|
<LockOpen className="w-3.5 h-3.5" style={{ color: "var(--text-3)" }} strokeWidth={1.5} />
|
|
|
|
|
)}
|
|
|
|
|
API Token
|
|
|
|
|
<span
|
|
|
|
|
className="ml-1 text-xs font-normal px-1.5 py-0.5 rounded-full"
|
|
|
|
|
style={{
|
|
|
|
|
background: "var(--surface)",
|
|
|
|
|
border: "1px solid var(--border)",
|
|
|
|
|
color: "var(--text-3)",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
optional
|
|
|
|
|
</span>
|
2026-04-24 13:56:13 -05:00
|
|
|
</Label>
|
|
|
|
|
<Textarea
|
2026-04-24 21:30:48 -05:00
|
|
|
id="honcho-token"
|
|
|
|
|
value={token}
|
|
|
|
|
onChange={(e) => setToken(e.target.value)}
|
|
|
|
|
rows={2}
|
|
|
|
|
placeholder="eyJ... (required only if your instance has auth enabled)"
|
2026-04-24 13:56:13 -05:00
|
|
|
className="font-mono rounded-xl"
|
2026-04-24 21:30:48 -05:00
|
|
|
/>
|
|
|
|
|
{health?.status === "auth-required" && !token && (
|
|
|
|
|
<motion.p
|
|
|
|
|
initial={{ opacity: 0, y: -4 }}
|
|
|
|
|
animate={{ opacity: 1, y: 0 }}
|
|
|
|
|
className="text-xs mt-1"
|
2026-04-24 13:56:13 -05:00
|
|
|
style={{ color: COLOR.warning }}
|
2026-04-24 21:30:48 -05:00
|
|
|
>
|
|
|
|
|
This instance requires an API token to proceed
|
|
|
|
|
</motion.p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-04-24 13:56:13 -05:00
|
|
|
<Button
|
2026-04-24 21:30:48 -05:00
|
|
|
type="submit"
|
2026-04-24 13:56:13 -05:00
|
|
|
variant="primary"
|
|
|
|
|
className="w-full py-2.5 px-4 rounded-xl"
|
|
|
|
|
style={saved ? { background: "#059669" } : undefined}
|
2026-04-24 21:30:48 -05:00
|
|
|
>
|
|
|
|
|
{saved ? "✓ Saved" : "Save Connection"}
|
2026-04-24 13:56:13 -05:00
|
|
|
</Button>
|
2026-04-24 21:30:48 -05:00
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|