import { actions , createKernelRef , selectors } from "@nteract/core" ; import { ContentRef } from "@nteract/types" ; import { remote } from "electron" ; import * as path from "path" ; import { DesktopStore } from "../../../notebook/store" ; import { documentDirectoryFor , systemDocumentDirectory } from "./directories" ; interface SaveDialogOptions { title : string ; filters : Array < { name : string ; extensions : string [ ] ; } > ; defaultPath ? : string ; } export function showSaveAsDialog ( ) { return new Promise ( ( resolve , _reject ) => { const options = { title : "Save Notebook" , filters : [ { name : "Notebooks" , extensions : [ "ipynb" ] } ] } ; const defaultPath = systemDocumentDirectory ( ) ; if ( process . cwd ( ) !== defaultPath ) { options . defaultPath = defaultPath ; } remote . dialog . showSaveDialog ( options ) . then ( ( { filePath } ) => { resolve ( filePath && path . extname ( filePath ) === "" ? `${ filePath }.ipynb` : filePath ) ; } ) ; } ) ; } export function promptUserAboutNewKernel ( ownProps , store , filepath ) { return new Promise ( resolve => { remote . dialog . showMessageBox ( { type : "question" , buttons : [ "Launch New Kernel" , "Don't Launch New Kernel" ] , title : "New Kernel Needs to Be Launched" , message : "It looks like you've saved your notebook file to a new location." , detail : "The kernel executing your code thinks your notebook is still in " + "the old location. Would you like to launch a new kernel to match " + "it with the new location of the notebook?" } ) . then ( ( { response : index } ) => { if ( index === 0 ) { const state = store . getState ( ) ; const oldKernelRef = selectors . kernelRefByContentRef ( state , ownProps ) ; if ( ! oldKernelRef ) { console . error ( "kernel not available for relaunch" ) ; return ; } const kernel = selectors . kernel ( state , { kernelRef : oldKernelRef } ) ; if ( ! kernel ) { console . error ( "kernel not available for relaunch" ) ; return ; } store . dispatch ( actions . launchKernelByName ( { kernelSpecName : kernel . kernelSpecName , cwd : documentDirectoryFor ( filepath ) , kernelRef : createKernelRef ( ) , selectNextKernel : true , contentRef : ownProps . contentRef } ) ) ; } resolve ( ) ; } ) ; } ) ; }