From 21fb1ca9a44cc784b6fa7845b89786695e8d89bb Mon Sep 17 00:00:00 2001 From: Paul Pestov Date: Tue, 21 Jan 2025 16:39:10 +0100 Subject: [PATCH] refactor: use a map object instead of array for collections in store --- src/store/DataStore.tsx | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/store/DataStore.tsx b/src/store/DataStore.tsx index 3e236b08..9af7e649 100644 --- a/src/store/DataStore.tsx +++ b/src/store/DataStore.tsx @@ -1,17 +1,21 @@ import { create } from 'zustand' import { apiRequest } from '@/utils/api.ts' +interface CollectionMap { + [key: string]: Collection +} + interface DataStoreType { - collections: Collection[] + collections: CollectionMap initCollection: (url: string) => Promise } export const dataStore = create((set, get) => ({ - collections: [], + collections: {}, initCollection: async (url: string) => { const collection = await apiRequest(url) - const collections: Collection[] = [ ...get().collections ] - collections.push(collection) + const collections: CollectionMap = { ...get().collections } + collections[collection.id] = collection set({ collections }) return collection }