Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions js/app/packages/app/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="/manifest.json" />
<!-- Preload the blank markdown Loro snapshot so create-new-doc is instant. -->
<link rel="preload" href="/markdown-golden.1.bin" as="fetch" crossorigin />
</head>

<body>
Expand Down
Binary file added js/app/packages/app/public/markdown-golden.1.bin
Binary file not shown.
96 changes: 56 additions & 40 deletions js/app/packages/block-md/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@ 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 { makeFileFromBlob } from '@service-storage/util/makeFileFromBlob';
import { createSyncServiceSource } from '@service-sync/source';
import { err, ok } from 'neverthrow';
Expand Down Expand Up @@ -35,53 +40,64 @@ export const definition = defineBlock({
});
}

const [maybeDocument, maybeLocation, maybeToken] = await Promise.all([
loadResult(storageServiceClient.getDocumentMetadata({ documentId })),
loadResult(storageServiceClient.getDocumentLocation({ documentId })),
storageServiceClient.permissionsTokens.createPermissionToken({
document_id: documentId,
}),
]);
const pre = queryClient.getQueryData<DocumentLoadBundle>(
documentLoadKeys.bundle(documentId).queryKey
);

if (maybeToken.isErr()) {
return LoadErrors.UNAUTHORIZED;
}
let token: string;
let documentMetadata: DocumentMetadata;
let userAccessLevel: AccessLevel;

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,
}),
]);

const { token } = maybeToken.value;
if (maybeToken.isErr()) return LoadErrors.UNAUTHORIZED;
if (maybeDocument.isErr()) return err(maybeDocument.error);
if (maybeLocation.isErr()) return err(maybeLocation.error);

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;

const documentResult = maybeDocument.value;
const { documentMetadata, userAccessLevel } = documentResult;
let { data: location } = maybeLocation.value;
let { data: 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;
});
}

