-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessTable.tsx
More file actions
173 lines (160 loc) · 5.82 KB
/
ProcessTable.tsx
File metadata and controls
173 lines (160 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { useState, useMemo } from 'react';
import type { ProcessInfo, SortField, SortDirection } from '../types';
interface ProcessTableProps {
processes: ProcessInfo[];
filter: string;
}
export function ProcessTable({ processes, filter }: ProcessTableProps) {
const [sortField, setSortField] = useState<SortField>('cpu');
const [sortDirection, setSortDirection] = useState<SortDirection>('desc');
const [selectedPid, setSelectedPid] = useState<number | null>(null);
const handleSort = (field: SortField) => {
if (sortField === field) {
setSortDirection(sortDirection === 'asc' ? 'desc' : 'asc');
} else {
setSortField(field);
setSortDirection('desc');
}
};
const filteredAndSortedProcesses = useMemo(() => {
let filtered = processes;
if (filter) {
const lowerFilter = filter.toLowerCase();
filtered = processes.filter(
p =>
p.command.toLowerCase().includes(lowerFilter) ||
p.user.toLowerCase().includes(lowerFilter) ||
p.pid.toString().includes(filter)
);
}
return [...filtered].sort((a, b) => {
let aVal: number | string;
let bVal: number | string;
switch (sortField) {
case 'pid':
aVal = a.pid;
bVal = b.pid;
break;
case 'user':
aVal = a.user;
bVal = b.user;
break;
case 'cpu':
aVal = a.cpu;
bVal = b.cpu;
break;
case 'mem':
aVal = a.mem;
bVal = b.mem;
break;
case 'command':
aVal = a.command;
bVal = b.command;
break;
default:
return 0;
}
if (typeof aVal === 'string' && typeof bVal === 'string') {
return sortDirection === 'asc'
? aVal.localeCompare(bVal)
: bVal.localeCompare(aVal);
}
return sortDirection === 'asc'
? (aVal as number) - (bVal as number)
: (bVal as number) - (aVal as number);
});
}, [processes, filter, sortField, sortDirection]);
const getSortIndicator = (field: SortField): string => {
if (sortField !== field) return ' ';
return sortDirection === 'asc' ? '▲' : '▼';
};
const getCpuColor = (cpu: number): string => {
if (cpu < 10) return 'var(--color-text)';
if (cpu < 30) return 'var(--color-green)';
if (cpu < 60) return 'var(--color-yellow)';
if (cpu < 85) return 'var(--color-orange)';
return 'var(--color-red)';
};
const getMemColor = (mem: number): string => {
if (mem < 5) return 'var(--color-text)';
if (mem < 15) return 'var(--color-green)';
if (mem < 30) return 'var(--color-yellow)';
if (mem < 50) return 'var(--color-orange)';
return 'var(--color-red)';
};
return (
<div className="process-table" role="table" aria-label="Process list">
<div className="table-header" role="row">
<span
role="columnheader"
className="col-pid sortable"
onClick={() => handleSort('pid')}
aria-sort={sortField === 'pid' ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none'}
>
PID{getSortIndicator('pid')}
</span>
<span
role="columnheader"
className="col-user sortable"
onClick={() => handleSort('user')}
aria-sort={sortField === 'user' ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none'}
>
USER{getSortIndicator('user')}
</span>
<span
role="columnheader"
className="col-cpu sortable"
onClick={() => handleSort('cpu')}
aria-sort={sortField === 'cpu' ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none'}
>
CPU%{getSortIndicator('cpu')}
</span>
<span
role="columnheader"
className="col-mem sortable"
onClick={() => handleSort('mem')}
aria-sort={sortField === 'mem' ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none'}
>
MEM%{getSortIndicator('mem')}
</span>
<span role="columnheader" className="col-virt">VIRT</span>
<span role="columnheader" className="col-res">RES</span>
<span role="columnheader" className="col-state">S</span>
<span role="columnheader" className="col-time">TIME</span>
<span
role="columnheader"
className="col-command sortable"
onClick={() => handleSort('command')}
aria-sort={sortField === 'command' ? (sortDirection === 'asc' ? 'ascending' : 'descending') : 'none'}
>
COMMAND{getSortIndicator('command')}
</span>
</div>
<div className="table-body">
{filteredAndSortedProcesses.map((process) => (
<div
key={process.pid}
role="row"
className={`table-row ${selectedPid === process.pid ? 'selected' : ''}`}
onClick={() => setSelectedPid(process.pid === selectedPid ? null : process.pid)}
aria-selected={selectedPid === process.pid}
>
<span className="col-pid">{process.pid}</span>
<span className="col-user">{process.user.substring(0, 8)}</span>
<span className="col-cpu" style={{ color: getCpuColor(process.cpu) }}>
{process.cpu.toFixed(1).padStart(5, ' ')}
</span>
<span className="col-mem" style={{ color: getMemColor(process.mem) }}>
{process.mem.toFixed(1).padStart(5, ' ')}
</span>
<span className="col-virt">{process.vsz}</span>
<span className="col-res">{process.rss}</span>
<span className="col-state">{process.stat.charAt(0)}</span>
<span className="col-time">{process.time}</span>
<span className="col-command">{process.command}</span>
</div>
))}
</div>
</div>
);
}