Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
chore: dedupe and clean up md loader fetch w/tanstack (#4373)
Wolf Mermelsteind404b96d1778
diff --git a/js/app/packages/block-md/definition.ts b/js/app/packages/block-md/definition.tsindex 26e1607c06..dd54763baf 100644--- a/js/app/packages/block-md/definition.ts+++ b/js/app/packages/block-md/definition.ts@@ -5,13 +5,11 @@ import {loadResult,} from '@core/block';import { ENABLE_MARKDOWN_LIVE_COLLABORATION } from '@core/constant/featureFlags';-import { queryClient } from '@queries/client';-import { waitForDocumentSyncServiceReady } from '@queries/storage/document-location';-import type { DocumentLoadBundle } from '@queries/storage/documentLoad/documentLoadBundle';-import { documentLoadKeys } from '@queries/storage/documentLoad/keys';-import { storageServiceClient } from '@service-storage/client';-import type { AccessLevel } from '@service-storage/generated/schemas/accessLevel';-import type { DocumentMetadata } from '@service-storage/generated/schemas/documentMetadata';+import {+ fetchDocumentLocation,+ waitForDocumentSyncServiceReady,+} from '@queries/storage/document-location';+import { fetchDocumentLoadBundle } from '@queries/storage/documentLoad/documentLoadBundle';import { makeFileFromBlob } from '@service-storage/util/makeFileFromBlob';import { createSyncServiceSource } from '@service-sync/source';import { err, ok } from 'neverthrow';@@ -40,64 +38,42 @@ export const definition = defineBlock({});}- const pre = queryClient.getQueryData<DocumentLoadBundle>(- documentLoadKeys.bundle(documentId).queryKey- );-- let token: string;- let documentMetadata: DocumentMetadata;- let userAccessLevel: AccessLevel;+ const [maybeBundle, maybeLocation] = await Promise.all([+ fetchDocumentLoadBundle(documentId),+ loadResult(fetchDocumentLocation({ documentId })),+ ]);+ if (maybeBundle.isErr()) return err(maybeBundle.error);+ const { token, documentMetadata, userAccessLevel } = maybeBundle.value;- if (pre) {- token = pre.token;- documentMetadata = pre.documentMetadata;- userAccessLevel = pre.userAccessLevel;- } else {- const [maybeDocument, maybeLocation, maybeToken] = await Promise.all([- loadResult(storageServiceClient.getDocumentMetadata({ documentId })),- loadResult(storageServiceClient.getDocumentLocation({ documentId })),- storageServiceClient.permissionsTokens.createPermissionToken({- document_id: documentId,- }),- ]);+ if (maybeLocation.isErr()) return err(maybeLocation.error);- if (maybeToken.isErr()) return LoadErrors.UNAUTHORIZED;- if (maybeDocument.isErr()) return err(maybeDocument.error);- if (maybeLocation.isErr()) return err(maybeLocation.error);-- token = maybeToken.value.token;- const documentResult = maybeDocument.value;- documentMetadata = documentResult.documentMetadata;- userAccessLevel = documentResult.userAccessLevel;-- let { data: location } = maybeLocation.value;- if (- location.type === 'presignedUrl' &&- location.content.state === 'pending'- ) {- location = await waitForDocumentSyncServiceReady({- documentId,- }).catch((error) => {+ let location = maybeLocation.value;+ if (+ location.type === 'presignedUrl' &&+ location.content.state === 'pending'+ ) {+ location = await waitForDocumentSyncServiceReady({ documentId }).catch(+ (error) => {console.error('Failed waiting for markdown sync-service location',error);return location;- });- }+ }+ );+ }- // Markdown initialization and lifecycle persistence are backend-owned.- // If a markdown document still resolves to object storage here, opening- // it would require a backend repair/backfill path rather than a frontend- // sync-service mutation that leaves DB content metadata inconsistent.- if (location.type !== 'syncServiceContent') {- console.error(- 'Markdown document is not available in sync-service',- documentId,- location.content- );- return LoadErrors.INVALID;- }+ // Markdown initialization and lifecycle persistence are backend-owned.+ // If a markdown document still resolves to object storage here, opening+ // it would require a backend repair/backfill path rather than a frontend+ // sync-service mutation that leaves DB content metadata inconsistent.+ if (location.type !== 'syncServiceContent') {+ console.error(+ 'Markdown document is not available in sync-service',+ documentId,+ location.content+ );+ return LoadErrors.INVALID;}const { source: syncSource, doInitialSync } = createSyncServiceSource(diff --git a/js/app/packages/core/auth/logout.ts b/js/app/packages/core/auth/logout.tsindex d12fd2c85c..5770b567c9 100644--- a/js/app/packages/core/auth/logout.ts+++ b/js/app/packages/core/auth/logout.ts@@ -4,6 +4,7 @@ import { isNativeMobilePlatform } from '@core/mobile/isNativeMobilePlatform';import { syncLoginStorage } from '@core/util/cookies';import { authKeys } from '@queries/auth/user-info';import { queryClient } from '@queries/client';+import { clearDocumentQueryCache } from '@queries/storage/document-cache';import { authServiceClient } from '@service-auth/client';import { createCallback } from '@solid-primitives/rootless';import { useNavigate } from '@solidjs/router';@@ -16,6 +17,7 @@ export function useLogout() {document.cookie ='login=false; expires=Thu, 01 Jan 1970 00:00:00 UTC; max-age=0; path=/; SameSite=Lax';syncLoginStorage(false);+ clearDocumentQueryCache(queryClient);queryClient.setQueryData(authKeys.userInfo.queryKey, {id: '',diff --git a/js/app/packages/queries/storage/document-cache.test.ts b/js/app/packages/queries/storage/document-cache.test.tsnew file mode 100644index 0000000000..3796ed91c6--- /dev/null+++ b/js/app/packages/queries/storage/document-cache.test.ts@@ -0,0 +1,32 @@+import { QueryClient } from '@tanstack/solid-query';+import { beforeEach, describe, expect, it } from 'vitest';+import { clearDocumentQueryCache } from './document-cache';+import { documentLoadKeys } from './documentLoad/keys';+import { documentLocationKeys } from './keys';++describe('clearDocumentQueryCache', () => {+ let queryClient: QueryClient;++ beforeEach(() => {+ queryClient = new QueryClient();+ });++ it('removes user-scoped document data while preserving unrelated queries', () => {+ const documentId = 'document-1';+ const documentLoadKey = documentLoadKeys.bundle(documentId).queryKey;+ const documentLocationKey =+ documentLocationKeys.location(documentId).queryKey;++ queryClient.setQueryData(documentLoadKey, { token: 'sensitive-token' });+ queryClient.setQueryData(documentLocationKey, {+ type: 'syncServiceContent',+ });+ queryClient.setQueryData(['unrelated'], 'keep-me');++ clearDocumentQueryCache(queryClient);++ expect(queryClient.getQueryData(documentLoadKey)).toBeUndefined();+ expect(queryClient.getQueryData(documentLocationKey)).toBeUndefined();+ expect(queryClient.getQueryData(['unrelated'])).toBe('keep-me');+ });+});diff --git a/js/app/packages/queries/storage/document-cache.ts b/js/app/packages/queries/storage/document-cache.tsnew file mode 100644index 0000000000..3eee64fd14--- /dev/null+++ b/js/app/packages/queries/storage/document-cache.ts@@ -0,0 +1,9 @@+import type { QueryClient } from '@tanstack/solid-query';+import { documentLoadKeys } from './documentLoad/keys';+import { documentLocationKeys } from './keys';++/** Remove document data that may be scoped to the signed-in user. */+export function clearDocumentQueryCache(queryClient: QueryClient) {+ queryClient.removeQueries({ queryKey: documentLoadKeys._def });+ queryClient.removeQueries({ queryKey: documentLocationKeys._def });+}diff --git a/js/app/packages/queries/storage/document-location.ts b/js/app/packages/queries/storage/document-location.tsindex d56506fd12..2d8873a62c 100644--- a/js/app/packages/queries/storage/document-location.ts+++ b/js/app/packages/queries/storage/document-location.ts@@ -1,4 +1,5 @@import {+ catchToResult,type ResultError,type ResultType,throwOnErr,@@ -47,18 +48,17 @@ function retryDelay(return Math.min(initialDelayMs * 2 ** attempt, maxDelayMs);}-function _documentLocationQueryOptions(args: DocumentLocationArgs) {+function documentLocationQueryOptions(args: DocumentLocationArgs) {return {queryKey: documentLocationKeys.location(args.documentId, args.versionId).queryKey,- queryFn: () => fetchDocumentLocation(args),+ queryFn: () => requestDocumentLocation(args),staleTime: 5 * 60 * 1000,gcTime: 10 * 60 * 1000,- enabled: !!args.documentId,};}-async function fetchDocumentLocation(+async function requestDocumentLocation(args: DocumentLocationArgs): Promise<DocumentLocation> {return throwOnErr(async () =>@@ -66,6 +66,18 @@ async function fetchDocumentLocation().then((result) => result.data);}+/**+ * Fetches the current document location through TanStack Query, deduping with+ * the readiness-wait helpers that share the same cache key.+ */+export async function fetchDocumentLocation(+ args: DocumentLocationArgs+): Promise<Result<DocumentLocation, ResultError<string>[]>> {+ return catchToResult(() =>+ queryClient.fetchQuery(documentLocationQueryOptions(args))+ );+}+function invalidateDocumentLocation(args: DocumentLocationArgs) {storageServiceClient.getDocumentLocation.invalidate({documentId: args.documentId,@@ -100,7 +112,7 @@ async function waitForDocumentLocation().queryKey,queryFn: async () => {await invalidateDocumentLocation(args);- const location = await fetchDocumentLocation(args);+ const location = await requestDocumentLocation(args);lastLocation = location;if (!options.isReady(location)) {diff --git a/js/app/packages/queries/storage/documentLoad/documentLoadBundle.ts b/js/app/packages/queries/storage/documentLoad/documentLoadBundle.tsindex c3d70b77fa..c98b01227e 100644--- a/js/app/packages/queries/storage/documentLoad/documentLoadBundle.ts+++ b/js/app/packages/queries/storage/documentLoad/documentLoadBundle.ts@@ -1,6 +1,9 @@+import { type LoadErrors, loadResult } from '@core/block';+import { catchToResult, type ResultError, throwOnErr } from '@core/util/result';import { storageServiceClient } from '@service-storage/client';import type { AccessLevel } from '@service-storage/generated/schemas/accessLevel';import type { DocumentMetadata } from '@service-storage/generated/schemas/documentMetadata';+import type { Result } from 'neverthrow';import { queryClient } from '../../client';import { documentLoadKeys } from './keys';@@ -10,39 +13,53 @@ export type DocumentLoadBundle = {token: string;};+type LoadBundleResult = Result<+ DocumentLoadBundle,+ ResultError<keyof typeof LoadErrors>[]+>;+const STALE_TIME = 60 * 1000;const GC_TIME = 60 * 1000;-async function fetchDocumentLoadBundle(+async function resolveDocumentLoadBundle(documentId: string): Promise<DocumentLoadBundle> {const [maybeDocument, maybeToken] = await Promise.all([- storageServiceClient.getDocumentMetadata({ documentId }),- storageServiceClient.permissionsTokens.createPermissionToken({- document_id: documentId,- }),+ throwOnErr(() => storageServiceClient.getDocumentMetadata({ documentId })),+ throwOnErr(() =>+ storageServiceClient.permissionsTokens.createPermissionToken({+ document_id: documentId,+ })+ ),]);- if (maybeToken.isErr()) throw new Error('UNAUTHORIZED');- if (maybeDocument.isErr())- throw new Error('Failed to fetch document metadata');-return {- documentMetadata: maybeDocument.value.documentMetadata,- userAccessLevel: maybeDocument.value.userAccessLevel,- token: maybeToken.value.token,+ documentMetadata: maybeDocument.documentMetadata,+ userAccessLevel: maybeDocument.userAccessLevel,+ token: maybeToken.token,};}export function documentLoadQueryOptions(documentId: string) {return {queryKey: documentLoadKeys.bundle(documentId).queryKey,- queryFn: () => fetchDocumentLoadBundle(documentId),+ queryFn: () => resolveDocumentLoadBundle(documentId),staleTime: STALE_TIME,gcTime: GC_TIME,+ retry: false,};}+export function fetchDocumentLoadBundle(+ documentId: string+): Promise<LoadBundleResult> {+ return loadResult(+ catchToResult(() =>+ queryClient.fetchQuery(documentLoadQueryOptions(documentId))+ )+ );+}+export function seedDocumentLoadBundle(documentId: string,bundle: DocumentLoadBundle