fork/gdu
git clone https://git.julia.blue/fork/gdu
d672bf4
ignore
1import { useMemo } from 'react' ; 2import type { Node } from '../types' ; 3import { computeSlices } from '../slices' ; 4import { formatSize , percent } from '../format' ; 5 6interface DonutChartProps { 7children :Node []; 8apparent :boolean ; 9useSIPrefix :boolean ; 10hoveredPath :string | null ; 11onHover :( path :string | null ) => void ; 12onSelect :( 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 ) { 29const { slices, total} = useMemo ( 30() => computeSlices ( children , apparent ), 31[ children , apparent ], 32); 33 34const hovered = slices . find (( s ) => s . node ?. path === hoveredPath ) ?? null ; 35const centerPrimary = hovered ?hovered . label :formatSize ( total , useSIPrefix ); 36const centerSecondary = hovered 37 ?` ${ formatSize ( hovered . value , useSIPrefix )} · ${ percent ( hovered . value , total ). toFixed ( 1 )} %` 38 :` ${ slices . length } item ${ slices . length === 1 ? '' : 's' } ` ; 39 40let offsetFraction = 0 ; 41 42return ( 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 47cx = { CENTER } 48cy = { CENTER } 49r = { RADIUS } 50fill = "none" 51stroke = "var(--track)" 52strokeWidth = { STROKE } 53/> 54{ total > 0 && 55slices . map (( slice ) => { 56const fraction = slice . value / total ; 57const dash = fraction * CIRCUMFERENCE ; 58const dashOffset = - offsetFraction * CIRCUMFERENCE ; 59offsetFraction += fraction ; 60 61const key = slice . node ?slice . node . path :'__other__' ; 62const isHovered = hoveredPath !== null && slice . node ?. path === hoveredPath ; 63const dimmed = hoveredPath !== null && ! isHovered ; 64const clickable = slice . node ?. isDir ?? false ; 65 66return ( 67< circle 68key = { key } 69className = "donut-slice" 70cx = { CENTER } 71cy = { CENTER } 72r = { RADIUS } 73fill = "none" 74stroke = { slice . color } 75strokeWidth = { isHovered ?STROKE + 6 :STROKE } 76strokeDasharray = { ` ${ dash } ${ CIRCUMFERENCE - dash } ` } 77strokeDashoffset = { dashOffset } 78opacity = { dimmed ?0.35 :1 } 79style = {{ cursor :clickable ?'pointer' :'default' }} 80onMouseEnter = {() => slice . node && onHover ( slice . node . path )} 81onMouseLeave = {() => onHover ( null )} 82onClick = {() => { 83if ( slice . node && slice . node . isDir ) { 84onSelect ( 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}