Skip to content

Commit 1cb10b2

Browse files
authored
fix: finish readOnly logic COMPASS-7972 (#5905)
1 parent 826fca9 commit 1cb10b2

File tree

5 files changed

+140
-39
lines changed

5 files changed

+140
-39
lines changed

packages/compass-connections-navigation/src/connections-navigation-tree.spec.tsx

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,27 @@ const connections: ConnectedConnection[] = [
5050
collectionsStatus: 'ready',
5151
collectionsLength: 3,
5252
collections: [
53-
{ _id: 'db_ready.meow', name: 'meow', type: 'collection' },
54-
{ _id: 'db_ready.woof', name: 'woof', type: 'timeseries' },
55-
{ _id: 'db_ready.bwok', name: 'bwok', type: 'view' },
53+
{
54+
_id: 'db_ready.meow',
55+
name: 'meow',
56+
type: 'collection',
57+
sourceName: '',
58+
pipeline: [],
59+
},
60+
{
61+
_id: 'db_ready.woof',
62+
name: 'woof',
63+
type: 'timeseries',
64+
sourceName: '',
65+
pipeline: [],
66+
},
67+
{
68+
_id: 'db_ready.bwok',
69+
name: 'bwok',
70+
type: 'view',
71+
sourceName: '',
72+
pipeline: [],
73+
},
5674
],
5775
},
5876
],
@@ -235,6 +253,31 @@ describe('ConnectionsNavigationTree', function () {
235253
});
236254

