From 2e7a3ecfd89cf04e7ab4e5941e871b3ca89f6e73 Mon Sep 17 00:00:00 2001 From: Nathan Date: Wed, 17 Sep 2025 12:50:32 +0800 Subject: [PATCH] chore: fix key --- src/application/database-yjs/dispatch.ts | 11 +++++------ src/application/database-yjs/row_meta.ts | 19 +++++-------------- .../services/js-services/cache/index.ts | 4 ++-- .../app/hooks/useDatabaseOperations.ts | 18 +++++++++--------- src/components/database/Database.tsx | 6 +++--- .../cell/relation/RelationCellMenuContent.tsx | 4 ++-- .../cell/relation/RelationItems.tsx | 9 ++++----- 7 files changed, 30 insertions(+), 41 deletions(-) diff --git a/src/application/database-yjs/dispatch.ts b/src/application/database-yjs/dispatch.ts index 5ff5a5fc..8e83caf0 100644 --- a/src/application/database-yjs/dispatch.ts +++ b/src/application/database-yjs/dispatch.ts @@ -48,7 +48,7 @@ import { createSelectOptionCell } from '@/application/database-yjs/fields/select import { createDateTimeField, createTextField } from '@/application/database-yjs/fields/text/utils'; import { dateFilterFillData, filterFillData, getDefaultFilterCondition } from '@/application/database-yjs/filter'; import { getOptionsFromRow, initialDatabaseRow } from '@/application/database-yjs/row'; -import { generateRowMeta, getMetaIdMap, getMetaJSON, getRowDocumentId } from '@/application/database-yjs/row_meta'; +import { generateRowMeta, getMetaIdMap, getMetaJSON, getRowKey } from '@/application/database-yjs/row_meta'; import { useBoardLayoutSettings, useCalendarLayoutSetting, useDatabaseViewLayout, useFieldSelector, useFieldType } from '@/application/database-yjs/selector'; import { executeOperations } from '@/application/slate-yjs/utils/yjs'; import { @@ -1222,9 +1222,8 @@ export function useNewRowDispatch() { } const rowId = uuidv4(); - const documentId = getRowDocumentId(rowId); - - const rowDoc = await createRow(documentId); + const rowKey = getRowKey(guid, rowId); + const rowDoc = await createRow(rowKey); let shouldOpenRowModal = false; rowDoc.transact(() => { @@ -1505,8 +1504,8 @@ export function useDuplicateRowDispatch() { [RowMetaKey.CoverId]: cover ? JSON.stringify(cover) : null, }); - const documentId = getRowDocumentId(rowId); - const rowDoc = await createRow(documentId); + const rowKey = getRowKey(guid, rowId); + const rowDoc = await createRow(rowKey); rowDoc.transact(() => { initialDatabaseRow(rowId, database.get(YjsDatabaseKey.id), rowDoc); diff --git a/src/application/database-yjs/row_meta.ts b/src/application/database-yjs/row_meta.ts index a8858f82..41e7669f 100644 --- a/src/application/database-yjs/row_meta.ts +++ b/src/application/database-yjs/row_meta.ts @@ -34,17 +34,8 @@ export function generateRowMeta(rowId: string, data: Record>(); -/** - * Generate row document ID from row ID using UUID v5 - * This matches the Rust implementation: - * pub fn database_row_document_id_from_row_id(row_id: &RowId) -> RowId { - * Uuid::new_v5(row_id, RowMetaKey::DocumentId.as_str().as_bytes()) - * } - */ -export function getRowDocumentId(rowId: string): string { - const parser = metaIdFromRowId(rowId); - - return parser(RowMetaKey.DocumentId); +export function getRowKey(guid: string, rowId: string): string { + return `${guid}_rows_${rowId}`; } export function getMetaIdMap(rowId: string) { @@ -80,9 +71,9 @@ export function getMetaJSON(rowId: string, meta: Y.Map) { try { cover = metaJson[coverKey] ? (JSON.parse(metaJson[coverKey]) as { - data: string; - cover_type: RowCoverType; - }) + data: string; + cover_type: RowCoverType; + }) : null; } catch (e) { // do nothing diff --git a/src/application/services/js-services/cache/index.ts b/src/application/services/js-services/cache/index.ts index e71b3742..9785f427 100644 --- a/src/application/services/js-services/cache/index.ts +++ b/src/application/services/js-services/cache/index.ts @@ -1,5 +1,5 @@ +import { getRowKey } from '@/application/database-yjs/row_meta'; import { closeCollabDB, db, openCollabDB } from '@/application/db'; -import { getRowDocumentId } from '@/application/database-yjs/row_meta'; import { Fetcher, StrategyType } from '@/application/services/js-services/cache/types'; import { DatabaseId, @@ -275,7 +275,7 @@ async function updateRows(collab: YDoc, rows: Record) { const bulkData = []; for (const [key, value] of Object.entries(rows)) { - const rowKey = getRowDocumentId(key); + const rowKey = getRowKey(collab.guid, key); const doc = await createRowDoc(rowKey); const dbRow = await db.rows.get(key); diff --git a/src/components/app/hooks/useDatabaseOperations.ts b/src/components/app/hooks/useDatabaseOperations.ts index 726550c5..0af8f6e8 100644 --- a/src/components/app/hooks/useDatabaseOperations.ts +++ b/src/components/app/hooks/useDatabaseOperations.ts @@ -1,29 +1,29 @@ -import { useCallback, useRef } from 'react'; import { PromptDatabaseConfiguration } from '@/components/chat'; +import { useCallback, useRef } from 'react'; +import { FieldType } from '@/application/database-yjs'; +import { getCellDataText } from '@/application/database-yjs/cell.parse'; +import { getRowKey } from '@/application/database-yjs/row_meta'; import { DatabasePrompt, DatabasePromptField, DatabasePromptRow, GenerateAISummaryRowPayload, GenerateAITranslateRowPayload, - YDoc, YDatabase, - YjsEditorKey, + YDoc, YjsDatabaseKey, + YjsEditorKey, } from '@/application/types'; -import { FieldType } from '@/application/database-yjs'; -import { getCellDataText } from '@/application/database-yjs/cell.parse'; -import { getRowDocumentId } from '@/application/database-yjs/row_meta'; import { useAuthInternal } from '../contexts/AuthInternalContext'; // Hook for managing database-related operations export function useDatabaseOperations( - loadView?: (id: string, isSubDocument?: boolean, loadAwareness?: boolean) => Promise, + loadView?: (id: string, isSubDocument?: boolean, loadAwareness?: boolean) => Promise, createRowDoc?: (rowKey: string) => Promise ) { const { service, currentWorkspaceId } = useAuthInternal(); - + const rowDocsRef = useRef>(new Map()); // Generate AI summary for row @@ -80,7 +80,7 @@ export function useDatabaseOperations( if (!createRowDoc) return; - const rowKey = getRowDocumentId(row.id); + const rowKey = getRowKey(doc?.guid || '', row.id); const rowDoc = await createRowDoc(rowKey); const rowSharedRoot = rowDoc?.getMap(YjsEditorKey.data_section); diff --git a/src/components/database/Database.tsx b/src/components/database/Database.tsx index 72193ed0..178dcf01 100644 --- a/src/components/database/Database.tsx +++ b/src/components/database/Database.tsx @@ -1,7 +1,7 @@ import { debounce } from 'lodash-es'; import { useCallback, useEffect, useMemo, useState } from 'react'; -import { getRowDocumentId } from '@/application/database-yjs/row_meta'; +import { getRowKey } from '@/application/database-yjs/row_meta'; import { AppendBreadcrumb, CreateFolderViewPayload, @@ -80,7 +80,7 @@ function Database(props: Database2Props) { return; } - const rowKey = getRowDocumentId(id); + const rowKey = getRowKey(doc.guid, id); const rowDoc = await createRowDoc(rowKey); return { id, rowDoc }; @@ -177,7 +177,7 @@ function Database(props: Database2Props) { setOpenModalViewId(viewId); setOpenModalRowDatabaseDoc(viewDoc); - const rowDoc = await createRowDoc?.(getRowDocumentId(rowId)); + const rowDoc = await createRowDoc?.(getRowKey(viewDoc.guid, rowId)); if (!rowDoc) { throw new Error('Row document not found'); diff --git a/src/components/database/components/cell/relation/RelationCellMenuContent.tsx b/src/components/database/components/cell/relation/RelationCellMenuContent.tsx index 58fc4f9f..00aad31c 100644 --- a/src/components/database/components/cell/relation/RelationCellMenuContent.tsx +++ b/src/components/database/components/cell/relation/RelationCellMenuContent.tsx @@ -3,7 +3,7 @@ import { useTranslation } from 'react-i18next'; import { getPrimaryFieldId, useDatabaseContext } from '@/application/database-yjs'; import { parseYDatabaseCellToCell } from '@/application/database-yjs/cell.parse'; -import { getRowDocumentId } from '@/application/database-yjs/row_meta'; +import { getRowKey } from '@/application/database-yjs/row_meta'; import { View, YDatabase, YDatabaseRow, YDoc, YjsDatabaseKey, YjsEditorKey } from '@/application/types'; import { ReactComponent as MinusIcon } from '@/assets/icons/minus.svg'; import { ReactComponent as PlusIcon } from '@/assets/icons/plus.svg'; @@ -140,7 +140,7 @@ function RelationCellMenuContent({ continue; } - const rowKey = getRowDocumentId(rowId); + const rowKey = getRowKey(guid, rowId); const rowDoc = await createRowDoc(rowKey); rowDocsRef.current.set(rowId, rowDoc); diff --git a/src/components/database/components/cell/relation/RelationItems.tsx b/src/components/database/components/cell/relation/RelationItems.tsx index bde069ec..cc2ff6b0 100644 --- a/src/components/database/components/cell/relation/RelationItems.tsx +++ b/src/components/database/components/cell/relation/RelationItems.tsx @@ -8,7 +8,7 @@ import { useDatabaseIdFromField, } from '@/application/database-yjs'; import { RelationCell, RelationCellData } from '@/application/database-yjs/cell.type'; -import { getRowDocumentId } from '@/application/database-yjs/row_meta'; +import { getRowKey } from '@/application/database-yjs/row_meta'; import { YDoc, YjsEditorKey } from '@/application/types'; import { notify } from '@/components/_shared/notify'; import { RelationPrimaryValue } from '@/components/database/components/cell/relation/RelationPrimaryValue'; @@ -69,7 +69,7 @@ function RelationItems({ const rows: Record = {}; for (const rowId of rowIds) { - const rowDoc = await createRowDoc(getRowDocumentId(rowId)); + const rowDoc = await createRowDoc(getRowKey(docGuid, rowId)); rows[rowId] = rowDoc; } @@ -161,9 +161,8 @@ function RelationItems({ notify.error(e.message); } }} - className={`min-w-fit overflow-hidden text-text-primary underline ${ - relatedViewId ? 'cursor-pointer hover:text-text-action' : '' - }`} + className={`min-w-fit overflow-hidden text-text-primary underline ${relatedViewId ? 'cursor-pointer hover:text-text-action' : '' + }`} >