Spaces:
Paused
Paused
File size: 12,175 Bytes
178e38e | 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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | import { useState, useCallback } from "preact/hooks";
import { useT } from "@shared/i18n/context";
import type { TranslationKey } from "@shared/i18n/translations";
import type { ProxiesState } from "@shared/hooks/use-proxies";
const PROTOCOLS = ["http", "https", "socks5", "socks5h"] as const;
const statusStyles: Record<string, [string, TranslationKey]> = {
active: [
"bg-green-100 text-green-700 border-green-200 dark:bg-[#11281d] dark:text-primary dark:border-[#1a442e]",
"proxyActive",
],
unreachable: [
"bg-red-100 text-red-600 border-red-200 dark:bg-red-900/20 dark:text-red-400 dark:border-red-800/30",
"proxyUnreachable",
],
disabled: [
"bg-slate-100 text-slate-500 border-slate-200 dark:bg-slate-800/30 dark:text-slate-400 dark:border-slate-700/30",
"proxyDisabled",
],
};
const inputCls = "px-3 py-2 text-sm border border-gray-200 dark:border-border-dark rounded-lg bg-white dark:bg-bg-dark text-slate-700 dark:text-text-main focus:outline-none focus:ring-1 focus:ring-primary";
interface ProxyPoolProps {
proxies: ProxiesState;
}
export function ProxyPool({ proxies }: ProxyPoolProps) {
const t = useT();
const [showAdd, setShowAdd] = useState(false);
const [newName, setNewName] = useState("");
const [newProtocol, setNewProtocol] = useState("http");
const [newHost, setNewHost] = useState("");
const [newPort, setNewPort] = useState("");
const [newUsername, setNewUsername] = useState("");
const [newPassword, setNewPassword] = useState("");
const [addError, setAddError] = useState("");
const [checking, setChecking] = useState<string | null>(null);
const [checkingAll, setCheckingAll] = useState(false);
const resetForm = useCallback(() => {
setNewName("");
setNewProtocol("http");
setNewHost("");
setNewPort("");
setNewUsername("");
setNewPassword("");
setAddError("");
}, []);
const handleAdd = useCallback(async () => {
setAddError("");
if (!newHost.trim()) {
setAddError(t("proxyHost") + " is required");
return;
}
const err = await proxies.addProxy({
name: newName,
protocol: newProtocol,
host: newHost,
port: newPort,
username: newUsername,
password: newPassword,
});
if (err) {
setAddError(err);
} else {
resetForm();
setShowAdd(false);
}
}, [newName, newProtocol, newHost, newPort, newUsername, newPassword, proxies, resetForm, t]);
const handleCheck = useCallback(
async (id: string) => {
setChecking(id);
await proxies.checkProxy(id);
setChecking(null);
},
[proxies],
);
const handleCheckAll = useCallback(async () => {
setCheckingAll(true);
await proxies.checkAll();
setCheckingAll(false);
}, [proxies]);
const handleDelete = useCallback(
async (id: string) => {
if (!confirm(t("removeProxyConfirm"))) return;
await proxies.removeProxy(id);
},
[proxies, t],
);
return (
<section>
{/* Header */}
<div class="flex items-center justify-between mb-2">
<div>
<h2 class="text-lg font-semibold">{t("proxyPool")}</h2>
<p class="text-xs text-slate-500 dark:text-text-dim mt-0.5">
{t("proxyPoolDesc")}
</p>
</div>
<div class="flex items-center gap-2">
{proxies.proxies.length > 0 && (
<button
onClick={handleCheckAll}
disabled={checkingAll}
class="px-3 py-1.5 text-xs font-medium rounded-lg border border-gray-200 dark:border-border-dark hover:bg-slate-50 dark:hover:bg-border-dark transition-colors disabled:opacity-50"
>
{checkingAll ? "..." : t("checkAllHealth")}
</button>
)}
<button
onClick={() => setShowAdd(!showAdd)}
class="px-3 py-1.5 text-xs font-medium rounded-lg bg-primary text-white hover:bg-primary/90 transition-colors"
>
{t("addProxy")}
</button>
</div>
</div>
{/* Add form */}
{showAdd && (
<div class="bg-white dark:bg-card-dark border border-gray-200 dark:border-border-dark rounded-xl p-4 mb-4">
<div class="grid grid-cols-[auto_1fr_auto] sm:grid-cols-[auto_1fr_80px] gap-2 items-center">
{/* Row 1: Protocol + Host + Port */}
<select
value={newProtocol}
onChange={(e) => setNewProtocol((e.target as HTMLSelectElement).value)}
class={`${inputCls} w-[110px]`}
>
{PROTOCOLS.map((p) => (
<option key={p} value={p}>{p}://</option>
))}
</select>
<input
type="text"
placeholder={t("proxyHost")}
value={newHost}
onInput={(e) => setNewHost((e.target as HTMLInputElement).value)}
class={`${inputCls} font-mono`}
/>
<input
type="text"
placeholder={t("proxyPort")}
value={newPort}
onInput={(e) => setNewPort((e.target as HTMLInputElement).value)}
class={`${inputCls} font-mono`}
/>
</div>
<div class="grid grid-cols-2 sm:grid-cols-3 gap-2 mt-2">
{/* Row 2: Name + Username + Password */}
<input
type="text"
placeholder={`${t("proxyName")} (${t("proxyOptional")})`}
value={newName}
onInput={(e) => setNewName((e.target as HTMLInputElement).value)}
class={inputCls}
/>
<input
type="text"
placeholder={`${t("proxyUsername")} (${t("proxyOptional")})`}
value={newUsername}
onInput={(e) => setNewUsername((e.target as HTMLInputElement).value)}
class={inputCls}
/>
<input
type="password"
placeholder={`${t("proxyPassword")} (${t("proxyOptional")})`}
value={newPassword}
onInput={(e) => setNewPassword((e.target as HTMLInputElement).value)}
class={`${inputCls} col-span-2 sm:col-span-1`}
/>
</div>
<div class="flex items-center justify-between mt-3">
{addError && (
<p class="text-xs text-red-500">{addError}</p>
)}
<div class="ml-auto">
<button
onClick={handleAdd}
class="px-4 py-2 text-sm font-medium rounded-lg bg-primary text-white hover:bg-primary/90 transition-colors whitespace-nowrap"
>
{t("addProxy")}
</button>
</div>
</div>
</div>
)}
{/* Empty state */}
{proxies.proxies.length === 0 && !showAdd && (
<div class="bg-white dark:bg-card-dark border border-gray-200 dark:border-border-dark rounded-xl p-6 text-center text-sm text-slate-500 dark:text-text-dim">
{t("noProxies")}
</div>
)}
{/* Proxy list */}
{proxies.proxies.length > 0 && (
<div class="space-y-2">
{proxies.proxies.map((proxy) => {
const [statusCls, statusKey] =
statusStyles[proxy.status] || statusStyles.disabled;
const isChecking = checking === proxy.id;
return (
<div
key={proxy.id}
class="bg-white dark:bg-card-dark border border-gray-200 dark:border-border-dark rounded-xl p-3 hover:shadow-sm transition-all"
>
<div class="flex items-center justify-between">
{/* Left: name + url + status */}
<div class="flex items-center gap-3 min-w-0 flex-1">
<div class="min-w-0">
<div class="flex items-center gap-2">
<span class="font-medium text-sm truncate">
{proxy.name}
</span>
<span
class={`px-2 py-0.5 rounded-full text-[0.65rem] font-medium border ${statusCls}`}
>
{t(statusKey)}
</span>
</div>
<p class="text-xs text-slate-400 dark:text-text-dim font-mono truncate mt-0.5">
{proxy.url}
</p>
</div>
</div>
{/* Center: health info */}
{proxy.health && (
<div class="hidden sm:flex items-center gap-4 px-4 text-xs text-slate-500 dark:text-text-dim">
{proxy.health.exitIp && (
<span>
{t("exitIp")}: <span class="font-mono font-medium text-slate-700 dark:text-text-main">{proxy.health.exitIp}</span>
</span>
)}
<span>
{proxy.health.latencyMs}ms
</span>
{proxy.health.error && (
<span class="text-red-500 truncate max-w-[200px]" title={proxy.health.error}>
{proxy.health.error}
</span>
)}
</div>
)}
{/* Right: actions */}
<div class="flex items-center gap-1 ml-2">
<button
onClick={() => handleCheck(proxy.id)}
disabled={isChecking}
class="px-2 py-1 text-xs rounded-md hover:bg-slate-100 dark:hover:bg-border-dark transition-colors disabled:opacity-50"
title={t("checkHealth")}
>
{isChecking ? "..." : t("checkHealth")}
</button>
{proxy.status === "disabled" ? (
<button
onClick={() => proxies.enableProxy(proxy.id)}
class="px-2 py-1 text-xs rounded-md hover:bg-green-50 dark:hover:bg-green-900/20 text-green-600 transition-colors"
>
{t("enableProxy")}
</button>
) : (
<button
onClick={() => proxies.disableProxy(proxy.id)}
class="px-2 py-1 text-xs rounded-md hover:bg-slate-100 dark:hover:bg-border-dark text-slate-500 transition-colors"
>
{t("disableProxy")}
</button>
)}
<button
onClick={() => handleDelete(proxy.id)}
class="p-1 text-slate-400 dark:text-text-dim hover:text-red-500 transition-colors rounded-md hover:bg-red-50 dark:hover:bg-red-900/20"
title={t("deleteProxy")}
>
<svg
class="size-4"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="1.5"
>
<path
stroke-linecap="round"
stroke-linejoin="round"
d="M6 18L18 6M6 6l12 12"
/>
</svg>
</button>
</div>
</div>
{/* Mobile health info */}
{proxy.health && (
<div class="sm:hidden flex items-center gap-3 mt-2 text-xs text-slate-500 dark:text-text-dim">
{proxy.health.exitIp && (
<span>IP: <span class="font-mono">{proxy.health.exitIp}</span></span>
)}
<span>{proxy.health.latencyMs}ms</span>
</div>
)}
</div>
);
})}
</div>
)}
</section>
);
}
|