diff --git a/apps/desktop/layer/renderer/src/store/utils/clear.ts b/apps/desktop/layer/renderer/src/store/utils/clear.ts index 3aba51ce4..bfd12cd0a 100644 --- a/apps/desktop/layer/renderer/src/store/utils/clear.ts +++ b/apps/desktop/layer/renderer/src/store/utils/clear.ts @@ -1,9 +1,4 @@ -import { entryActions } from "@follow/store/entry/store" -import { feedActions } from "@follow/store/feed/store" -import { inboxActions } from "@follow/store/inbox/store" -import { listActions } from "@follow/store/list/store" -import { subscriptionActions } from "@follow/store/subscription/store" -import { unreadActions } from "@follow/store/unread/store" +import { resetStore } from "@follow/store/reset" import { getStorageNS } from "@follow/utils/ns" import { clearUISettings } from "~/atoms/settings/ui" @@ -11,21 +6,9 @@ import { clearUISettings } from "~/atoms/settings/ui" import { clearImageDimensionsDb } from "../image/db" export const clearLocalPersistStoreData = async () => { - // All clear and reset method will aggregate here - ;[ - entryActions, - subscriptionActions, - unreadActions, - feedActions, - listActions, - inboxActions, - ].forEach((actions) => { - actions.reset() - }) + await Promise.all([resetStore(), clearImageDimensionsDb()]) clearUISettings() - - await clearImageDimensionsDb() } const storedUserId = getStorageNS("user_id") diff --git a/packages/internal/database/src/services/user.ts b/packages/internal/database/src/services/user.ts index ed7ee1d4e..88e428dab 100644 --- a/packages/internal/database/src/services/user.ts +++ b/packages/internal/database/src/services/user.ts @@ -3,9 +3,10 @@ import { eq } from "drizzle-orm" import { db } from "../db" import { usersTable } from "../schemas" import type { UserSchema } from "../schemas/types" +import type { Resetable } from "./internal/base" import { conflictUpdateAllExcept } from "./internal/utils" -class UserServiceStatic { +class UserServiceStatic implements Resetable { getUserAll() { return db.query.usersTable.findMany() } @@ -24,6 +25,10 @@ class UserServiceStatic { async removeCurrentUser() { await db.update(usersTable).set({ isMe: false }).where(eq(usersTable.isMe, true)) } + + async reset() { + await db.delete(usersTable).execute() + } } export const UserService = new UserServiceStatic() diff --git a/packages/internal/store/package.json b/packages/internal/store/package.json index 81a686f28..dbbf4a267 100644 --- a/packages/internal/store/package.json +++ b/packages/internal/store/package.json @@ -76,6 +76,11 @@ "require": "./src/morph/*.ts", "import": "./src/morph/*.ts" }, + "./reset": { + "types": "./src/reset.ts", + "require": "./src/reset.ts", + "import": "./src/reset.ts" + }, "./subscription/*": { "types": "./src/subscription/*.ts", "require": "./src/subscription/*.ts", diff --git a/packages/internal/store/src/image/store.ts b/packages/internal/store/src/image/store.ts index 0c3454b65..c0063e827 100644 --- a/packages/internal/store/src/image/store.ts +++ b/packages/internal/store/src/image/store.ts @@ -1,7 +1,7 @@ import type { ImageSchema } from "@follow/database/schemas/types" import { ImagesService } from "@follow/database/services/image" -import type { Hydratable } from "../internal/base" +import type { Hydratable, Resetable } from "../internal/base" import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper" export type ImageModel = ImageSchema @@ -9,18 +9,30 @@ type ImageStore = { images: Record } -export const useImagesStore = createZustandStore("images")(() => ({ +const defaultState: ImageStore = { images: {}, -})) +} +export const useImagesStore = createZustandStore("images")(() => defaultState) + +const set = useImagesStore.setState const immerSet = createImmerSetter(useImagesStore) -class ImageActions implements Hydratable { +class ImageActions implements Hydratable, Resetable { async hydrate() { const images = await ImagesService.getImageAll() imageActions.upsertManyInSession(images) } + async reset() { + const tx = createTransaction() + tx.store(() => { + set(defaultState) + }) + tx.persist(() => ImagesService.reset()) + await tx.run() + } + upsertManyInSession(images: ImageModel[]) { immerSet((state) => { for (const image of images) { diff --git a/packages/internal/store/src/reset.ts b/packages/internal/store/src/reset.ts new file mode 100644 index 000000000..dd74f6c85 --- /dev/null +++ b/packages/internal/store/src/reset.ts @@ -0,0 +1,30 @@ +import { collectionActions } from "./collection/store" +import { entryActions } from "./entry/store" +import { feedActions } from "./feed/store" +import { imageActions } from "./image/store" +import { inboxActions } from "./inbox/store" +import type { Resetable } from "./internal/base" +import { listActions } from "./list/store" +import { subscriptionActions } from "./subscription/store" +import { summaryActions } from "./summary/store" +import { translationActions } from "./translation/store" +import { unreadActions } from "./unread/store" +import { userActions } from "./user/store" + +const resets: Resetable[] = [ + feedActions, + subscriptionActions, + inboxActions, + listActions, + unreadActions, + userActions, + entryActions, + collectionActions, + summaryActions, + translationActions, + imageActions, +] + +export const resetStore = async () => { + await Promise.all(resets.map((h) => h.reset())) +} diff --git a/packages/internal/store/src/translation/store.ts b/packages/internal/store/src/translation/store.ts index f1c15c15c..d4b7c4a8d 100644 --- a/packages/internal/store/src/translation/store.ts +++ b/packages/internal/store/src/translation/store.ts @@ -4,8 +4,8 @@ import type { SupportedActionLanguage } from "@follow/shared" import { apiClient } from "../context" import { getEntry } from "../entry/getter" -import type { Hydratable } from "../internal/base" -import { createImmerSetter, createZustandStore } from "../internal/helper" +import type { Hydratable, Resetable } from "../internal/base" +import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper" import type { EntryTranslation, TranslationFieldArray } from "./types" import { translationFields } from "./types" @@ -14,21 +14,34 @@ type TranslationModel = Omit interface TranslationState { data: Record>> } -const emptyDataSet: Record = {} +const defaultState: TranslationState = { + data: {}, +} -export const useTranslationStore = createZustandStore("translation")(() => ({ - data: emptyDataSet, -})) +export const useTranslationStore = createZustandStore("translation")( + () => defaultState, +) const get = useTranslationStore.getState +const set = useTranslationStore.setState const immerSet = createImmerSetter(useTranslationStore) -class TranslationActions implements Hydratable { +class TranslationActions implements Hydratable, Resetable { async hydrate() { const translations = await TranslationService.getTranslationToHydrate() translationActions.upsertManyInSession(translations) } + async reset() { + const tx = createTransaction() + tx.store(() => { + set(defaultState) + }) + tx.persist(() => TranslationService.reset()) + + await tx.run() + } + upsertManyInSession(translations: TranslationModel[]) { immerSet((state) => { translations.forEach((translation) => { diff --git a/packages/internal/store/src/user/store.ts b/packages/internal/store/src/user/store.ts index d95f881b1..5193dafe1 100644 --- a/packages/internal/store/src/user/store.ts +++ b/packages/internal/store/src/user/store.ts @@ -5,7 +5,7 @@ import type { AuthSession } from "@follow/shared/hono" import { create, indexedResolver, windowScheduler } from "@yornaath/batshit" import { apiClient, authClient } from "../context" -import type { Hydratable } from "../internal/base" +import type { Hydratable, Resetable } from "../internal/base" import { createImmerSetter, createTransaction, createZustandStore } from "../internal/helper" import { honoMorph } from "../morph/hono" import type { UserProfileEditable } from "./types" @@ -22,13 +22,16 @@ type UserStore = { role: UserRole | null } -export const useUserStore = createZustandStore("user")(() => ({ +const defaultState: UserStore = { users: {}, whoami: null, role: null, -})) +} + +export const useUserStore = createZustandStore("user")(() => defaultState) const get = useUserStore.getState +const set = useUserStore.setState const immerSet = createImmerSetter(useUserStore) class UserSyncService { @@ -197,11 +200,21 @@ class UserSyncService { } } -class UserActions implements Hydratable { +class UserActions implements Hydratable, Resetable { async hydrate() { const users = await UserService.getUserAll() userActions.upsertManyInSession(users) } + + async reset() { + const tx = createTransaction() + tx.store(() => { + set(defaultState) + }) + tx.persist(() => UserService.reset()) + await tx.run() + } + upsertManyInSession(users: UserModel[]) { immerSet((state) => { for (const user of users) {