import { useCallback, useEffect, useMemo, useState } from 'react'; import type { Node, NodeResponse, SortKey, SortOrder, Status } from './types'; import { fetchNode, fetchStatus, subscribeStatus } from './api'; import { colorMapFor, computeSlices } from './slices'; import { formatSize } from './format'; import { DonutChart } from './components/DonutChart'; import { FileTable } from './components/FileTable'; import { Breadcrumbs } from './components/Breadcrumbs'; import { ProgressBar } from './components/ProgressBar'; export function App() { const [status, setStatus] = useState(null); const [currentPath, setCurrentPath] = useState(null); const [nodeResp, setNodeResp] = useState(null); const [sort, setSort] = useState('size'); const [order, setOrder] = useState('desc'); const [apparent, setApparent] = useState(null); const [hoveredPath, setHoveredPath] = useState(null); const [loadError, setLoadError] = useState(null); // Initial status + live updates over SSE. useEffect(() => { fetchStatus().then(setStatus).catch(() => undefined); return subscribeStatus(setStatus); }, []); // Adopt the display default for size metric once, from the server flags. useEffect(() => { if (status && apparent === null) { setApparent(status.showApparentSize); } }, [status, apparent]); // Once a scan is done, default into the root directory. useEffect(() => { if (status?.state === 'done' && currentPath === null && status.rootPath) { setCurrentPath(status.rootPath); } }, [status, currentPath]); // Load the current node whenever the path, sort, or scan completion changes. useEffect(() => { if (currentPath === null) { return; } let cancelled = false; fetchNode(currentPath, sort, order) .then((resp) => { if (!cancelled) { setNodeResp(resp); setLoadError(null); } }) .catch((err: unknown) => { if (!cancelled) { setLoadError(err instanceof Error ? err.message : String(err)); } }); return () => { cancelled = true; }; }, [currentPath, sort, order, status?.state]); const effectiveApparent = apparent ?? status?.showApparentSize ?? false; const useSIPrefix = status?.useSIPrefix ?? false; const children = nodeResp?.children ?? []; const { total } = useMemo( () => computeSlices(children, effectiveApparent), [children, effectiveApparent], ); const colorMap = useMemo( () => colorMapFor(children, effectiveApparent), [children, effectiveApparent], ); const handleSortChange = useCallback( (key: SortKey) => { if (key === sort) { setOrder((o) => (o === 'asc' ? 'desc' : 'asc')); } else { setSort(key); setOrder(key === 'name' ? 'asc' : 'desc'); } }, [sort], ); const handleSelect = useCallback((node: Node) => { if (node.isDir) { setCurrentPath(node.path); setHoveredPath(null); } }, []); const handleNavigate = useCallback((node: Node) => { setCurrentPath(node.path); setHoveredPath(null); }, []); if (!status) { return
Connecting…
; } if (status.state === 'error') { return (

Scan failed

{status.error ?? 'Unknown error'}

); } if (currentPath === null || (status.state === 'scanning' && !nodeResp)) { return ; } return (
gdu {nodeResp && ( )}
{formatSize(total, useSIPrefix)} {status.state === 'scanning' && scanning…}
{loadError &&
{loadError}
}
); }