if (
location.type === 'presignedUrl' &&
location.content.state === 'pending'
) {
location = await waitForDocumentSyncServiceReady({
documentId,
}).catch((error) => {
// 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(
'Failed waiting for markdown sync-service location',
error
'Markdown document is not available in sync-service',
documentId,
location.content
);
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;
return LoadErrors.INVALID;
}
Comment on lines +47 to +100

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can pull all of this out into a tanstack query, and expose a method to seed the cache. This way you don't need the hacky cache, you can let tanstack manage caching the result for this.

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 { queryClient } from '../client';
import { documentLoadKeys } from './keys';

export type DocumentLoadBundle = {
  documentMetadata: DocumentMetadata;
  userAccessLevel: AccessLevel;
  token: string;
};

const STALE_TIME = 60 * 1000;
const GC_TIME = 10 * 60 * 1000;

async function fetchDocumentLoadBundle(
  documentId: string
): Promise<DocumentLoadBundle> {
  const [maybeDocument, maybeToken] = await Promise.all([
    storageServiceClient.getDocumentMetadata({ documentId }),
    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,
  };
}

export function documentLoadQueryOptions(documentId: string) {
  return {
    queryKey: documentLoadKeys.bundle(documentId).queryKey,
    queryFn: () => fetchDocumentLoadBundle(documentId),
    staleTime: STALE_TIME,
    gcTime: GC_TIME,
  };
}

/** Seed the bundle from a create response so the loader serves it from cache. */
export function seedDocumentLoadBundle(
  documentId: string,
  bundle: DocumentLoadBundle
) {
  queryClient.setQueryData(documentLoadKeys.bundle(documentId).queryKey, bundle);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only caveat, is you might want to prevent caching when just opening a document. Perhaps by immediately invalidating it

}

const { source: syncSource, doInitialSync } = createSyncServiceSource(
Expand Down
19 changes: 17 additions & 2 deletions js/app/packages/core/util/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ import { invalidateUserQuota } from '@queries/auth';
import { postNewHistoryItem } from '@queries/history/history';
import { setPreviewOnCreate } from '@queries/preview/preview';
import { refetchSoupEntity } from '@queries/soup/cache';
import { seedDocumentLoadBundle } from '@queries/storage/documentLoad/documentLoadBundle';
import { cognitionApiServiceClient } from '@service-cognition/client';
import type { CreateChatRequest } from '@service-cognition/generated/schemas';
import { staticFileClient } from '@service-static-files/client';
import { storageServiceClient } from '@service-storage/client';
import { AccessLevel } from '@service-storage/generated/schemas/accessLevel';
import type { PropertyInput } from '@service-storage/generated/schemas/propertyInput';

import { uploadToPresignedUrl } from '@service-storage/util/uploadToPresignedUrl';
import { err, ok } from 'neverthrow';
import { isPaymentError } from './handlePaymentError';
Expand Down Expand Up @@ -45,7 +48,13 @@ export async function createMarkdownFile(

if (result.isErr()) return;

const { documentId } = result.value;
const { documentId, documentMetadata, token } = result.value;

seedDocumentLoadBundle(documentId, {
documentMetadata,
userAccessLevel: AccessLevel.owner,
token,
});

setPreviewOnCreate({
itemId: documentId,
Expand Down Expand Up @@ -101,7 +110,13 @@ export async function createTask(

if (result.isErr()) return;

const { documentId } = result.value;
const { documentId, documentMetadata, token } = result.value;

seedDocumentLoadBundle(documentId, {
documentMetadata,
userAccessLevel: AccessLevel.owner,
token,
});

setPreviewOnCreate({
itemId: documentId,
Expand Down
54 changes: 54 additions & 0 deletions js/app/packages/queries/storage/documentLoad/documentLoadBundle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 { queryClient } from '../../client';
import { documentLoadKeys } from './keys';

export type DocumentLoadBundle = {
documentMetadata: DocumentMetadata;
userAccessLevel: AccessLevel;
token: string;
};

const STALE_TIME = 60 * 1000;
const GC_TIME = 60 * 1000;

async function fetchDocumentLoadBundle(
documentId: string
): Promise<DocumentLoadBundle> {
const [maybeDocument, maybeToken] = await Promise.all([
storageServiceClient.getDocumentMetadata({ documentId }),
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,
};
}

export function documentLoadQueryOptions(documentId: string) {
return {
queryKey: documentLoadKeys.bundle(documentId).queryKey,
queryFn: () => fetchDocumentLoadBundle(documentId),
staleTime: STALE_TIME,
gcTime: GC_TIME,
};
}

export function seedDocumentLoadBundle(
documentId: string,
bundle: DocumentLoadBundle
) {
queryClient.setQueryData(
documentLoadKeys.bundle(documentId).queryKey,
bundle
);
}
7 changes: 7 additions & 0 deletions js/app/packages/queries/storage/documentLoad/keys.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createQueryKeys } from '@lukemorales/query-key-factory';

export const documentLoadKeys = createQueryKeys('documentLoad', {
bundle: (documentId: string) => ({
queryKey: [documentId],
}),
});
6 changes: 5 additions & 1 deletion js/app/packages/service-clients/service-storage/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,7 +1052,11 @@ export const storageServiceClient = {
}

const response = result.value;
return ok({ documentId: response.documentId });
return ok({
documentId: response.documentId,
documentMetadata: response.documentMetadata,
token: response.token,
});
},

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
import type { DocumentResponseMetadata } from './documentResponseMetadata';

/**
* Response for creating a markdown document.
*/
export interface CreateMarkdownDocumentResponse {
/** The document ID of the created markdown document. */
/** The document ID of the created markdown document */
documentId: string;
/** Metadata for the created document */
documentMetadata: DocumentResponseMetadata;
/** A pre-generated permission token that you can use for SS */
token: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/
import type { DocumentResponseMetadata } from './documentResponseMetadata';

/**
* Response for creating a markdown document.
*/
export type CreateMarkdownHandler200 = {
/** The document ID of the created markdown document. */
/** The document ID of the created markdown document */
documentId: string;
/** Metadata for the created document */
documentMetadata: DocumentResponseMetadata;
/** A pre-generated permission token that you can use for SS */
token: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/

import type { CreateTaskHandler200TeamId } from './createTaskHandler200TeamId';
import type { CreateTaskHandler200TeamTaskId } from './createTaskHandler200TeamTaskId';
import type { DocumentResponseMetadata } from './documentResponseMetadata';

/**
* Response for creating a task.
*/
export type CreateTaskHandler200 = {
/** The document ID of the created task. */
documentId: string;
/** Metadata for the created document */
documentMetadata: DocumentResponseMetadata;
/** The team this task number is scoped to. */
teamId?: CreateTaskHandler200TeamId;
/** The task number assigned within the team. */
teamTaskId?: CreateTaskHandler200TeamTaskId;
/** A pre-generated permission token that you can use for SS */
token: string;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,23 @@
* document_storage_service
* OpenAPI spec version: 0.1.0
*/

import type { CreateTaskResponseTeamId } from './createTaskResponseTeamId';
import type { CreateTaskResponseTeamTaskId } from './createTaskResponseTeamTaskId';
import type { DocumentResponseMetadata } from './documentResponseMetadata';

/**
* Response for creating a task.
*/
export interface CreateTaskResponse {
/** The document ID of the created task. */
documentId: string;
/** Metadata for the created document */
documentMetadata: DocumentResponseMetadata;
/** The team this task number is scoped to. */
teamId?: CreateTaskResponseTeamId;
/** The task number assigned within the team. */
teamTaskId?: CreateTaskResponseTeamTaskId;
/** A pre-generated permission token that you can use for SS */
token: string;
}
Loading