Spaces:
Paused
Paused
File size: 4,260 Bytes
f878eff | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | import { useState, useEffect, useCallback } from "preact/hooks";
import type { ProxyEntry } from "../types";
export interface AssignmentAccount {
id: string;
email: string;
status: string;
proxyId: string;
proxyName: string;
}
export interface ImportDiff {
changes: Array<{ email: string; accountId: string; from: string; to: string }>;
unchanged: number;
}
export interface ProxyAssignmentsState {
accounts: AssignmentAccount[];
proxies: ProxyEntry[];
loading: boolean;
refresh: () => Promise<void>;
assignBulk: (assignments: Array<{ accountId: string; proxyId: string }>) => Promise<void>;
assignRule: (accountIds: string[], rule: string, targetProxyIds: string[]) => Promise<void>;
exportAssignments: () => Promise<Array<{ email: string; proxyId: string }>>;
importPreview: (data: Array<{ email: string; proxyId: string }>) => Promise<ImportDiff | null>;
applyImport: (assignments: Array<{ accountId: string; proxyId: string }>) => Promise<void>;
}
export function useProxyAssignments(): ProxyAssignmentsState {
const [accounts, setAccounts] = useState<AssignmentAccount[]>([]);
const [proxies, setProxies] = useState<ProxyEntry[]>([]);
const [loading, setLoading] = useState(true);
const refresh = useCallback(async () => {
try {
const resp = await fetch("/api/proxies/assignments");
const data = await resp.json();
setAccounts(data.accounts || []);
setProxies(data.proxies || []);
} catch {
// ignore
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
refresh();
}, [refresh]);
const assignBulk = useCallback(
async (assignments: Array<{ accountId: string; proxyId: string }>) => {
const resp = await fetch("/api/proxies/assign-bulk", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ assignments }),
});
if (!resp.ok) {
const err = await resp.json().catch(() => ({ error: "Request failed" }));
throw new Error((err as { error?: string }).error ?? "Request failed");
}
await refresh();
},
[refresh],
);
const assignRule = useCallback(
async (accountIds: string[], rule: string, targetProxyIds: string[]) => {
const resp = await fetch("/api/proxies/assign-rule", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ accountIds, rule, targetProxyIds }),
});
if (!resp.ok) {
const err = await resp.json().catch(() => ({ error: "Request failed" }));
throw new Error((err as { error?: string }).error ?? "Request failed");
}
await refresh();
},
[refresh],
);
const exportAssignments = useCallback(async (): Promise<Array<{ email: string; proxyId: string }>> => {
const resp = await fetch("/api/proxies/assignments/export");
if (!resp.ok) return [];
const data: { assignments?: Array<{ email: string; proxyId: string }> } = await resp.json();
return data.assignments || [];
}, []);
const importPreview = useCallback(
async (data: Array<{ email: string; proxyId: string }>): Promise<ImportDiff | null> => {
const resp = await fetch("/api/proxies/assignments/import", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ assignments: data }),
});
if (!resp.ok) return null;
const result: ImportDiff = await resp.json();
return result;
},
[],
);
const applyImport = useCallback(
async (assignments: Array<{ accountId: string; proxyId: string }>) => {
const resp = await fetch("/api/proxies/assignments/apply", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ assignments }),
});
if (!resp.ok) {
const err = await resp.json().catch(() => ({ error: "Request failed" }));
throw new Error((err as { error?: string }).error ?? "Request failed");
}
await refresh();
},
[refresh],
);
return {
accounts,
proxies,
loading,
refresh,
assignBulk,
assignRule,
exportAssignments,
importPreview,
applyImport,
};
}
|