fork/gdu
git clone https://git.julia.blue/fork/gdu
d672bf4
ignore
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 { 7children :Node []; 8colorMap :Map < string , string >; 9apparent :boolean ; 10useSIPrefix :boolean ; 11total :number ; 12sort :SortKey ; 13order :SortOrder ; 14onSortChange :( key :SortKey ) => void ; 15hoveredPath :string | null ; 16onHover :( path :string | null ) => void ; 17onSelect :( 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 ) { 40const maxValue = useMemo ( 41() => children . reduce (( max , n ) => Math . max ( max , metricValue ( n , apparent )), 0 ), 42[ children , apparent ], 43); 44 45return ( 46< table className = "file-table" > 47< thead > 48< tr > 49{ COLUMNS . map (( col ) => ( 50< th 51key = { col . key } 52className = { col . numeric ?'num' :'' } 53onClick = {() => 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 ) => { 65const value = metricValue ( node , apparent ); 66const barWidth = maxValue > 0 ?( value / maxValue ) * 100 :0 ; 67const color = colorMap . get ( node . path ) ?? 'var(--other)' ; 68const isHovered = node . path === hoveredPath ; 69 70return ( 71< tr 72key = { node . path } 73className = { isHovered ?'hovered' :'' } 74onMouseEnter = {() => onHover ( node . path )} 75onMouseLeave = {() => onHover ( null )} 76onClick = {() => node . isDir && onSelect ( node )} 77style = {{ 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" > 96Empty 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}