feat(upload): simplify upload file list display and add live speed & ETA (#513)

* feat(upload): simplify upload file list display and add live speed and ETA

* fix(upload): retain authoritative server search results and handle local failed upload state gracefully
This commit is contained in:
Yiling-J
2026-11-01 13:29:52 +08:00
committed by GitHub
parent fbff404b9b
commit 20ad0abeba
14 changed files with 372 additions and 97 deletions
+28 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { formatRemainingTime, getTrashDaysLeft } from './time'
import { formatRemainingTime, formatRemainingTimeSeconds, getTrashDaysLeft } from './time'
describe('formatRemainingTime', () => {
const baseNow = new Date('2026-08-17T12:00:00Z')
@@ -45,6 +45,33 @@ describe('formatRemainingTime', () => {
})
})
describe('formatRemainingTimeSeconds', () => {
it('formats hours and minutes', () => {
expect(formatRemainingTimeSeconds(3 * 3600 + 18 * 60)).toBe('3h 18m')
})
it('formats exact hours', () => {
expect(formatRemainingTimeSeconds(3 * 3600)).toBe('3h')
})
it('formats minutes', () => {
expect(formatRemainingTimeSeconds(8 * 60)).toBe('8m')
expect(formatRemainingTimeSeconds(45 * 60 + 20)).toBe('45m')
})
it('formats seconds', () => {
expect(formatRemainingTimeSeconds(45)).toBe('45s')
expect(formatRemainingTimeSeconds(1)).toBe('1s')
})
it('handles zero, negative, or invalid numbers', () => {
expect(formatRemainingTimeSeconds(0)).toBe('< 1s')
expect(formatRemainingTimeSeconds(-5)).toBe('< 1s')
expect(formatRemainingTimeSeconds(NaN)).toBe('< 1s')
expect(formatRemainingTimeSeconds(Infinity)).toBe('< 1s')
})
})
describe('getTrashDaysLeft', () => {
const now = new Date('2026-08-17T12:00:00Z')
+22
View File
@@ -75,6 +75,28 @@ export const formatRemainingTime = (targetDate: string | Date, now: Date = new D
return minutes > 0 ? `${minutes}m` : '< 1m'
}
export const formatRemainingTimeSeconds = (totalSeconds: number): string => {
if (!isFinite(totalSeconds) || totalSeconds <= 0) {
return '< 1s'
}
const total = Math.round(totalSeconds)
const days = Math.floor(total / 86400)
const hours = Math.floor((total % 86400) / 3600)
const minutes = Math.floor((total % 3600) / 60)
const seconds = total % 60
if (days > 0) {
return hours > 0 ? `${days}d ${hours}h` : `${days}d`
}
if (hours > 0) {
return minutes > 0 ? `${hours}h ${minutes}m` : `${hours}h`
}
if (minutes > 0) {
return `${minutes}m`
}
return `${seconds}s`
}
/**
* Whole days remaining before a soft-deleted asset is permanently purged.
* Returns `null` when there is no valid `deletedAt` timestamp, and `0` once the
+45 -2
View File
@@ -409,12 +409,53 @@ export async function uploadFilesWithUppy({
window.addEventListener('offline', handleOffline)
}
const fileSpeedTrackers = new Map<
string,
{ lastBytes: number; lastTime: number; smoothedSpeed?: number }
>()
uppy.on('upload-progress', (file, progress) => {
if (!file) return
const fileId = file.meta.fileId as string
if (fileId && progress.bytesUploaded != null) {
updateFileProgress(taskId, fileId, progress.bytesUploaded)
if (!fileId || progress.bytesUploaded == null) return
const now = Date.now()
const tracker = fileSpeedTrackers.get(fileId)
let speed: number | undefined
let eta: number | undefined
if (tracker) {
const timeDelta = (now - tracker.lastTime) / 1000
const bytesDelta = progress.bytesUploaded - tracker.lastBytes
if (timeDelta >= 0.5 && bytesDelta >= 0) {
const instantSpeed = bytesDelta / timeDelta
speed =
tracker.smoothedSpeed !== undefined
? tracker.smoothedSpeed * 0.7 + instantSpeed * 0.3
: instantSpeed
tracker.smoothedSpeed = speed
tracker.lastBytes = progress.bytesUploaded
tracker.lastTime = now
const remainingBytes = (progress.bytesTotal ?? 0) - progress.bytesUploaded
if (speed > 0 && remainingBytes > 0) {
eta = Math.round(remainingBytes / speed)
}
} else {
speed = tracker.smoothedSpeed
if (speed && speed > 0) {
const remainingBytes = (progress.bytesTotal ?? 0) - progress.bytesUploaded
eta = Math.round(remainingBytes / speed)
}
}
} else {
fileSpeedTrackers.set(fileId, {
lastBytes: progress.bytesUploaded,
lastTime: now,
})
}
updateFileProgress(taskId, fileId, progress.bytesUploaded, speed, eta)
})
uppy.on('upload-success', (file) => {
@@ -423,6 +464,7 @@ export async function uploadFilesWithUppy({
if (fileId) {
pendingNetworkRetries.delete(fileId)
retryCounts.delete(fileId)
fileSpeedTrackers.delete(fileId)
}
activeUploads.delete(fileId)
@@ -459,6 +501,7 @@ export async function uploadFilesWithUppy({
const uploadUrl = (file.meta?.url as string | undefined) || active?.url
activeUploads.delete(fileId)
fileSpeedTrackers.delete(fileId)
failFile(taskId, fileId)
if (isLocalhostStorageMismatch(uploadUrl)) {