import byteSize from 'byte-size'; import clsx from 'clsx'; import Skeleton from 'react-loading-skeleton'; import { Statistics, useLibraryContext, useLibraryQuery } from '@sd/client'; import { useCounter } from '~/hooks'; import { usePlatform } from '~/util/Platform'; interface StatItemProps { title: string; bytes: bigint; isLoading: boolean; } const StatItemNames: Partial> = { total_bytes_capacity: 'Total capacity', preview_media_bytes: 'Preview media', library_db_size: 'Index size', total_bytes_free: 'Free space' }; const EMPTY_STATISTICS = { id: 0, date_captured: '', total_bytes_capacity: '0', preview_media_bytes: '0', library_db_size: '0', total_object_count: 0, total_bytes_free: '0', total_bytes_used: '0', total_unique_bytes: '0' }; const displayableStatItems = Object.keys(StatItemNames) as unknown as keyof typeof StatItemNames; let mounted = false; const StatItem = (props: StatItemProps) => { const { title, bytes = BigInt('0'), isLoading } = props; const size = byteSize(Number(bytes)); // TODO: This BigInt to Number conversion will truncate the number if the number is too large. `byteSize` doesn't support BigInt so we are gonna need to come up with a longer term solution at some point. const count = useCounter({ name: title, end: +size.value, duration: mounted ? 0 : 1, saveState: false }); return (
{title} {isLoading && (
)}
{count} {size.unit}
); }; export default () => { const platform = usePlatform(); const { library } = useLibraryContext(); const stats = useLibraryQuery(['library.getStatistics'], { initialData: { ...EMPTY_STATISTICS } }); mounted = true; return (
{/* STAT CONTAINER */}
{Object.entries(stats?.data || []).map(([key, value]) => { if (!displayableStatItems.includes(key)) return null; return ( ); })}
); };