From 570a67c746ad43b7a28f3109863b4cc3ec794e55 Mon Sep 17 00:00:00 2001 From: Laurent Nguyen Date: Mon, 22 Apr 2024 13:43:28 +0200 Subject: [PATCH] Fix bugs with clicking on newly created documents --- .../Tabs/DocumentsTabV2/DocumentsTabV2.tsx | 35 ++++++++++++------- src/Explorer/Tree/DocumentId.ts | 14 ++++++-- 2 files changed, 34 insertions(+), 15 deletions(-) diff --git a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx index c26256178..6b5f28472 100644 --- a/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx +++ b/src/Explorer/Tabs/DocumentsTabV2/DocumentsTabV2.tsx @@ -282,6 +282,24 @@ const DocumentsTabComponent: React.FunctionComponent<{ partitionKeyPropertyHeader.replace(/[/]+/g, ".").substring(1).replace(/[']+/g, ""), ); + // new DocumentId() requires a DocumentTab which we mock with only the required properties + const newDocumentId = ( + rawDocument: DataModels.DocumentId, + partitionKeyProperties: string[], + partitionKeyValue: string[], + ) => + new DocumentId( + { + partitionKey, + partitionKeyProperties, + // Fake unused mocks + isEditorDirty: () => false, + selectDocument: () => Promise.reject(), + }, + rawDocument, + partitionKeyValue, + ); + // const isPreferredApiMongoDB = useMemo( // () => userContext.apiType === "Mongo" || props.isPreferredApiMongoDB, // [props.isPreferredApiMongoDB], @@ -395,7 +413,7 @@ const DocumentsTabComponent: React.FunctionComponent<{ setIsExecuting(true); return createDocument(props.collection, document) .then( - (savedDocument: unknown) => { + (savedDocument: DataModels.DocumentId) => { const value: string = renderObjectForEditor(savedDocument || {}, null, 4); setSelectedDocumentContentBaseline(value); setInitialDocumentContent(value); @@ -403,7 +421,7 @@ const DocumentsTabComponent: React.FunctionComponent<{ savedDocument, partitionKey as PartitionKeyDefinition, ); - const id = new DocumentId(this, savedDocument, partitionKeyValueArray); + const id = newDocumentId(savedDocument, partitionKeyProperties, partitionKeyValueArray as string[]); const ids = documentIds; ids.push(id); @@ -770,15 +788,7 @@ const DocumentsTabComponent: React.FunctionComponent<{ partitionKeyPropertyHeader.replace(/[/]+/g, ".").substring(1).replace(/[']+/g, ""), ); - return new DocumentId( - { - partitionKey, - partitionKeyPropertyHeaders, - partitionKeyProperties, - } as DocumentsTab, - rawDocument, - partitionKeyValue, - ); + return newDocumentId(rawDocument, partitionKeyProperties, partitionKeyValue); }); const merged = currentDocuments.concat(nextDocumentIds); @@ -1370,7 +1380,8 @@ const DocumentsTabComponent: React.FunctionComponent<{ // eslint-disable-next-line @typescript-eslint/no-explicit-any .map((rawDocument: any) => { const partitionKeyValue = rawDocument._partitionKeyValue; - return new DocumentId(this, rawDocument, [partitionKeyValue]); + return newDocumentId(rawDocument, partitionKeyProperties, [partitionKeyValue]); + // return new DocumentId(this, rawDocument, [partitionKeyValue]); }); const merged = currentDocuments.concat(nextDocumentIds); diff --git a/src/Explorer/Tree/DocumentId.ts b/src/Explorer/Tree/DocumentId.ts index 4ccf71e40..7976431d9 100644 --- a/src/Explorer/Tree/DocumentId.ts +++ b/src/Explorer/Tree/DocumentId.ts @@ -1,10 +1,18 @@ import * as ko from "knockout"; import * as DataModels from "../../Contracts/DataModels"; import { useDialog } from "../Controls/Dialog"; -import DocumentsTab from "../Tabs/DocumentsTab"; +/** + * Replaces DocumentsTab so we can switch container + */ +export interface IDocumentIdContainer { + partitionKeyProperties?: string[]; + partitionKey: DataModels.PartitionKey; + isEditorDirty: () => boolean; + selectDocument: (documentId: DocumentId) => Promise; +} export default class DocumentId { - public container: DocumentsTab; + public container: IDocumentIdContainer; public rid: string; public self: string; public ts: string; @@ -15,7 +23,7 @@ export default class DocumentId { public stringPartitionKeyValues: string[]; public isDirty: ko.Observable; - constructor(container: DocumentsTab, data: any, partitionKeyValue: any[]) { + constructor(container: IDocumentIdContainer, data: any, partitionKeyValue: any[]) { this.container = container; this.self = data._self; this.rid = data._rid;