fork/gdu

git clone https://git.julia.blue/fork/gdu

Daniel Mildefeat: add web UI (#617)d672bf4

subvolume
3.4 KiB112 linesraw
1import { useMemo } from 'react';
2import type { Node, SortKey, SortOrder } from '../types';
3import { metricValue } from '../slices';
4import { formatCount, formatMtime, formatSize, percent } from '../format';
5
6interface FileTableProps {
7  children: Node[];
8  colorMap: Map<string, string>;
9  apparent: boolean;
10  useSIPrefix: boolean;
11  total: number;
12  sort: SortKey;
13  order: SortOrder;
14  onSortChange: (key: SortKey) => void;
15  hoveredPath: string | null;
16  onHover: (path: string | null) => void;
17  onSelect: (node: Node) => void;
18}
19
20const COLUMNS: { key: SortKey; label: string; numeric: boolean }[] = [
21  { key: 'name', label: 'Name', numeric: false },
22  { key: 'size', label: 'Size', numeric: true },
23  { key: 'itemCount', label: 'Items', numeric: true },
24  { key: 'mtime', label: 'Modified', numeric: true },
25];
26
27export function FileTable({
28  children,
29  colorMap,
30  apparent,
31  useSIPrefix,
32  total,
33  sort,
34  order,
35  onSortChange,
36  hoveredPath,
37  onHover,
38  onSelect,
39}: FileTableProps) {
40  const maxValue = useMemo(
41    () => children.reduce((max, n) => Math.max(max, metricValue(n, apparent)), 0),
42    [children, apparent],
43  );
44
45  return (
46    <table className="file-table">
47      <thead>
48        <tr>
49          {COLUMNS.map((col) => (
50            <th
51              key={col.key}
52              className={col.numeric ? 'num' : ''}
53              onClick={() => onSortChange(col.key)}
54            >
55              {col.label}
56              {sort === col.key && (
57                <span className="sort-arrow">{order === 'asc' ? ' ▲' : ' ▼'}</span>
58              )}
59            </th>
60          ))}
61        </tr>
62      </thead>
63      <tbody>
64        {children.map((node) => {
65          const value = metricValue(node, apparent);
66          const barWidth = maxValue > 0 ? (value / maxValue) * 100 : 0;
67          const color = colorMap.get(node.path) ?? 'var(--other)';
68          const isHovered = node.path === hoveredPath;
69
70          return (
71            <tr
72              key={node.path}
73              className={isHovered ? 'hovered' : ''}
74              onMouseEnter={() => onHover(node.path)}
75              onMouseLeave={() => onHover(null)}
76              onClick={() => node.isDir && onSelect(node)}
77              style={{ cursor: node.isDir ? 'pointer' : 'default' }}
78            >
79              <td className="name-cell">
80                <span className="swatch" style={{ backgroundColor: color }} />
81                <span className="name">
82                  {node.isDir ? '📁' : '📄'} {node.name}
83                  {node.flag === '!' && <span className="flag-error" title="Access error"> !</span>}
84                </span>
85                <span className="bar" style={{ width: `${barWidth}%`, backgroundColor: color }} />
86              </td>
87              <td className="num">{formatSize(value, useSIPrefix)}</td>
88              <td className="num">{formatCount(node.itemCount)}</td>
89              <td className="num muted">{formatMtime(node.mtime)}</td>
90            </tr>
91          );
92        })}
93        {children.length === 0 && (
94          <tr>
95            <td colSpan={4} className="empty">
96              Empty directory
97            </td>
98          </tr>
99        )}
100      </tbody>
101      <tfoot>
102        <tr>
103          <td className="muted">{children.length} items</td>
104          <td className="num">{formatSize(total, useSIPrefix)}</td>
105          <td className="num muted" colSpan={2}>
106            {percent(total, total) > 0 ? '100%' : ''}
107          </td>
108        </tr>
109      </tfoot>
110    </table>
111  );
112}