mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 12:54:55 +00:00
feat(upload): track byte-level progress with XMLHttpRequest
- Replace fetch uploads with XMLHttpRequest to capture upload progress - Track active upload tasks and file progress in Zustand store - Show byte-level uploader progress in the sidebar list - Replace plain 'Uploading' card status text with a circular percentage animation - Replace plain document icon in file list view rows with a mini progress circle
This commit is contained in:
@@ -467,6 +467,28 @@ export function FileBrowser({
|
||||
onSuccess: async (data, variables) => {
|
||||
const isCurrentFolderUpload = variables.json.parentId === assetId
|
||||
|
||||
// Register task and files in the store for real-time progress tracking
|
||||
const filesProgressInfo = filesToUpload
|
||||
.map((f) => {
|
||||
const urlInfo = (
|
||||
data.presignedUrls as { id?: string; url?: string; fileId?: string }[] | undefined
|
||||
)?.find((p) => p.id === f.id)
|
||||
return {
|
||||
fileId: urlInfo?.fileId || f.id,
|
||||
name: f.file.name,
|
||||
size: f.file.size,
|
||||
}
|
||||
})
|
||||
.filter((info) => !!info.fileId)
|
||||
|
||||
const visibleFiles = filesToUpload.filter((f) => !f.file.name.startsWith('.'))
|
||||
const taskName =
|
||||
visibleFiles.length === 1 ? visibleFiles[0].file.name : `${visibleFiles.length} Items`
|
||||
|
||||
if (data.taskId) {
|
||||
startTask(data.taskId, taskName, filesProgressInfo)
|
||||
}
|
||||
|
||||
if (isCurrentFolderUpload) {
|
||||
const newLocalFiles: AssetInfo[] = filesToUpload
|
||||
.filter((f) => {
|
||||
@@ -509,6 +531,10 @@ export function FileBrowser({
|
||||
|
||||
const incrementUploading = useUploadStore((state) => state.increment)
|
||||
const decrementUploading = useUploadStore((state) => state.decrement)
|
||||
const startTask = useUploadStore((state) => state.startTask)
|
||||
const updateFileProgress = useUploadStore((state) => state.updateFileProgress)
|
||||
const completeFile = useUploadStore((state) => state.completeFile)
|
||||
const failFile = useUploadStore((state) => state.failFile)
|
||||
const { fields } = useFieldStore()
|
||||
const displayedFields = useMemo(() => {
|
||||
if (!isShareView) return fields.filter((f) => f.visible)
|
||||
@@ -688,25 +714,53 @@ export function FileBrowser({
|
||||
incrementUploading()
|
||||
try {
|
||||
try {
|
||||
const resp = await fetch(uploadInfo.url, {
|
||||
method: 'PUT',
|
||||
body: file.file,
|
||||
headers: {
|
||||
'Content-Type': file.file.type,
|
||||
const xhr = new XMLHttpRequest()
|
||||
const uploadPromise = new Promise<{ ok: boolean; status: number }>(
|
||||
(resolve, reject) => {
|
||||
xhr.open('PUT', uploadInfo.url)
|
||||
xhr.setRequestHeader('Content-Type', file.file.type)
|
||||
|
||||
xhr.upload.onprogress = (event) => {
|
||||
if (event.lengthComputable) {
|
||||
updateFileProgress(taskId, uploadInfo.fileId, event.loaded)
|
||||
}
|
||||
}
|
||||
|
||||
xhr.onload = () => {
|
||||
if (xhr.status >= 200 && xhr.status < 300) {
|
||||
resolve({ ok: true, status: xhr.status })
|
||||
} else {
|
||||
resolve({ ok: false, status: xhr.status })
|
||||
}
|
||||
}
|
||||
|
||||
xhr.onerror = () => {
|
||||
reject(new Error('Network error'))
|
||||
}
|
||||
|
||||
xhr.onabort = () => {
|
||||
reject(new Error('Upload aborted'))
|
||||
}
|
||||
|
||||
xhr.send(file.file)
|
||||
},
|
||||
})
|
||||
)
|
||||
|
||||
const resp = await uploadPromise
|
||||
|
||||
if (resp.ok) {
|
||||
completeFile(taskId, uploadInfo.fileId)
|
||||
await confirmUpload({
|
||||
param: { teamId: teamId, taskId: taskId },
|
||||
param: { teamId: teamId!, taskId: taskId },
|
||||
json: {
|
||||
fileId: uploadInfo.fileId,
|
||||
},
|
||||
})
|
||||
} else {
|
||||
failFile(taskId, uploadInfo.fileId)
|
||||
toast.error(`Failed to upload file: ${file.file.name}`)
|
||||
await confirmUpload({
|
||||
param: { teamId: teamId, taskId: taskId },
|
||||
param: { teamId: teamId!, taskId: taskId },
|
||||
json: {
|
||||
fileId: uploadInfo.fileId,
|
||||
errorMessage: `upload failed with status: ${resp.status}`,
|
||||
@@ -715,9 +769,10 @@ export function FileBrowser({
|
||||
return
|
||||
}
|
||||
} catch (error) {
|
||||
failFile(taskId, uploadInfo.fileId)
|
||||
toast.error(`Failed to upload file: ${file.file.name}`)
|
||||
await confirmUpload({
|
||||
param: { teamId: teamId, taskId: taskId },
|
||||
param: { teamId: teamId!, taskId: taskId },
|
||||
json: {
|
||||
fileId: uploadInfo.fileId,
|
||||
errorMessage: `upload failed with error: ${error instanceof Error ? error.message : String(error)}`,
|
||||
|
||||
@@ -6,11 +6,11 @@ import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Button } from '@/ui/components/ui/button'
|
||||
import { Checkbox } from '@/ui/components/ui/checkbox'
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuItem,
|
||||
DropdownMenuSeparator,
|
||||
DropdownMenuTrigger,
|
||||
} from '@/ui/components/ui/dropdown-menu'
|
||||
import { EditableText } from '@/ui/components/ui/editable-text'
|
||||
import { Skeleton } from '@/ui/components/ui/skeleton'
|
||||
@@ -22,6 +22,8 @@ import React, { useEffect, useMemo, useRef, useState } from 'react'
|
||||
import type { DragState } from '../dnd-types'
|
||||
import FieldRenderer from '../field-renderer'
|
||||
import { FilePreview } from './file-preview'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
|
||||
|
||||
import { FieldType, type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
|
||||
import { FIELD_TYPE_ICONS } from '../fields-manager'
|
||||
@@ -82,6 +84,13 @@ export function FileCard({
|
||||
const [name, setName] = useState(item.name)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const fileUploadState = useUploadStore((state) => state.fileProgress[displayItem.id || ''])
|
||||
const uploadPercent = fileUploadState
|
||||
? fileUploadState.total > 0
|
||||
? (fileUploadState.loaded / fileUploadState.total) * 100
|
||||
: 0
|
||||
: 0
|
||||
|
||||
const shouldPoll =
|
||||
(item.type === 'file' || item.type === 'version_stack') &&
|
||||
!!teamId &&
|
||||
@@ -247,20 +256,24 @@ export function FileCard({
|
||||
{displayItem.status !== 'error' && (
|
||||
<Skeleton className="absolute inset-0 h-full w-full" />
|
||||
)}
|
||||
<span
|
||||
className={cn(
|
||||
'z-10 font-medium px-2 text-center text-sm',
|
||||
displayItem.status === 'error'
|
||||
? 'text-destructive font-semibold'
|
||||
: 'text-muted-foreground capitalize',
|
||||
)}
|
||||
>
|
||||
{displayItem.status === 'uploaded'
|
||||
? 'Processing'
|
||||
: displayItem.status === 'error'
|
||||
? 'Failed to upload'
|
||||
: displayItem.status}
|
||||
</span>
|
||||
{displayItem.status === 'uploading' ? (
|
||||
<ProgressCircle progress={uploadPercent} className="w-16 h-16 z-10" />
|
||||
) : (
|
||||
<span
|
||||
className={cn(
|
||||
'z-10 font-medium px-2 text-center text-sm',
|
||||
displayItem.status === 'error'
|
||||
? 'text-destructive font-semibold'
|
||||
: 'text-muted-foreground capitalize',
|
||||
)}
|
||||
>
|
||||
{displayItem.status === 'uploaded'
|
||||
? 'Processing'
|
||||
: displayItem.status === 'error'
|
||||
? 'Failed to upload'
|
||||
: displayItem.status}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
) : (
|
||||
<FilePreview item={displayItem} />
|
||||
|
||||
@@ -11,6 +11,8 @@ import FieldRenderer from '../field-renderer'
|
||||
import { Badge } from '@/ui/components/ui/badge'
|
||||
import { Checkbox } from '@/ui/components/ui/checkbox'
|
||||
import { EditableText } from '@/ui/components/ui/editable-text'
|
||||
import { useUploadStore } from '@/ui/stores/upload'
|
||||
import { ProgressCircle } from '@/ui/components/ui/progress-circle'
|
||||
|
||||
import { type FieldInfo as MetadataFieldInfo } from '@shumai/dtos'
|
||||
|
||||
@@ -60,6 +62,13 @@ export function FileListItem({
|
||||
const [name, setName] = useState(item.name)
|
||||
const inputRef = useRef<HTMLInputElement>(null)
|
||||
|
||||
const fileUploadState = useUploadStore((state) => state.fileProgress[displayItem.id || ''])
|
||||
const uploadPercent = fileUploadState
|
||||
? fileUploadState.total > 0
|
||||
? (fileUploadState.loaded / fileUploadState.total) * 100
|
||||
: 0
|
||||
: 0
|
||||
|
||||
const shouldPoll =
|
||||
(item.type === 'file' || item.type === 'version_stack') &&
|
||||
!!teamId &&
|
||||
@@ -197,7 +206,9 @@ export function FileListItem({
|
||||
className="h-4 w-4 shrink-0 data-[state=checked]:bg-primary data-[state=checked]:border-primary"
|
||||
/>
|
||||
<div className="w-8 h-8 shrink-0 flex items-center justify-center bg-muted rounded overflow-hidden">
|
||||
{displayItem.preview?.thumbnailUrl ? (
|
||||
{displayItem.status === 'uploading' ? (
|
||||
<ProgressCircle progress={uploadPercent} className="w-7 h-7" />
|
||||
) : displayItem.preview?.thumbnailUrl ? (
|
||||
<img
|
||||
src={displayItem.preview.thumbnailUrl}
|
||||
alt=""
|
||||
|
||||
Reference in New Issue
Block a user