Skip to content

Commit 1d15300

Browse files
committed
Clean up "Actions" generic types
1 parent 5e8935a commit 1d15300

File tree

11 files changed

+40
-43
lines changed

11 files changed

+40
-43
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
@@ -116,7 +116,7 @@ export function DropdownMenuButton<Action extends string>({
116116
<MenuItem
117117
active={activeAction === action}
118118
key={action}
119-
data-testid={actionTestId<Action>(dataTestId, action)}
119+
data-testid={actionTestId(dataTestId, action)}
120120
data-action={action}
121121
data-menuitem={true}
122122
glyph={<ActionGlyph glyph={icon} size={iconSize} />}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { ItemActionButtonSize } from './constants';
55
import type { ItemComponentProps } from './types';
66
import { SmallIconButton } from './small-icon-button';
77

8-
export function ItemActionButton<Action extends string>({
8+
export function ItemActionButton({
99
action,
1010
icon = <></>,
1111
label,
@@ -17,7 +17,7 @@ export function ItemActionButton<Action extends string>({
1717
iconStyle,
1818
isDisabled,
1919
'data-testid': dataTestId,
20-
}: ItemComponentProps<Action>) {
20+
}: ItemComponentProps) {
2121
return (
2222
<SmallIconButton
2323
key={action}

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-controls.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export type ItemActionControlsProps<Action extends string> = {
3232
'data-testid'?: string;
3333
};
3434

35-
export function ItemActionControls<Action extends string>({
35+
export function ItemActionControls<Action extends string = string>({
3636
isVisible = true,
3737
actions,
3838
onAction,

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ export function ItemActionMenu<Action extends string>({
140140
return (
141141
<MenuItem
142142
key={action}
143-
data-testid={actionTestId<Action>(dataTestId, action)}
143+
data-testid={actionTestId(dataTestId, action)}
144144
data-action={action}
145145
data-menuitem={true}
146146
glyph={<ActionGlyph glyph={icon} size={iconSize} />}

packages/compass-components/src/components/actions/types.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { glyphs } from '@leafygreen-ui/icon';
22

33
import type { ItemActionButtonSize } from './constants';
44

5-
export type ItemBase<Action extends string> = {
5+
export type ItemBase<Action extends string = string> = {
66
action: Action;
77
label: string;
88
icon?: React.ReactChild;
@@ -18,7 +18,7 @@ export type ItemBase<Action extends string> = {
1818
expandedAs?: React.ComponentType<ItemComponentProps<Action>>;
1919
};
2020

21-
export type ItemComponentProps<Action extends string> = Omit<
21+
export type ItemComponentProps<Action extends string = string> = Omit<
2222
ItemBase<Action>,
2323
'expandedAs'
2424
> & {
@@ -30,7 +30,7 @@ export type ItemComponentProps<Action extends string> = Omit<
3030
onClick(evt: React.MouseEvent<unknown>): void;
3131
};
3232

33-
export type ItemAction<Action extends string> = {
33+
export type ItemAction<Action extends string = string> = {
3434
icon: keyof typeof glyphs | React.ReactElement;
3535
} & ItemBase<Action>;
3636

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/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 = ({

0 commit comments

Comments
 (0)