Upload atoms now also report into an app-wide `uploadProgressAtom`, and `aggregateUploadProgress` turns every in-flight upload into one taskbar state: byte-weighted percentage while uploading, indeterminate until a size is known, red after a failure (held 4 s, then cleared), none when done. Cancelled uploads just disappear. `useTauriTaskbarProgress` sends it to the native `set_taskbar_progress`, at most ~4 times a second, always ending on the latest state. Verified with a throttled 6 MB upload: 0% → 99% at 250 ms steps, then cleared; with the upload request aborted: 0% → error → cleared after 4 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PPmy3tPq869XDW4njjVaKA
68 lines
2.0 KiB
TypeScript
68 lines
2.0 KiB
TypeScript
import { atom } from 'jotai';
|
|
import type { TUploadContent } from '../utils/matrix';
|
|
|
|
/**
|
|
* [cinny-desktop #10] Every in-flight upload across all rooms, so the desktop
|
|
* taskbar can show one combined progress bar. Written by `createUploadAtom`
|
|
* (state/upload.ts) as its uploads start, progress, finish or fail.
|
|
*/
|
|
export type UploadProgressEntry = {
|
|
loaded: number;
|
|
total: number;
|
|
failed: boolean;
|
|
/** ms timestamp of the last change; stale entries are ignored. */
|
|
updatedAt: number;
|
|
};
|
|
|
|
export const uploadProgressAtom = atom(new Map<TUploadContent, UploadProgressEntry>());
|
|
|
|
export type TaskbarProgress =
|
|
| { status: 'none' }
|
|
| { status: 'indeterminate' }
|
|
| { status: 'normal'; progress: number }
|
|
| { status: 'error'; progress: number };
|
|
|
|
/** An upload with no update for this long is treated as abandoned. */
|
|
export const UPLOAD_STALE_MS = 2 * 60_000;
|
|
|
|
/**
|
|
* Combine every upload into one taskbar state: the byte-weighted percentage
|
|
* while anything is uploading (indeterminate while no size is known yet), red
|
|
* when an upload failed and nothing else is running, nothing otherwise.
|
|
*/
|
|
export const aggregateUploadProgress = (
|
|
entries: Iterable<UploadProgressEntry>,
|
|
now: number,
|
|
): TaskbarProgress => {
|
|
let loaded = 0;
|
|
let total = 0;
|
|
let active = 0;
|
|
let failed = 0;
|
|
let failedLoaded = 0;
|
|
let failedTotal = 0;
|
|
Array.from(entries).forEach((e) => {
|
|
if (now - e.updatedAt > UPLOAD_STALE_MS) return;
|
|
if (e.failed) {
|
|
failed += 1;
|
|
failedLoaded += e.loaded;
|
|
failedTotal += e.total;
|
|
return;
|
|
}
|
|
active += 1;
|
|
loaded += Math.min(e.loaded, e.total);
|
|
total += e.total;
|
|
});
|
|
if (active > 0) {
|
|
if (total <= 0) return { status: 'indeterminate' };
|
|
return { status: 'normal', progress: Math.min(100, Math.floor((loaded / total) * 100)) };
|
|
}
|
|
if (failed > 0) {
|
|
return {
|
|
status: 'error',
|
|
progress:
|
|
failedTotal > 0 ? Math.min(100, Math.floor((failedLoaded / failedTotal) * 100)) : 100,
|
|
};
|
|
}
|
|
return { status: 'none' };
|
|
};
|