50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
import { create } from 'zustand'
|
|
|
|
export interface DownloadProgressProps {
|
|
id: string
|
|
progress: number
|
|
name: string
|
|
current: number
|
|
total: number
|
|
}
|
|
|
|
// Zustand store for thinking block state
|
|
export type DownloadState = {
|
|
downloads: { [id: string]: DownloadProgressProps }
|
|
removeDownload: (id: string) => void
|
|
updateProgress: (
|
|
id: string,
|
|
progress: number,
|
|
name?: string,
|
|
current?: number,
|
|
total?: number
|
|
) => void
|
|
}
|
|
|
|
/**
|
|
* This store is used to manage the download progress of files.
|
|
*/
|
|
export const useDownloadStore = create<DownloadState>((set) => ({
|
|
downloads: {},
|
|
removeDownload: (id: string) =>
|
|
set((state) => {
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
const { [id]: _, ...rest } = state.downloads
|
|
return { downloads: rest }
|
|
}),
|
|
|
|
updateProgress: (id, progress, name, current, total) =>
|
|
set((state) => ({
|
|
downloads: {
|
|
...state.downloads,
|
|
[id]: {
|
|
...state.downloads[id],
|
|
name: name || state.downloads[id]?.name || '',
|
|
progress,
|
|
current: current || state.downloads[id]?.current || 0,
|
|
total: total || state.downloads[id]?.total || 0,
|
|
},
|
|
},
|
|
})),
|
|
}))
|