Skip to content

Commit afb58a9

Browse files
committed
chore(preferences): add readWrite preference that disables some admin features in compass
1 parent 266375e commit afb58a9

File tree

12 files changed

+189
-133
lines changed

12 files changed

+189
-133
lines changed

packages/compass-collection/src/components/collection-header-actions/collection-header-actions.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ const CollectionHeaderActions: React.FunctionComponent<
8989
const { id: connectionId, atlasMetadata } = connectionInfo;
9090
const { openCollectionWorkspace, openEditViewWorkspace, openShellWorkspace } =
9191
useOpenWorkspace();
92-
const { readOnly: preferencesReadOnly, enableShell: showOpenShellButton } =
93-
usePreferences(['readOnly', 'enableShell']);
92+
const { readWrite: preferencesReadWrite, enableShell: showOpenShellButton } =
93+
usePreferences(['readWrite', 'enableShell']);
9494
const track = useTelemetry();
9595

9696
// Get experiment assignment for Mock Data Generator
@@ -119,6 +119,10 @@ const CollectionHeaderActions: React.FunctionComponent<
119119
!hasSchemaAnalysisData &&
120120
schemaAnalysisStatus !== SCHEMA_ANALYSIS_STATE_ANALYZING;
121121

122+
const isView = isReadonly && sourceName && !editViewName;
123+
124+
const showViewEdit = isView && !preferencesReadWrite;
125+
122126
return (
123127
<div
124128
className={collectionHeaderActionsStyles}
@@ -198,7 +202,7 @@ const CollectionHeaderActions: React.FunctionComponent<
198202
Visualize Your Data
199203
</Button>
200204
)}
201-
{isReadonly && sourceName && !editViewName && !preferencesReadOnly && (
205+
{showViewEdit && (
202206
<Button
203207
data-testid="collection-header-actions-edit-button"
204208
size={ButtonSize.Small}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
} from '@mongodb-js/compass-components';
2222
import { useConnectable } from '@mongodb-js/compass-connections/provider';
2323
import type { WorkspaceTab } from '@mongodb-js/compass-workspaces';
24-
import { usePreference } from 'compass-preferences-model/provider';
24+
import { usePreferences } from 'compass-preferences-model/provider';
2525
import type { NavigationItemActions } from './item-actions';
2626
import {
2727
collectionItemActions,
@@ -56,12 +56,21 @@ const ConnectionsNavigationTree: React.FunctionComponent<
5656
onItemExpand,
5757
onItemAction,
5858
}) => {
59-
const preferencesShellEnabled = usePreference('enableShell');
60-
const preferencesReadOnly = usePreference('readOnly');
61-
const isRenameCollectionEnabled = usePreference(
62-
'enableRenameCollectionModal'
63-
);
64-
const showDisabledConnections = !!usePreference('showDisabledConnections');
59+
const {
60+
enableRenameCollectionModal,
61+
enableShell: preferencesShellEnabled,
62+
readOnly: preferencesReadOnly,
63+
readWrite: preferencesReadWrite,
64+
showDisabledConnections,
65+
} = usePreferences([
66+
'enableShell',
67+
'readOnly',
68+
'readWrite',
69+
'enableRenameCollectionModal',
70+
'showDisabledConnections',
71+
]);
72+
const isRenameCollectionEnabled =
73+
enableRenameCollectionModal && !preferencesReadWrite;
6574

6675
const id = useId();
6776
const getConnectable = useConnectable();

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

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -164,32 +164,38 @@ export const notConnectedConnectionItemActions = ({
164164

165165
export const databaseItemActions = ({
166166
hasWriteActionsDisabled,
167+
canDeleteDatabase,
167168
}: {
168169
hasWriteActionsDisabled: boolean;
170+
canDeleteDatabase: boolean;
169171
}): NavigationItemActions => {
170172
if (hasWriteActionsDisabled) {
171173
return [];
172174
}
173-
return [
175+
return stripNullActions([
174176
{
175177
action: 'create-collection',
176178
icon: 'Plus',
177179
label: 'Create collection',
178180
},
179-
{
180-
action: 'drop-database',
181-
icon: 'Trash',
182-
label: 'Drop database',
183-
},
184-
];
181+
canDeleteDatabase
182+
? {
183+
action: 'drop-database',
184+
icon: 'Trash',
185+
label: 'Drop database',
186+
}
187+
: null,
188+
]);
185189
};
186190

187191
export const collectionItemActions = ({
188192
hasWriteActionsDisabled,
193+
canEditCollection,
189194
type,
190195
isRenameCollectionEnabled,
191196
}: {
192197
hasWriteActionsDisabled: boolean;
198+
canEditCollection: boolean;
193199
type: 'collection' | 'view' | 'timeseries';
194200
isRenameCollectionEnabled: boolean;
195201
}): NavigationItemActions => {
@@ -205,7 +211,7 @@ export const collectionItemActions = ({
205211
return actions;
206212
}
207213

208-
if (type === 'view') {
214+
if (canEditCollection && type === 'view') {
209215
actions.push({ separator: true });
210216
actions.push(
211217
{
@@ -228,7 +234,7 @@ export const collectionItemActions = ({
228234
return actions;
229235
}
230236

231-
if (type !== 'timeseries' && isRenameCollectionEnabled) {
237+
if (type !== 'timeseries' && canEditCollection && isRenameCollectionEnabled) {
232238
actions.push({ separator: true });
233239
actions.push({
234240
action: 'rename-collection',
@@ -237,11 +243,13 @@ export const collectionItemActions = ({
237243
});
238244
}
239245

240-
actions.push({
241-
action: 'drop-collection',
242-
label: 'Drop collection',
243-
icon: 'Trash',
244-
});
246+
if (canEditCollection) {
247+
actions.push({
248+
action: 'drop-collection',
249+
label: 'Drop collection',
250+
icon: 'Trash',
251+
});
252+
}
245253

246254
return actions;
247255
};
@@ -308,12 +316,14 @@ export const connectionContextMenuActions = ({
308316

309317
export const databaseContextMenuActions = ({
310318
hasWriteActionsDisabled,
319+
canDeleteDatabase,
311320
isShellEnabled,
312321
isPerformanceTabAvailable,
313322
isPerformanceTabSupported,
314323
isAtlas,
315324
}: {
316325
hasWriteActionsDisabled: boolean;
326+
canDeleteDatabase: boolean;
317327
isShellEnabled: boolean;
318328
isPerformanceTabAvailable: boolean;
319329
isPerformanceTabSupported: boolean;
@@ -336,7 +346,7 @@ export const databaseContextMenuActions = ({
336346
icon: 'Plus',
337347
label: 'Create database',
338348
},
339-
hasWriteActionsDisabled
349+
hasWriteActionsDisabled && canDeleteDatabase
340350
? null
341351
: {
342352
action: 'drop-database',
@@ -358,6 +368,7 @@ export const databaseContextMenuActions = ({
358368

359369
export const collectionContextMenuActions = ({
360370
hasWriteActionsDisabled,
371+
canEditCollection,
361372
type,
362373
isRenameCollectionEnabled,
363374
isPerformanceTabAvailable,
@@ -366,6 +377,7 @@ export const collectionContextMenuActions = ({
366377
isShellEnabled,
367378
}: {
368379
hasWriteActionsDisabled: boolean;
380+
canEditCollection: boolean;
369381
type: 'collection' | 'view' | 'timeseries';
370382
isRenameCollectionEnabled: boolean;
371383
isShellEnabled: boolean;
@@ -385,7 +397,7 @@ export const collectionContextMenuActions = ({
385397
let writeActions: NavigationItemActions = [];
386398

387399
if (!hasWriteActionsDisabled) {
388-
if (type === 'view') {
400+
if (type === 'view' && canEditCollection) {
389401
writeActions = [
390402
{ separator: true },
391403
{
@@ -407,7 +419,7 @@ export const collectionContextMenuActions = ({
407419
} else {
408420
writeActions = stripNullActions([
409421
{ separator: true },
410-
type !== 'timeseries' && isRenameCollectionEnabled
422+
type !== 'timeseries' && canEditCollection && isRenameCollectionEnabled
411423
? {
412424
action: 'rename-collection',
413425
label: 'Rename collection',
@@ -419,11 +431,13 @@ export const collectionContextMenuActions = ({
419431
icon: 'Plus',
420432
label: 'Create collection',
421433
},
422-
{
423-
action: 'drop-collection',
424-
label: 'Drop collection',
425-
icon: 'Trash',
426-
},
434+
canEditCollection
435+
? {
436+
action: 'drop-collection',
437+
label: 'Drop collection',
438+
icon: 'Trash',
439+
}
440+
: null,
427441
]);
428442
}
429443
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ export type DatabaseTreeItem = VirtualTreeItem & {
103103
connectionItem: ConnectedConnectionTreeItem;
104104
dbName: string;
105105
hasWriteActionsDisabled: boolean;
106+
canDeleteDatabase: boolean;
106107
inferredFromPrivileges: boolean;
107108
};
108109

@@ -115,6 +116,7 @@ export type CollectionTreeItem = VirtualTreeItem & {
115116
databaseItem: DatabaseTreeItem;
116117
namespace: string;
117118
hasWriteActionsDisabled: boolean;
119+
canEditCollection: boolean;
118120
inferredFromPrivileges: boolean;
119121
};
120122

@@ -180,13 +182,15 @@ const connectedConnectionToItems = ({
180182
connectionsLength,
181183
expandedItems = {},
182184
preferencesReadOnly,
185+
preferencesReadWrite,
183186
preferencesShellEnabled,
184187
}: {
185188
connection: ConnectedConnection;
186189
connectionIndex: number;
187190
connectionsLength: number;
188191
expandedItems: Record<string, false | Record<string, boolean>>;
189192
preferencesReadOnly: boolean;
193+
preferencesReadWrite: boolean;
190194
preferencesShellEnabled: boolean;
191195
}): SidebarTreeItem[] => {
192196
const isExpanded = !!expandedItems[connectionInfo.id];
@@ -250,6 +254,8 @@ const connectedConnectionToItems = ({
250254
databasesLength,
251255
databaseIndex,
252256
hasWriteActionsDisabled,
257+
canDeleteDatabase: !preferencesReadWrite,
258+
canEditCollection: !preferencesReadWrite,
253259
});
254260
})
255261
);
@@ -272,6 +278,8 @@ const databaseToItems = ({
272278
databaseIndex,
273279
databasesLength,
274280
hasWriteActionsDisabled,
281+
canDeleteDatabase,
282+
canEditCollection,
275283
}: {
276284
database: Database;
277285
connectionId: string;
@@ -282,6 +290,8 @@ const databaseToItems = ({
282290
databaseIndex: number;
283291
databasesLength: number;
284292
hasWriteActionsDisabled: boolean;
293+
canDeleteDatabase: boolean;
294+
canEditCollection: boolean;
285295
}): SidebarTreeItem[] => {
286296
const isExpanded = !!expandedItems[id];
287297
const databaseTI: DatabaseTreeItem = {
@@ -299,6 +309,7 @@ const databaseToItems = ({
299309
isExpandable: true,
300310
hasWriteActionsDisabled,
301311
inferredFromPrivileges,
312+
canDeleteDatabase,
302313
};
303314

304315
const sidebarData: SidebarTreeItem[] = [databaseTI];
@@ -341,6 +352,7 @@ const databaseToItems = ({
341352
hasWriteActionsDisabled,
342353
isExpandable: false,
343354
inferredFromPrivileges,
355+
canEditCollection,
344356
})
345357
)
346358
);
@@ -359,11 +371,13 @@ export function getVirtualTreeItems({
359371
connections,
360372
expandedItems = {},
361373
preferencesReadOnly,
374+
preferencesReadWrite,
362375
preferencesShellEnabled,
363376
}: {
364377
connections: (NotConnectedConnection | ConnectedConnection)[];
365378
expandedItems: Record<string, false | Record<string, boolean>>;
366379
preferencesReadOnly: boolean;
380+
preferencesReadWrite: boolean;
367381
preferencesShellEnabled: boolean;
368382
}): SidebarTreeItem[] {
369383
return connections.flatMap((connection, connectionIndex) => {
@@ -374,6 +388,7 @@ export function getVirtualTreeItems({
374388
connectionIndex,
375389
connectionsLength: connections.length,
376390
preferencesReadOnly,
391+
preferencesReadWrite,
377392
preferencesShellEnabled,
378393
});
379394
} else {

0 commit comments

Comments
 (0)