Skip to content

Commit 9c8cce4

Browse files
committed
fix: send row document source when creating row pages
1 parent b615fa5 commit 9c8cce4

9 files changed

Lines changed: 64 additions & 18 deletions

File tree

src/application/database-yjs/context.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
LoadView,
1717
LoadViewMeta,
1818
RowId,
19+
RowDocumentSourcePayload,
1920
Subscription,
2021
TestDatabasePromptConfig,
2122
TimeFormat,
@@ -84,7 +85,7 @@ export interface DatabaseContextState {
8485
* Only available in app mode - not provided in publish mode.
8586
* Returns the doc_state (Y.js update) to initialize the local document.
8687
*/
87-
createRowDocument?: (documentId: string) => Promise<Uint8Array | null>;
88+
createRowDocument?: (documentId: string, source?: RowDocumentSourcePayload) => Promise<Uint8Array | null>;
8889
/** Fire-and-forget: ask the server to duplicate the row document with inline DB deep copy. */
8990
duplicateRowDocument?: (databaseId: string, sourceRowId: string, newRowId: string, clientDocStateB64?: string) => Promise<void>;
9091
navigateToView?: (viewId: string, blockId?: string) => Promise<void>;

src/application/services/js-services/http/view-api.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AppOutlineResponse } from '@/application/services/services.type';
2-
import { View } from '@/application/types';
2+
import { CreateOrphanedViewPayload, View } from '@/application/types';
33

44
import { APIResponse, executeAPIRequest, getAxios } from './core';
55

@@ -149,7 +149,7 @@ export async function getAppTrash(workspaceId: string) {
149149
);
150150
}
151151

