Skip to content

Commit 619362d

Browse files
committed
break something
1 parent 2351763 commit 619362d

File tree

9 files changed

+169
-171
lines changed

9 files changed

+169
-171
lines changed

packages/atlas-service/src/atlas-service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export class AtlasService {
7878
// https://github.com/10gen/mms/blob/9f858bb987aac6aa80acfb86492dd74c89cbb862/client/packages/project/common/ajaxPrefilter.ts#L34-L49
7979
return this.cloudEndpoint(path);
8080
}
81+
userDataEndpoint(path?: string): string {
82+
return `https://cluster-connection.cloud-dev.mongodb.com/userData${normalizePath(
83+
path
84+
)}`;
85+
}
8186
driverProxyEndpoint(path?: string): string {
8287
return `${this.config.ccsBaseUrl}${normalizePath(path)}`;
8388
}

packages/compass-user-data/src/user-data.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ export abstract class IUserData<T extends z.Schema> {
6565
abstract write(id: string, content: z.input<T>): Promise<boolean>;
6666
abstract delete(id: string): Promise<boolean>;
6767
abstract readAll(options?: ReadOptions): Promise<ReadAllResult<T>>;
68+
abstract readOne(id: string, options: ReadOptions): Promise<z.output<T>>;
6869
abstract updateAttributes(
6970
id: string,
7071
data: Partial<z.input<T>>

packages/compass-web/src/entrypoint.tsx

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
DatabasesWorkspaceTab,
1515
CollectionsWorkspaceTab,
1616
} from '@mongodb-js/compass-databases-collections';
17+
import { EJSON } from 'bson';
18+
import { atlasServiceLocator } from '@mongodb-js/atlas-service/provider';
1719
import { CompassComponentsProvider, css } from '@mongodb-js/compass-components';
1820
import {
1921
WorkspaceTab as CollectionWorkspace,
@@ -62,6 +64,11 @@ import {
6264
CompassAssistantDrawer,
6365
CompassAssistantProvider,
6466
} from '@mongodb-js/compass-assistant';
67+
import {
68+
AtlasUserData,
69+
type IUserData,
70+
} from '../../compass-user-data/dist/user-data';
71+
import { WorkspacesStateSchema } from '@mongodb-js/compass-workspaces';
6572

6673
export type TrackFunction = (
6774
event: string,
@@ -84,14 +91,15 @@ const WithAtlasProviders: React.FC = ({ children }) => {
8491

8592
type CompassWorkspaceProps = Pick<
8693
React.ComponentProps<typeof WorkspacesPlugin>,
87-
| 'initialWorkspaceTabs'
88-
| 'onActiveWorkspaceTabChange'
89-
| 'savedWorkspacesPromise'
94+
'initialWorkspaceTabs' | 'onActiveWorkspaceTabChange'
9095
> &
9196
Pick<
9297
React.ComponentProps<typeof CompassSidebarPlugin>,
9398
'onOpenConnectViaModal'
94-
>;
99+
> & {
100+
orgId: string;
101+
projectId: string;
102+
};
95103

96104
type CompassWebProps = {
97105
/**
@@ -143,11 +151,9 @@ type CompassWebProps = {
143151
initialPreferences?: Partial<AllPreferences>;
144152

145153
/**
146-
* Optional promise that resolves with saved workspaces state from a
147-
* previous session. If provided, compass-web will restore tabs from that
148-
* state on first render.
154+
* UserData instance to use for persisting workspace state
149155
*/
150-
savedWorkspacesPromise?: Promise<OpenWorkspaceOptions[] | null>;
156+
userData: IUserData<typeof WorkspacesStateSchema>;
151157

152158
/**
153159
* Callback prop called every time any code inside Compass logs something
@@ -182,8 +188,23 @@ function CompassWorkspace({
182188
initialWorkspaceTabs,
183189
onActiveWorkspaceTabChange,
184190
onOpenConnectViaModal,
185-
savedWorkspacesPromise,
191+
orgId,
192+
projectId,
186193
}: CompassWorkspaceProps) {
194+
const atlasService = atlasServiceLocator();
195+
const workspacesUserData = useRef(
196+
new AtlasUserData(WorkspacesStateSchema, 'savedWorkspaces', {
197+
orgId,
198+
projectId,
199+
getResourceUrl: (path?: string) => {
200+
const url = atlasService.userDataEndpoint(`/${path || ''}`);
201+
return url;
202+
},
203+
authenticatedFetch: atlasService.authenticatedFetch.bind(atlasService),
204+
serialize: (content) => EJSON.stringify(content),
205+
deserialize: (content: string) => EJSON.parse(content),
206+
})
207+
);
187208
return (
188209
<WorkspacesProvider
189210
value={[
@@ -214,7 +235,7 @@ function CompassWorkspace({
214235
className={connectedContainerStyles}
215236
>
216237
<WorkspacesPlugin
217-
savedWorkspacesPromise={savedWorkspacesPromise}
238+
userData={workspacesUserData}
218239
initialWorkspaceTabs={initialWorkspaceTabs}
219240
openOnEmptyWorkspace={{ type: 'Welcome' }}
220241
onActiveWorkspaceTabChange={onActiveWorkspaceTabChange}
@@ -277,7 +298,6 @@ const CompassWeb = ({
277298
darkMode,
278299
initialAutoconnectId,
279300
initialWorkspace,
280-
savedWorkspacesPromise,
281301
onActiveWorkspaceTabChange,
282302
initialPreferences,
283303
onLog,
@@ -431,12 +451,11 @@ const CompassWeb = ({
431451
<FieldStorePlugin>
432452
<WithConnectionsStore>
433453
<CompassWorkspace
454+
orgId={orgId}
455+
projectId={projectId}
434456
initialWorkspaceTabs={
435457
initialWorkspaceTabsRef.current
436458
}
437-
savedWorkspacesPromise={
438-
savedWorkspacesPromise
439-
}
440459
onActiveWorkspaceTabChange={
441460
onActiveWorkspaceTabChange
442461
}

packages/compass-workspaces/src/components/index.tsx

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from 'react';
1+
import React, { useEffect, useRef } from 'react';
22
import { css, cx, palette, useDarkMode } from '@mongodb-js/compass-components';
33
import type { CollectionTabInfo } from '../stores/workspaces';
44
import {
@@ -10,7 +10,8 @@ import type { WorkspaceTab } from '../types';
1010
import Workspaces from './workspaces';
1111
import { connect } from '../stores/context';
1212
import { WorkspacesServiceProvider } from '../provider';
13-
import { convertSavedStateToInitialTabs } from '../stores/workspaces-middleware';
13+
import type { IUserData } from '../../../compass-user-data/dist/user-data';
14+
import type { WorkspacesStateSchema } from '../stores/workspaces-storage';
1415

1516
type WorkspacesWithSidebarProps = {
1617
/**
@@ -38,6 +39,11 @@ type WorkspacesWithSidebarProps = {
3839
* Initial workspace tab to show (by default no tabs will be shown initially)
3940
*/
4041
initialWorkspaceTabs?: OpenWorkspaceOptions[] | null;
42+
43+
/**
44+
* UserData instance to use for persisting workspace state
45+
*/
46+
userData: IUserData<typeof WorkspacesStateSchema>;
4147
/**
4248
* Workspace configuration to be opened when all tabs are closed (defaults to
4349
* "My Queries")
@@ -55,8 +61,6 @@ type WorkspacesWithSidebarProps = {
5561
* actions from service locator context
5662
*/
5763
renderModals?: () => React.ReactElement | null;
58-
59-
savedWorkspacesPromise?: Promise<any>;
6064
};
6165

6266
const containerLightThemeStyles = css({
@@ -87,30 +91,6 @@ const sidebarStyles = css({
8791
minHeight: 0,
8892
});
8993

90-
function usePromise(promise) {
91-
const [state, setState] = useState({
92-
data: null,
93-
error: null,
94-
loading: true,
95-
});
96-
97-
useEffect(() => {
98-
if (!promise) return;
99-
100-
setState({ data: null, error: null, loading: true });
101-
102-
promise
103-
.then((data) => {
104-
setState({ data, error: null, loading: false });
105-
})
106-
.catch((error) => {
107-
setState({ data: null, error, loading: false });
108-
});
109-
}, [promise]);
110-
111-
return state;
112-
}
113-
11494
const WorkspacesWithSidebar: React.FunctionComponent<
11595
WorkspacesWithSidebarProps
11696
> = ({
@@ -120,7 +100,7 @@ const WorkspacesWithSidebar: React.FunctionComponent<
120100
onActiveWorkspaceTabChange,
121101
renderSidebar,
122102
renderModals,
123-
savedWorkspacesPromise,
103+
userData,
124104
}) => {
125105
const darkMode = useDarkMode();
126106
const onChange = useRef(onActiveWorkspaceTabChange);
@@ -129,17 +109,6 @@ const WorkspacesWithSidebar: React.FunctionComponent<
129109
onChange.current(activeTab, activeTabCollectionInfo);
130110
}, [activeTab, activeTabCollectionInfo]);
131111

132-
// const {
133-
// data: savedTabs,
134-
// loading,
135-
// error,
136-
// } = usePromise(savedWorkspacesPromise);
137-
// // TODO: test these
138-
// if (loading) return <div>Loading...</div>;
139-
// if (error) return <div>Error: {error.message}</div>;
140-
141-
// console.log('Loaded saved tabs in workspace with sidebar', savedTabs);
142-
143112
return (
144113
<WorkspacesServiceProvider>
145114
<div
@@ -152,7 +121,7 @@ const WorkspacesWithSidebar: React.FunctionComponent<
152121
<div className={workspacesStyles}>
153122
<Workspaces
154123
openOnEmptyWorkspace={openOnEmptyWorkspace}
155-
savedWorkspacesPromise={savedWorkspacesPromise}
124+
userData={userData}
156125
></Workspaces>
157126
</div>
158127
</div>

packages/compass-workspaces/src/components/workspaces.tsx

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ import { useLogger } from '@mongodb-js/compass-logging/provider';
4040
import { connect } from '../stores/context';
4141
import { WorkspaceTabContextProvider } from './workspace-tab-context-provider';
4242
import type { WorkspaceTab } from '../types';
43+
import { loadWorkspaceStateFromUserData } from '@mongodb-js/compass-workspaces';
44+
import type { IUserData } from '../../../compass-user-data/dist/user-data';
45+
import type { WorkspacesStateSchema } from '@mongodb-js/compass-workspaces';
4346

4447
const emptyWorkspaceStyles = css({
4548
margin: '0 auto',
@@ -78,7 +81,7 @@ type CompassWorkspacesProps = {
7881
collectionInfo: Record<string, CollectionTabInfo>;
7982
databaseInfo: Record<string, DatabaseTabInfo>;
8083
openOnEmptyWorkspace?: OpenWorkspaceOptions | null;
81-
savedWorkspacesPromise?: Promise<OpenWorkspaceOptions[] | null>;
84+
userData: IUserData<typeof WorkspacesStateSchema>;
8285

8386
onSelectTab(at: number): void;
8487
onSelectNextTab(): void;
@@ -101,7 +104,7 @@ const CompassWorkspaces: React.FunctionComponent<CompassWorkspacesProps> = ({
101104
collectionInfo,
102105
databaseInfo,
103106
openOnEmptyWorkspace,
104-
savedWorkspacesPromise,
107+
userData,
105108
onSelectTab,
106109
onSelectNextTab,
107110
onSelectPrevTab,
@@ -124,68 +127,68 @@ const CompassWorkspaces: React.FunctionComponent<CompassWorkspacesProps> = ({
124127

125128
const connectionActions = useConnectionActions();
126129
const { getConnectionById } = useConnectionsListRef();
127-
const savedWorkspacesPromiseRef = useRef(savedWorkspacesPromise);
130+
const savedWorkspacesPromiseRef = useRef(
131+
loadWorkspaceStateFromUserData(userData)
132+
);
128133

129134
useEffect(() => {
130-
if (savedWorkspacesPromiseRef.current) {
131-
savedWorkspacesPromiseRef.current.then(
132-
(res) => {
133-
if (res !== null) {
134-
showConfirmation({
135-
title: 'Reopen closed tabs?',
136-
description:
137-
'Your connection and tabs were closed, this action will reopen your previous session',
138-
buttonText: 'Reopen tabs',
139-
}).then(
140-
(confirm) => {
141-
if (confirm) {
142-
const workspacesToRestore: OpenWorkspaceOptions[] = [];
143-
const connectionsToRestore: Map<string, ConnectionInfo> =
144-
new Map();
145-
res.forEach((workspace) => {
146-
// If the workspace is tied to a connection, check if the connection exists
147-
// and add it to the list of connections to restore if so.
148-
if ('connectionId' in workspace) {
149-
const connectionInfo = getConnectionById(
150-
workspace.connectionId
151-
)?.info;
152-
153-
if (!connectionInfo) {
154-
return;
155-
}
135+
savedWorkspacesPromiseRef.current.then(
136+
(res) => {
137+
if (res !== null) {
138+
showConfirmation({
139+
title: 'Reopen closed tabs?',
140+
description:
141+
'Your connection and tabs were closed, this action will reopen your previous session',
142+
buttonText: 'Reopen tabs',
143+
}).then(
144+
(confirm) => {
145+
if (confirm) {
146+
const workspacesToRestore: OpenWorkspaceOptions[] = [];
147+
const connectionsToRestore: Map<string, ConnectionInfo> =
148+
new Map();
149+
res.forEach((workspace) => {
150+
// If the workspace is tied to a connection, check if the connection exists
151+
// and add it to the list of connections to restore if so.
152+
if ('connectionId' in workspace) {
153+
const connectionInfo = getConnectionById(
154+
workspace.connectionId
155+
)?.info;
156156

157-
connectionsToRestore.set(
158-
workspace.connectionId,
159-
connectionInfo
160-
);
157+
if (!connectionInfo) {
158+
return;
161159
}
162160

163-
workspacesToRestore.push(workspace);
164-
});
161+
connectionsToRestore.set(
162+
workspace.connectionId,
163+
connectionInfo
164+
);
165+
}
166+
167+
workspacesToRestore.push(workspace);
168+
});
165169

166-
connectionsToRestore.forEach((connectionInfo) => {
167-
void connectionActions.connect(connectionInfo);
168-
});
170+
connectionsToRestore.forEach((connectionInfo) => {
171+
void connectionActions.connect(connectionInfo);
172+
});
169173

170-
onRestoreTabs(workspacesToRestore);
171-
}
172-
},
173-
(err) => {
174-
throw err;
174+
onRestoreTabs(workspacesToRestore);
175175
}
176-
);
177-
}
178-
},
179-
(err) => {
180-
log.error(
181-
mongoLogId(1_001_000_361),
182-
'Workspaces',
183-
'Failed to load saved workspaces from previous session',
184-
{ error: err }
176+
},
177+
(err) => {
178+
throw err;
179+
}
185180
);
186181
}
187-
);
188-
}
182+
},
183+
(err) => {
184+
log.error(
185+
mongoLogId(1_001_000_361),
186+
'Workspaces',
187+
'Failed to load saved workspaces from previous session',
188+
{ error: err }
189+
);
190+
}
191+
);
189192
}, [
190193
savedWorkspacesPromiseRef,
191194
onRestoreTabs,

0 commit comments

Comments
 (0)