Skip to content
Draft
Show file tree
Hide file tree
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
4 changes: 4 additions & 0 deletions packages/compass-web/src/entrypoint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ export type CompassWebProps = {
* safely closed without losing any important unsaved changes
*/
onBeforeUnloadCallbackRequest?: (canCloseCallback: () => boolean) => void;

shouldSaveWorkspaces?: () => boolean;
};

function CompassWorkspace({
Expand Down Expand Up @@ -495,6 +497,7 @@ const CompassWeb = ({
onOpenConnectViaModal,
onFailToLoadConnections,
onBeforeUnloadCallbackRequest,
shouldSaveWorkspaces,
}: CompassWebProps) => {
const appRegistry = useInitialValue(new AppRegistry());
const logger = useCompassWebLogger({
Expand Down Expand Up @@ -585,6 +588,7 @@ const CompassWeb = ({
<WithConnectionsStore>
<CompassWorkspace
initialWorkspaceTabs={initialWorkspaceTabs}
shouldSaveWorkspaces={shouldSaveWorkspaces}
onActiveWorkspaceTabChange={
onActiveWorkspaceTabChange
}
Expand Down
2 changes: 2 additions & 0 deletions packages/compass-workspaces/src/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type WorkspacesWithSidebarProps = {
* safely closed without losing any important unsaved changes
*/
onBeforeUnloadCallbackRequest?: (canCloseCallback: () => boolean) => void;

shouldSaveWorkspaces?: () => boolean;
};

const containerLightThemeStyles = css({
Expand Down
27 changes: 17 additions & 10 deletions packages/compass-workspaces/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export type WorkspacesServices = {

export function configureStore(
initialWorkspaceTabs: OpenWorkspaceOptions[] | undefined | null,
services: WorkspacesServices
services: WorkspacesServices,
shouldSaveWorkspaces?: () => boolean
) {
const initialTabs =
initialWorkspaceTabs && initialWorkspaceTabs.length > 0
Expand All @@ -73,7 +74,7 @@ export function configureStore(
},
applyMiddleware(
thunk.withExtraArgument(services),
workspacesStateChangeMiddleware(services)
workspacesStateChangeMiddleware(services, shouldSaveWorkspaces)
)
);

Expand All @@ -84,9 +85,11 @@ export function activateWorkspacePlugin(
{
initialWorkspaceTabs,
onBeforeUnloadCallbackRequest,
shouldSaveWorkspaces,
}: {
initialWorkspaceTabs?: OpenWorkspaceOptions[] | null;
onBeforeUnloadCallbackRequest?: (canCloseCallback: () => boolean) => void;
shouldSaveWorkspaces?: () => boolean;
},
{
globalAppRegistry,
Expand All @@ -98,14 +101,18 @@ export function activateWorkspacePlugin(
}: WorkspacesServices,
{ on, cleanup, addCleanup }: ActivateHelpers
) {
const store = configureStore(initialWorkspaceTabs, {
globalAppRegistry,
instancesManager,
connections,
logger,
preferences,
userData,
});
const store = configureStore(
initialWorkspaceTabs,
{
globalAppRegistry,
instancesManager,
connections,
logger,
preferences,
userData,
},
shouldSaveWorkspaces
);

void store.dispatch(loadSavedWorkspaces());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const handleWorkspacesStateChange = (() => {
* This allows you to perform side effects when the state is updated.
*/
export function workspacesStateChangeMiddleware(
services: WorkspacesServices
services: WorkspacesServices,
shouldSaveWorkspaces?: () => boolean
): Middleware<Record<string, never>, WorkspacesState> {
return (store) => (next) => (action: AnyAction) => {
const prevState = store.getState();
Expand All @@ -32,7 +33,10 @@ export function workspacesStateChangeMiddleware(

// Only call the callback if the workspaces state actually changed
if (prevState !== nextState) {
if (services.preferences.getPreferences().enableRestoreWorkspaces) {
if (
services.preferences.getPreferences().enableRestoreWorkspaces &&
shouldSaveWorkspaces?.()
) {
handleWorkspacesStateChange(nextState, services);
}
}
Expand Down
Loading