mirror of
https://github.com/get-drexa/drive.git
synced 2025-12-02 22:41:39 +00:00
32 lines
791 B
TypeScript
32 lines
791 B
TypeScript
|
|
import { atom } from "jotai"
|
||
|
|
import { atomFamily } from "jotai/utils"
|
||
|
|
|
||
|
|
type FileUpload = {
|
||
|
|
id: string
|
||
|
|
progress: number
|
||
|
|
}
|
||
|
|
|
||
|
|
export const fileUploadsAtom = atom<Record<string, FileUpload>>({})
|
||
|
|
|
||
|
|
export const fileUploadAtomFamily = atomFamily((id: string) =>
|
||
|
|
atom(
|
||
|
|
(get) => get(fileUploadsAtom)[id],
|
||
|
|
(get, set, progress: number) => {
|
||
|
|
const fileUploads = { ...get(fileUploadsAtom) }
|
||
|
|
fileUploads[id] = { id, progress }
|
||
|
|
set(fileUploadsAtom, fileUploads)
|
||
|
|
},
|
||
|
|
),
|
||
|
|
)
|
||
|
|
|
||
|
|
export const clearFileUploadAtom = atom(null, (get, set, id: string) => {
|
||
|
|
const fileUploads = { ...get(fileUploadsAtom) }
|
||
|
|
delete fileUploads[id]
|
||
|
|
fileUploadAtomFamily.remove(id)
|
||
|
|
set(fileUploadsAtom, fileUploads)
|
||
|
|
})
|
||
|
|
|
||
|
|
export const hasFileUploadsAtom = atom(
|
||
|
|
(get) => Object.keys(get(fileUploadsAtom)).length > 0,
|
||
|
|
)
|