chore: use object instead of array for file listing

This commit is contained in:
鲁树人 2023-05-08 15:54:00 +01:00
parent 37f6667e50
commit 1b507774ed
2 changed files with 9 additions and 11 deletions

View File

@ -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() {
</Tr>
</Thead>
<Tbody>
{files.map((file) => (
<Tr key={file.id}>
{Object.entries(files).map(([id, file]) => (
<Tr key={id}>
<Td>
{file.metadata.cover && <Avatar size="sm" name="专辑封面" src={file.metadata.cover} />}
{!file.metadata.cover && <Text></Text>}

View File

@ -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<string, DecryptedAudioFile>;
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;
}