|
| 1 | +import React, { useState, useEffect, useMemo } from 'react' |
| 2 | +import { Cpu, RefreshCw, XCircle, Search, AlertCircle, Activity, Loader2 } from 'lucide-react' |
| 3 | +import { cn, isPS5 } from '../../utils/helpers' |
| 4 | + |
| 5 | +const ActiveProcessesView = ({ ip, addToast, showConfirm }) => { |
| 6 | + const [processes, setProcesses] = useState([]) |
| 7 | + const [loading, setLoading] = useState(true) |
| 8 | + const [error, setError] = useState(false) |
| 9 | + const [showAll, setShowAll] = useState(false) |
| 10 | + const [search, setSearch] = useState('') |
| 11 | + |
| 12 | + const fetchProcesses = async (isBackground = false) => { |
| 13 | + if (!isBackground) setLoading(true) |
| 14 | + if (!isBackground) setError(false) |
| 15 | + try { |
| 16 | + const res = await fetch('/processes_list') |
| 17 | + if (!res.ok) throw new Error() |
| 18 | + const data = await res.json() |
| 19 | + if (data && data.processes) { |
| 20 | + setProcesses(data.processes) |
| 21 | + } else { |
| 22 | + setProcesses([]) |
| 23 | + } |
| 24 | + } catch { |
| 25 | + if (!isBackground) setError(true) |
| 26 | + } finally { |
| 27 | + if (!isBackground) setLoading(false) |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + useEffect(() => { |
| 32 | + fetchProcesses() |
| 33 | + |
| 34 | + const intervalId = setInterval(() => { |
| 35 | + fetchProcesses(true) |
| 36 | + }, 15000) |
| 37 | + |
| 38 | + return () => clearInterval(intervalId) |
| 39 | + }, []) |
| 40 | + |
| 41 | + const filteredProcesses = useMemo(() => { |
| 42 | + let result = processes |
| 43 | + if (!showAll) { |
| 44 | + result = result.filter(p => p.is_daemon) |
| 45 | + } |
| 46 | + if (search.trim() !== '') { |
| 47 | + const q = search.toLowerCase() |
| 48 | + result = result.filter(p => p.name.toLowerCase().includes(q)) |
| 49 | + } |
| 50 | + return result |
| 51 | + }, [processes, showAll, search]) |
| 52 | + |
| 53 | + const handleKill = (p) => { |
| 54 | + if (p.name === 'pldmgr.elf' || p.name === 'elfldr.elf') { |
| 55 | + addToast(`Cannot kill ${p.name}`, "error") |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + showConfirm( |
| 60 | + "Kill Process", |
| 61 | + `Are you sure you want to kill ${p.name} (PID: ${p.pid})?`, |
| 62 | + async () => { |
| 63 | + try { |
| 64 | + const res = await fetch(`/process_kill?pid=${p.pid}`) |
| 65 | + if (res.ok) { |
| 66 | + addToast(`Successfully killed ${p.name}`) |
| 67 | + setTimeout(() => { |
| 68 | + fetchProcesses(true) |
| 69 | + }, 500) |
| 70 | + } else { |
| 71 | + addToast(`Failed to kill ${p.name}`, "error") |
| 72 | + } |
| 73 | + } catch (e) { |
| 74 | + addToast(`Error killing ${p.name}`, "error") |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + return ( |
| 81 | + <div className="space-y-12"> |
| 82 | + <div className="flex flex-col md:flex-row md:items-center justify-between gap-8"> |
| 83 | + <h2 className="text-4xl font-extrabold text-white tracking-tight"> |
| 84 | + Active <span className="text-ps-blue">Processes</span> |
| 85 | + </h2> |
| 86 | + <div className="flex items-center space-x-4"> |
| 87 | + <label className="flex items-center space-x-3 cursor-pointer group"> |
| 88 | + <span className="text-zinc-400 font-bold tracking-tight group-hover:text-white transition-colors">Show All System Processes</span> |
| 89 | + <div className="relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-ps-blue focus:ring-offset-2 focus:ring-offset-black bg-white/10 group-hover:bg-white/20"> |
| 90 | + <input |
| 91 | + type="checkbox" |
| 92 | + className="sr-only" |
| 93 | + checked={showAll} |
| 94 | + onChange={(e) => setShowAll(e.target.checked)} |
| 95 | + /> |
| 96 | + <span |
| 97 | + className={cn( |
| 98 | + "inline-block h-4 w-4 transform rounded-full bg-white transition-transform", |
| 99 | + showAll ? "translate-x-6 bg-ps-blue" : "translate-x-1" |
| 100 | + )} |
| 101 | + /> |
| 102 | + </div> |
| 103 | + </label> |
| 104 | + </div> |
| 105 | + </div> |
| 106 | + |
| 107 | + <section className="space-y-6"> |
| 108 | + {/* Search bar */} |
| 109 | + <div className="flex items-center bg-black/40 border border-white/10 rounded-2xl px-4 py-3 focus-within:border-ps-blue/50 transition-colors"> |
| 110 | + <Search className="w-5 h-5 text-zinc-500 mr-3" /> |
| 111 | + <input |
| 112 | + type="text" |
| 113 | + placeholder="Search processes by name..." |
| 114 | + value={search} |
| 115 | + onChange={(e) => setSearch(e.target.value)} |
| 116 | + className="bg-transparent border-none outline-none text-white w-full font-medium placeholder:text-zinc-600" |
| 117 | + /> |
| 118 | + </div> |
| 119 | + |
| 120 | + {loading && processes.length === 0 ? ( |
| 121 | + <div className="py-24 glass-panel rounded-ps-3xl border-white/5 flex flex-col items-center justify-center space-y-6"> |
| 122 | + <Loader2 className="w-16 h-16 text-ps-blue animate-spin" /> |
| 123 | + <p className="label-caps">Fetching process list...</p> |
| 124 | + </div> |
| 125 | + ) : error ? ( |
| 126 | + <div className="py-20 glass-card rounded-ps-3xl border-red-500/20 flex flex-col items-center justify-center space-y-6 bg-red-950/5"> |
| 127 | + <AlertCircle className="w-16 h-16 text-red-500 opacity-50" /> |
| 128 | + <div className="text-center"> |
| 129 | + <p className="text-xl font-bold text-white uppercase tracking-tight">Failed to load processes</p> |
| 130 | + </div> |
| 131 | + <button onClick={() => fetchProcesses()} className="px-8 py-3 bg-white/5 border border-white/10 hover:bg-white/10 text-white rounded-xl font-bold uppercase text-xs transition-all">Retry</button> |
| 132 | + </div> |
| 133 | + ) : filteredProcesses.length === 0 ? ( |
| 134 | + <div className="py-20 border-2 border-dashed border-white/5 rounded-ps-3xl flex flex-col items-center justify-center space-y-4 bg-white/[0.01]"> |
| 135 | + <Activity className="w-16 h-16 text-white/5" /> |
| 136 | + <p className="text-zinc-500 font-bold uppercase tracking-widest text-sm italic">No processes found</p> |
| 137 | + </div> |
| 138 | + ) : ( |
| 139 | + <div className="grid grid-cols-1 gap-4"> |
| 140 | + {filteredProcesses.map((p) => ( |
| 141 | + <div key={p.pid} className={cn( |
| 142 | + "glass-card p-4 md:p-6 rounded-2xl flex flex-row items-center justify-between gap-4 border-white/10 hover:border-ps-blue/20 transition-all bg-white/[0.01]" |
| 143 | + )}> |
| 144 | + <div className="flex items-center space-x-4 min-w-0 flex-1"> |
| 145 | + <div className={cn( |
| 146 | + "p-3 rounded-xl flex-shrink-0", |
| 147 | + p.is_daemon ? "bg-ps-blue/10 text-ps-blue" : "bg-white/5 text-zinc-400" |
| 148 | + )}> |
| 149 | + <Cpu className="w-6 h-6" /> |
| 150 | + </div> |
| 151 | + <div className="min-w-0 flex-1 space-y-1"> |
| 152 | + <h3 className="text-xl font-bold text-white truncate">{p.name}</h3> |
| 153 | + <div className="flex items-center space-x-4 text-xs font-mono text-zinc-500 uppercase tracking-wider"> |
| 154 | + <span>PID: <span className="text-zinc-300">{p.pid}</span></span> |
| 155 | + <span>MEM: <span className="text-zinc-300">{p.memory.toFixed(1)} MiB</span></span> |
| 156 | + </div> |
| 157 | + </div> |
| 158 | + </div> |
| 159 | + {p.is_daemon && p.name !== 'pldmgr.elf' && p.name !== 'elfldr.elf' && ( |
| 160 | + <button |
| 161 | + onClick={() => handleKill(p)} |
| 162 | + className="p-3 md:px-6 md:py-3 rounded-xl bg-red-950/20 text-red-500 border border-red-500/10 hover:bg-red-500 hover:text-white transition-all flex items-center justify-center space-x-2 shrink-0 group" |
| 163 | + title="Kill Process" |
| 164 | + > |
| 165 | + <XCircle className="w-5 h-5 group-hover:scale-110 transition-transform" /> |
| 166 | + <span className="hidden md:inline font-bold uppercase tracking-tight text-sm">Kill</span> |
| 167 | + </button> |
| 168 | + )} |
| 169 | + {(p.name === 'pldmgr.elf' || p.name === 'elfldr.elf') && ( |
| 170 | + <div className="p-3 md:px-6 md:py-3 rounded-xl bg-white/5 text-zinc-500 border border-white/5 flex items-center justify-center space-x-2 shrink-0 opacity-50 cursor-not-allowed" title="Cannot kill critical process"> |
| 171 | + <XCircle className="w-5 h-5" /> |
| 172 | + <span className="hidden md:inline font-bold uppercase tracking-tight text-sm">Kill</span> |
| 173 | + </div> |
| 174 | + )} |
| 175 | + </div> |
| 176 | + ))} |
| 177 | + </div> |
| 178 | + )} |
| 179 | + </section> |
| 180 | + </div> |
| 181 | + ) |
| 182 | +} |
| 183 | + |
| 184 | +export default ActiveProcessesView |
0 commit comments