Skip to content

Commit 7a89701

Browse files
authored
chore(aggregations): Refactor aggregations stage toolbar (#6572)
* Re-export GlyphName instead of IconGlyph * Use React composition instead of arrays of data
1 parent 5b17b26 commit 7a89701

File tree

7 files changed

+87
-85
lines changed

7 files changed

+87
-85
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import React from 'react';
2+
3+
import {
4+
css,
5+
Icon,
6+
MenuItem,
7+
palette,
8+
spacing,
9+
type GlyphName,
10+
} from '@mongodb-js/compass-components';
11+
12+
const styles = css({
13+
display: 'flex',
14+
alignItems: 'center',
15+
gap: spacing[2],
16+
});
17+
18+
type OptionMenuItemProps = {
19+
label: string;
20+
icon: GlyphName;
21+
onClick: () => void;
22+
setMenuOpen: (open: boolean) => void;
23+
};
24+
25+
export function OptionMenuItem({
26+
label,
27+
onClick,
28+
setMenuOpen,
29+
icon,
30+
}: OptionMenuItemProps) {
31+
return (
32+
<MenuItem
33+
data-text={label}
34+
onClick={() => {
35+
setMenuOpen(false);
36+
onClick();
37+
}}
38+
>
39+
<div className={styles}>
40+
<Icon color={palette.gray.dark2} glyph={icon} size="small" />
41+
{label}
42+
</div>
43+
</MenuItem>
44+
);
45+
}

packages/compass-aggregations/src/components/stage-toolbar/option-menu.tsx

Lines changed: 33 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
import React, { useMemo, useState } from 'react';
1+
import React, { useState } from 'react';
22
import { connect } from 'react-redux';
3-
import {
4-
Menu,
5-
MenuItem,
6-
css,
7-
IconButton,
8-
Icon,
9-
palette,
10-
spacing,
11-
} from '@mongodb-js/compass-components';
3+
import { Menu, IconButton, Icon } from '@mongodb-js/compass-components';
124
import {
135
addStage,
146
removeStage,
@@ -17,12 +9,7 @@ import {
179
} from '../../modules/pipeline-builder/stage-editor';
1810
import type { StoreStage } from '../../modules/pipeline-builder/stage-editor';
1911
import type { RootState } from '../../modules';
20-
21-
const menuItemStyles = css({
22-
display: 'flex',
23-
alignItems: 'center',
24-
gap: spacing[2],
25-
});
12+
import { OptionMenuItem } from './option-menu-item';
2613

2714
export const OptionMenu = ({
2815
index,
@@ -39,51 +26,6 @@ export const OptionMenu = ({
3926
}) => {
4027
const [menuOpen, setMenuOpen] = useState(false);
4128

42-
const menuItems = useMemo(() => {
43-
return [
44-
{
45-
label: 'Add stage after',
46-
onClick: () => {
47-
onAddStageClick(index);
48-
setMenuOpen(false);
49-
},
50-
icon: 'PlusWithCircle',
51-
},
52-
{
53-
label: 'Add stage before',
54-
onClick: () => {
55-
onAddStageClick(index - 1);
56-
setMenuOpen(false);
57-
},
58-
icon: 'PlusWithCircle',
59-
},
60-
{
61-
label: 'Delete stage',
62-
onClick: () => {
63-
onDeleteStageClick(index);
64-
setMenuOpen(false);
65-
},
66-
icon: 'Trash',
67-
},
68-
{
69-
label: 'Expand documents',
70-
onClick: () => {
71-
onExpand(index);
72-
setMenuOpen(false);
73-
},
74-
icon: 'ChevronDown',
75-
},
76-
{
77-
label: 'Collapse documents',
78-
onClick: () => {
79-
onCollapse(index);
80-
setMenuOpen(false);
81-
},
82-
icon: 'ChevronUp',
83-
},
84-
];
85-
}, [index, onAddStageClick, onDeleteStageClick, onExpand, onCollapse]);
86-
8729
return (
8830
<Menu
8931
open={menuOpen}
@@ -105,18 +47,36 @@ export const OptionMenu = ({
10547
);
10648
}}
10749
>
108-
{menuItems.map((item) => (
109-
<MenuItem
110-
key={item.label}
111-
data-text={item.label}
112-
onClick={item.onClick}
113-
>
114-
<div className={menuItemStyles}>
115-
<Icon color={palette.gray.dark2} glyph={item.icon} size="small" />
116-
{item.label}
117-
</div>
118-
</MenuItem>
119-
))}
50+
<OptionMenuItem
51+
label="Add stage after"
52+
icon="PlusWithCircle"
53+
onClick={() => onAddStageClick(index)}
54+
setMenuOpen={setMenuOpen}
55+
/>
56+
<OptionMenuItem
57+
label="Add stage before"
58+
icon="PlusWithCircle"
59+
onClick={() => onAddStageClick(index - 1)}
60+
setMenuOpen={setMenuOpen}
61+
/>
62+
<OptionMenuItem
63+
label="Delete stage"
64+
icon="Trash"
65+
onClick={() => onDeleteStageClick(index)}
66+
setMenuOpen={setMenuOpen}
67+
/>
68+
<OptionMenuItem
69+
label="Expand documents"
70+
icon="ChevronDown"
71+
onClick={() => onExpand(index)}
72+
setMenuOpen={setMenuOpen}
73+
/>
74+
<OptionMenuItem
75+
label="Collapse documents"
76+
icon="ChevronUp"
77+
onClick={() => onCollapse(index)}
78+
setMenuOpen={setMenuOpen}
79+
/>
12080
</Menu>
12181
);
12282
};

packages/compass-components/src/components/workspace-tabs/tab.tsx

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useMemo } from 'react';
22
import { css, cx } from '@leafygreen-ui/emotion';
33
import { palette } from '@leafygreen-ui/palette';
44
import { spacing } from '@leafygreen-ui/tokens';
5-
import type { glyphs } from '@leafygreen-ui/icon';
5+
import type { GlyphName } from '@leafygreen-ui/icon';
66
import { useSortable } from '@dnd-kit/sortable';
77
import { CSS as cssDndKit } from '@dnd-kit/utilities';
88
import { useDarkMode } from '../../hooks/use-theme';
@@ -180,8 +180,6 @@ const closeButtonStyles = css({
180180
marginRight: spacing[100],
181181
});
182182

183-
type IconGlyph = Extract<keyof typeof glyphs, string>;
184-
185183
type TabProps = {
186184
connectionName?: string;
187185
type: string;
@@ -190,7 +188,7 @@ type TabProps = {
190188
isDragging: boolean;
191189
onSelect: () => void;
192190
onClose: () => void;
193-
iconGlyph: IconGlyph | 'Logo' | 'Server';
191+
iconGlyph: GlyphName | 'Logo' | 'Server';
194192
tabContentId: string;
195193
tooltip?: [string, string][];
196194
tabTheme?: Partial<TabTheme>;

packages/compass-components/src/components/workspace-tabs/workspace-tabs.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import React, {
88
import { css, cx } from '@leafygreen-ui/emotion';
99
import { palette } from '@leafygreen-ui/palette';
1010
import { spacing } from '@leafygreen-ui/tokens';
11-
import type { glyphs } from '@leafygreen-ui/icon';
11+
import type { GlyphName } from '@leafygreen-ui/icon';
1212
import { rgba } from 'polished';
1313

1414
import {
@@ -174,7 +174,7 @@ export type TabProps = {
174174
title: string;
175175
tooltip?: [string, string][];
176176
connectionId?: string;
177-
iconGlyph: Extract<keyof typeof glyphs, string> | 'Logo' | 'Server';
177+
iconGlyph: GlyphName | 'Logo' | 'Server';
178178
} & Omit<React.HTMLProps<HTMLDivElement>, 'id' | 'title'>;
179179

180180
export function useRovingTabIndex<T extends HTMLElement = HTMLElement>({

packages/compass-components/src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,13 @@ export {
143143
FadeInPlaceholder,
144144
} from './components/content-with-fallback';
145145
export { InlineDefinition } from './components/inline-definition';
146-
import type { glyphs } from '@leafygreen-ui/icon';
146+
export type { GlyphName } from '@leafygreen-ui/icon';
147147
export { createGlyphComponent, createIconComponent } from '@leafygreen-ui/icon';
148148
export {
149149
SignalPopover,
150150
SignalHooksProvider,
151151
} from './components/signal-popover';
152152
export type { Signal } from './components/signal-popover';
153-
export type IconGlyph = Extract<keyof typeof glyphs, string>;
154153

155154
export { EmptyContent } from './components/empty-content';
156155
export { ErrorBoundary } from './components/error-boundary';

packages/compass-editor/src/action-button.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { cx } from '@mongodb-js/compass-components';
22
import { css } from '@mongodb-js/compass-components';
3-
import { Button, Icon, type IconGlyph } from '@mongodb-js/compass-components';
3+
import { Button, Icon, type GlyphName } from '@mongodb-js/compass-components';
44
import React, { useCallback, useEffect, useState } from 'react';
55
import type { EditorView } from '@codemirror/view';
66

77
export type Action = {
8-
icon: IconGlyph;
8+
icon: GlyphName;
99
label: string;
1010
action: (editor: EditorView) => boolean | void;
1111
};

packages/databases-collections-list/src/namespace-card.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
} from '@mongodb-js/compass-components';
2121
import type {
2222
BadgeVariant,
23-
IconGlyph,
23+
GlyphName,
2424
ItemAction,
2525
SignalPopover,
2626
} from '@mongodb-js/compass-components';
@@ -107,7 +107,7 @@ export type BadgeProp = {
107107
id: string;
108108
name: string;
109109
variant?: BadgeVariant;
110-
icon?: IconGlyph;
110+
icon?: GlyphName;
111111
hint?: React.ReactNode;
112112
};
113113

0 commit comments

Comments
 (0)