refactor: use record in inbox store

This commit is contained in:
lawvs
2025-01-22 17:38:57 +08:00
parent a1676fc535
commit d92f496065
2 changed files with 21 additions and 6 deletions
+7
View File
@@ -0,0 +1,7 @@
import { useInboxStore } from "./store"
export const useInbox = (inboxId: string) => {
return useInboxStore((state) => {
return state.inboxes[inboxId]
})
}
+14 -6
View File
@@ -3,19 +3,27 @@ import { InboxService } from "@/src/services/inbox"
import { createTransaction, createZustandStore } from "../internal/helper"
const defaultState = {
inboxes: [],
interface InboxState {
inboxes: Record<string, InboxSchema>
}
export const useInboxStore = createZustandStore<{
inboxes: InboxSchema[]
}>("inbox")(() => defaultState)
const defaultState = {
inboxes: {},
}
export const useInboxStore = createZustandStore<InboxState>("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,