From d92f49606525badabcbed3bb8d30a382b72ea2c2 Mon Sep 17 00:00:00 2001 From: lawvs <18554747+lawvs@users.noreply.github.com> Date: Wed, 22 Jan 2025 17:38:57 +0800 Subject: [PATCH] refactor: use record in inbox store --- apps/mobile/src/store/inbox/hooks.ts | 7 +++++++ apps/mobile/src/store/inbox/store.ts | 20 ++++++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 apps/mobile/src/store/inbox/hooks.ts diff --git a/apps/mobile/src/store/inbox/hooks.ts b/apps/mobile/src/store/inbox/hooks.ts new file mode 100644 index 000000000..5af370036 --- /dev/null +++ b/apps/mobile/src/store/inbox/hooks.ts @@ -0,0 +1,7 @@ +import { useInboxStore } from "./store" + +export const useInbox = (inboxId: string) => { + return useInboxStore((state) => { + return state.inboxes[inboxId] + }) +} diff --git a/apps/mobile/src/store/inbox/store.ts b/apps/mobile/src/store/inbox/store.ts index a344a7865..bcd77310f 100644 --- a/apps/mobile/src/store/inbox/store.ts +++ b/apps/mobile/src/store/inbox/store.ts @@ -3,19 +3,27 @@ import { InboxService } from "@/src/services/inbox" import { createTransaction, createZustandStore } from "../internal/helper" -const defaultState = { - inboxes: [], +interface InboxState { + inboxes: Record } -export const useInboxStore = createZustandStore<{ - inboxes: InboxSchema[] -}>("inbox")(() => defaultState) + +const defaultState = { + inboxes: {}, +} + +export const useInboxStore = createZustandStore("inbox")(() => defaultState) // const get = useInboxStore.getState const set = useInboxStore.setState class InboxActions { async upsertManyInSession(inboxes: InboxSchema[]) { const state = useInboxStore.getState() - const nextInboxes = [...state.inboxes, ...inboxes] + const nextInboxes: InboxState["inboxes"] = { + ...state.inboxes, + } + inboxes.forEach((inbox) => { + nextInboxes[inbox.id] = inbox + }) set({ ...state, inboxes: nextInboxes,