237255
describe('when connection is writable', function () {
256+
it('should show all connection actions', async function () {
257+
await renderConnectionsNavigationTree();
258+
259+
userEvent.hover(screen.getByText('turtles'));
260+
261+
const connection = screen.getByTestId('connection_ready');
262+
263+
expect(within(connection).getByTitle('Create database')).to.be.visible;
264+
265+
const otherActions = within(connection).getByTitle('Show actions');
266+
expect(otherActions).to.exist;
267+
268+
userEvent.click(otherActions);
269+
270+
expect(screen.getByText('Open MongoDB shell')).to.be.visible;
271+
expect(
272+
screen.getByTestId('sidebar-navigation-item-actions-open-shell-action')
273+
).not.to.have.attribute('disabled');
274+
expect(screen.getByText('View performance metrics')).to.be.visible;
275+
expect(screen.getByText('Show connection info')).to.be.visible;
276+
expect(screen.getByText('Copy connection string')).to.be.visible;
277+
expect(screen.getByText('Unfavorite')).to.be.visible;
278+
expect(screen.getByText('Disconnect')).to.be.visible;
279+
});
280+
238281
it('should show all database actions on hover', async function () {
239282
await renderConnectionsNavigationTree({
240283
expanded: { connection_ready: {} },
@@ -379,6 +422,41 @@ describe('ConnectionsNavigationTree', function () {
379422
},
380423
].forEach(function ({ name, renderReadonlyComponent }) {
381424
describe(name, function () {
425+
it('should show reduced connection actions', async function () {
426+
await renderReadonlyComponent();
427+
428+
userEvent.hover(screen.getByText('turtles'));
429+
430+
const connection = screen.getByTestId('connection_ready');
431+
432+
expect(within(connection).queryByTitle('Create database')).not.to.exist;
433+
434+
const otherActions = within(connection).getByTitle('Show actions');
435+
expect(otherActions).to.exist;
436+
437+
userEvent.click(otherActions);
438+
439+
expect(screen.getByText('Open MongoDB shell')).to.be.visible;
440+
if (name !== 'when connection is datalake') {
441+
expect(
442+
screen.getByTestId(
443+
'sidebar-navigation-item-actions-open-shell-action'
444+
)
445+
).to.have.attribute('disabled');
446+
} else {
447+
expect(
448+
screen.getByTestId(
449+
'sidebar-navigation-item-actions-open-shell-action'
450+
)
451+
).not.to.have.attribute('disabled');
452+
}
453+
expect(screen.getByText('View performance metrics')).to.be.visible;
454+
expect(screen.getByText('Show connection info')).to.be.visible;
455+
expect(screen.getByText('Copy connection string')).to.be.visible;
456+
expect(screen.getByText('Unfavorite')).to.be.visible;
457+
expect(screen.getByText('Disconnect')).to.be.visible;
458+
});
459+
382460
it('should not show database actions', async function () {
383461
await renderReadonlyComponent({
384462
expanded: {

packages/compass-connections-navigation/src/connections-navigation-tree.tsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,13 @@ const ConnectionsNavigationTree: React.FunctionComponent<
6666
const id = useId();
6767

6868
const treeData = useMemo(() => {
69-
return getVirtualTreeItems(connections, isSingleConnection, expanded);
70-
}, [connections, isSingleConnection, expanded]);
69+
return getVirtualTreeItems({
70+
connections,
71+
isSingleConnection,
72+
expandedItems: expanded,
73+
preferencesReadOnly,
74+
});
75+
}, [connections, isSingleConnection, expanded, preferencesReadOnly]);
7176

7277
const onDefaultAction: OnDefaultAction<SidebarActionableItem> = useCallback(
7378
(item, evt) => {
@@ -112,7 +117,8 @@ const ConnectionsNavigationTree: React.FunctionComponent<
112117
item.connectionInfo?.savedConnectionType === 'favorite';
113118
if (item.connectionStatus === ConnectionStatus.Connected) {
114119
return connectedConnectionItemActions({
115-
isReadOnly: preferencesReadOnly || item.isConnectionReadOnly,
120+
hasWriteActionsEnabled: item.hasWriteActionsEnabled,
121+
isShellEnabled: item.isShellEnabled,
116122
isFavorite,
117123
isPerformanceTabSupported: item.isPerformanceTabSupported,
118124
});
@@ -124,17 +130,17 @@ const ConnectionsNavigationTree: React.FunctionComponent<
124130
}
125131
case 'database':
126132
return databaseItemActions({
127-
isReadOnly: preferencesReadOnly || item.isConnectionReadOnly,
133+
hasWriteActionsEnabled: item.hasWriteActionsEnabled,
128134
});
129135
default:
130136
return collectionItemActions({
131-
isReadOnly: preferencesReadOnly || item.isConnectionReadOnly,
137+
hasWriteActionsEnabled: item.hasWriteActionsEnabled,
132138
type: item.type,
133139
isRenameCollectionEnabled,
134140
});
135141
}
136142
},
137-
[preferencesReadOnly, isRenameCollectionEnabled]
143+
[isRenameCollectionEnabled]
138144
);
139145

140146
const isTestEnv = process.env.NODE_ENV === 'test';

packages/compass-connections-navigation/src/item-actions.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,18 @@ export const notConnectedConnectionItemActions = ({
5050
};
5151

5252
export const connectedConnectionItemActions = ({
53-
isReadOnly,
53+
hasWriteActionsEnabled,
5454
isFavorite,
5555
isPerformanceTabSupported,
56+
isShellEnabled,
5657
}: {
57-
isReadOnly: boolean;
58+
hasWriteActionsEnabled: boolean;
5859
isFavorite: boolean;
5960
isPerformanceTabSupported: boolean;
61+
isShellEnabled: boolean;
6062
}): NavigationItemActions => {
6163
const actions: NavigationItemActions = [];
62-
if (!isReadOnly) {
64+
if (!hasWriteActionsEnabled) {
6365
actions.push({
6466
action: 'create-database',
6567
icon: 'Plus',
@@ -71,6 +73,8 @@ export const connectedConnectionItemActions = ({
7173
action: 'open-shell',
7274
icon: 'Shell',
7375
label: 'Open MongoDB shell',
76+
isDisabled: !isShellEnabled,
77+
disabledDescription: 'Not available',
7478
},
7579
{
7680
action: 'connection-performance-metrics',
@@ -105,11 +109,11 @@ export const connectedConnectionItemActions = ({
105109
};
106110

107111
export const databaseItemActions = ({
108-
isReadOnly,
112+
hasWriteActionsEnabled,
109113
}: {
110-
isReadOnly: boolean;
114+
hasWriteActionsEnabled: boolean;
111115
}): NavigationItemActions => {
112-
if (isReadOnly) {
116+
if (hasWriteActionsEnabled) {
113117
return [];
114118
}
115119
return [
@@ -127,11 +131,11 @@ export const databaseItemActions = ({
127131
};
128132

129133
export const collectionItemActions = ({
130-
isReadOnly,
134+
hasWriteActionsEnabled,
131135
type,
132136
isRenameCollectionEnabled,
133137
}: {
134-
isReadOnly: boolean;
138+
hasWriteActionsEnabled: boolean;
135139
type: 'collection' | 'view' | 'timeseries';
136140
isRenameCollectionEnabled: boolean;
137141
}): NavigationItemActions => {
@@ -143,7 +147,7 @@ export const collectionItemActions = ({
143147
},
144148
];
145149

146-
if (isReadOnly) {
150+
if (hasWriteActionsEnabled) {
147151
return actions;
148152
}
149153

packages/compass-connections-navigation/src/navigation-item.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,12 @@ export function NavigationItem({
6969
if (item.type === 'connection') {
7070
if (
7171
item.connectionStatus !== ConnectionStatus.Connected ||
72-
!item.isConnectionReadOnly
72+
!item.hasWriteActionsEnabled
7373
) {
7474
return 1;
7575
}
76-
// when connection is not readonly we have create-database as first
77-
// action which is why we would like to collapse entire actions when the
78-
// connection is readonly to avoid showing any other items from the menu
76+
// when connected connection is readonly we don't show the create-database action
77+
// so the whole action menu is collapsed
7978
return 0;
8079
}
8180
})();

packages/compass-connections-navigation/src/tree-data.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ export type ConnectedConnectionTreeItem = VirtualTreeItem & {
8080
connectionInfo: ConnectionInfo;
8181
connectionStatus: ConnectionStatus.Connected;
8282
isPerformanceTabSupported: boolean;
83-
isConnectionReadOnly: boolean;
83+
hasWriteActionsEnabled: boolean;
84+
isShellEnabled: boolean;
8485
};
8586

8687
export type DatabaseTreeItem = VirtualTreeItem & {
@@ -90,7 +91,7 @@ export type DatabaseTreeItem = VirtualTreeItem & {
9091
isExpanded: boolean;
9192
connectionId: string;
9293
dbName: string;
93-
isConnectionReadOnly: boolean;
94+
hasWriteActionsEnabled: boolean;
9495
};
9596

9697
export type CollectionTreeItem = VirtualTreeItem & {
@@ -100,7 +101,7 @@ export type CollectionTreeItem = VirtualTreeItem & {
100101
colorCode?: string;
101102
connectionId: string;
102103
namespace: string;
103-
isConnectionReadOnly: boolean;
104+
hasWriteActionsEnabled: boolean;
104105
};
105106

106107
export type SidebarActionableItem =
@@ -150,15 +151,19 @@ const connectedConnectionToItems = ({
150151
connectionIndex,
151152
connectionsLength,
152153
expandedItems = {},
154+
preferencesReadOnly,
153155
}: {
154156
connection: ConnectedConnection;
155157
connectionIndex: number;
156158
connectionsLength: number;
157159
expandedItems: Record<string, false | Record<string, boolean>>;
160+
preferencesReadOnly: boolean;
158161
}): SidebarTreeItem[] => {
159162
const isExpanded = !!expandedItems[connectionInfo.id];
160163
const colorCode = connectionInfo.favorite?.color;
161-
const isConnectionReadOnly = isDataLake || !isWritable;
164+
const hasWriteActionsEnabled =
165+
preferencesReadOnly || isDataLake || !isWritable;
166+
const isShellEnabled = !preferencesReadOnly && isWritable;
162167
const connectionTI: ConnectedConnectionTreeItem = {
163168
id: connectionInfo.id,
164169
level: 1,
@@ -172,7 +177,8 @@ const connectedConnectionToItems = ({
172177
connectionInfo,
173178
connectionStatus,
174179
isPerformanceTabSupported,
175-
isConnectionReadOnly,
180+
hasWriteActionsEnabled,
181+
isShellEnabled,
176182
};
177183

178184
const sidebarData: SidebarTreeItem[] = [connectionTI];
@@ -210,7 +216,7 @@ const connectedConnectionToItems = ({
210216
colorCode,
211217
databasesLength,
212218
databaseIndex,
213-
isConnectionReadOnly,
219+
hasWriteActionsEnabled,
214220
});
215221
})
216222
);
@@ -230,7 +236,7 @@ const databaseToItems = ({
230236
colorCode,
231237
databaseIndex,
232238
databasesLength,
233-
isConnectionReadOnly,
239+
hasWriteActionsEnabled,
234240
}: {
235241
database: Database;
236242
connectionId: string;
@@ -239,7 +245,7 @@ const databaseToItems = ({
239245
colorCode?: string;
240246
databaseIndex: number;
241247
databasesLength: number;
242-
isConnectionReadOnly: boolean;
248+
hasWriteActionsEnabled: boolean;
243249
}): SidebarTreeItem[] => {
244250
const isExpanded = !!expandedItems[id];
245251
const databaseTI: DatabaseTreeItem = {
@@ -254,7 +260,7 @@ const databaseToItems = ({
254260
connectionId,
255261
dbName: id,
256262
isExpandable: true,
257-
isConnectionReadOnly,
263+
hasWriteActionsEnabled,
258264
};
259265

260266
const sidebarData: SidebarTreeItem[] = [databaseTI];
@@ -292,7 +298,7 @@ const databaseToItems = ({
292298
colorCode,
293299
connectionId,
294300
namespace: id,
295-
isConnectionReadOnly,
301+
hasWriteActionsEnabled,
296302
isExpandable: false,
297303
}))
298304
);
@@ -311,11 +317,17 @@ const databaseToItems = ({
311317
* @param isSingleConnection - Whether the connections are a single connection.
312318
* @param expandedItems - The expanded items.
313319
*/
314-
export function getVirtualTreeItems(
315-
connections: (NotConnectedConnection | ConnectedConnection)[],
316-
isSingleConnection: boolean,
317-
expandedItems: Record<string, false | Record<string, boolean>> = {}
318-
): SidebarTreeItem[] {
320+
export function getVirtualTreeItems({
321+
connections,
322+
isSingleConnection,
323+
expandedItems = {},
324+
preferencesReadOnly,
325+
}: {
326+
connections: (NotConnectedConnection | ConnectedConnection)[];
327+
isSingleConnection: boolean;
328+
expandedItems: Record<string, false | Record<string, boolean>>;
329+
preferencesReadOnly: boolean;
330+
}): SidebarTreeItem[] {
319331
if (!isSingleConnection) {
320332
return connections.flatMap((connection, connectionIndex) => {
321333
if (connection.connectionStatus === ConnectionStatus.Connected) {
@@ -324,6 +336,7 @@ export function getVirtualTreeItems(
324336
expandedItems,
325337
connectionIndex,
326338
connectionsLength: connections.length,
339+
preferencesReadOnly,
327340
});
328341
} else {
329342
return notConnectedConnectionToItems({
@@ -342,7 +355,8 @@ export function getVirtualTreeItems(
342355
}
343356

344357
const dbExpandedItems = expandedItems[connection.connectionInfo.id] || {};
345-
const isConnectionReadOnly = connection.isDataLake || !connection.isWritable;
358+
const hasWriteActionsEnabled =
359+
preferencesReadOnly || connection.isDataLake || !connection.isWritable;
346360
return connection.databases.flatMap((database, databaseIndex) => {
347361
return databaseToItems({
348362
connectionId: connection.connectionInfo.id,
@@ -351,7 +365,7 @@ export function getVirtualTreeItems(
351365
level: 1,
352366
databasesLength: connection.databasesLength,
353367
databaseIndex,
354-
isConnectionReadOnly,
368+
hasWriteActionsEnabled,
355369
});
356370
});
357371
}

0 commit comments

Comments
 (0)