import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { SettingsHint } from "@/components/settings/shared/SettingsHint"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from "@/components/ui/dialog"; import { SharedProxyEditor } from "@/components/settings/shared/SharedProxyEditor"; export function SharedProxySetupDialog({ onClose, onSave, busy }: { busy: boolean; onClose: () => void; onSave: (address: string) => Promise; }) { const { t } = useTranslation(); const [address, setAddress] = useState(""); const [saving, setSaving] = useState(false); const [error, setError] = useState(""); const input = useRef(null); const origin = useRef(document.activeElement?.closest("[data-proxy-control]") ?? null); const title = t("settings.runtimeConfig.fields.network_proxy.label"); useEffect(() => { if (error && !saving) input.current?.focus(); }, [error, saving]); const save = async () => { if (saving || busy || !address.trim()) return; setSaving(true); setError(""); try { await onSave(address); onClose(); } catch (cause) { setError(cause instanceof Error ? cause.message : String(cause)); } finally { setSaving(false); } }; return ( { if (!open && !saving) onClose(); }}> { event.preventDefault(); input.current?.focus(); }} onCloseAutoFocus={(event) => { event.preventDefault(); const row = origin.current; const target = row?.querySelector('[role="switch"]') ?? row?.querySelector("button:last-child"); target?.focus(); }} onEscapeKeyDown={(event) => { if (saving) event.preventDefault(); }} onPointerDownOutside={(event) => { if (saving) event.preventDefault(); }}>
{ event.preventDefault(); void save(); }}> {title} {t("settings.network.sharedProxyScope")}
{ setAddress(next); setError(""); }} /> {error ? : null}
); }