Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 53 additions & 2 deletions runtime/src/js/pgadmin.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,60 @@ function notifyUpdateInstalled() {
}
}

// Native file dialogs don't reliably remember the last used directory across
// calls, so we track it ourselves and use it as the default when the caller
// hasn't asked for a specific location.
async function withLastVisitedDirectory(options) {
if (options.defaultPath) {
return options;
}
const lastVisitedDirectory = configStore.get('lastVisitedDirectory');
if (!lastVisitedDirectory) {
return options;
}
try {
const stats = await fs.promises.stat(lastVisitedDirectory);
if (stats.isDirectory()) {
return { ...options, defaultPath: lastVisitedDirectory };
}
} catch {
// Remembered directory no longer exists (e.g. removable/network drive) - fall through.
}
return options;
}

function rememberVisitedDirectory(options, result) {
const chosenPath = result.filePath || result.filePaths?.[0];
if (!chosenPath) {
return;
}
const isDirectory = options.properties?.includes('openDirectory');
try {
configStore.set('lastVisitedDirectory', isDirectory ? chosenPath : path.dirname(chosenPath));
} catch (error) {
try {
misc.writeServerLog(`Error remembering last visited directory: ${error}`);
} catch (logError) {
console.error('Error remembering last visited directory:', error, logError);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

// setup preload events.
ipcMain.handle('showOpenDialog', (e, options) => dialog.showOpenDialog(BrowserWindow.fromWebContents(e.sender), options));
ipcMain.handle('showSaveDialog', (e, options) => dialog.showSaveDialog(BrowserWindow.fromWebContents(e.sender), options));
ipcMain.handle('showOpenDialog', async (e, options) => {
const result = await dialog.showOpenDialog(BrowserWindow.fromWebContents(e.sender), await withLastVisitedDirectory(options));
if (!result.canceled) {
rememberVisitedDirectory(options, result);
}
return result;
});
ipcMain.handle('showSaveDialog', async (e, options) => {
const result = await dialog.showSaveDialog(BrowserWindow.fromWebContents(e.sender), await withLastVisitedDirectory(options));
if (!result.canceled) {
rememberVisitedDirectory(options, result);
}
return result;
});
ipcMain.handle('showMessageBox', (e, options) => dialog.showMessageBox(BrowserWindow.fromWebContents(e.sender), options));
ipcMain.handle('getStoreData', (_e, key) => key ? configStore.get(key) : configStore.store);
ipcMain.handle('setStoreData', (_e, newValues) => {
Expand Down
Loading