Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(queries/email): persistence primitives + email cache persistence to indexdb (#945)

teo0d61363f2bfe
diff --git a/js/app/bun.lock b/js/app/bun.lock
index 4eb65b6b70..cb833758f7 100644
--- a/js/app/bun.lock
+++ b/js/app/bun.lock
@@ -1,5 +1,6 @@
{
"lockfileVersion": 1,
+ "configVersion": 0,
"workspaces": {
"": {
"name": "@coparse/monorepo",
@@ -54,6 +55,7 @@
"@tailwindcss/vite": "^4.1.14",
"@tanstack/solid-query": "^5.90.3",
"@tanstack/solid-query-devtools": "^5.90.3",
+ "@tanstack/solid-query-persist-client": "^5.90.19",
"@tanstack/solid-virtual": "^3.13.13",
"@tauri-apps/plugin-haptics": "~2",
"@tauri-apps/plugin-http": "~2",
@@ -1160,12 +1162,16 @@
"@tanstack/query-devtools": ["@tanstack/[email protected]", "", {}, "sha512-GtINOPjPUH0OegJExZ70UahT9ykmAhmtNVcmtdnOZbxLwT7R5OmRztR5Ahe3/Cu7LArEmR6/588tAycuaWb1xQ=="],
+ "@tanstack/query-persist-client-core": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-core": "5.90.16" } }, "sha512-vnPSfQVo41EKJN8v20nkhWNZPyB1dMJIy5icOvCGzcCJzsmRefYY1owtr63ICOcjOiPPTuNEfPsdjdBhkzYnmA=="],
+
"@tanstack/react-query": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-core": "5.90.12" }, "peerDependencies": { "react": "^18 || ^19" } }, "sha512-graRZspg7EoEaw0a8faiUASCyJrqjKPdqJ9EwuDRUF9mEYJ1YPczI9H+/agJ0mOJkPCJDk0lsz5QTrLZ/jQ2rg=="],
"@tanstack/solid-query": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-core": "5.90.2" }, "peerDependencies": { "solid-js": "^1.6.0" } }, "sha512-XEP+fzZYes+GLA4HSvmitj2NCC7o9iwMvwdkI5R7YSQD08jd1rA9p4KUYWvcu8VfMD+jodOCn/iMHmPz8zV3qw=="],
"@tanstack/solid-query-devtools": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-devtools": "5.90.1" }, "peerDependencies": { "@tanstack/solid-query": "^5.90.3", "solid-js": "^1.6.0" } }, "sha512-UWb2/tHgHrMetOHrQFCzpivCJyttG7vb6axav/aiih9CN5PArxb6um1/7eOuWTb43vSRaMt2LP2Dj3fhixtDZA=="],
+ "@tanstack/solid-query-persist-client": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/query-persist-client-core": "5.91.15" }, "peerDependencies": { "@tanstack/solid-query": "^5.90.19", "solid-js": "^1.6.0" } }, "sha512-LKWbRKhDX5TC6SFJrGAg+0+mshCOEQ0NWxFwWkzvPLyk+/Owsgfva3R2qxO+2VKyUhjmARnaL4EZj80IDzZFRQ=="],
+
"@tanstack/solid-virtual": ["@tanstack/[email protected]", "", { "dependencies": { "@tanstack/virtual-core": "3.13.13" }, "peerDependencies": { "solid-js": "^1.3.0" } }, "sha512-ym4LhX2CygATku2gvs6StE16J6BwzB+5PoMXH5gUcNRwfTJLTB47ZBt3lAJru6uFjFoRdHN/8emfiQXm9Armuw=="],
"@tanstack/virtual-core": ["@tanstack/[email protected]", "", {}, "sha512-uQFoSdKKf5S8k51W5t7b2qpfkyIbdHMzAn+AMQvHPxKUPeo1SsGaA4JRISQT87jm28b7z8OEqPcg1IOZagQHcA=="],
@@ -3780,6 +3786,8 @@
"@tailwindcss/oxide-wasm32-wasi/tslib": ["[email protected]", "", { "bundled": true }, "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w=="],
+ "@tanstack/query-persist-client-core/@tanstack/query-core": ["@tanstack/[email protected]", "", {}, "sha512-MvtWckSVufs/ja463/K4PyJeqT+HMlJWtw6PrCpywznd2NSgO3m4KwO9RqbFqGg6iDE8vVMFWMeQI4Io3eEYww=="],
+
"@tanstack/react-query/@tanstack/query-core": ["@tanstack/[email protected]", "", {}, "sha512-T1/8t5DhV/SisWjDnaiU2drl6ySvsHj1bHBCWNXd+/T+Hh1cf6JodyEYMd5sgwm+b/mETT4EV3H+zCVczCU5hg=="],
"@testing-library/dom/aria-query": ["[email protected]", "", { "dependencies": { "dequal": "^2.0.3" } }, "sha512-b0P0sZPKtyu8HkeRAfCq0IfURZK+SuwMjY1UXGBU27wpAiTwQAIlq56IbIO+ytk/JjS1fMR14ee5WBBfKi5J6A=="],
diff --git a/js/app/package.json b/js/app/package.json
index 62c9d6bdb1..e2796ef7d9 100644
--- a/js/app/package.json
+++ b/js/app/package.json
@@ -69,6 +69,7 @@
"@tailwindcss/vite": "^4.1.14",
"@tanstack/solid-query": "^5.90.3",
"@tanstack/solid-query-devtools": "^5.90.3",
+ "@tanstack/solid-query-persist-client": "^5.90.19",
"@tanstack/solid-virtual": "^3.13.13",
"@tauri-apps/plugin-haptics": "~2",
"@tauri-apps/plugin-http": "~2",
diff --git a/js/app/packages/queries/client.ts b/js/app/packages/queries/client.ts
index 5f1699bb03..0573cce11e 100644
--- a/js/app/packages/queries/client.ts
+++ b/js/app/packages/queries/client.ts
@@ -1,4 +1,10 @@
import { QueryClient } from '@tanstack/solid-query';
+import { createIDBPersister } from './storage/idb';
+import {
+ createPersistenceKey,
+ queryKeyHasPrefix,
+ setupQueryPersistence,
+} from './persistence';
export const queryClient = new QueryClient({
defaultOptions: {
@@ -11,6 +17,21 @@ export const queryClient = new QueryClient({
},
});
+setupQueryPersistence({
+ queryClient,
+ buster: import.meta.env.__APP_VERSION__ ?? 'dev',
+ scopes: [
+ {
+ persister: createIDBPersister({
+ key: createPersistenceKey('email-threads', 0),
+ }),
+ maxAgeMs: 1000 * 60 * 60 * 24 * 7,
+ shouldDehydrateQuery: (q) =>
+ queryKeyHasPrefix(q.queryKey, ['email', 'threadMessages']),
+ },
+ ],
+});
+
export function useQueryClient() {
return queryClient;
}
diff --git a/js/app/packages/queries/persistence.ts b/js/app/packages/queries/persistence.ts
new file mode 100644
index 0000000000..4d22910288
--- /dev/null
+++ b/js/app/packages/queries/persistence.ts
@@ -0,0 +1,73 @@
+import {
+ type PersistQueryClientOptions,
+ persistQueryClient,
+} from '@tanstack/solid-query-persist-client';
+import type { Persister } from '@tanstack/solid-query-persist-client';
+import type { QueryKey } from '@tanstack/query-core';
+
+type Query = NonNullable<
+ PersistQueryClientOptions['dehydrateOptions']
+>['shouldDehydrateQuery'] extends ((query: infer Q) => boolean) | undefined
+ ? Q
+ : never;
+
+type PersistQueryClient = PersistQueryClientOptions['queryClient'];
+
+/**
+ * Structurally compatible QueryClient type. Accepts any QueryClient instance
+ * to work around version mismatches in @tanstack packages.
+ */
+type QueryClientLike = {
+ getQueryCache: () => unknown;
+ getMutationCache: () => unknown;
+};
+
+export type PersistScope = Readonly<{
+ persister: Persister;
+ maxAgeMs: number;
+ shouldDehydrateQuery: (query: Query) => boolean;
+}>;
+
+export function queryKeyHasPrefix(
+ key: QueryKey,
+ prefix: readonly unknown[]
+): boolean {
+ if (!Array.isArray(key)) return false;
+ if (prefix.length > key.length) return false;
+ for (let i = 0; i < prefix.length; i++) {
+ if (key[i] !== prefix[i]) return false;
+ }
+ return true;
+}
+
+type PersistenceKey = `${string}-persist-v${number}`;
+
+export function createPersistenceKey(
+ name: string,
+ version: number
+): PersistenceKey {
+ return `${name}-persist-v${version}`;
+}
+
+export function setupQueryPersistence(
+ params: Readonly<{
+ queryClient: QueryClientLike;
+ buster: string;
+ scopes: readonly PersistScope[];
+ }>
+) {
+ for (const scope of params.scopes) {
+ try {
+ persistQueryClient({
+ queryClient: params.queryClient as PersistQueryClient,
+ persister: scope.persister,
+ maxAge: scope.maxAgeMs,
+ buster: params.buster,
+ dehydrateOptions: {
+ shouldDehydrateQuery: (q) =>
+ q.state.status === 'success' && scope.shouldDehydrateQuery(q),
+ },
+ });
+ } catch {}
+ }
+}
diff --git a/js/app/packages/queries/storage/idb.ts b/js/app/packages/queries/storage/idb.ts
new file mode 100644
index 0000000000..ff6247a952
--- /dev/null
+++ b/js/app/packages/queries/storage/idb.ts
@@ -0,0 +1,85 @@
+import type {
+ PersistedClient,
+ Persister,
+} from '@tanstack/solid-query-persist-client';
+
+type IDBPersisterOptions = Readonly<{
+ dbName?: string;
+ storeName?: string;
+ key?: string;
+}>;
+
+const DEFAULT_DB = 'macro-query-cache';
+const DEFAULT_STORE = 'persisted';
+const DEFAULT_KEY = 'tanstack';
+
+function openDB(dbName: string, storeName: string): Promise<IDBDatabase> {
+ return new Promise((resolve, reject) => {
+ const req = indexedDB.open(dbName, 1);
+ req.onupgradeneeded = () => {
+ const db = req.result;
+ if (!db.objectStoreNames.contains(storeName)) {
+ db.createObjectStore(storeName);
+ }
+ };
+ req.onsuccess = () => resolve(req.result);
+ req.onerror = () => reject(req.error);
+ });
+}
+
+async function withStore<T>(
+ dbName: string,
+ storeName: string,
+ mode: IDBTransactionMode,
+ fn: (store: IDBObjectStore) => IDBRequest<T>
+): Promise<T> {
+ const db = await openDB(dbName, storeName);
+ try {
+ return await new Promise<T>((resolve, reject) => {
+ const tx = db.transaction(storeName, mode);
+ const store = tx.objectStore(storeName);
+ const req = fn(store);
+ req.onsuccess = () => resolve(req.result);
+ req.onerror = () => reject(req.error);
+ tx.onabort = () => reject(tx.error);
+ });
+ } finally {
+ db.close();
+ }
+}
+
+export function createIDBPersister(
+ options: IDBPersisterOptions = {}
+): Persister {
+ const dbName = options.dbName ?? DEFAULT_DB;
+ const storeName = options.storeName ?? DEFAULT_STORE;
+ const key = options.key ?? DEFAULT_KEY;
+
+ return {
+ persistClient: async (client: PersistedClient) => {
+ const value = JSON.stringify(client);
+ await withStore(dbName, storeName, 'readwrite', (store) =>
+ store.put(value, key)
+ );
+ },
+ restoreClient: async () => {
+ const value = await withStore<string | undefined>(
+ dbName,
+ storeName,
+ 'readonly',
+ (store) => store.get(key)
+ );
+ if (!value) return undefined;
+ try {
+ return JSON.parse(value) as PersistedClient;
+ } catch {
+ return undefined;
+ }
+ },
+ removeClient: async () => {
+ await withStore(dbName, storeName, 'readwrite', (store) =>
+ store.delete(key)
+ );
+ },
+ };
+}