import { AlertTriangle, Check, ChevronDown, Copy, Globe, KeyRound, LayoutGrid, List as ListIcon, Pencil, Plus, Search, Settings2, Trash2, } from "lucide-react"; import React, { useMemo, useState } from "react"; import { useI18n } from "../application/i18n/I18nProvider"; import { useStoredViewMode } from "../application/state/useStoredViewMode"; import { isValidProxyPort, removeProxyProfileReferences } from "../domain/proxyProfiles"; import { STORAGE_KEY_VAULT_PROXY_PROFILES_VIEW_MODE, } from "../infrastructure/config/storageKeys"; import { cn } from "../lib/utils"; import type { GroupConfig, Host, ProxyConfig, ProxyProfile } from "../types"; import { AsidePanel, AsidePanelContent, AsidePanelFooter, } from "./ui/aside-panel"; import { Badge } from "./ui/badge"; import { Button } from "./ui/button"; import { Card } from "./ui/card"; import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuSeparator, ContextMenuTrigger, } from "./ui/context-menu"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "./ui/dialog"; import { Dropdown, DropdownContent, DropdownTrigger } from "./ui/dropdown"; import { Input } from "./ui/input"; import { toast } from "./ui/toast"; interface ProxyProfilesManagerProps { proxyProfiles: ProxyProfile[]; hosts: Host[]; groupConfigs: GroupConfig[]; onUpdateProxyProfiles: (profiles: ProxyProfile[]) => void; onUpdateHosts: (hosts: Host[]) => void; onUpdateGroupConfigs: (configs: GroupConfig[]) => void; } const createDraftProfile = (): ProxyProfile => { const now = Date.now(); return { id: crypto.randomUUID(), label: "", config: { type: "http", host: "", port: 8080, }, createdAt: now, updatedAt: now, }; }; const getProfileUsageCount = ( profileId: string, hosts: Host[], groupConfigs: GroupConfig[], ): number => hosts.filter((host) => host.proxyProfileId === profileId).length + groupConfigs.filter((config) => config.proxyProfileId === profileId).length; type ProxyProfilesViewMode = "grid" | "list"; interface ProxyProfileCardProps { profile: ProxyProfile; usageCount: number; viewMode: ProxyProfilesViewMode; isSelected: boolean; onClick: () => void; onEdit: () => void; onDuplicate: () => void; onDelete: () => void; } const ProxyProfileCard: React.FC = ({ profile, usageCount, viewMode, isSelected, onClick, onEdit, onDuplicate, onDelete, }) => { const { t } = useI18n(); const usageLabel = t("proxyProfiles.usage", { count: usageCount }); const accessibleLabel = `${profile.label}, ${profile.config.type.toUpperCase()}, ${profile.config.host}:${profile.config.port}, ${usageLabel}`; return ( {t("action.edit")} {t("action.duplicate")} {t("action.delete")} ); }; export const ProxyProfilesManager: React.FC = ({ proxyProfiles, hosts, groupConfigs, onUpdateProxyProfiles, onUpdateHosts, onUpdateGroupConfigs, }) => { const { t } = useI18n(); const [search, setSearch] = useState(""); const [viewMode, setViewMode] = useStoredViewMode( STORAGE_KEY_VAULT_PROXY_PROFILES_VIEW_MODE, "grid", ); const proxyProfilesViewMode: ProxyProfilesViewMode = viewMode === "list" ? "list" : "grid"; const [draft, setDraft] = useState(null); const [deleteTarget, setDeleteTarget] = useState(null); const usageByProfileId = useMemo(() => { const map = new Map(); for (const profile of proxyProfiles) { map.set(profile.id, getProfileUsageCount(profile.id, hosts, groupConfigs)); } return map; }, [groupConfigs, hosts, proxyProfiles]); const filteredProfiles = useMemo(() => { const q = search.trim().toLowerCase(); if (!q) return proxyProfiles; return proxyProfiles.filter((profile) => profile.label.toLowerCase().includes(q) || profile.config.host.toLowerCase().includes(q) || profile.config.type.toLowerCase().includes(q), ); }, [proxyProfiles, search]); const updateDraftConfig = (field: keyof ProxyConfig, value: string | number) => { setDraft((prev) => { if (!prev) return prev; return { ...prev, config: { ...prev.config, [field]: value, }, }; }); }; const openCreate = () => { setDraft(createDraftProfile()); }; const openEdit = (profile: ProxyProfile) => { setDraft({ ...profile, config: { ...profile.config }, }); }; const duplicateProfile = (profile: ProxyProfile) => { const now = Date.now(); onUpdateProxyProfiles([ ...proxyProfiles, { ...profile, id: crypto.randomUUID(), label: t("proxyProfiles.copyName", { name: profile.label }), config: { ...profile.config }, createdAt: now, updatedAt: now, }, ]); }; const saveDraft = () => { if (!draft) return; const label = draft.label.trim(); const host = draft.config.host.trim(); if (!label || !host || !draft.config.port) { toast.error(t("proxyProfiles.error.required")); return; } if (!isValidProxyPort(draft.config.port)) { toast.error(t("proxyProfiles.error.port")); return; } const saved: ProxyProfile = { ...draft, label, config: { ...draft.config, host, port: Number(draft.config.port), username: draft.config.username?.trim() || undefined, password: draft.config.password || undefined, }, updatedAt: Date.now(), }; onUpdateProxyProfiles( proxyProfiles.some((profile) => profile.id === saved.id) ? proxyProfiles.map((profile) => profile.id === saved.id ? saved : profile) : [...proxyProfiles, saved], ); setDraft(null); }; const confirmDelete = () => { if (!deleteTarget) return; const cleaned = removeProxyProfileReferences(deleteTarget.id, { hosts, groupConfigs, }); onUpdateProxyProfiles(proxyProfiles.filter((profile) => profile.id !== deleteTarget.id)); onUpdateHosts(cleaned.hosts); onUpdateGroupConfigs(cleaned.groupConfigs); if (draft?.id === deleteTarget.id) { setDraft(null); } setDeleteTarget(null); }; return (
setSearch(event.target.value)} placeholder={t("proxyProfiles.search.placeholder")} className="h-10 pl-9 w-full bg-secondary border-border/60 text-sm" />

{t("proxyProfiles.section.proxies")}

{t("proxyProfiles.count.items", { count: filteredProfiles.length })}
{filteredProfiles.length === 0 ? (

{t("proxyProfiles.empty.title")}

{t("proxyProfiles.empty.desc")}

) : (
{filteredProfiles.map((profile) => ( openEdit(profile)} onEdit={() => openEdit(profile)} onDuplicate={() => duplicateProfile(profile)} onDelete={() => setDeleteTarget(profile)} /> ))}
)}
{draft && ( setDraft(null)} title={draft.label || t("proxyProfiles.panel.newTitle")} >

{t("proxyProfiles.field.name")}

setDraft({ ...draft, label: event.target.value })} placeholder={t("proxyProfiles.field.name")} className="h-10" />

{t("field.type")}

updateDraftConfig("host", event.target.value)} placeholder={t("hostDetails.proxyPanel.hostPlaceholder")} className="h-10 flex-1" /> updateDraftConfig("port", event.target.value === "" ? 0 : Number(event.target.value))} placeholder="3128" min={1} max={65535} step={1} className="h-10 w-24 text-center" />

{t("hostDetails.proxyPanel.credentials")}

{t("common.optional")}
updateDraftConfig("username", event.target.value)} placeholder={t("hostDetails.proxyPanel.usernamePlaceholder")} className="h-10" /> updateDraftConfig("password", event.target.value)} placeholder={t("hostDetails.proxyPanel.passwordPlaceholder")} className="h-10" />
)} !open && setDeleteTarget(null)}> {t("proxyProfiles.delete.title")} {deleteTarget ? t("proxyProfiles.delete.desc", { name: deleteTarget.label, count: usageByProfileId.get(deleteTarget.id) ?? 0, }) : ""}
); }; export default ProxyProfilesManager;