Skip to content

Commit 40cf352

Browse files
committed
refactor: Reuse promptForNotebookName in rename command, simplify createAndAddNotebookToProject function
Signed-off-by: Tomas Kislan <[email protected]>
1 parent ff24a92 commit 40cf352

File tree

2 files changed

+9
-75
lines changed

2 files changed

+9
-75
lines changed

src/notebooks/deepnote/deepnoteExplorerView.ts

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,12 @@ export class DeepnoteExplorerView {
4343
/**
4444
* Shared helper that creates and adds a new notebook to a project
4545
* @param fileUri The URI of the project file
46-
* @param projectId The project ID
4746
* @returns Object with notebook ID and name if successful, or null if aborted/failed
4847
*/
49-
public async createAndAddNotebookToProject(
50-
fileUri: Uri,
51-
projectId: string
52-
): Promise<{ id: string; name: string } | null> {
48+
public async createAndAddNotebookToProject(fileUri: Uri): Promise<{ id: string; name: string } | null> {
5349
// Read the Deepnote project file
5450
const projectData = await readDeepnoteProjectFile(fileUri);
5551

56-
if (projectData.project.id !== projectId) {
57-
await window.showErrorMessage(l10n.t('Project ID mismatch'));
58-
return null;
59-
}
60-
6152
if (!projectData?.project) {
6253
await window.showErrorMessage(l10n.t('Invalid Deepnote file format'));
6354
return null;
@@ -84,7 +75,7 @@ export class DeepnoteExplorerView {
8475
projectData.project.notebooks.push(newNotebook);
8576

8677
// Save and open the new notebook
87-
await this.saveProjectAndOpenNotebook(fileUri, projectData, projectId, newNotebook.id);
78+
await this.saveProjectAndOpenNotebook(fileUri, projectData, newNotebook.id);
8879

8980
return { id: newNotebook.id, name: notebookName };
9081
}
@@ -120,19 +111,7 @@ export class DeepnoteExplorerView {
120111

121112
const existingNames = new Set(projectData.project.notebooks.map((nb: DeepnoteNotebook) => nb.name));
122113

123-
const newName = await window.showInputBox({
124-
prompt: l10n.t('Enter new notebook name'),
125-
value: currentName,
126-
validateInput: (value) => {
127-
if (!value || value.trim().length === 0) {
128-
return l10n.t('Notebook name cannot be empty');
129-
}
130-
if (existingNames.has(value)) {
131-
return l10n.t('A notebook with this name already exists');
132-
}
133-
return null;
134-
}
135-
});
114+
const newName = await this.promptForNotebookName(currentName, existingNames);
136115

137116
if (!newName || newName === currentName) {
138117
return;
@@ -482,13 +461,11 @@ export class DeepnoteExplorerView {
482461
* Saves the project data to file and opens the specified notebook
483462
* @param fileUri The URI of the project file
484463
* @param projectData The project data to save
485-
* @param projectId The project ID
486464
* @param notebookId The notebook ID to open
487465
*/
488466
private async saveProjectAndOpenNotebook(
489467
fileUri: Uri,
490468
projectData: DeepnoteFile,
491-
projectId: string,
492469
notebookId: string
493470
): Promise<void> {
494471
// Update metadata timestamp
@@ -506,7 +483,7 @@ export class DeepnoteExplorerView {
506483
this.treeDataProvider.refresh();
507484

508485
// Open the new notebook
509-
this.manager.selectNotebookForProject(projectId, notebookId);
486+
this.manager.selectNotebookForProject(projectData.project.id, notebookId);
510487
const notebookUri = fileUri.with({ query: `notebook=${notebookId}` });
511488
const document = await workspace.openNotebookDocument(notebookUri);
512489
await window.showNotebookDocument(document, {
@@ -722,14 +699,6 @@ export class DeepnoteExplorerView {
722699
}
723700

724701
const document = activeEditor.notebook;
725-
const metadata = document.metadata;
726-
727-
// Get project information from notebook metadata
728-
const projectId = metadata?.deepnoteProjectId as string | undefined;
729-
if (!projectId) {
730-
await window.showErrorMessage(l10n.t('Could not determine project ID'));
731-
return;
732-
}
733702

734703
// Get the file URI (strip query params if present)
735704
let fileUri = document.uri;
@@ -739,7 +708,7 @@ export class DeepnoteExplorerView {
739708

740709
try {
741710
// Use shared helper to create and add notebook
742-
const result = await this.createAndAddNotebookToProject(fileUri, projectId);
711+
const result = await this.createAndAddNotebookToProject(fileUri);
743712

744713
if (result) {
745714
await window.showInformationMessage(l10n.t('Created new notebook: {0}', result.name));
@@ -968,14 +937,11 @@ export class DeepnoteExplorerView {
968937
return;
969938
}
970939

971-
const project = treeItem.data as DeepnoteFile;
972-
const projectId = project.project.id;
973-
974940
try {
975941
const fileUri = Uri.file(treeItem.context.filePath);
976942

977943
// Use shared helper to create and add notebook
978-
const result = await this.createAndAddNotebookToProject(fileUri, projectId);
944+
const result = await this.createAndAddNotebookToProject(fileUri);
979945

980946
if (result) {
981947
await window.showInformationMessage(l10n.t('Created new notebook: {0}', result.name));

src/notebooks/deepnote/deepnoteExplorerView.unit.test.ts

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -798,7 +798,7 @@ suite('DeepnoteExplorerView - Empty State Commands', () => {
798798
);
799799

800800
// Execute the method
801-
const result = await explorerView.createAndAddNotebookToProject(fileUri, projectId);
801+
const result = await explorerView.createAndAddNotebookToProject(fileUri);
802802

803803
// Verify result
804804
expect(result).to.exist;
@@ -819,38 +819,6 @@ suite('DeepnoteExplorerView - Empty State Commands', () => {
819819
expect(updatedProjectData.project.notebooks[1].executionMode).to.equal('block');
820820
});
821821

822-
test('should return null if project ID does not match', async () => {
823-
const projectId = 'expected-project-id';
824-
const differentProjectId = 'different-project-id';
825-
const fileUri = Uri.file('/workspace/test-project.deepnote');
826-
827-
// Mock existing project data with different ID
828-
const existingProjectData = {
829-
version: 1.0,
830-
project: {
831-
id: differentProjectId,
832-
name: 'Test Project',
833-
notebooks: []
834-
}
835-
};
836-
837-
const yamlContent = yaml.dump(existingProjectData);
838-
839-
// Mock file system
840-
const mockFS = mock<typeof workspace.fs>();
841-
when(mockFS.readFile(anything())).thenReturn(Promise.resolve(Buffer.from(yamlContent)));
842-
when(mockedVSCodeNamespaces.workspace.fs).thenReturn(instance(mockFS));
843-
844-
when(mockedVSCodeNamespaces.window.showErrorMessage(anything())).thenReturn(Promise.resolve(undefined));
845-
846-
// Execute the method
847-
const result = await explorerView.createAndAddNotebookToProject(fileUri, projectId);
848-
849-
// Verify result is null and error was shown
850-
expect(result).to.be.null;
851-
verify(mockedVSCodeNamespaces.window.showErrorMessage(anything())).once();
852-
});
853-
854822
test('should return null if user cancels notebook name input', async () => {
855823
const projectId = 'test-project-id';
856824
const fileUri = Uri.file('/workspace/test-project.deepnote');
@@ -877,7 +845,7 @@ suite('DeepnoteExplorerView - Empty State Commands', () => {
877845
when(mockedVSCodeNamespaces.window.showInputBox(anything())).thenReturn(Promise.resolve(undefined));
878846

879847
// Execute the method
880-
const result = await explorerView.createAndAddNotebookToProject(fileUri, projectId);
848+
const result = await explorerView.createAndAddNotebookToProject(fileUri);
881849

882850
// Verify result is null and file was not written
883851
expect(result).to.be.null;
@@ -925,7 +893,7 @@ suite('DeepnoteExplorerView - Empty State Commands', () => {
925893
);
926894

927895
// Execute the method
928-
await explorerView.createAndAddNotebookToProject(fileUri, projectId);
896+
await explorerView.createAndAddNotebookToProject(fileUri);
929897

930898
// Verify suggested name is 'Notebook 3' (next in sequence)
931899
expect(capturedInputBoxOptions).to.exist;

0 commit comments

Comments
 (0)