Skip to content

Commit 2e40ef2

Browse files
committed
Clean up "Actions" generic types
1 parent fdb64f8 commit 2e40ef2

File tree

9 files changed

+36
-38
lines changed

9 files changed

+36
-38
lines changed

packages/compass-components/src/components/actions/dropdown-menu-button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ export function DropdownMenuButton<Action extends string>({
117117
<MenuItem
118118
active={activeAction === action}
119119
key={action}
120-
data-testid={actionTestId<Action>(dataTestId, action)}
120+
data-testid={actionTestId(dataTestId, action)}
121121
data-action={action}
122122
data-menuitem={true}
123123
glyph={<ActionGlyph glyph={icon} size={iconSize} />}

packages/compass-components/src/components/actions/item-action-controls.spec.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('item action controls components', function () {
2020
actions={[]}
2121
onAction={() => {}}
2222
data-testid="test-actions"
23-
></ItemActionControls>
23+
/>
2424
);
2525

2626
expect(screen.queryByTestId('test-actions')).to.not.exist;
@@ -32,7 +32,7 @@ describe('item action controls components', function () {
3232
actions={[{ action: 'copy', label: 'Copy', icon: 'Copy' }]}
3333
onAction={() => {}}
3434
data-testid="test-actions"
35-
></ItemActionControls>
35+
/>
3636
);
3737

3838
expect(screen.getByTestId('test-actions')).to.exist;
@@ -47,7 +47,7 @@ describe('item action controls components', function () {
4747
onAction={() => {}}
4848
data-testid="test-actions"
4949
collapseToMenuThreshold={1}
50-
></ItemActionControls>
50+
/>
5151
);
5252

5353
const trigger = screen.getByTestId('test-actions-show-actions');
@@ -69,7 +69,7 @@ describe('item action controls components', function () {
6969
onAction={onAction}
7070
data-testid="test-actions"
7171
collapseToMenuThreshold={3}
72-
></ItemActionControls>
72+
/>
7373
);
7474

7575
expect(onAction).not.to.be.called;
@@ -90,7 +90,7 @@ describe('item action controls components', function () {
9090
onAction={onAction}
9191
data-testid="test-actions"
9292
collapseToMenuThreshold={1}
93-
></ItemActionControls>
93+
/>
9494
);
9595

9696
expect(onAction).not.to.be.called;
@@ -112,7 +112,7 @@ describe('item action controls components', function () {
112112
onAction={() => {}}
113113
data-testid="test-actions"
114114
collapseAfter={1}
115-
></ItemActionControls>
115+
/>
116116
);
117117

118118
expect(screen.queryByTestId('test-actions-connect-action')).to.exist;
@@ -146,7 +146,7 @@ describe('item action controls components', function () {
146146
onAction={() => {}}
147147
data-testid="test-actions"
148148
collapseAfter={1}
149-
></ItemActionControls>
149+
/>
150150
);
151151

152152
const actionButton = screen.getByTestId('test-actions-connect-action');

packages/compass-components/src/components/actions/item-action-group.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ export function ItemActionGroup<Action extends string>({
8383
iconStyle={iconStyle}
8484
iconClassName={iconClassName}
8585
onClick={onClick}
86-
data-testid={actionTestId<Action>(dataTestId, itemProps.action)}
86+
data-testid={actionTestId(dataTestId, itemProps.action)}
8787
/>
8888
);
8989

packages/compass-components/src/components/actions/item-action-menu.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function ItemActionMenu<Action extends string>({
141141
return (
142142
<MenuItem
143143
key={action}
144-
data-testid={actionTestId<Action>(dataTestId, action)}
144+
data-testid={actionTestId(dataTestId, action)}
145145
data-action={action}
146146
data-menuitem={true}
147147
glyph={<ActionGlyph glyph={icon} size={iconSize} />}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
export function actionTestId<Action extends string>(
2-
dataTestId: string | undefined,
3-
action: Action
4-
) {
1+
export function actionTestId(dataTestId: string | undefined, action: string) {
52
return dataTestId ? `${dataTestId}-${action}-action` : undefined;
63
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
ItemActionControls,
77
cx,
88
} from '@mongodb-js/compass-components';
9-
import { ROW_HEIGHT, type Actions } from './constants';
9+
import { type Actions, ROW_HEIGHT } from './constants';
1010
import { ExpandButton } from './tree-item';
1111
import { type NavigationItemActions } from './item-actions';
1212

@@ -128,13 +128,13 @@ export const NavigationBaseItem: React.FC<NavigationBaseItemProps> = ({
128128
<span title={name}>{name}</span>
129129
</div>
130130
<div className={actionControlsWrapperStyles}>
131-
<ItemActionControls<Actions>
131+
<ItemActionControls
132132
menuClassName={menuStyles}
133133
isVisible={isActive || isHovered || isFocused}
134134
data-testid="sidebar-navigation-item-actions"
135135
iconSize="xsmall"
136136
{...actionProps}
137-
></ItemActionControls>
137+
/>
138138
{children}
139139
</div>
140140
</div>

packages/compass-connections-navigation/src/connect-button.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import {
33
Button,
44
type ItemComponentProps,
55
} from '@mongodb-js/compass-components';
6+
import type { Actions } from './constants';
67

7-
type ConnectButtonProps = ItemComponentProps<string>;
8+
type ConnectButtonProps = ItemComponentProps<Actions>;
89

910
export function ConnectButton({
1011
action,

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

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
import type { ItemAction } from '@mongodb-js/compass-components';
22
import { type ConnectionInfo } from '@mongodb-js/connection-info';
3-
import { type Actions } from './constants';
43
import { type ItemSeparator } from '@mongodb-js/compass-components';
54
import { type NotConnectedConnectionStatus } from './tree-data';
65
import { ConnectButton } from './connect-button';
6+
import type { Actions } from './constants';
77

8-
export type NavigationItemActions = (ItemAction<Actions> | ItemSeparator)[];
8+
export type NavigationItemAction = ItemAction<Actions> | ItemSeparator;
9+
export type NavigationItemActions = NavigationItemAction[];
10+
export type NullableNavigationItemActions = (NavigationItemAction | null)[];
11+
12+
function stripNullActions(
13+
actions: NullableNavigationItemActions
14+
): NavigationItemActions {
15+
return actions.filter(
16+
(action): action is Exclude<typeof action, null> => action !== null
17+
);
18+
}
919

1020
export const commonConnectionItemActions = ({
1121
connectionInfo,
1222
}: {
1323
connectionInfo: ConnectionInfo;
14-
}): NavigationItemActions => {
24+
}): NavigationItemAction[] => {
1525
const isAtlas = !!connectionInfo.atlasMetadata;
16-
const actions: (ItemAction<Actions> | ItemSeparator | null)[] = [
26+
return stripNullActions([
1727
isAtlas
1828
? null
1929
: {
@@ -58,11 +68,7 @@ export const commonConnectionItemActions = ({
5868
icon: 'Trash',
5969
variant: 'destructive',
6070
},
61-
];
62-
63-
return actions.filter((action): action is Exclude<typeof action, null> => {
64-
return !!action;
65-
});
71+
]);
6672
};
6773

6874
export const connectedConnectionItemActions = ({
@@ -86,7 +92,7 @@ export const connectedConnectionItemActions = ({
8692
const connectionManagementActions = commonConnectionItemActions({
8793
connectionInfo,
8894
});
89-
const actions: (ItemAction<Actions> | ItemSeparator | null)[] = [
95+
return stripNullActions([
9096
hasWriteActionsDisabled
9197
? null
9298
: {
@@ -130,11 +136,7 @@ export const connectedConnectionItemActions = ({
130136
},
131137
{ separator: true },
132138
...connectionManagementActions,
133-
];
134-
135-
return actions.filter((action): action is Exclude<typeof action, null> => {
136-
return !!action;
137-
});
139+
]);
138140
};
139141

140142
export const notConnectedConnectionItemActions = ({

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import { ConnectionStatus } from '@mongodb-js/compass-connections/provider';
2020
import { WithStatusMarker } from './with-status-marker';
2121
import type { Actions } from './constants';
2222

23-
type NavigationActions = 'open-non-genuine-mongodb-modal' | 'open-csfle-modal';
24-
2523
const nonGenuineBtnStyles = css({
2624
color: palette.yellow.dark2,
2725
background: palette.yellow.light3,
@@ -214,7 +212,7 @@ export function NavigationItem({
214212
return [];
215213
}
216214

217-
const actions: ItemAction<NavigationActions>[] = [];
215+
const actions: ItemAction<Actions>[] = [];
218216
if (!item.isGenuineMongoDB) {
219217
actions.push({
220218
action: 'open-non-genuine-mongodb-modal',
@@ -272,14 +270,14 @@ export function NavigationItem({
272270
actionProps={actionProps}
273271
>
274272
{!!connectionStaticActions.length && (
275-
<ItemActionControls<NavigationActions>
273+
<ItemActionControls
276274
iconSize="xsmall"
277275
actions={connectionStaticActions}
278276
onAction={onAction}
279277
// these are static buttons that we want visible always on the
280278
// sidebar, not as menu item but as action group
281279
collapseAfter={connectionStaticActions.length}
282-
></ItemActionControls>
280+
/>
283281
)}
284282
</NavigationBaseItem>
285283
)}

0 commit comments

Comments
 (0)