From 1b507774ed164909074f5148d3b5527692c07795 Mon Sep 17 00:00:00 2001 From: Jixun Wu Date: Mon, 8 May 2023 15:54:00 +0100 Subject: [PATCH] chore: use object instead of array for file listing --- src/features/file-listing/FileListing.tsx | 8 ++++---- src/features/file-listing/fileListingSlice.ts | 12 +++++------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/features/file-listing/FileListing.tsx b/src/features/file-listing/FileListing.tsx index d137589..c246002 100644 --- a/src/features/file-listing/FileListing.tsx +++ b/src/features/file-listing/FileListing.tsx @@ -10,8 +10,8 @@ export function FileListing() { useEffect(() => { // FIXME: Remove test data - if (files.length === 0) { - dispatch(addNewFile({ id: String(Date.now()), fileName: '测试文件名.mgg', blobURI: '' })); + if (Object.keys(files).length === 0) { + dispatch(addNewFile({ id: 'dummy', fileName: '测试文件名.mgg', blobURI: '' })); } }, []); @@ -26,8 +26,8 @@ export function FileListing() { - {files.map((file) => ( - + {Object.entries(files).map(([id, file]) => ( + {file.metadata.cover && } {!file.metadata.cover && 暂无封面} diff --git a/src/features/file-listing/fileListingSlice.ts b/src/features/file-listing/fileListingSlice.ts index 032fe2c..adb4cb0 100644 --- a/src/features/file-listing/fileListingSlice.ts +++ b/src/features/file-listing/fileListingSlice.ts @@ -22,7 +22,6 @@ export interface AudioMetadata { } export interface DecryptedAudioFile { - id: string; fileName: string; raw: string; // blob uri decrypted: string; // blob uri @@ -32,11 +31,11 @@ export interface DecryptedAudioFile { } export interface FileListingState { - files: DecryptedAudioFile[]; + files: Record; displayMode: ListingMode; } const initialState: FileListingState = { - files: [], + files: Object.create(null), displayMode: ListingMode.LIST, }; @@ -45,8 +44,7 @@ export const fileListingSlice = createSlice({ initialState, reducers: { addNewFile: (state, { payload }: PayloadAction<{ id: string; fileName: string; blobURI: string }>) => { - state.files.push({ - id: payload.id, + state.files[payload.id] = { fileName: payload.fileName, raw: payload.blobURI, decrypted: '', @@ -59,10 +57,10 @@ export const fileListingSlice = createSlice({ albumArtist: '', cover: '', }, - }); + }; }, setDecryptedContent: (state, { payload }: PayloadAction<{ id: string; decryptedBlobURI: string }>) => { - const file = state.files.find((file) => file.id === payload.id); + const file = state.files[payload.id]; if (file) { file.decrypted = payload.decryptedBlobURI; }