diff --git a/frontend/src/components/UploadQueue.tsx b/frontend/src/components/UploadQueue.tsx index 71751f3..49d167c 100644 --- a/frontend/src/components/UploadQueue.tsx +++ b/frontend/src/components/UploadQueue.tsx @@ -1,4 +1,5 @@ import { useEffect, useRef } from 'react' +import { formatBytes, formatDuration } from '../format' interface UploadItem { id: string @@ -6,6 +7,12 @@ interface UploadItem { progress: number // 0-100 status: 'uploading' | 'done' | 'error' | 'canceled' error?: string + /** Schon draußen, in Bytes. */ + loaded?: number + /** Größe der Datei, in Bytes. */ + total?: number + /** Bytes je Sekunde, geglättet. 0 heißt „noch nicht messbar". */ + speed?: number } interface Props { @@ -98,21 +105,36 @@ export default function UploadQueue({ items, onDismiss, onCancel }: Props) { title={item.status === 'uploading' ? 'Cancel upload' : 'Dismiss'} aria-label={item.status === 'uploading' ? 'Cancel upload' : 'Dismiss'} className="ml-2 opacity-50 hover:opacity-100 text-xs shrink-0" - style={{ color: 'var(--color-text-muted)' }}>✕ + style={{ color: 'var(--color-text-muted)' }} + // Beim Darüberfahren rot – **nur solange der Upload läuft**. + // Dasselbe Zeichen tut hier zwei verschiedene Dinge: eine + // Meldung wegräumen ist folgenlos, einen laufenden Vorgang + // fallen zu lassen nicht. Die Farbe ist der einzige Hinweis + // darauf, bevor man klickt. + onMouseEnter={e => { + if (item.status === 'uploading') e.currentTarget.style.color = 'var(--color-danger)' + }} + onMouseLeave={e => { e.currentTarget.style.color = 'var(--color-text-muted)' }} + >✕ {item.status === 'uploading' && ( -
✓ Uploaded
++ ✓ Uploaded{item.total ? ` · ${formatBytes(item.total)}` : ''} +
)} {item.status === 'error' && (✕ {item.error}
@@ -136,3 +158,40 @@ export default function UploadQueue({ items, onDismiss, onCancel }: Props) { ) } + +/** + * Die Zeile unter dem Balken: wie viel, wie schnell, wie lange noch. + * + * Ein Prozentwert allein sagt nichts über das Warten. Bei 40 % weiß man nicht, + * ob noch zehn Sekunden oder zehn Minuten kommen – und genau das ist die + * Frage, wegen der man auf den Balken sieht. + * + * Jeder Teil erscheint erst, wenn er stimmt: Die Größe kennt der Browser + * sofort, das Tempo erst nach dem zweiten Messpunkt, die Restzeit erst mit + * beidem. Eine Schätzung aus dem Nichts wäre schlimmer als keine – man richtet + * sich danach. + */ +function Details({ item }: { item: UploadItem }) { + const teile: string[] = [] + + if (item.total) { + teile.push(`${formatBytes(item.loaded ?? 0)} / ${formatBytes(item.total)}`) + } + if (item.speed) { + teile.push(`${formatBytes(Math.round(item.speed))}/s`) + if (item.total) { + const rest = formatDuration((item.total - (item.loaded ?? 0)) / item.speed) + if (rest) teile.push(`${rest} left`) + } + } + if (teile.length === 0) return null + + return ( ++ {teile.join(' · ')} +
+ ) +} diff --git a/frontend/src/format.test.ts b/frontend/src/format.test.ts index 8aaaade..4848374 100644 --- a/frontend/src/format.test.ts +++ b/frontend/src/format.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { formatBytes } from './format' +import { formatBytes, formatDuration } from './format' describe('formatBytes', () => { it('lässt kleine Zahlen als Bytes stehen', () => { @@ -23,3 +23,33 @@ describe('formatBytes', () => { expect(formatBytes(2048 * 1024 ** 3)).toBe('2048.0 GB') }) }) + +describe('formatDuration', () => { + it('zeigt unter einer Minute Sekunden', () => { + expect(formatDuration(1)).toBe('1 s') + expect(formatDuration(45.4)).toBe('45 s') + expect(formatDuration(59)).toBe('59 s') + }) + + it('sagt nie „0 s", solange noch etwas läuft', () => { + expect(formatDuration(0)).toBe('1 s') + expect(formatDuration(0.2)).toBe('1 s') + }) + + it('zeigt darüber Minuten mit zweistelligen Sekunden', () => { + expect(formatDuration(60)).toBe('1:00 min') + expect(formatDuration(187)).toBe('3:07 min') + expect(formatDuration(3599)).toBe('59:59 min') + }) + + it('zeigt ab einer Stunde Stunden', () => { + expect(formatDuration(3600)).toBe('1:00 h') + expect(formatDuration(3600 * 2 + 60 * 5)).toBe('2:05 h') + }) + + it('schweigt bei allem, was keine Dauer ist', () => { + expect(formatDuration(Infinity)).toBe('') + expect(formatDuration(NaN)).toBe('') + expect(formatDuration(-1)).toBe('') + }) +}) diff --git a/frontend/src/format.ts b/frontend/src/format.ts index 2d82923..10cd931 100644 --- a/frontend/src/format.ts +++ b/frontend/src/format.ts @@ -21,3 +21,25 @@ export function formatBytes(n: number): string { if (n < 1024 ** 3) return `${(n / 1024 ** 2).toFixed(1)} MB` return `${(n / 1024 ** 3).toFixed(1)} GB` } + +/** + * Eine Dauer, wie man sie beim Warten liest. + * + * Grob genug, dass die Zahl nicht zappelt, genau genug, dass sie etwas sagt: + * unter einer Minute in Sekunden, darüber als `3:07 min`. Sekunden bis in die + * zehnte Minute auszuschreiben hieße eine Zahl, die sich jede Sekunde ändert + * und trotzdem niemandem hilft. + * + * Leer bei allem, was keine Dauer ist – unendlich, negativ, keine Zahl. Der + * Aufrufer zeigt dann gar nichts an, statt „NaN s" zu schreiben. + */ +export function formatDuration(seconds: number): string { + if (!Number.isFinite(seconds) || seconds < 0) return '' + // Unter einer Sekunde bleibt "1 s" stehen: "0 s" sähe aus wie fertig, + // während der Balken noch läuft. + if (seconds < 60) return `${Math.max(1, Math.round(seconds))} s` + const total = Math.round(seconds) + const m = Math.floor(total / 60) + if (m < 60) return `${m}:${String(total % 60).padStart(2, '0')} min` + return `${Math.floor(m / 60)}:${String(m % 60).padStart(2, '0')} h` +} diff --git a/frontend/src/hooks/useUploads.ts b/frontend/src/hooks/useUploads.ts index 07519db..c7505d8 100644 --- a/frontend/src/hooks/useUploads.ts +++ b/frontend/src/hooks/useUploads.ts @@ -65,6 +65,17 @@ export function useUploads({ folderId, onFinished }: Options) { */ const canceled = useRef(new Set