import { useState, useEffect, useCallback } from "preact/hooks"; import type { ProxyEntry, ProxyAssignment } from "../types"; export interface AddProxyFields { name: string; protocol: string; host: string; port: string; username: string; password: string; } export interface ProxiesState { proxies: ProxyEntry[]; assignments: ProxyAssignment[]; healthCheckIntervalMinutes: number; loading: boolean; refresh: () => Promise; addProxy: (fields: AddProxyFields) => Promise; removeProxy: (id: string) => Promise; checkProxy: (id: string) => Promise; checkAll: () => Promise; enableProxy: (id: string) => Promise; disableProxy: (id: string) => Promise; assignProxy: (accountId: string, proxyId: string) => Promise; unassignProxy: (accountId: string) => Promise; setInterval: (minutes: number) => Promise; } export function useProxies(): ProxiesState { const [proxies, setProxies] = useState([]); const [assignments, setAssignments] = useState([]); const [healthCheckIntervalMinutes, setHealthInterval] = useState(5); const [loading, setLoading] = useState(true); const refresh = useCallback(async () => { try { const resp = await fetch("/api/proxies"); const data = await resp.json(); setProxies(data.proxies || []); setAssignments(data.assignments || []); setHealthInterval(data.healthCheckIntervalMinutes || 5); } catch { // ignore } finally { setLoading(false); } }, []); useEffect(() => { refresh(); }, [refresh]); const addProxy = useCallback( async (fields: AddProxyFields): Promise => { try { const resp = await fetch("/api/proxies", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ name: fields.name, protocol: fields.protocol, host: fields.host, port: fields.port, username: fields.username, password: fields.password, }), }); const data = await resp.json(); if (!resp.ok) return data.error || "Failed to add proxy"; await refresh(); return null; } catch (err) { return err instanceof Error ? err.message : "Network error"; } }, [refresh], ); const removeProxy = useCallback( async (id: string): Promise => { try { const resp = await fetch(`/api/proxies/${encodeURIComponent(id)}`, { method: "DELETE", }); if (!resp.ok) { const data = await resp.json(); return data.error || "Failed to remove proxy"; } await refresh(); return null; } catch (err) { return err instanceof Error ? err.message : "Network error"; } }, [refresh], ); const checkProxy = useCallback( async (id: string) => { try { await fetch(`/api/proxies/${encodeURIComponent(id)}/check`, { method: "POST", }); } catch { // network error — refresh will show stale state } await refresh(); }, [refresh], ); const checkAll = useCallback(async () => { try { await fetch("/api/proxies/check-all", { method: "POST" }); } catch { // network error } await refresh(); }, [refresh]); const enableProxy = useCallback( async (id: string) => { try { await fetch(`/api/proxies/${encodeURIComponent(id)}/enable`, { method: "POST", }); } catch { // network error } await refresh(); }, [refresh], ); const disableProxy = useCallback( async (id: string) => { try { await fetch(`/api/proxies/${encodeURIComponent(id)}/disable`, { method: "POST", }); } catch { // network error } await refresh(); }, [refresh], ); const assignProxy = useCallback( async (accountId: string, proxyId: string) => { try { await fetch("/api/proxies/assign", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accountId, proxyId }), }); } catch { // network error } await refresh(); }, [refresh], ); const unassignProxy = useCallback( async (accountId: string) => { try { await fetch(`/api/proxies/assign/${encodeURIComponent(accountId)}`, { method: "DELETE", }); } catch { // network error } await refresh(); }, [refresh], ); const setIntervalMinutes = useCallback( async (minutes: number) => { try { await fetch("/api/proxies/settings", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ healthCheckIntervalMinutes: minutes }), }); } catch { // network error } await refresh(); }, [refresh], ); return { proxies, assignments, healthCheckIntervalMinutes, loading, refresh, addProxy, removeProxy, checkProxy, checkAll, enableProxy, disableProxy, assignProxy, unassignProxy, setInterval: setIntervalMinutes, }; }