Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 7 additions & 2 deletions packages/bruno-app/src/providers/App/useIpcEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import { collectionAddEnvFileEvent, openCollectionEvent, hydrateCollectionWithUi
import {
workspaceOpenedEvent,
workspaceConfigUpdatedEvent,
hydrateSnapshotForOpenedCollection
hydrateSnapshotForOpenedCollection,
restoreActiveWorkspaceFromSnapshot
} from 'providers/ReduxStore/slices/workspaces/actions';
import { workspaceDotEnvUpdateEvent, setWorkspaceDotEnvVariables } from 'providers/ReduxStore/slices/workspaces';
import toast from 'react-hot-toast';
Expand Down Expand Up @@ -115,7 +116,6 @@ const useIpcEvents = () => {
dispatch(apiSpecChangeFileEvent({ data: val }));
}
};

ipcRenderer.invoke('renderer:ready');

const removeCollectionTreeUpdateListener = ipcRenderer.on('main:collection-tree-updated', _collectionTreeUpdated);
Expand All @@ -134,6 +134,10 @@ const useIpcEvents = () => {
dispatch(workspaceOpenedEvent(workspacePath, workspaceUid, workspaceConfig));
});

const removeWorkspacesReadyListener = ipcRenderer.on('main:workspaces-ready', () => {
dispatch(restoreActiveWorkspaceFromSnapshot());
});

