2026-05-19 16:05:09 +08:00
|
|
|
import { create } from 'zustand'
|
|
|
|
|
|
2026-06-19 19:44:35 +08:00
|
|
|
export interface FileUploadState {
|
|
|
|
|
fileId: string
|
|
|
|
|
name: string
|
|
|
|
|
loaded: number
|
|
|
|
|
total: number
|
|
|
|
|
status: 'uploading' | 'completed' | 'failed'
|
2026-11-01 13:29:52 +08:00
|
|
|
speed?: number
|
|
|
|
|
eta?: number
|
2026-06-19 19:44:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface TaskUploadState {
|
|
|
|
|
taskId: string
|
|
|
|
|
name: string
|
|
|
|
|
loaded: number
|
|
|
|
|
total: number
|
|
|
|
|
files: Record<string, FileUploadState>
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-19 16:05:09 +08:00
|
|
|
type UploadStore = {
|
|
|
|
|
uploading: number
|
2026-06-19 19:44:35 +08:00
|
|
|
tasks: Record<string, TaskUploadState>
|
|
|
|
|
fileProgress: Record<string, FileUploadState>
|
2026-05-19 16:05:09 +08:00
|
|
|
increment: () => void
|
|
|
|
|
decrement: () => void
|
2026-06-19 19:44:35 +08:00
|
|
|
startTask: (
|
|
|
|
|
taskId: string,
|
|
|
|
|
taskName: string,
|
|
|
|
|
files: Array<{ fileId: string; name: string; size: number }>,
|
|
|
|
|
) => void
|
2026-11-01 13:29:52 +08:00
|
|
|
updateFileProgress: (
|
|
|
|
|
taskId: string,
|
|
|
|
|
fileId: string,
|
|
|
|
|
loaded: number,
|
|
|
|
|
speed?: number,
|
|
|
|
|
eta?: number,
|
|
|
|
|
) => void
|
2026-06-19 19:44:35 +08:00
|
|
|
completeFile: (taskId: string, fileId: string) => void
|
|
|
|
|
failFile: (taskId: string, fileId: string) => void
|
2026-10-31 16:39:54 +08:00
|
|
|
cancelTask: (taskId: string) => void
|
2026-05-19 16:05:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const useUploadStore = create<UploadStore>((set) => ({
|
|
|
|
|
uploading: 0,
|
2026-06-19 19:44:35 +08:00
|
|
|
tasks: {},
|
|
|
|
|
fileProgress: {},
|
2026-05-19 16:05:09 +08:00
|
|
|
increment: () => set((state) => ({ uploading: state.uploading + 1 })),
|
|
|
|
|
decrement: () => set((state) => ({ uploading: state.uploading - 1 })),
|
2026-06-19 19:44:35 +08:00
|
|
|
|
|
|
|
|
startTask: (taskId, taskName, files) =>
|
|
|
|
|
set((state) => {
|
|
|
|
|
const taskFiles: Record<string, FileUploadState> = {}
|
|
|
|
|
let totalBytes = 0
|
|
|
|
|
|
|
|
|
|
files.forEach((f) => {
|
|
|
|
|
taskFiles[f.fileId] = {
|
|
|
|
|
fileId: f.fileId,
|
|
|
|
|
name: f.name,
|
|
|
|
|
loaded: 0,
|
|
|
|
|
total: f.size,
|
|
|
|
|
status: 'uploading',
|
|
|
|
|
}
|
|
|
|
|
totalBytes += f.size
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const newTasks = {
|
|
|
|
|
...state.tasks,
|
|
|
|
|
[taskId]: {
|
|
|
|
|
taskId,
|
|
|
|
|
name: taskName,
|
|
|
|
|
loaded: 0,
|
|
|
|
|
total: totalBytes,
|
|
|
|
|
files: taskFiles,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newFileProgress = { ...state.fileProgress }
|
|
|
|
|
files.forEach((f) => {
|
|
|
|
|
newFileProgress[f.fileId] = taskFiles[f.fileId]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
tasks: newTasks,
|
|
|
|
|
fileProgress: newFileProgress,
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
2026-11-01 13:29:52 +08:00
|
|
|
updateFileProgress: (taskId, fileId, loaded, speed, eta) =>
|
2026-06-19 19:44:35 +08:00
|
|
|
set((state) => {
|
|
|
|
|
const task = state.tasks[taskId]
|
|
|
|
|
if (!task) return {}
|
|
|
|
|
|
|
|
|
|
const file = task.files[fileId]
|
|
|
|
|
if (!file) return {}
|
|
|
|
|
|
2026-11-01 13:29:52 +08:00
|
|
|
const updatedFile: FileUploadState = {
|
|
|
|
|
...file,
|
|
|
|
|
loaded,
|
|
|
|
|
...(speed !== undefined ? { speed } : {}),
|
|
|
|
|
...(eta !== undefined ? { eta } : {}),
|
|
|
|
|
}
|
2026-06-19 19:44:35 +08:00
|
|
|
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
|
|
|
|
|
|
|
|
|
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
tasks: {
|
|
|
|
|
...state.tasks,
|
|
|
|
|
[taskId]: {
|
|
|
|
|
...task,
|
|
|
|
|
loaded: loadedBytes,
|
|
|
|
|
files: updatedFiles,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
fileProgress: {
|
|
|
|
|
...state.fileProgress,
|
|
|
|
|
[fileId]: updatedFile,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
completeFile: (taskId, fileId) =>
|
|
|
|
|
set((state) => {
|
|
|
|
|
const task = state.tasks[taskId]
|
|
|
|
|
if (!task) return {}
|
|
|
|
|
|
|
|
|
|
const file = task.files[fileId]
|
|
|
|
|
if (!file) return {}
|
|
|
|
|
|
2026-10-31 16:39:54 +08:00
|
|
|
const wasUploading = file.status === 'uploading'
|
2026-06-19 19:44:35 +08:00
|
|
|
const updatedFile: FileUploadState = { ...file, status: 'completed', loaded: file.total }
|
|
|
|
|
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
|
|
|
|
|
|
|
|
|
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
|
|
|
|
|
|
|
|
|
return {
|
2026-10-31 16:39:54 +08:00
|
|
|
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
2026-06-19 19:44:35 +08:00
|
|
|
tasks: {
|
|
|
|
|
...state.tasks,
|
|
|
|
|
[taskId]: {
|
|
|
|
|
...task,
|
|
|
|
|
loaded: loadedBytes,
|
|
|
|
|
files: updatedFiles,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
fileProgress: {
|
|
|
|
|
...state.fileProgress,
|
|
|
|
|
[fileId]: updatedFile,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
|
|
failFile: (taskId, fileId) =>
|
|
|
|
|
set((state) => {
|
|
|
|
|
const task = state.tasks[taskId]
|
|
|
|
|
if (!task) return {}
|
|
|
|
|
|
|
|
|
|
const file = task.files[fileId]
|
|
|
|
|
if (!file) return {}
|
|
|
|
|
|
2026-10-31 16:39:54 +08:00
|
|
|
const wasUploading = file.status === 'uploading'
|
2026-06-19 19:44:35 +08:00
|
|
|
const updatedFile: FileUploadState = { ...file, status: 'failed' }
|
|
|
|
|
const updatedFiles = { ...task.files, [fileId]: updatedFile }
|
|
|
|
|
|
|
|
|
|
const loadedBytes = Object.values(updatedFiles).reduce((sum, f) => sum + f.loaded, 0)
|
|
|
|
|
|
|
|
|
|
return {
|
2026-10-31 16:39:54 +08:00
|
|
|
uploading: wasUploading ? Math.max(0, state.uploading - 1) : state.uploading,
|
2026-06-19 19:44:35 +08:00
|
|
|
tasks: {
|
|
|
|
|
...state.tasks,
|
|
|
|
|
[taskId]: {
|
|
|
|
|
...task,
|
|
|
|
|
loaded: loadedBytes,
|
|
|
|
|
files: updatedFiles,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
fileProgress: {
|
|
|
|
|
...state.fileProgress,
|
|
|
|
|
[fileId]: updatedFile,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}),
|
2026-10-31 16:39:54 +08:00
|
|
|
|
|
|
|
|
cancelTask: (taskId) =>
|
|
|
|
|
set((state) => {
|
|
|
|
|
const task = state.tasks[taskId]
|
|
|
|
|
if (!task) return {}
|
|
|
|
|
|
|
|
|
|
let decrements = 0
|
|
|
|
|
const updatedFiles = { ...task.files }
|
|
|
|
|
const updatedFileProgress = { ...state.fileProgress }
|
|
|
|
|
|
|
|
|
|
for (const [fileId, file] of Object.entries(task.files)) {
|
|
|
|
|
if (file.status === 'uploading') {
|
|
|
|
|
updatedFiles[fileId] = { ...file, status: 'failed' }
|
|
|
|
|
updatedFileProgress[fileId] = { ...file, status: 'failed' }
|
|
|
|
|
decrements++
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
uploading: Math.max(0, state.uploading - decrements),
|
|
|
|
|
tasks: {
|
|
|
|
|
...state.tasks,
|
|
|
|
|
[taskId]: {
|
|
|
|
|
...task,
|
|
|
|
|
files: updatedFiles,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
fileProgress: updatedFileProgress,
|
|
|
|
|
}
|
|
|
|
|
}),
|
2026-05-19 16:05:09 +08:00
|
|
|
}))
|