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
@@ -0,0 +1,156 @@
|
||||
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
|
||||
import { useApi, type ProcessActionResult } from '@/lib/api';
|
||||
|
||||
export type HookProcessOpKind = 'load' | 'unload' | 'refresh';
|
||||
|
||||
export interface HookProcessOps {
|
||||
/** Returns the in-flight op kind for a pid, or null if idle. */
|
||||
statusOf(pid: number): HookProcessOpKind | null;
|
||||
/** Latest user-facing status; auto-clears a few seconds after the last update. */
|
||||
banner: string;
|
||||
load(pid: number): Promise<void>;
|
||||
unload(pid: number): Promise<void>;
|
||||
refresh(pid: number): Promise<void>;
|
||||
}
|
||||
|
||||
export interface UnloadFailedAlert {
|
||||
pid: number;
|
||||
error: string;
|
||||
}
|
||||
|
||||
export interface UseHookProcessOpsResult {
|
||||
ops: HookProcessOps;
|
||||
unloadFailedAlert: UnloadFailedAlert | null;
|
||||
dismissUnloadFailedAlert: () => void;
|
||||
}
|
||||
|
||||
interface KindMessages {
|
||||
start(pid: number): string;
|
||||
ok(pid: number): string;
|
||||
fail(message: string): string;
|
||||
}
|
||||
|
||||
const KIND_MESSAGES: Record<HookProcessOpKind, KindMessages> = {
|
||||
load: {
|
||||
start: (pid) => `正在向进程 ${pid} 加载 SnowLuma…`,
|
||||
ok: (pid) => `已向进程 ${pid} 注入 SnowLuma,等待管道连接…`,
|
||||
fail: (m) => `加载失败:${m}`,
|
||||
},
|
||||
unload: {
|
||||
start: (pid) => `正在从进程 ${pid} 卸载…`,
|
||||
ok: (pid) => `已从进程 ${pid} 卸载`,
|
||||
fail: (m) => `卸载失败:${m}`,
|
||||
},
|
||||
refresh: {
|
||||
start: (pid) => `正在刷新进程 ${pid} 的管道状态…`,
|
||||
ok: (pid) => `已刷新进程 ${pid} 的管道状态`,
|
||||
fail: (m) => `刷新失败:${m}`,
|
||||
},
|
||||
};
|
||||
|
||||
const BANNER_CLEAR_MS = 4000;
|
||||
|
||||
/**
|
||||
* Drives load/unload/refresh actions on Hook-target processes. Owns the
|
||||
* per-pid inflight dedup, the per-kind status indicator, the banner message
|
||||
* lifecycle, and the "unload silently failed" alert that surfaces when the
|
||||
* backend reports the named pipe is still alive after an unload attempt.
|
||||
*/
|
||||
export function useHookProcessOps(
|
||||
opts: { onAfterOp?: () => Promise<void> | void } = {},
|
||||
): UseHookProcessOpsResult {
|
||||
const api = useApi();
|
||||
const [byPid, setByPid] = useState<Map<number, HookProcessOpKind>>(() => new Map());
|
||||
const [banner, setBanner] = useState('');
|
||||
const [unloadFailedAlert, setUnloadFailedAlert] = useState<UnloadFailedAlert | null>(null);
|
||||
|
||||
// Synchronous inflight guard — protects against rapid double-clicks that
|
||||
// would race the byPid setter (state updates are batched, refs are not).
|
||||
const inflightRef = useRef<Set<number>>(new Set());
|
||||
const bannerTimerRef = useRef<number | null>(null);
|
||||
const onAfterOpRef = useRef(opts.onAfterOp);
|
||||
onAfterOpRef.current = opts.onAfterOp;
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
if (bannerTimerRef.current != null) window.clearTimeout(bannerTimerRef.current);
|
||||
},
|
||||
[],
|
||||
);
|
||||
|
||||
const scheduleBannerClear = useCallback(() => {
|
||||
if (bannerTimerRef.current != null) window.clearTimeout(bannerTimerRef.current);
|
||||
bannerTimerRef.current = window.setTimeout(() => {
|
||||
setBanner('');
|
||||
bannerTimerRef.current = null;
|
||||
}, BANNER_CLEAR_MS);
|
||||
}, []);
|
||||
|
||||
const setKind = useCallback((pid: number, kind: HookProcessOpKind | null) => {
|
||||
setByPid((prev) => {
|
||||
const next = new Map(prev);
|
||||
if (kind == null) next.delete(pid);
|
||||
else next.set(pid, kind);
|
||||
return next;
|
||||
});
|
||||
}, []);
|
||||
|
||||
const run = useCallback(
|
||||
async (
|
||||
pid: number,
|
||||
kind: HookProcessOpKind,
|
||||
action: () => Promise<ProcessActionResult>,
|
||||
) => {
|
||||
if (inflightRef.current.has(pid)) return;
|
||||
inflightRef.current.add(pid);
|
||||
setKind(pid, kind);
|
||||
const messages = KIND_MESSAGES[kind];
|
||||
setBanner(messages.start(pid));
|
||||
try {
|
||||
const result = await action();
|
||||
if (kind === 'unload' && result.process?.status === 'connecting' && result.process.error) {
|
||||
setUnloadFailedAlert({ pid, error: result.process.error });
|
||||
setBanner(`进程 ${pid} 卸载失败`);
|
||||
} else {
|
||||
setBanner(messages.ok(pid));
|
||||
}
|
||||
await onAfterOpRef.current?.();
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : '未知错误';
|
||||
setBanner(messages.fail(msg));
|
||||
} finally {
|
||||
inflightRef.current.delete(pid);
|
||||
setKind(pid, null);
|
||||
scheduleBannerClear();
|
||||
}
|
||||
},
|
||||
[setKind, scheduleBannerClear],
|
||||
);
|
||||
|
||||
const load = useCallback(
|
||||
(pid: number) => run(pid, 'load', () => api.processes.load(pid)),
|
||||
[api, run],
|
||||
);
|
||||
const unload = useCallback(
|
||||
(pid: number) => run(pid, 'unload', () => api.processes.unload(pid)),
|
||||
[api, run],
|
||||
);
|
||||
const refresh = useCallback(
|
||||
(pid: number) => run(pid, 'refresh', () => api.processes.refresh(pid)),
|
||||
[api, run],
|
||||
);
|
||||
|
||||
const statusOf = useCallback(
|
||||
(pid: number): HookProcessOpKind | null => byPid.get(pid) ?? null,
|
||||
[byPid],
|
||||
);
|
||||
|
||||
const dismissUnloadFailedAlert = useCallback(() => setUnloadFailedAlert(null), []);
|
||||
|
||||
const ops = useMemo<HookProcessOps>(
|
||||
() => ({ statusOf, banner, load, unload, refresh }),
|
||||
[statusOf, banner, load, unload, refresh],
|
||||
);
|
||||
|
||||
return { ops, unloadFailedAlert, dismissUnloadFailedAlert };
|
||||
}
|
||||
Reference in new issue
Block a user