diff --git a/app/stores/PoliciesStore.ts b/app/stores/PoliciesStore.ts index ddb4aef8c2..bf03adaa44 100644 --- a/app/stores/PoliciesStore.ts +++ b/app/stores/PoliciesStore.ts @@ -7,6 +7,8 @@ import Store from "./base/Store"; export default class PoliciesStore extends Store { actions = []; + persistable = true; + constructor(rootStore: RootStore) { super(rootStore, Policy); } diff --git a/app/stores/RootStore.ts b/app/stores/RootStore.ts index 6f4ff0de2f..2117ca8508 100644 --- a/app/stores/RootStore.ts +++ b/app/stores/RootStore.ts @@ -35,8 +35,7 @@ import UserMembershipsStore from "./UserMembershipsStore"; import UsersStore from "./UsersStore"; import ViewsStore from "./ViewsStore"; import WebhookSubscriptionsStore from "./WebhookSubscriptionStore"; -import type Store from "./base/Store"; -import StorePersistence from "./base/StorePersistence"; +import Store from "./base/Store"; export default class RootStore { apiKeys: ApiKeysStore; @@ -121,9 +120,9 @@ export default class RootStore { } /** - * Enable IndexedDB persistence for eligible stores, scoped to the current - * team. A no-op when there is no authenticated team or persistence is - * already enabled. + * Enable IndexedDB persistence for all stores marked as persistable, scoped + * to the current team. A no-op when there is no authenticated team or + * persistence is already enabled. */ public enablePersistence() { const teamId = this.auth.currentTeamId; @@ -131,9 +130,11 @@ export default class RootStore { return; } - void this.policies.enablePersistence( - StorePersistence.databaseName("policies", teamId) - ); + Object.values(this).forEach((store) => { + if (store instanceof Store && store.persistable) { + void store.enablePersistence(teamId); + } + }); } /** diff --git a/app/stores/base/Store.ts b/app/stores/base/Store.ts index b905a4ce1c..3908065fd8 100644 --- a/app/stores/base/Store.ts +++ b/app/stores/base/Store.ts @@ -76,6 +76,12 @@ export default abstract class Store { apiEndpoint: string; + /** + * Whether the store's data should be persisted to IndexedDB, and restored + * at boot, once persistence is enabled for the authenticated team. + */ + persistable = false; + rootStore: RootStore; protected persistence?: StorePersistence; @@ -105,19 +111,22 @@ export default abstract class Store { } /** - * Enables persistence of this store's data to IndexedDB and hydrates any - * previously persisted records into the store. Safe to call multiple times, - * subsequent calls are a no-op. + * Enables persistence of this store's data to IndexedDB, scoped to the + * given team, and hydrates any previously persisted records into the store. + * Safe to call multiple times, subsequent calls are a no-op. * - * @param databaseName the name of the IndexedDB database to persist to. + * @param teamId the ID of the team the persisted data belongs to. * @returns a promise that resolves when hydration is complete. */ - async enablePersistence(databaseName: string): Promise { + async enablePersistence(teamId: string): Promise { if (this.persistence || !StorePersistence.isSupported) { return; } - this.persistence = new StorePersistence(this, databaseName); + this.persistence = new StorePersistence( + this, + StorePersistence.databaseName(this.apiEndpoint, teamId) + ); await this.persistence.hydrate(); } diff --git a/app/utils/developer.ts b/app/utils/developer.ts index 161a6b80cd..f963fbfe2b 100644 --- a/app/utils/developer.ts +++ b/app/utils/developer.ts @@ -1,5 +1,6 @@ import { flatten } from "es-toolkit/compat"; import stores from "~/stores"; +import Store from "~/stores/base/Store"; import StorePersistence from "~/stores/base/StorePersistence"; import { flattenTree } from "@shared/utils/tree"; @@ -13,10 +14,15 @@ export async function deleteAllDatabases() { return; } - if (stores.auth.currentTeamId) { - window.indexedDB.deleteDatabase( - StorePersistence.databaseName("policies", stores.auth.currentTeamId) - ); + const teamId = stores.auth.currentTeamId; + if (teamId) { + Object.values(stores).forEach((store) => { + if (store instanceof Store && store.persistable) { + window.indexedDB.deleteDatabase( + StorePersistence.databaseName(store.apiEndpoint, teamId) + ); + } + }); } if ("databases" in window.indexedDB) {