const removeWorkspaceConfigUpdatedListener = ipcRenderer.on('main:workspace-config-updated', (workspacePath, workspaceUid, workspaceConfig) => {
dispatch(workspaceConfigUpdatedEvent(workspacePath, workspaceUid, workspaceConfig));
});
Expand Down Expand Up @@ -368,6 +372,7 @@ const useIpcEvents = () => {
removeApiSpecTreeUpdateListener();
removeOpenCollectionListener();
removeOpenWorkspaceListener();
removeWorkspacesReadyListener();
removeWorkspaceConfigUpdatedListener();
removeWorkspaceEnvironmentAddedListener();
removeWorkspaceEnvironmentChangedListener();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import toast from 'react-hot-toast';

const { ipcRenderer } = window;
let snapshotHydrationTimer = null;
let startupWorkspaceRestorePending = true;
const SNAPSHOT_HYDRATION_LONG_STOP_GUARD_MS = 5 * 60 * 1000;

const COLLECTION_SORT_ORDER_BY_WORKSPACE_SORTING = {
Expand Down Expand Up @@ -792,6 +793,40 @@ export const loadLastOpenedWorkspaces = () => {
};
};

export const restoreActiveWorkspaceFromSnapshot = () => {
return async (dispatch, getState) => {
startupWorkspaceRestorePending = false;

try {
const snapshot = await ipcRenderer.invoke('renderer:snapshot:get');
const activeWorkspacePath = snapshot?.activeWorkspacePath;
const { workspaces } = getState().workspaces;

if (activeWorkspacePath) {
const normalizedActiveWorkspacePath = normalizePath(activeWorkspacePath);
const matchingWorkspace = workspaces.find(
(workspace) => normalizePath(workspace.pathname || '') === normalizedActiveWorkspacePath
);

if (matchingWorkspace) {
await dispatch(switchWorkspace(matchingWorkspace.uid));
return;
}
}

const defaultWorkspace = workspaces.find((workspace) => workspace.type === 'default');
if (defaultWorkspace) {
await dispatch(switchWorkspace(defaultWorkspace.uid));
}
} catch (err) {
const defaultWorkspace = getState().workspaces.workspaces.find((workspace) => workspace.type === 'default');
if (defaultWorkspace) {
await dispatch(switchWorkspace(defaultWorkspace.uid));
}
}
};
};

export const workspaceOpenedEvent = (workspacePath, workspaceUid, workspaceConfig) => {
return async (dispatch, getState) => {
dispatch(createWorkspace({
Expand All @@ -805,16 +840,10 @@ export const workspaceOpenedEvent = (workspacePath, workspaceUid, workspaceConfi
} catch (error) {
}

const state = getState();
const activeWorkspaceUid = state.workspaces.activeWorkspaceUid;

let shouldSwitch = false;
try {
const snapshot = await ipcRenderer.invoke('renderer:snapshot:get');
const activeWorkspacePath = snapshot?.activeWorkspacePath;
const normalizedWorkspacePath = normalizePath(workspacePath || '');

const currentState = getState();

if (!currentState.app.snapshotReady && snapshot?.extras?.devTools) {
const { open } = snapshot.extras.devTools;
if (open) {
Expand All @@ -825,33 +854,34 @@ export const workspaceOpenedEvent = (workspacePath, workspaceUid, workspaceConfi
const { activeTab = 'terminal' } = snapshot.extras.devTools;
dispatch(setActiveDevToolsTab(activeTab));
}
} catch (err) {
}

if (startupWorkspaceRestorePending) {
return;
}

const state = getState();
const activeWorkspaceUid = state.workspaces.activeWorkspaceUid;
let shouldSwitch = false;

try {
const snapshot = await ipcRenderer.invoke('renderer:snapshot:get');
const activeWorkspacePath = snapshot?.activeWorkspacePath;
const normalizedWorkspacePath = normalizePath(workspacePath || '');

if (activeWorkspacePath) {
const normalizedActiveWorkspacePath = normalizePath(activeWorkspacePath);
shouldSwitch = normalizedWorkspacePath === normalizedActiveWorkspacePath;

// If the snapshot points to a workspace that no longer exists on disk,
// fall back to the default workspace instead of leaving stale active state.
if (!shouldSwitch && workspaceConfig.type === 'default') {
const lastOpenedWorkspacePaths = await ipcRenderer.invoke('renderer:get-last-opened-workspaces').catch(() => []);
const normalizedLastOpenedWorkspacePaths = new Set(
(Array.isArray(lastOpenedWorkspacePaths) ? lastOpenedWorkspacePaths : [])
.map((pathname) => normalizePath(pathname))
);
const hasActiveWorkspacePath = normalizedLastOpenedWorkspacePaths.has(normalizedActiveWorkspacePath);

if (!hasActiveWorkspacePath) {
shouldSwitch = true;
}
}
} else {
shouldSwitch = !activeWorkspaceUid || workspaceConfig.type === 'default';
}
} catch (err) {
shouldSwitch = !activeWorkspaceUid || workspaceConfig.type === 'default';
}

if (shouldSwitch) {
dispatch(switchWorkspace(workspaceUid));
await dispatch(switchWorkspace(workspaceUid));
}
};
};
Expand Down
66 changes: 20 additions & 46 deletions packages/bruno-electron/src/ipc/workspace.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const LastOpenedWorkspaces = require('../store/last-opened-workspaces');
const { defaultWorkspaceManager } = require('../store/default-workspace');
const { globalEnvironmentsManager } = require('../store/workspace-environments');
const { globalEnvironmentsStore } = require('../store/global-environments');
const { resolveLastOpenedWorkspacePaths } = require('../utils/workspace-startup');

const {
createWorkspaceConfig,
Expand Down Expand Up @@ -236,23 +237,9 @@ const registerWorkspaceIpc = (mainWindow, workspaceWatcher) => {

ipcMain.handle('renderer:get-last-opened-workspaces', async () => {
try {
const workspacePaths = lastOpenedWorkspaces.getAll();
const validWorkspaces = [];
const invalidPaths = [];

for (const workspacePath of workspacePaths) {
const workspaceYmlPath = path.join(workspacePath, 'workspace.yml');

if (fs.existsSync(workspaceYmlPath)) {
validWorkspaces.push(workspacePath);
} else {
invalidPaths.push(workspacePath);
}
}

for (const invalidPath of invalidPaths) {
lastOpenedWorkspaces.remove(invalidPath);
}
const { validWorkspaces } = resolveLastOpenedWorkspacePaths(lastOpenedWorkspaces, {
validateConfig: true
});

return validWorkspaces;
} catch (error) {
Expand Down Expand Up @@ -691,45 +678,32 @@ const registerWorkspaceIpc = (mainWindow, workspaceWatcher) => {
}
}

const workspacePaths = lastOpenedWorkspaces.getAll();
const invalidPaths = [];

for (const workspacePath of workspacePaths) {
if (defaultWorkspacePath && workspacePath === defaultWorkspacePath) {
continue;
}

const workspaceYmlPath = path.join(workspacePath, 'workspace.yml');
const { validWorkspaces } = resolveLastOpenedWorkspacePaths(lastOpenedWorkspaces, {
defaultWorkspacePath
});

if (fs.existsSync(workspaceYmlPath)) {
try {
const workspaceConfig = readWorkspaceConfig(workspacePath);
validateWorkspaceConfig(workspaceConfig);
const workspaceUid = getWorkspaceUid(workspacePath);
const isDefault = workspaceUid === 'default';
const configForClient = prepareWorkspaceConfigForClient(workspaceConfig, workspacePath, isDefault);
for (const workspacePath of validWorkspaces) {
try {
const workspaceConfig = readWorkspaceConfig(workspacePath);
const workspaceUid = getWorkspaceUid(workspacePath);
const isDefault = workspaceUid === 'default';
const configForClient = prepareWorkspaceConfigForClient(workspaceConfig, workspacePath, isDefault);

win.webContents.send('main:workspace-opened', workspacePath, workspaceUid, configForClient);
win.webContents.send('main:workspace-opened', workspacePath, workspaceUid, configForClient);

if (workspaceWatcher) {
workspaceWatcher.addWatcher(win, workspacePath);
}
} catch (error) {
console.error(`Error loading workspace ${workspacePath}:`, error);
invalidPaths.push(workspacePath);
if (workspaceWatcher) {
workspaceWatcher.addWatcher(win, workspacePath);
}
} else {
invalidPaths.push(workspacePath);
} catch (error) {
console.error(`Error loading workspace ${workspacePath}:`, error);
lastOpenedWorkspaces.remove(workspacePath);
}
}

for (const invalidPath of invalidPaths) {
lastOpenedWorkspaces.remove(invalidPath);
}
} catch (error) {
console.error('Error initializing workspaces:', error);
}

win.webContents.send('main:workspaces-ready');
ipcMain.emit('main:workspaces-ready', win);
});
};
Expand Down
111 changes: 111 additions & 0 deletions packages/bruno-electron/src/utils/workspace-startup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const fs = require('fs');
const path = require('path');
const snapshotManager = require('../services/snapshot');
const { readWorkspaceConfig, validateWorkspaceConfig } = require('./workspace-config');

const normalizeWorkspacePathname = (workspacePath) => {
if (typeof workspacePath !== 'string' || !workspacePath.length) {
return '';
}

return path.normalize(workspacePath.replace(/\\+$/, '').replace(/\/+$/, ''));
};

const prioritizeActiveWorkspacePath = (workspacePaths, activeWorkspacePath) => {
if (!activeWorkspacePath || !Array.isArray(workspacePaths) || workspacePaths.length <= 1) {
return workspacePaths;
}

const normalizedActivePath = normalizeWorkspacePathname(activeWorkspacePath);
if (!normalizedActivePath) {
return workspacePaths;
}

const activeIndex = workspacePaths.findIndex(
(workspacePath) => normalizeWorkspacePathname(workspacePath) === normalizedActivePath
);

if (activeIndex <= 0) {
return workspacePaths;
}

const prioritizedPaths = [...workspacePaths];
const [activePath] = prioritizedPaths.splice(activeIndex, 1);
prioritizedPaths.unshift(activePath);
return prioritizedPaths;
};

const isValidWorkspacePathOnDisk = (workspacePath, { validateConfig = false } = {}) => {
const workspaceYmlPath = path.join(workspacePath, 'workspace.yml');

if (!fs.existsSync(workspaceYmlPath)) {
return false;
}

if (!validateConfig) {
return true;
}

try {
const workspaceConfig = readWorkspaceConfig(workspacePath);
validateWorkspaceConfig(workspaceConfig);
return true;
} catch (error) {
return false;
}
};

const resolveLastOpenedWorkspacePaths = (lastOpenedWorkspaces, {
defaultWorkspacePath = null,
validateConfig = false
} = {}) => {
const workspacePaths = lastOpenedWorkspaces.getAll();
const activeWorkspacePath = snapshotManager.getSnapshot()?.activeWorkspacePath;
const validWorkspaces = [];
const invalidPaths = [];
for (const workspacePath of workspacePaths) {
if (defaultWorkspacePath && workspacePath === defaultWorkspacePath) {
Comment thread
sid-bruno marked this conversation as resolved.
Resolved
continue;
}

if (isValidWorkspacePathOnDisk(workspacePath, { validateConfig })) {
validWorkspaces.push(workspacePath);
} else {
invalidPaths.push(workspacePath);
}
}

let prioritizedWorkspaces = prioritizeActiveWorkspacePath(validWorkspaces, activeWorkspacePath);

if (activeWorkspacePath) {
const normalizedActivePath = normalizeWorkspacePathname(activeWorkspacePath);
const alreadyIncluded = prioritizedWorkspaces.some(
(workspacePath) => normalizeWorkspacePathname(workspacePath) === normalizedActivePath
);

if (
!alreadyIncluded
&& normalizedActivePath
&& (!defaultWorkspacePath || activeWorkspacePath !== defaultWorkspacePath)
Comment thread
sid-bruno marked this conversation as resolved.
Resolved
&& isValidWorkspacePathOnDisk(activeWorkspacePath, { validateConfig })
) {
prioritizedWorkspaces.unshift(activeWorkspacePath);
}
}

for (const invalidPath of invalidPaths) {
lastOpenedWorkspaces.remove(invalidPath);
}

return {
validWorkspaces: prioritizedWorkspaces,
invalidPaths
};
};

module.exports = {
normalizeWorkspacePathname,
prioritizeActiveWorkspacePath,
isValidWorkspacePathOnDisk,
resolveLastOpenedWorkspacePaths
};
Loading
Loading