fix: all store resetable
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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<string, ImageModel>
|
||||
}
|
||||
|
||||
export const useImagesStore = createZustandStore<ImageStore>("images")(() => ({
|
||||
const defaultState: ImageStore = {
|
||||
images: {},
|
||||
}))
|
||||
}
|
||||
|
||||
export const useImagesStore = createZustandStore<ImageStore>("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) {
|
||||
|
||||
@@ -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()))
|
||||
}
|
||||
@@ -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<TranslationSchema, "createdAt">
|
||||
interface TranslationState {
|
||||
data: Record<string, Partial<Record<SupportedActionLanguage, EntryTranslation>>>
|
||||
}
|
||||
const emptyDataSet: Record<string, EntryTranslation> = {}
|
||||
const defaultState: TranslationState = {
|
||||
data: {},
|
||||
}
|
||||
|
||||
export const useTranslationStore = createZustandStore<TranslationState>("translation")(() => ({
|
||||
data: emptyDataSet,
|
||||
}))
|
||||
export const useTranslationStore = createZustandStore<TranslationState>("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) => {
|
||||
|
||||
@@ -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<UserStore>("user")(() => ({
|
||||
const defaultState: UserStore = {
|
||||
users: {},
|
||||
whoami: null,
|
||||
role: null,
|
||||
}))
|
||||
}
|
||||
|
||||
export const useUserStore = createZustandStore<UserStore>("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) {
|
||||
|
||||
Reference in New Issue
Block a user