@@ -227,6 +227,47 @@ export class DeepnoteExplorerView {
227227 } ) ;
228228 }
229229
230+ /**
231+ * Shared helper that creates and adds a new notebook to a project
232+ * @param fileUri The URI of the project file
233+ * @param projectId The project ID
234+ * @returns Object with notebook ID and name if successful, or null if aborted/failed
235+ */
236+ private async createAndAddNotebookToProject (
237+ fileUri : Uri ,
238+ projectId : string
239+ ) : Promise < { id : string ; name : string } | null > {
240+ // Read the Deepnote project file
241+ const projectData = await this . readDeepnoteProjectFile ( fileUri ) ;
242+
243+ if ( ! projectData ?. project ) {
244+ await window . showErrorMessage ( l10n . t ( 'Invalid Deepnote file format' ) ) ;
245+ return null ;
246+ }
247+
248+ // Generate suggested name and prompt user
249+ const suggestedName = this . generateSuggestedNotebookName ( projectData ) ;
250+ const notebookName = await this . promptForNotebookName ( suggestedName ) ;
251+
252+ if ( ! notebookName ) {
253+ return null ;
254+ }
255+
256+ // Create new notebook with initial block
257+ const newNotebook = this . createNotebookWithFirstBlock ( notebookName ) ;
258+
259+ // Add new notebook to the project (initialize array if needed)
260+ if ( ! projectData . project . notebooks ) {
261+ projectData . project . notebooks = [ ] ;
262+ }
263+ projectData . project . notebooks . push ( newNotebook ) ;
264+
265+ // Save and open the new notebook
266+ await this . saveProjectAndOpenNotebook ( fileUri , projectData , projectId , newNotebook . id ) ;
267+
268+ return { id : newNotebook . id , name : notebookName } ;
269+ }
270+
230271 private refreshExplorer ( ) : void {
231272 this . treeDataProvider . refresh ( ) ;
232273 }
@@ -450,35 +491,12 @@ export class DeepnoteExplorerView {
450491 }
451492
452493 try {
453- // Read the current file
454- const projectData = await this . readDeepnoteProjectFile ( fileUri ) ;
455-
456- if ( ! projectData ?. project ) {
457- await window . showErrorMessage ( l10n . t ( 'Invalid Deepnote file format' ) ) ;
458- return ;
459- }
460-
461- // Generate suggested name and prompt user
462- const suggestedName = this . generateSuggestedNotebookName ( projectData ) ;
463- const notebookName = await this . promptForNotebookName ( suggestedName ) ;
464-
465- if ( ! notebookName ) {
466- return ;
467- }
468-
469- // Create new notebook with initial block
470- const newNotebook = this . createNotebookWithFirstBlock ( notebookName ) ;
494+ // Use shared helper to create and add notebook
495+ const result = await this . createAndAddNotebookToProject ( fileUri , projectId ) ;
471496
472- // Add new notebook to the project
473- if ( ! projectData . project . notebooks ) {
474- projectData . project . notebooks = [ ] ;
497+ if ( result ) {
498+ await window . showInformationMessage ( l10n . t ( 'Created new notebook: {0}' , result . name ) ) ;
475499 }
476- projectData . project . notebooks . push ( newNotebook ) ;
477-
478- // Save and open the new notebook
479- await this . saveProjectAndOpenNotebook ( fileUri , projectData , projectId , newNotebook . id ) ;
480-
481- await window . showInformationMessage ( l10n . t ( 'Created new notebook: {0}' , notebookName ) ) ;
482500 } catch ( error ) {
483501 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
484502 await window . showErrorMessage ( l10n . t ( 'Failed to add notebook: {0}' , errorMessage ) ) ;
@@ -946,34 +964,13 @@ export class DeepnoteExplorerView {
946964
947965 try {
948966 const fileUri = Uri . file ( treeItem . context . filePath ) ;
949- const projectData = await this . readDeepnoteProjectFile ( fileUri ) ;
950-
951- if ( ! projectData ?. project ) {
952- await window . showErrorMessage ( l10n . t ( 'Invalid Deepnote file format' ) ) ;
953- return ;
954- }
955967
956- // Generate suggested name and prompt user
957- const suggestedName = this . generateSuggestedNotebookName ( projectData ) ;
958- const notebookName = await this . promptForNotebookName ( suggestedName ) ;
968+ // Use shared helper to create and add notebook
969+ const result = await this . createAndAddNotebookToProject ( fileUri , projectId ) ;
959970
960- if ( ! notebookName ) {
961- return ;
971+ if ( result ) {
972+ await window . showInformationMessage ( l10n . t ( 'Created new notebook: {0}' , result . name ) ) ;
962973 }
963-
964- // Create new notebook with initial block
965- const newNotebook = this . createNotebookWithFirstBlock ( notebookName ) ;
966-
967- // Add new notebook to the project
968- if ( ! projectData . project . notebooks ) {
969- projectData . project . notebooks = [ ] ;
970- }
971- projectData . project . notebooks . push ( newNotebook ) ;
972-
973- // Save and open the new notebook
974- await this . saveProjectAndOpenNotebook ( fileUri , projectData , projectId , newNotebook . id ) ;
975-
976- await window . showInformationMessage ( l10n . t ( 'Created new notebook: {0}' , notebookName ) ) ;
977974 } catch ( error ) {
978975 const errorMessage = error instanceof Error ? error . message : 'Unknown error' ;
979976 await window . showErrorMessage ( l10n . t ( 'Failed to add notebook: {0}' , errorMessage ) ) ;
0 commit comments