import { useMemo, useState } from 'react'; import { motion } from 'motion/react'; import { AlertCircle, CheckCircle2, Cpu, Eye, Loader2, RefreshCw, Unplug } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { ConfirmDialog } from '@/components/confirm-dialog'; import { ProcessProbeDialog } from '@/components/process-probe-dialog'; import { SkeletonSwap } from '@/components/interior/skeleton-swap'; import { cn } from '@/lib/utils'; import type { HookProcessInfo } from '@/types'; import { useAppState } from '@/contexts/AppStateContext'; import { useLayout } from '@/contexts/LayoutContext'; import { useTheme } from '@/contexts/ThemeContext'; const SORT_OPTIONS: { id: string; label: string }[] = [ { id: 'pid', label: 'PID' }, { id: 'name', label: '名称' }, { id: 'status', label: '状态' }, ]; // Online-first, then by how actionable the state is. const STATUS_ORDER: Record = { online: 0, loaded: 1, connecting: 2, loading: 3, available: 4, disconnected: 5, error: 6, }; const processStatusLabel: Record = { available: '可加载', loading: '加载中', connecting: '等待连接', loaded: '等待登录', online: '已在线', error: '错误', disconnected: '已断开', }; function processBadgeVariant(status: HookProcessInfo['status']) { if (status === 'online') return 'success' as const; if (status === 'error') return 'destructive' as const; if (status === 'disconnected') return 'destructive' as const; if (status === 'loading' || status === 'connecting' || status === 'loaded') return 'default' as const; return 'secondary' as const; } /** * Process / injection control surface. Split out of the overview page so the * homepage stays a read-only monitoring dashboard. State (processList, * processOps) is owned by AppLayout and read from context, so this page needs * no plumbing of its own; the layout keeps polling and the unload-failed alert * regardless of which page is mounted. */ export function ProcessesPage() { const { processList, processOps, refreshProcesses, resources } = useAppState(); const { statusOf, banner: processActionStatus, load, unload, refresh } = processOps; const { pages, setPages } = useLayout(); const off = useTheme().appearance.disableMotion; const [confirm, setConfirm] = useState<{ kind: 'load' | 'unload'; pid: number; name: string } | null>(null); const [probeDialog, setProbeDialog] = useState<{ pid: number; name: string } | null>(null); const sortKey = pages.processesSort; const sorted = useMemo(() => { const arr = [...processList]; if (sortKey === 'name') arr.sort((a, b) => (a.name || '').localeCompare(b.name || '')); else if (sortKey === 'status') arr.sort((a, b) => (STATUS_ORDER[a.status] ?? 9) - (STATUS_ORDER[b.status] ?? 9) || a.pid - b.pid); else arr.sort((a, b) => a.pid - b.pid); return arr; }, [processList, sortKey]); return (
进程注入 加载 SnowLuma 后会监听登录状态,登录后自动接入 OneBot 流程
{processList.length > 1 && (
{SORT_OPTIONS.map((o) => { const active = sortKey === o.id; return ( ); })}
)}
{processActionStatus && ( {processActionStatus} )} {resources.processList.error && processList.length > 0 && (
{resources.processList.error};当前显示上次成功读取的进程状态。
)} {resources.processList.ready ? ( resources.processList.error && processList.length === 0 ? (

{resources.processList.error}

) : processList.length === 0 ? (

未检测到可加载 QQ 主进程

请确认 QQ 已启动后点击右上角刷新

) : (
{sorted.map((proc, idx) => { 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 // can re-check the pipe and trigger a reconnect on demand). const showRefresh = proc.injected || proc.status === 'connecting' || proc.status === 'disconnected'; return ( {/* icon + info stay horizontal; on phones the actions drop to a second row so name / PID / path get the full width instead of collapsing to "Q..". */}
{isOnline ? ( ) : proc.status === 'error' ? ( ) : ( )}
{proc.name || 'QQ.exe'} {processStatusLabel[proc.status]}
PID {proc.pid} {proc.uin && proc.uin !== '0' ? ` · UIN ${proc.uin}` : ''}
{proc.path && (
{proc.path}
)} {proc.error && (
{proc.error}
)}
{showRefresh && ( )}
); })}
) ) : null}
!o && setConfirm(null)} title={confirm?.kind === 'unload' ? '确认卸载 SnowLuma?' : '确认加载 SnowLuma?'} description={ confirm ? confirm.kind === 'unload' ? `将从进程 ${confirm.name} 卸载 SnowLuma 注入,可能导致当前会话断开。` : `将向进程 ${confirm.name} 注入 SnowLuma,并开始监听登录状态。` : '' } confirmText={confirm?.kind === 'unload' ? '卸载' : '加载'} destructive={confirm?.kind === 'unload'} onConfirm={async () => { if (!confirm) return; if (confirm.kind === 'unload') await unload(confirm.pid); else await load(confirm.pid); }} /> {probeDialog && ( !open && setProbeDialog(null)} onLoad={() => { if (probeDialog) { setConfirm({ kind: 'load', pid: probeDialog.pid, name: probeDialog.name }); setProbeDialog(null); } }} /> )}
); }