fork/gdu

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

Daniel Mildefeat: add web UI (#617)d672bf4

ignore
3.2 KiB100 linesraw
1import { useMemo } from 'react';
2import type { Node } from '../types';
3import { computeSlices } from '../slices';
4import { formatSize, percent } from '../format';
5
6interface DonutChartProps {
7  children: Node[];
8  apparent: boolean;
9  useSIPrefix: boolean;
10  hoveredPath: string | null;
11  onHover: (path: string | null) => void;
12  onSelect: (node: Node) => void;
13}
14
15const SIZE = 240;
16const CENTER = SIZE / 2;
17const RADIUS = 90;
18const STROKE = 34;
19const CIRCUMFERENCE = 2 * Math.PI * RADIUS;
20
21export function DonutChart({
22  children,
23  apparent,
24  useSIPrefix,
25  hoveredPath,
26  onHover,
27  onSelect,
28}: DonutChartProps) {
29  const { slices, total } = useMemo(
30    () => computeSlices(children, apparent),
31    [children, apparent],
32  );
33
34  const hovered = slices.find((s) => s.node?.path === hoveredPath) ?? null;
35  const centerPrimary = hovered ? hovered.label : formatSize(total, useSIPrefix);
36  const centerSecondary = hovered
37    ? `${formatSize(hovered.value, useSIPrefix)} · ${percent(hovered.value, total).toFixed(1)}%`
38    : `${slices.length} item${slices.length === 1 ? '' : 's'}`;
39
40  let offsetFraction = 0;
41
42  return (
43    <div className="donut" role="img" aria-label="Directory size breakdown">
44      <svg viewBox={`0 0 ${SIZE} ${SIZE}`} width="100%" height="100%">
45        <g transform={`rotate(-90 ${CENTER} ${CENTER})`}>
46          <circle
47            cx={CENTER}
48            cy={CENTER}
49            r={RADIUS}
50            fill="none"
51            stroke="var(--track)"
52            strokeWidth={STROKE}
53          />
54          {total > 0 &&
55            slices.map((slice) => {
56              const fraction = slice.value / total;
57              const dash = fraction * CIRCUMFERENCE;
58              const dashOffset = -offsetFraction * CIRCUMFERENCE;
59              offsetFraction += fraction;
60
61              const key = slice.node ? slice.node.path : '__other__';
62              const isHovered = hoveredPath !== null && slice.node?.path === hoveredPath;
63              const dimmed = hoveredPath !== null && !isHovered;
64              const clickable = slice.node?.isDir ?? false;
65
66              return (
67                <circle
68                  key={key}
69                  className="donut-slice"
70                  cx={CENTER}
71                  cy={CENTER}
72                  r={RADIUS}
73                  fill="none"
74                  stroke={slice.color}
75                  strokeWidth={isHovered ? STROKE + 6 : STROKE}
76                  strokeDasharray={`${dash} ${CIRCUMFERENCE - dash}`}
77                  strokeDashoffset={dashOffset}
78                  opacity={dimmed ? 0.35 : 1}
79                  style={{ cursor: clickable ? 'pointer' : 'default' }}
80                  onMouseEnter={() => slice.node && onHover(slice.node.path)}
81                  onMouseLeave={() => onHover(null)}
82                  onClick={() => {
83                    if (slice.node && slice.node.isDir) {
84                      onSelect(slice.node);
85                    }
86                  }}
87                />
88              );
89            })}
90        </g>
91      </svg>
92      <div className="donut-center">
93        <div className="donut-center-primary" title={centerPrimary}>
94          {centerPrimary}
95        </div>
96        <div className="donut-center-secondary">{centerSecondary}</div>
97      </div>
98    </div>
99  );
100}