152-
export async function createOrphanedView(workspaceId: string, payload: { document_id: string }): Promise<Uint8Array> {
152+
export async function createOrphanedView(workspaceId: string, payload: CreateOrphanedViewPayload): Promise<Uint8Array> {
153153
const url = `/api/workspace/${workspaceId}/orphaned-view`;
154154

155155
// Server returns doc_state as Vec<u8> which is JSON encoded as number[]

src/application/types.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,17 @@ export interface UpdatePagePayload {
13481348
is_locked?: boolean;
13491349
}
13501350

1351+
export interface RowDocumentSourcePayload {
1352+
database_id: string;
1353+
database_view_id: string;
1354+
row_id: string;
1355+
}
1356+
1357+
export interface CreateOrphanedViewPayload {
1358+
document_id: string;
1359+
row_document_source?: RowDocumentSourcePayload;
1360+
}
1361+
13511362
export type ViewMetaCover = ViewCover;
13521363

13531364
export interface ViewMetaProps {
@@ -1396,7 +1407,7 @@ export interface ViewComponentProps {
13961407
* Create a row document on the server (orphaned view).
13971408
* Only available in app mode - not provided in publish mode.
13981409
*/
1399-
createRowDocument?: (documentId: string) => Promise<Uint8Array | null>;
1410+
createRowDocument?: (documentId: string, source?: RowDocumentSourcePayload) => Promise<Uint8Array | null>;
14001411
duplicateRowDocument?: (
14011412
databaseId: string,
14021413
sourceRowId: string,

src/components/app/contexts/AppOperationsContext.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { SyncContext } from '@/application/services/js-services/sync-protocol';
55
import {
66
CreateDatabaseViewPayload,
77
CreateDatabaseViewResponse,
8+
CreateOrphanedViewPayload,
89
DuplicatePageOperationOptions,
910
CreatePagePayload,
1011
CreatePageResponse,
@@ -15,6 +16,7 @@ import {
1516
LoadDatabasePrompts,
1617
LoadView,
1718
LoadViewMeta,
19+
RowDocumentSourcePayload,
1820
Subscription,
1921
TestDatabasePromptConfig,
2022
TextCount,
@@ -113,7 +115,7 @@ export interface AppOperationsContextType {
113115

114116
// ── Database operations ────────────────────────────────────────────
115117
/** Create an orphaned view (e.g. for inline database within a document). */
116-
createOrphanedView?: (payload: { document_id: string }) => Promise<Uint8Array>;
118+
createOrphanedView?: (payload: CreateOrphanedViewPayload) => Promise<Uint8Array>;
117119
/** Load AI prompt templates for a database. */
118120
loadDatabasePrompts?: LoadDatabasePrompts;
119121
/** Test an AI prompt config against a database. */
@@ -123,7 +125,7 @@ export interface AppOperationsContextType {
123125
/** Load an existing row document. */
124126
loadRowDocument?: (documentId: string) => Promise<YDoc | null>;
125127
/** Create a new row document (returns encoded initial state). */
126-
createRowDocument?: (documentId: string) => Promise<Uint8Array | null>;
128+
createRowDocument?: (documentId: string, source?: RowDocumentSourcePayload) => Promise<Uint8Array | null>;
127129
/** Fire-and-forget: ask the server to duplicate the row document with inline DB deep copy. */
128130
duplicateRowDocument?: (databaseId: string, sourceRowId: string, newRowId: string, clientDocStateB64?: string) => Promise<void>;
129131
/** Resolve a database ID to its primary view ID. */

src/components/app/hooks/useDatabaseOperations.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
DatabasePromptRow,
1212
GenerateAISummaryRowPayload,
1313
GenerateAITranslateRowPayload,
14+
RowDocumentSourcePayload,
1415
Types,
1516
YDatabase,
1617
YDoc,
@@ -332,15 +333,18 @@ export function useDatabaseOperations(
332333

333334
// Create a row document on the server (orphaned view)
334335
const createRowDocument = useCallback(
335-
async (documentId: string): Promise<Uint8Array | null> => {
336+
async (documentId: string, source?: RowDocumentSourcePayload): Promise<Uint8Array | null> => {
336337
if (!currentWorkspaceId) {
337338
Log.warn('[createRowDocument] service or workspaceId not available');
338339
return null;
339340
}
340341

341342
try {
342-
Log.debug('[createRowDocument] creating', { documentId });
343-
const docState = await ViewService.createOrphaned(currentWorkspaceId, { document_id: documentId });
343+
Log.debug('[createRowDocument] creating', { documentId, source });
344+
const docState = await ViewService.createOrphaned(currentWorkspaceId, {
345+
document_id: documentId,
346+
row_document_source: source,
347+
});
344348

345349
return docState;
346350
} catch (e) {

src/components/app/hooks/usePageOperations.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@/application/services/js-services/http/publish-api';
1212
import {
1313
CreateDatabaseViewPayload,
14+
CreateOrphanedViewPayload,
1415
DuplicatePageOperationOptions,
1516
CreatePagePayload,
1617
CreateSpacePayload,
@@ -458,7 +459,7 @@ export function usePageOperations({
458459

459460
// Create orphaned view
460461
const createOrphanedViewOp = useCallback(
461-
async (payload: { document_id: string }) => {
462+
async (payload: CreateOrphanedViewPayload) => {
462463
if (!currentWorkspaceId) {
463464
throw new Error('No workspace or service found');
464465
}

src/components/database/Database.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
LoadView,
2828
LoadViewMeta,
2929
RowId,
30+
RowDocumentSourcePayload,
3031
UIVariant,
3132
UpdatePagePayload,
3233
View,
@@ -74,7 +75,7 @@ export interface Database2Props {
7475
* Create a row document on the server (orphaned view).
7576
* Only available in app mode - not provided in publish mode.
7677
*/
77-
createRowDocument?: (documentId: string) => Promise<Uint8Array | null>;
78+
createRowDocument?: (documentId: string, source?: RowDocumentSourcePayload) => Promise<Uint8Array | null>;
7879
duplicateRowDocument?: (
7980
databaseId: string,
8081
sourceRowId: string,

src/components/database/components/database-row/DatabaseRowSubDocument.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
YDatabaseRow,
2626
YDoc,
2727
YDocWithMeta,
28+
RowDocumentSourcePayload,
2829
YjsDatabaseKey,
2930
YjsEditorKey,
3031
} from '@/application/types';
@@ -124,6 +125,20 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
124125
const createRowDocument = context?.createRowDocument;
125126
const checkIfRowDocumentExists = context?.checkIfRowDocumentExists;
126127
const bindViewSync = context?.bindViewSync;
128+
const rowDocumentSource = useMemo<RowDocumentSourcePayload | undefined>(() => {
129+
const databaseId = (database?.get(YjsDatabaseKey.id) as string | undefined) || context?.databaseDoc?.guid;
130+
const databaseViewId = context?.activeViewId || context?.databasePageId;
131+
132+
if (!databaseId || !databaseViewId) {
133+
return undefined;
134+
}
135+
136+
return {
137+
database_id: databaseId,
138+
database_view_id: databaseViewId,
139+
row_id: rowId,
140+
};
141+
}, [context?.activeViewId, context?.databaseDoc, context?.databasePageId, database, rowId]);
127142
const updateRowMeta = useUpdateRowMetaDispatch(rowId);
128143
const editorRef = useRef<YjsEditor | null>(null);
129144
const lastIsEmptyRef = useRef<boolean | null>(null);
@@ -412,7 +427,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
412427

413428
try {
414429
Log.debug('[DatabaseRowSubDocument] calling createRowDocument', { documentId, requireServerReady });
415-
docState = await createRowDocument(documentId);
430+
docState = await createRowDocument(documentId, rowDocumentSource);
416431
Log.debug('[DatabaseRowSubDocument] createRowDocument success', {
417432
documentId,
418433
docStateSize: docState?.length ?? 0,
@@ -452,7 +467,15 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
452467
setLoading(false);
453468
}
454469
},
455-
[createRowDocument, handleOpenDocument, hasLocalDocContent, loadRowDocument, openDocumentWithState, rowId]
470+
[
471+
createRowDocument,
472+
handleOpenDocument,
473+
hasLocalDocContent,
474+
loadRowDocument,
475+
openDocumentWithState,
476+
rowDocumentSource,
477+
rowId,
478+
]
456479
);
457480

458481
const scheduleEnsureRowDocumentExists = useCallback(() => {
@@ -477,7 +500,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
477500
rowId,
478501
documentId,
479502
});
480-
await createRowDocument(documentId);
503+
await createRowDocument(documentId, rowDocumentSource);
481504
}
482505

483506
if (pendingNonEmptyRef.current) {
@@ -507,6 +530,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
507530
createRowDocument,
508531
documentId,
509532
isDocumentEmpty,
533+
rowDocumentSource,
510534
updateRowMeta,
511535
rowId,
512536
]);
@@ -682,7 +706,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
682706
rowId,
683707
documentId,
684708
});
685-
await createRowDocument(documentId);
709+
await createRowDocument(documentId, rowDocumentSource);
686710
} catch {
687711
// Ignore; we'll retry loading below.
688712
}
@@ -722,6 +746,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
722746
isDocumentEmptyResolved,
723747
hasLocalDocContent,
724748
createRowDocument,
749+
rowDocumentSource,
725750
rowId,
726751
doc,
727752
]);
@@ -823,7 +848,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
823848
// Only create orphaned view if document doesn't exist
824849
if (createRowDocument) {
825850
try {
826-
await createRowDocument(documentId);
851+
await createRowDocument(documentId, rowDocumentSource);
827852
rowDocEnsuredRef.current = true;
828853
return true;
829854
} catch {
@@ -833,7 +858,7 @@ export const DatabaseRowSubDocument = memo(({ rowId }: { rowId: string }) => {
833858
}
834859

835860
return false;
836-
}, [checkIfRowDocumentExists, createRowDocument, documentId]);
861+
}, [checkIfRowDocumentExists, createRowDocument, documentId, rowDocumentSource]);
837862

838863
useEffect(() => {
839864
if (!doc) return;

src/components/editor/EditorContext.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
Subscription,
2525
MentionablePerson,
2626
DatabaseRelations,
27+
RowDocumentSourcePayload,
2728
YDoc,
2829
} from '@/application/types';
2930
import { SyncContext } from '@/application/services/js-services/sync-protocol';
@@ -77,7 +78,7 @@ export interface EditorContextState {
7778
loadView?: LoadView;
7879
loadRowDocument?: (documentId: string) => Promise<YDoc | null>;
7980
checkIfRowDocumentExists?: (documentId: string) => Promise<boolean>;
80-
createRowDocument?: (documentId: string) => Promise<Uint8Array | null>;
81+
createRowDocument?: (documentId: string, source?: RowDocumentSourcePayload) => Promise<Uint8Array | null>;
8182
createRow?: CreateRow;
8283
bindViewSync?: (doc: YDoc) => SyncContext | null;
8384
readSummary?: boolean;

0 commit comments

Comments
 (0)