fork/gdu
git clone https://git.julia.blue/fork/gdu
d672bf4
subvolume
1import { useCallback , useEffect , useMemo , useState } from 'react' ; 2import type { Node , NodeResponse , SortKey , SortOrder , Status } from './types' ; 3import { fetchNode , fetchStatus , subscribeStatus } from './api' ; 4import { colorMapFor , computeSlices } from './slices' ; 5import { formatSize } from './format' ; 6import { DonutChart } from './components/DonutChart' ; 7import { FileTable } from './components/FileTable' ; 8import { Breadcrumbs } from './components/Breadcrumbs' ; 9import { ProgressBar } from './components/ProgressBar' ; 10 11export function App () { 12const [ status , setStatus ] = useState < Status | null >( null ); 13const [ currentPath , setCurrentPath ] = useState < string | null >( null ); 14const [ nodeResp , setNodeResp ] = useState < NodeResponse | null >( null ); 15const [ sort , setSort ] = useState < SortKey >( 'size' ); 16const [ order , setOrder ] = useState < SortOrder >( 'desc' ); 17const [ apparent , setApparent ] = useState < boolean | null >( null ); 18const [ hoveredPath , setHoveredPath ] = useState < string | null >( null ); 19const [ loadError , setLoadError ] = useState < string | null >( null ); 20 21// Initial status + live updates over SSE. 22useEffect (() => { 23fetchStatus (). then ( setStatus ). catch (() => undefined ); 24return subscribeStatus ( setStatus ); 25}, []); 26 27// Adopt the display default for size metric once, from the server flags. 28useEffect (() => { 29if ( status && apparent === null ) { 30setApparent ( status . showApparentSize ); 31} 32}, [ status , apparent ]); 33 34// Once a scan is done, default into the root directory. 35useEffect (() => { 36if ( status ?. state === 'done' && currentPath === null && status . rootPath ) { 37setCurrentPath ( status . rootPath ); 38} 39}, [ status , currentPath ]); 40 41// Load the current node whenever the path, sort, or scan completion changes. 42useEffect (() => { 43if ( currentPath === null ) { 44return ; 45} 46let cancelled = false ; 47fetchNode ( currentPath , sort , order ) 48. then (( resp ) => { 49if ( ! cancelled ) { 50setNodeResp ( resp ); 51setLoadError ( null ); 52} 53}) 54. catch (( err :unknown ) => { 55if ( ! cancelled ) { 56setLoadError ( err instanceof Error ?err . message :String ( err )); 57} 58}); 59return () => { 60cancelled = true ; 61}; 62}, [ currentPath , sort , order , status ?. state ]); 63 64const effectiveApparent = apparent ?? status ?. showApparentSize ?? false ; 65const useSIPrefix = status ?. useSIPrefix ?? false ; 66const children = nodeResp ?. children ?? []; 67 68const { total} = useMemo ( 69() => computeSlices ( children , effectiveApparent ), 70[ children , effectiveApparent ], 71); 72const colorMap = useMemo ( 73() => colorMapFor ( children , effectiveApparent ), 74[ children , effectiveApparent ], 75); 76 77const handleSortChange = useCallback ( 78( key :SortKey ) => { 79if ( key === sort ) { 80setOrder (( o ) => ( o === 'asc' ?'desc' :'asc' )); 81} else { 82setSort ( key ); 83setOrder ( key === 'name' ?'asc' :'desc' ); 84} 85}, 86[ sort ], 87); 88 89const handleSelect = useCallback (( node :Node ) => { 90if ( node . isDir ) { 91setCurrentPath ( node . path ); 92setHoveredPath ( null ); 93} 94}, []); 95 96const handleNavigate = useCallback (( node :Node ) => { 97setCurrentPath ( node . path ); 98setHoveredPath ( null ); 99}, []); 100 101if ( ! status ) { 102return < div className = "loading" > Connecting… </ div >; 103} 104 105if ( status . state === 'error' ) { 106return ( 107< div className = "error-screen" > 108< h2 > Scan failed </ h2 > 109< p >{ status . error ?? 'Unknown error' }</ p > 110</ div > 111); 112} 113 114if ( currentPath === null || ( status . state === 'scanning' && ! nodeResp )) { 115return < ProgressBar progress = { status . progress } useSIPrefix = { useSIPrefix } />; 116} 117 118return ( 119< div className = "app" > 120< header className = "app-header" > 121< div className = "app-title" > 122< span className = "logo" > gdu </ span > 123{ nodeResp && ( 124< Breadcrumbs breadcrumbs = { nodeResp . breadcrumbs } onNavigate = { handleNavigate } /> 125)} 126</ div > 127< div className = "app-actions" > 128< span className = "total" >{ formatSize ( total , useSIPrefix )}</ span > 129< button 130type = "button" 131className = "toggle" 132onClick = {() => setApparent (( a ) => ! ( a ?? status . showApparentSize ))} 133title = "Toggle between disk usage and apparent size" 134> 135{ effectiveApparent ?'Apparent size' :'Disk usage' } 136</ button > 137{ status . state === 'scanning' && < span className = "scanning-badge" > scanning… </ span >} 138</ div > 139</ header > 140 141{ loadError && < div className = "banner error" >{ loadError }</ div >} 142 143< main className = "content" > 144< section className = "chart-panel" > 145< DonutChart 146children = { children } 147apparent = { effectiveApparent } 148useSIPrefix = { useSIPrefix } 149hoveredPath = { hoveredPath } 150onHover = { setHoveredPath } 151onSelect = { handleSelect } 152/> 153</ section > 154< section className = "table-panel" > 155< FileTable 156children = { children } 157colorMap = { colorMap } 158apparent = { effectiveApparent } 159useSIPrefix = { useSIPrefix } 160total = { total } 161sort = { sort } 162order = { order } 163onSortChange = { handleSortChange } 164hoveredPath = { hoveredPath } 165onHover = { setHoveredPath } 166onSelect = { handleSelect } 167/> 168</ section > 169</ main > 170</ div > 171); 172}