refactor(webui): deepen Hook process ops behind a single seam
App.tsx held three copy-pasted handlers (load / unload / refresh) and three parallel processXxxPid state slots that were mutually exclusive but unenforced by the type system. The unload-failed alert was a separate path, and OverviewPage needed 11 props to drive the table. Extract useHookProcessOps: one inflight Set for dedup, one Map<pid, OpKind> for view state, one run() that branches on kind via a message table, and ownership of the unload-failed alert. OverviewPage takes a single HookProcessOps prop and queries statusOf(pid). App.tsx 270 → 216 LOC; OverviewPage props 11 → 7.
This commit is contained in:
3 files changed
+177
-108
No files matched your search
@@ -23,21 +23,16 @@ import { Skeleton } from '@/components/ui/skeleton';
|
||||
import { ConfirmDialog } from '@/components/confirm-dialog';
|
||||
import { cn, formatBytes, formatUptime } from '@/lib/utils';
|
||||
import type { HookProcessInfo, QQInfo, SystemInfo } from '@/types';
|
||||
import type { HookProcessOps } from '@/hooks/use-hook-process-ops';
|
||||
|
||||
interface OverviewPageProps {
|
||||
qqList: QQInfo[];
|
||||
status: string;
|
||||
processList: HookProcessInfo[];
|
||||
processLoadingPid: number | null;
|
||||
processUnloadingPid: number | null;
|
||||
processRefreshingPid: number | null;
|
||||
processActionStatus: string;
|
||||
systemInfo: SystemInfo | null;
|
||||
onRefreshProcesses: () => void;
|
||||
onRefreshSystem: () => void;
|
||||
onLoadProcess: (pid: number) => Promise<void> | void;
|
||||
onUnloadProcess: (pid: number) => Promise<void> | void;
|
||||
onRefreshProcess: (pid: number) => Promise<void> | void;
|
||||
processOps: HookProcessOps;
|
||||
}
|
||||
|
||||
const processStatusLabel: Record<HookProcessInfo['status'], string> = {
|
||||
@@ -100,17 +95,12 @@ export function OverviewPage({
|
||||
qqList,
|
||||
status,
|
||||
processList,
|
||||
processLoadingPid,
|
||||
processUnloadingPid,
|
||||
processRefreshingPid,
|
||||
processActionStatus,
|
||||
systemInfo,
|
||||
onRefreshProcesses,
|
||||
onRefreshSystem,
|
||||
onLoadProcess,
|
||||
onUnloadProcess,
|
||||
onRefreshProcess,
|
||||
processOps,
|
||||
}: OverviewPageProps) {
|
||||
const { statusOf, banner: processActionStatus, load, unload, refresh } = processOps;
|
||||
const [confirm, setConfirm] = useState<
|
||||
| { kind: 'load' | 'unload'; pid: number; name: string }
|
||||
| null
|
||||
@@ -292,10 +282,11 @@ export function OverviewPage({
|
||||
) : (
|
||||
<div className="grid grid-cols-1 gap-3 xl:grid-cols-2">
|
||||
{processList.map((proc, idx) => {
|
||||
const loading = processLoadingPid === proc.pid || proc.status === 'loading';
|
||||
const unloading = processUnloadingPid === proc.pid;
|
||||
const refreshing = processRefreshingPid === proc.pid;
|
||||
const busy = loading || unloading || refreshing;
|
||||
const op = statusOf(proc.pid);
|
||||
const loading = op === 'load' || proc.status === 'loading';
|
||||
const unloading = op === 'unload';
|
||||
const refreshing = op === 'refresh';
|
||||
const busy = op != null || proc.status === 'loading';
|
||||
const isOnline = proc.status === 'online';
|
||||
const canUnload = proc.injected;
|
||||
// Refresh is meaningful whenever a hook may exist (so the user
|
||||
@@ -355,7 +346,7 @@ export function OverviewPage({
|
||||
disabled={busy}
|
||||
aria-label={`刷新进程 ${proc.pid} 管道状态`}
|
||||
title="刷新管道状态 / 重连"
|
||||
onClick={() => onRefreshProcess(proc.pid)}
|
||||
onClick={() => refresh(proc.pid)}
|
||||
className="size-8 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{refreshing ? (
|
||||
@@ -449,8 +440,8 @@ export function OverviewPage({
|
||||
destructive={confirm?.kind === 'unload'}
|
||||
onConfirm={async () => {
|
||||
if (!confirm) return;
|
||||
if (confirm.kind === 'unload') await onUnloadProcess(confirm.pid);
|
||||
else await onLoadProcess(confirm.pid);
|
||||
if (confirm.kind === 'unload') await unload(confirm.pid);
|
||||
else await load(confirm.pid);
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user