import { AlertTriangle, BellRing, ChevronRight, FolderOpen, LayoutGrid, RefreshCw, Search, ShieldCheck, Trash2, X, } from "lucide-react"; import { useMemo, useState } from "react"; import { useI18n } from "../i18n"; import { api } from "../lib/api"; import { shortPath } from "../lib/format"; import type { PermissionsWorkspace } from "../hooks/usePermissionsWorkspace"; import type { StartupItem } from "../types"; import { Button, ConfirmDialog, EmptyState, IconButton, InlineError, PageHeader, SectionTitle, Toggle, } from "../components/ui"; function scopeLabel(scope: string, t: (en: string, zh: string) => string): string { return scope === "machine" ? t("All users", "所有用户") : t("Current user", "当前用户"); } // A row whose program binary no longer exists on disk — the config survived // an uninstall. MangoDisk calls these 残留项 and offers to delete the config. function isLeftover(item: StartupItem): boolean { return item.targetExists === false; } // 残留 / 已启用 / 已禁用 row tag; leftover wins the colour fight. The // source / trigger / signature chips used to sit on the group header — the // user found them noise; the detailed path already rides on every row. function statusChip( item: StartupItem, t: (en: string, zh: string) => string, ): [string, string] { if (isLeftover(item)) return [t("Leftover", "残留"), "is-leftover"]; return item.enabled ? [t("Enabled", "已启用"), "is-on"] : [t("Disabled", "已禁用"), "is-off"]; } // 启动文件夹 rows are removable by deleting the shortcut; the folder is a // nice human landing spot for anything else. function rowRevealPath(item: StartupItem): string { const anchor = item.target || item.command; if (!anchor) return ""; return anchor.replace(/"$/, "").replace(/^(.*\.(exe|lnk|bat|cmd))\s+/i, "$1"); } // MangoDisk's status filter: a segment with live counts, 残留 included. type StartupFilter = "all" | "enabled" | "disabled" | "leftover"; // 电脑管家 shows the owning software's icon on every row it can attribute to // an installed program; a grey initial covers the leftovers. function RowIcon({ icon, label }: { icon: string | null | undefined; label: string }) { return icon ? ( ) : ( ); } // 其他软件 carries no real product identity, so it never rides a member's // icon (ChatGPT's blue tile labelled 其他软件 read as a bug). A neutral // apps-grid glyph says "a pile of things we could not attribute". function GroupIcon({ software, items, neutral, }: { software: string; items: { icon: string | null }[]; neutral: boolean; }) { if (neutral) { return ( ); } const icon = items.find((item) => item.icon)?.icon; return icon ? ( ) : ( ); } export function PermissionsPage({ workspace, platform, }: { workspace: PermissionsWorkspace; platform?: string; }) { const { t } = useI18n(); const [query, setQuery] = useState(""); const [openGroups, setOpenGroups] = useState>(new Set()); const [openExtGroups, setOpenExtGroups] = useState>(new Set()); const [extCat, setExtCat] = useState(null); const [startupFilter, setStartupFilter] = useState("all"); const [showSystem, setShowSystem] = useState(false); const [removingIds, setRemovingIds] = useState(null); const view = workspace.view; const tabs = useMemo(() => { const all = [ { id: "startup" as const, label: t("Startup items", "开机启动项"), count: view?.startupCount ?? 0, fresh: false, }, { id: "extensions" as const, label: t("Context menus", "右键菜单"), count: view?.extensions.length ?? 0, fresh: false, }, { id: "advanced" as const, label: t("Advanced startup", "进阶启动项"), count: workspace.advanced?.entries.length ?? 0, fresh: true, }, { id: "popup" as const, label: t("Popup blocker", "弹窗拦截"), count: view?.rules.length ?? 0, fresh: false, }, ]; // Context menus, advanced autostart hooks and the popup blocker are // Windows shell surfaces; macOS only has login/background startup items. return platform === "windows" ? all : all.filter((item) => item.id === "startup"); }, [platform, t, view, workspace.advanced]); if (!view && workspace.scanning) { return (
{t("One moment…", "请稍候…")}
); } if (!view) { return (
} title={t("Nothing scanned yet", "还没有扫描结果")} description={t( "Squid will list startup entries, context-menu handlers and live windows.", "Squid 会列出启动项、右键菜单扩展和当前窗口。", )} action={ } />
); } const needle = query.trim().toLowerCase(); const startupSearch = view.startup.filter( (item) => !needle || item.name.toLowerCase().includes(needle) || item.displayName.toLowerCase().includes(needle) || item.command.toLowerCase().includes(needle), ); // MangoDisk's 显示系统项: Microsoft-signed / %SystemRoot% rows fold into a // hideable 系统组件 group instead of mixing into the software list. const systemName = t("System components", "系统组件"); const startupVisible = showSystem ? startupSearch : startupSearch.filter((item) => !item.systemItem); const leftoverCount = startupVisible.filter(isLeftover).length; const enabledCount = startupVisible.filter((item) => item.enabled && !isLeftover(item)).length; const disabledCount = startupVisible.length - enabledCount - leftoverCount; const counts: Record = { all: startupVisible.length, enabled: enabledCount, disabled: disabledCount, leftover: leftoverCount, }; const startup = startupVisible.filter((item) => startupFilter === "all" ? true : startupFilter === "leftover" ? isLeftover(item) : startupFilter === "enabled" ? item.enabled && !isLeftover(item) : !item.enabled && !isLeftover(item), ); // 电脑管家 style: one group per software (Chinese name in the header); // every entry the backend could not attribute to a real product lands in // a catch-all 其他软件 group; system rows sink to their own group last. const otherName = t("Other software", "其他软件"); const startupGroups = new Map(); for (const item of startup) { const key = item.systemItem ? systemName : item.named && item.displayName ? item.displayName : otherName; const list = startupGroups.get(key) ?? []; list.push(item); startupGroups.set(key, list); } const orderedGroups = [...startupGroups.entries()].sort( ([a], [b]) => Number(a === otherName) - Number(b === otherName) || Number(a === systemName) - Number(b === systemName), ); // Groups start folded (管家 habit): the header carries a master switch // and the status line; expanding shows the per-entry detail. Searching // force-expands everything so matches are never hidden. const searching = query.trim().length > 0; const isGroupOpen = (name: string) => searching || openGroups.has(name); const toggleGroupOpen = (name: string) => setOpenGroups((current) => { const next = new Set(current); if (next.has(name)) next.delete(name); else next.add(name); return next; }); const groupedExtensions = view.extensions .filter( (entry) => !needle || entry.name.toLowerCase().includes(needle) || (entry.friendlyName ?? "").toLowerCase().includes(needle) || (entry.publisher ?? "").toLowerCase().includes(needle) || (entry.module ?? "").toLowerCase().includes(needle), ) .reduce>((groups, entry) => { (groups[entry.extensionPoint] ??= []).push(entry); return groups; }, {}); // Categories stay in the backend's 文件 / 文件夹 / 桌面 / 驱动器 / 所有对象 // order; the horizontal nav shows one at a time so long lists never make // the page scroll for miles. const extCategories = Object.keys(groupedExtensions); const activeCategory = extCat && extCategories.includes(extCat) ? extCat : extCategories[0] ?? null; // Same software-grouping rule as the startup list: rows whose module // carries real product strings group under that product (微信's shell // extensions across several surfaces become one card); the rest sink to // 其他软件. Groups open folded, searching force-expands. const extGrouped = new Map(); for (const entry of activeCategory ? groupedExtensions[activeCategory] : []) { const named = entry.named && entry.friendlyName; const key = named ? entry.friendlyName! : otherName; const list = extGrouped.get(key) ?? []; list.push(entry); extGrouped.set(key, list); } const extGroups = [...extGrouped.entries()].sort( ([a], [b]) => Number(a === otherName) - Number(b === otherName), ); const isExtGroupOpen = (name: string) => searching || openExtGroups.has(name); const toggleExtGroupOpen = (name: string) => setOpenExtGroups((current) => { const next = new Set(current); if (next.has(name)) next.delete(name); else next.add(name); return next; }); return (
} onClick={() => void workspace.scan()} disabled={workspace.scanning} > {t("Rescan", "重新扫描")} } />
{tabs.map((tab) => ( ))}
{workspace.error ? ( ) : null} {workspace.tab === "startup" ? (
{( [ ["all", "All", "全部"], ["enabled", "Enabled", "已启用"], ["disabled", "Disabled", "已禁用"], ["leftover", "Leftover", "残留"], ] as const ).map(([id, en, zh]) => ( ))}
{orderedGroups.map(([software, items]) => { const onCount = items.filter((item) => item.enabled && !isLeftover(item)).length; const allOn = onCount === items.length; const open = isGroupOpen(software); const groupIds = items.map((item) => item.id); const leftovers = items.filter(isLeftover); return (
0 && leftovers.length === items.length ? "is-leftover" : "" }`} >

toggleGroupOpen(software)}> {software} {leftovers.length > 0 ? ( {t(`${leftovers.length} leftover`, `${leftovers.length} 个残留`)} ) : null} {allOn ? t("All on", "全部开启") : onCount === 0 ? t("All off", "已全部关闭") : t( `${onCount}/${items.length} on`, `部分开启 ${onCount}/${items.length}`, )} event.stopPropagation()} > { event.stopPropagation(); toggleGroupOpen(software); }} > {items.length > 1 ? t(`${items.length} items`, `共${items.length}项`) : t("1 item", "共1项")} void workspace.toggleStartupGroup(groupIds, next)} label={`${software} master ${allOn ? "disable" : "enable"}`} />

{open ? (
{items.map((item) => { const [statusText, statusTone] = statusChip(item, t); const reveal = rowRevealPath(item); return (
{item.name}{" "} {statusText} {item.sourceLabel} · {scopeLabel(item.scope, t)} {item.command ? ` · ${shortPath(item.command, 72)}` : ""} {item.command || t("(no command line)", "(无命令行记录)")}
{isLeftover(item) ? ( ) : null} {reveal ? ( void api.revealPath(reveal).catch(() => {})} > ) : null} void workspace.toggleStartup(item.id, next)} label={`${item.name} ${item.enabled ? "disable" : "enable"}`} />
); })} {leftovers.length > 0 && leftovers.length < items.length ? (
{t( `${leftovers.length} leftover entries in this group — their programs are gone.`, `该组有 ${leftovers.length} 个残留项:程序已不存在,只剩启动配置。`, )}
) : null}
) : null}
); })} {startup.length === 0 ? (

{counts.all === 0 ? t("No matches.", "没有匹配的项目。") : t("Nothing here under this filter.", "当前筛选下没有项目。")}

) : null}
) : null} } title={t("Delete startup entries?", "删除启动项?")} message={t( `Squid removes only the autostart registration (${removingIds?.length ?? 0} item(s)); no program files are touched.`, `Squid 只删除启动配置(共 ${removingIds?.length ?? 0} 项),不会动任何程序文件。`, )} note={t( "Registry values are deleted; Startup-folder shortcuts are removed; tasks are unscheduled; leftover services are set to disabled.", "注册表值将被删除;启动文件夹的快捷方式会被移除;计划任务会被注销;残留服务会改为禁用。", )} confirmLabel={t("Delete", "删除")} cancelLabel={t("Cancel", "取消")} busy={workspace.busyId !== null} onClose={() => setRemovingIds(null)} onConfirm={async () => { if (!removingIds) return; await workspace.removeStartup(removingIds); setRemovingIds(null); }} /> {workspace.tab === "extensions" ? (
{extCategories.map((point) => { const entries = groupedExtensions[point]; const on = entries.filter((e) => e.registered).length; return ( ); })}
{extGroups.map(([software, items]) => { const onCount = items.filter((entry) => entry.registered).length; const allOn = onCount === items.length; const open = isExtGroupOpen(software); return (

toggleExtGroupOpen(software)}> {software} {allOn ? t("All on", "全部开启") : onCount === 0 ? t("All off", "已全部关闭") : t( `${onCount}/${items.length} on`, `部分开启 ${onCount}/${items.length}`, )} event.stopPropagation()} > { event.stopPropagation(); toggleExtGroupOpen(software); }} > {items.length > 1 ? t( `${items.length} places`, `共${items.length}个菜单位置`, ) : t("1 place", "1个菜单位置")} { for (const entry of items) { if (entry.registered !== next) { await workspace.toggleExtension(entry.id, next); } } }} label={`${software} master ${allOn ? "disable" : "enable"}`} />

{open ? (
{items.map((entry) => (
{entry.friendlyName ?? entry.name} {entry.publisher ? `${t("Creator", "创建者")} ${entry.publisher} · ` : ""} {scopeLabel(entry.scope, t)} {entry.module ? ` · ${shortPath(entry.module, 56)}` : ""} {!entry.registered ? ` · ${t("removed", "已移除")}` : ""}
void workspace.toggleExtension(entry.id, next)} label={`${entry.name} ${entry.registered ? "remove" : "restore"}`} />
))}
) : null}
); })} {extCategories.length === 0 ? (

{t("No matches.", "没有匹配的项目。")}

) : null}
) : null} {workspace.tab === "advanced" ? (
{t( "Advanced rows can break boot or logon if changed carelessly — Squid only displays them here, every change records its original value, and nothing is auto-disabled.", "进阶区的项目改错了可能影响系统行为——默认只展示不勾选,每一项带数字签名与位置说明;改动会原样记录,可逐项恢复。", )}
{workspace.advancedScanning && !workspace.advanced ? (
{t( "Reading Winlogon / IFEO / BHO and verifying signatures…", "正在读取 Winlogon / IFEO / BHO 并核验数字签名…", )}
) : null} {workspace.advanced && workspace.advanced.entries.length === 0 ? (

{t( "No third-party Winlogon hooks, AppInit DLLs, IFEO debuggers or BHOs found — the machine is clean.", "没有发现第三方 Winlogon 挂钩、AppInit DLL、IFEO 调试器或 BHO——系统很干净。", )}

) : null} {workspace.advanced ? (["winlogon", "appinit", "ifeo", "bho"] as const).map((kind) => { const rows = workspace.advanced!.entries.filter((entry) => entry.kind === kind); if (rows.length === 0) return null; const titles: Record = { winlogon: ["Winlogon hooks", "Winlogon 与登录链"], appinit: ["AppInit DLL injection", "AppInit_DLLs 全局注入"], ifeo: ["IFEO debuggers (can hijack any program)", "IFEO 调试器(可劫持任意程序启动)"], bho: ["Browser helper objects", "浏览器加载项 BHO"], }; return (
{rows.map((entry) => (
{entry.signature === "microsoft" ? t("Microsoft signed", "微软签名") : entry.signature === "vendor" ? t("Signed by", "已知厂商") : entry.signature === "unsigned" ? t("Not signed", "无数字签名") : entry.signature === "invalid" ? t("Signature invalid", "签名无效") : t("Signature n/a", "无签名信息")}
{entry.name} {shortPath(entry.command, 90)} {entry.location} {entry.warning ? ( {entry.warning} ) : null}
void workspace.toggleAdvanced(entry.id, next)} label={entry.name} />
))}
); }) : null} {workspace.advanced && workspace.advanced.extras.length > 0 ? (
{workspace.advanced.extras.map((entry) => (
{scopeLabel(entry.scope, t)}
{entry.displayName || entry.name} {shortPath(entry.command, 90)} {entry.sourceLabel}
void workspace.toggleStartup(entry.id, next)} label={entry.name} />
))}
) : null}
) : null} {workspace.tab === "popup" ? ( <>
void workspace.setWatcher(next)} label={t("Popup blocker switch", "弹窗拦截总开关")} /> } /> {workspace.recentClosed.length > 0 ? (
{workspace.recentClosed[0].process} · {workspace.recentClosed[0].title}
) : null}
{view.rules.map((rule, index) => (
{rule.title} {shortPath(rule.exe, 64)} ·{" "} {t(`closed ${rule.closed}×`, `已关闭 ${rule.closed} 次`)}
void workspace.toggleRule(index, next)} label={`${rule.title} ${rule.enabled ? "disable" : "enable"}`} /> void workspace.removeRule(index)} >
))} {view.rules.length === 0 ? (

{t( "No rules yet — pick a window below.", "还没有规则,先从下面的当前窗口里“拦截”一个。", )}

) : null}
} onClick={() => void workspace.refreshWindows()} > {t("Refresh", "刷新")} } />
{workspace.windows.map((window) => (
{window.title} {window.process}
void workspace.closeWindow(window.hwnd)} >
))}
) : null}
); }