Skip to content

Commit 28e67c0

Browse files
authored
feat: move the signal from query bar to outside of it CLOUDP-311778 (#6868)
* extracted signal out from the query bar * added tests * fixed light bulb showing up when it's not supposed to * cleaned up traces of insights from query bar * remove unused imports
1 parent 20a1bf4 commit 28e67c0

File tree

6 files changed

+39
-50
lines changed

6 files changed

+39
-50
lines changed

packages/compass-components/src/components/signal-popover.spec.tsx

+24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,30 @@ describe('SignalPopover', function () {
4242
expect(screen.getByText('Unbounded array detected')).to.exist;
4343
userEvent.click(screen.getByTitle('Show next insight'));
4444
expect(screen.getByText('Possibly bloated documents')).to.exist;
45+
46+
it('and the badge should read 2 insights without the icon showing up', function () {
47+
expect(screen.getByText('2 insights')).to.exist;
48+
expect(screen.getByTestId('insight-badge-icon').style.opacity).to.equal(
49+
'0'
50+
);
51+
});
52+
});
53+
54+
it('should show insights badge text by default if shouldExpandBadge is true', function () {
55+
const { rerender } = render(
56+
<SignalPopover signals={signals[0]} shouldExpandBadge={false} />
57+
);
58+
59+
// opacity: 0 means that the text will not show up
60+
expect(screen.getByTestId('insight-badge-text').style.opacity).to.equal(
61+
'0'
62+
);
63+
64+
rerender(<SignalPopover signals={signals[0]} shouldExpandBadge={true} />);
65+
// opacity: 1 means that the text will show up
66+
expect(screen.getByTestId('insight-badge-text').style.opacity).to.equal(
67+
'1'
68+
);
4569
});
4670

4771
describe('SignalHooksProvider', function () {

packages/compass-components/src/components/signal-popover.tsx

+11-3
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ type SignalPopoverProps = {
124124
darkMode?: boolean;
125125
onPopoverOpenChange?: (open: boolean) => void;
126126
className?: string;
127+
shouldExpandBadge?: boolean;
127128
};
128129

129130
const signalCardContentStyles = css({
@@ -441,6 +442,7 @@ const SignalPopover: React.FunctionComponent<SignalPopoverProps> = ({
441442
darkMode: _darkMode,
442443
onPopoverOpenChange: _onPopoverOpenChange,
443444
className,
445+
shouldExpandBadge,
444446
}) => {
445447
const hooks = useContext(TrackingHooksContext);
446448
const darkMode = useDarkMode(_darkMode);
@@ -449,9 +451,11 @@ const SignalPopover: React.FunctionComponent<SignalPopoverProps> = ({
449451
const [hoverProps, isHovered, setHovered] = useHoverState();
450452
const [currentSignalIndex, setCurrentSignalIndex] = useState(0);
451453
const signals = Array.isArray(_signals) ? _signals : [_signals];
454+
452455
const currentSignal = signals[currentSignalIndex];
453456
const multiSignals = signals.length > 1;
454457
const isActive = isHovered || popoverOpen;
458+
const shouldShowFullBadge = isActive || shouldExpandBadge;
455459

456460
const triggerRef = useRef<HTMLButtonElement>(null);
457461

@@ -587,7 +591,9 @@ const SignalPopover: React.FunctionComponent<SignalPopoverProps> = ({
587591
]),
588592
className
589593
),
590-
style: { width: isActive ? activeBadgeWidth : 18 },
594+
style: {
595+
width: shouldShowFullBadge ? activeBadgeWidth : 18,
596+
},
591597
ref: triggerRef,
592598
},
593599
{ ref: guideCueRef }
@@ -604,16 +610,18 @@ const SignalPopover: React.FunctionComponent<SignalPopoverProps> = ({
604610
glyph="Bulb"
605611
size="small"
606612
className={cx(badgeIconStyles, badgeIconCollapsedStyles)}
607-
style={{ opacity: isActive ? 0 : 1 }}
613+
data-testid="insight-badge-icon"
614+
style={{ opacity: shouldShowFullBadge ? 0 : 1 }}
608615
></Icon>
609616
<strong
610617
className={cx(
611618
badgeLabelStyles,
612619
!multiSignals && singleInsightBadge
613620
)}
621+
data-testid="insight-badge-text"
614622
style={{
615623
width: activeBadgeWidth,
616-
opacity: isActive ? 1 : 0,
624+
opacity: shouldShowFullBadge ? 1 : 0,
617625
}}
618626
>
619627
{badgeLabel}

packages/compass-crud/src/components/crud-toolbar.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
ErrorSummary,
1313
Select,
1414
Option,
15+
SignalPopover,
1516
} from '@mongodb-js/compass-components';
1617
import type { MenuAction, Signal } from '@mongodb-js/compass-components';
1718
import { ViewSwitcher } from './view-switcher';
@@ -199,7 +200,6 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
199200
() => querySkip || queryLimit,
200201
[querySkip, queryLimit]
201202
);
202-
203203
return (
204204
<div className={crudToolbarStyles}>
205205
<div className={crudQueryBarStyles}>
@@ -210,7 +210,6 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
210210
onApply={onApplyClicked}
211211
onReset={onResetClicked}
212212
showExplainButton={enableExplainPlan}
213-
insights={insights}
214213
/>
215214
</div>
216215
<div className={crudBarStyles}>
@@ -259,6 +258,9 @@ const CrudToolbar: React.FunctionComponent<CrudToolbarProps> = ({
259258
onClick={onDeleteButtonClicked}
260259
></DeleteMenu>
261260
)}
261+
{insights && (
262+
<SignalPopover signals={insights} shouldExpandBadge={true} />
263+
)}
262264
</div>
263265
<div className={toolbarRightActionStyles}>
264266
<Select

packages/compass-query-bar/src/components/option-editor.tsx

-37
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import React, { useMemo, useRef } from 'react';
2-
import type { Signal } from '@mongodb-js/compass-components';
32
import {
43
css,
54
cx,
65
useFocusRing,
76
palette,
87
spacing,
9-
SignalPopover,
108
rafraf,
119
useDarkMode,
1210
} from '@mongodb-js/compass-components';
@@ -78,30 +76,6 @@ const editorWithErrorStyles = css({
7876
},
7977
});
8078

81-
const queryBarEditorOptionInsightsStyles = css({
82-
alignSelf: 'flex-start',
83-
// To align the icon in the middle of the first line of the editor input
84-
// (<input height> - <insight badge height>) / 2
85-
paddingTop: 3,
86-
paddingBottom: 3,
87-
paddingLeft: 3,
88-
paddingRight: 3,
89-
90-
// We make container the size of the collapsed insight to avoid additional
91-
// shrinking of the editor content when hoveing over the icon. In this case
92-
// it's okay for the content to be hidden by the expanded badge as user is
93-
// interacting with the badge
94-
width: spacing[4],
95-
height: spacing[4],
96-
overflow: 'visible',
97-
display: 'flex',
98-
justifyContent: 'flex-end',
99-
});
100-
101-
const insightsBadgeStyles = css({
102-
flex: 'none',
103-
});
104-
10579
type OptionEditorProps = {
10680
optionName: QueryOptionOfTypeDocument;
10781
namespace: string;
@@ -119,7 +93,6 @@ type OptionEditorProps = {
11993
serverVersion?: string;
12094
value?: string;
12195
['data-testid']?: string;
122-
insights?: Signal | Signal[];
12396
disabled?: boolean;
12497
recentQueries: AutoCompleteRecentQuery[];
12598
favoriteQueries: AutoCompleteFavoriteQuery[];
@@ -139,13 +112,11 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
139112
serverVersion = '3.6.0',
140113
value = '',
141114
['data-testid']: dataTestId,
142-
insights,
143115
disabled = false,
144116
recentQueries,
145117
favoriteQueries,
146118
onApplyQuery,
147119
}) => {
148-
const showInsights = usePreference('showInsights');
149120
const editorContainerRef = useRef<HTMLDivElement>(null);
150121
const editorRef = useRef<EditorRef>(null);
151122
const isQueryHistoryAutocompleteEnabled = usePreference(
@@ -299,14 +270,6 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
299270
onPaste={onPaste}
300271
onBlur={onBlur}
301272
/>
302-
{showInsights && insights && (
303-
<div className={queryBarEditorOptionInsightsStyles}>
304-
<SignalPopover
305-
className={insightsBadgeStyles}
306-
signals={insights}
307-
></SignalPopover>
308-
</div>
309-
)}
310273
</div>
311274
);
312275
};

packages/compass-query-bar/src/components/query-bar.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import {
1515
} from '@mongodb-js/compass-generative-ai';
1616
import { connect } from '../stores/context';
1717
import { useIsAIFeatureEnabled } from 'compass-preferences-model/provider';
18-
import type { Signal } from '@mongodb-js/compass-components';
1918

2019
import {
2120
OPTION_DEFINITION,
@@ -124,7 +123,6 @@ type QueryBarProps = {
124123
expanded: boolean;
125124
placeholders?: Record<QueryProperty, string>;
126125
onExplain?: () => void;
127-
insights?: Signal | Signal[];
128126
isAIInputVisible?: boolean;
129127
isAIFetching?: boolean;
130128
onShowAIInputClick: () => void;
@@ -153,7 +151,6 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
153151
expanded: isQueryOptionsExpanded,
154152
placeholders,
155153
onExplain,
156-
insights,
157154
isAIInputVisible = false,
158155
isAIFetching = false,
159156
onShowAIInputClick,
@@ -229,7 +226,6 @@ export const QueryBar: React.FunctionComponent<QueryBarProps> = ({
229226
id={filterQueryOptionId}
230227
onApply={onApply}
231228
placeholder={filterPlaceholder}
232-
insights={insights}
233229
disabled={isAIFetching}
234230
/>
235231
{showAIEntryButton && (

packages/compass-query-bar/src/components/query-option.tsx

-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { useCallback, useRef } from 'react';
2-
import type { Signal } from '@mongodb-js/compass-components';
32
import {
43
Label,
54
TextInput,
@@ -90,7 +89,6 @@ type QueryOptionProps = {
9089
onChange: (name: QueryBarProperty, value: string) => void;
9190
placeholder?: string | HTMLElement;
9291
onApply?(): void;
93-
insights?: Signal | Signal[];
9492
disabled?: boolean;
9593
};
9694

@@ -122,7 +120,6 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
122120
name,
123121
value,
124122
onApply,
125-
insights,
126123
disabled = false,
127124
}) => {
128125
const track = useTelemetry();
@@ -199,7 +196,6 @@ const QueryOption: React.FunctionComponent<QueryOptionProps> = ({
199196
value={value}
200197
data-testid={`query-bar-option-${name}-input`}
201198
onApply={onApply}
202-
insights={insights}
203199
disabled={disabled}
204200
/>
205201
) : (

0 commit comments

Comments
 (0)