Skip to content

Commit 3d8cc61

Browse files
committed
memo map function
1 parent ee1e6d8 commit 3d8cc61

File tree

2 files changed

+54
-55
lines changed

2 files changed

+54
-55
lines changed

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

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import sinon from 'sinon';
1414
import { createSandboxFromDefaultPreferences } from 'compass-preferences-model';
1515
import type { PreferencesAccess } from 'compass-preferences-model';
1616
import { PreferencesProvider } from 'compass-preferences-model/provider';
17-
import type { RecentQuery } from '@mongodb-js/my-queries-storage';
1817

1918
class MockPasteEvent extends window.Event {
2019
constructor(private text: string) {
@@ -48,7 +47,8 @@ describe('OptionEditor', function () {
4847
insertEmptyDocOnFocus
4948
onChange={() => {}}
5049
value=""
51-
savedQueries={[]}
50+
recentQueries={[]}
51+
favoriteQueries={[]}
5252
onApplyQuery={applyFromHistory}
5353
></OptionEditor>
5454
);
@@ -70,7 +70,8 @@ describe('OptionEditor', function () {
7070
insertEmptyDocOnFocus
7171
onChange={() => {}}
7272
value="{ foo: 1 }"
73-
savedQueries={[]}
73+
recentQueries={[]}
74+
favoriteQueries={[]}
7475
onApplyQuery={applyFromHistory}
7576
></OptionEditor>
7677
);
@@ -92,7 +93,8 @@ describe('OptionEditor', function () {
9293
insertEmptyDocOnFocus
9394
onChange={() => {}}
9495
value=""
95-
savedQueries={[]}
96+
favoriteQueries={[]}
97+
recentQueries={[]}
9698
onApplyQuery={applyFromHistory}
9799
></OptionEditor>
98100
);
@@ -120,7 +122,8 @@ describe('OptionEditor', function () {
120122
insertEmptyDocOnFocus
121123
onChange={() => {}}
122124
value=""
123-
savedQueries={[]}
125+
favoriteQueries={[]}
126+
recentQueries={[]}
124127
onApplyQuery={applyFromHistory}
125128
></OptionEditor>
126129
);
@@ -150,7 +153,8 @@ describe('OptionEditor', function () {
150153
insertEmptyDocOnFocus
151154
onChange={() => {}}
152155
value=""
153-
savedQueries={[]}
156+
favoriteQueries={[]}
157+
recentQueries={[]}
154158
onApplyQuery={applyFromHistory}
155159
></OptionEditor>
156160
);
@@ -183,21 +187,17 @@ describe('OptionEditor', function () {
183187
insertEmptyDocOnFocus
184188
onChange={() => {}}
185189
value=""
186-
savedQueries={[
190+
recentQueries={[
187191
{
188-
type: 'recent',
189-
lastExecuted: new Date(),
190-
queryProperties: {
191-
filter: { a: 1 },
192-
},
192+
_lastExecuted: new Date(),
193+
filter: { a: 1 },
193194
},
195+
]}
196+
favoriteQueries={[
194197
{
195-
type: 'favorite',
196-
lastExecuted: new Date(),
197-
queryProperties: {
198-
filter: { a: 2 },
199-
sort: { a: -1 },
200-
},
198+
_lastExecuted: new Date(),
199+
filter: { a: 2 },
200+
sort: { a: -1 },
201201
},
202202
]}
203203
onApplyQuery={onApplySpy}
@@ -249,20 +249,16 @@ describe('OptionEditor', function () {
249249
insertEmptyDocOnFocus
250250
onChange={() => {}}
251251
value=""
252-
savedQueries={[
252+
favoriteQueries={[
253253
{
254-
type: 'favorite',
255-
lastExecuted: new Date(),
256-
queryProperties: {
257-
project: { a: 1 },
258-
},
254+
_lastExecuted: new Date(),
255+
project: { a: 1 },
259256
},
257+
]}
258+
recentQueries={[
260259
{
261-
type: 'recent',
262-
lastExecuted: new Date(),
263-
queryProperties: {
264-
project: { a: 0 },
265-
},
260+
_lastExecuted: new Date(),
261+
project: { a: 0 },
266262
},
267263
]}
268264
onApplyQuery={onApplySpy}
@@ -310,21 +306,21 @@ describe('OptionEditor', function () {
310306
skip: 1,
311307
limit: 1,
312308
},
313-
] as unknown as RecentQuery[];
309+
];
314310

315311
it('filters out update queries', function () {
316312
const queries = getOptionBasedQueries('filter', 'recent', [
317313
...savedQueries,
318314
{ _lastExecuted: new Date(), update: { a: 1 }, filter: { a: 2 } },
319-
] as unknown as RecentQuery[]);
315+
]);
320316
expect(queries.length).to.equal(1);
321317
});
322318

323319
it('filters out empty queries', function () {
324320
const queries = getOptionBasedQueries('filter', 'recent', [
325321
...savedQueries,
326322
{ _lastExecuted: new Date() },
327-
] as unknown as RecentQuery[]);
323+
]);
328324
expect(queries.length).to.equal(1);
329325
});
330326

@@ -335,7 +331,7 @@ describe('OptionEditor', function () {
335331
...savedQueries,
336332
{ _lastExecuted: new Date() },
337333
{ _lastExecuted: new Date() },
338-
] as unknown as RecentQuery[]);
334+
]);
339335
expect(queries.length).to.equal(1);
340336
});
341337

@@ -365,9 +361,9 @@ describe('OptionEditor', function () {
365361

366362
expect(queries).to.deep.equal([
367363
{
368-
type: 'recent',
369364
lastExecuted: savedQueries[0]._lastExecuted,
370365
queryProperties,
366+
type: 'recent',
371367
},
372368
]);
373369
});

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

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ import type {
4141
} from '@mongodb-js/my-queries-storage';
4242
import type { QueryOptionOfTypeDocument } from '../constants/query-option-definition';
4343

44+
type AutoCompleteQuery<T extends { _lastExecuted: Date }> = Partial<T> &
45+
Pick<T, '_lastExecuted'>;
46+
type AutoCompleteRecentQuery = AutoCompleteQuery<RecentQuery>;
47+
type AutoCompleteFavoriteQuery = AutoCompleteQuery<FavoriteQuery>;
48+
4449
const editorContainerStyles = css({
4550
position: 'relative',
4651
display: 'flex',
@@ -116,7 +121,8 @@ type OptionEditorProps = {
116121
['data-testid']?: string;
117122
insights?: Signal | Signal[];
118123
disabled?: boolean;
119-
savedQueries: SavedQuery[];
124+
recentQueries: AutoCompleteRecentQuery[];
125+
favoriteQueries: AutoCompleteFavoriteQuery[];
120126
onApplyQuery: (query: BaseQuery, fieldsToPreserve: QueryProperty[]) => void;
121127
};
122128

@@ -135,7 +141,8 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
135141
['data-testid']: dataTestId,
136142
insights,
137143
disabled = false,
138-
savedQueries,
144+
recentQueries,
145+
favoriteQueries,
139146
onApplyQuery,
140147
}) => {
141148
const showInsights = usePreference('showInsights');
@@ -172,6 +179,13 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
172179
const schemaFields = useAutocompleteFields(namespace);
173180
const maxTimeMSPreference = usePreference('maxTimeMS');
174181

182+
const savedQueries = useMemo(() => {
183+
return [
184+
...getOptionBasedQueries(optionName, 'recent', recentQueries),
185+
...getOptionBasedQueries(optionName, 'favorite', favoriteQueries),
186+
];
187+
}, [optionName, recentQueries, favoriteQueries]);
188+
175189
const completer = useMemo(() => {
176190
return isQueryHistoryAutocompleteEnabled
177191
? createQueryWithHistoryAutocompleter({
@@ -300,7 +314,7 @@ export const OptionEditor: React.FunctionComponent<OptionEditorProps> = ({
300314
export function getOptionBasedQueries(
301315
optionName: QueryOptionOfTypeDocument,
302316
type: 'recent' | 'favorite',
303-
queries: (RecentQuery | FavoriteQuery)[]
317+
queries: (AutoCompleteRecentQuery | AutoCompleteFavoriteQuery)[]
304318
) {
305319
return (
306320
queries
@@ -338,24 +352,13 @@ export function getOptionBasedQueries(
338352
);
339353
}
340354

341-
const mapStateToProps = (
342-
state: RootState,
343-
ownProps: Pick<OptionEditorProps, 'optionName'>
344-
) => ({
345-
namespace: state.queryBar.namespace,
346-
serverVersion: state.queryBar.serverVersion,
347-
savedQueries: [
348-
...getOptionBasedQueries(
349-
ownProps.optionName,
350-
'recent',
351-
state.queryBar.recentQueries
352-
),
353-
...getOptionBasedQueries(
354-
ownProps.optionName,
355-
'favorite',
356-
state.queryBar.favoriteQueries
357-
),
358-
],
355+
const mapStateToProps = ({
356+
queryBar: { namespace, serverVersion, recentQueries, favoriteQueries },
357+
}: RootState) => ({
358+
namespace,
359+
serverVersion,
360+
recentQueries,
361+
favoriteQueries,
359362
});
360363

361364
const mapDispatchToProps = {

0 commit comments

Comments
 (0)