Skip to content

Commit 19c87f9

Browse files
committed
refactor: use a single queryBar prop
1 parent f7e523f commit 19c87f9

File tree

7 files changed

+74
-66
lines changed

7 files changed

+74
-66
lines changed

packages/compass-components/src/components/document-list/document.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ import {
1111
} from 'hadron-document';
1212
import { AutoFocusContext } from './auto-focus-context';
1313
import { useForceUpdate } from './use-force-update';
14-
import { calculateShowMoreToggleOffset, HadronElement } from './element';
14+
import {
15+
calculateShowMoreToggleOffset,
16+
HadronElement,
17+
type QueryBarController,
18+
} from './element';
1519
import { usePrevious } from './use-previous';
1620
import VisibleFieldsToggle from './visible-field-toggle';
1721
import { documentTypography } from './typography';
@@ -86,16 +90,14 @@ const HadronDocument: React.FunctionComponent<{
8690
editing?: boolean;
8791
onEditStart?: () => void;
8892
extraGutterWidth?: number;
89-
onAddToQuery?: (field: string, value: unknown) => void;
90-
isInQuery?: (field: string, value: unknown) => boolean;
93+
queryBar?: QueryBarController;
9194
}> = ({
9295
value: document,
9396
editable = false,
9497
editing = false,
9598
onEditStart,
9699
extraGutterWidth,
97-
onAddToQuery,
98-
isInQuery,
100+
queryBar,
99101
}) => {
100102
const { elements, visibleElements } = useHadronDocument(document);
101103
const [autoFocus, setAutoFocus] = useState<{
@@ -160,8 +162,7 @@ const HadronDocument: React.FunctionComponent<{
160162
});
161163
}}
162164
extraGutterWidth={extraGutterWidth}
163-
onAddToQuery={onAddToQuery}
164-
isInQuery={isInQuery}
165+
queryBar={queryBar}
165166
></HadronElement>
166167
);
167168
})}

packages/compass-components/src/components/document-list/element.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ export const getQueryFilterForElement = (
458458
return filter;
459459
};
460460

461+
export type QueryBarController = {
462+
isInQuery: (field: string, value: unknown) => boolean;
463+
toggleQueryFilter: (field: string, value: unknown) => void;
464+
};
465+
461466
export const HadronElement: React.FunctionComponent<{
462467
value: HadronElementType;
463468
editable: boolean;
@@ -466,8 +471,7 @@ export const HadronElement: React.FunctionComponent<{
466471
lineNumberSize: number;
467472
onAddElement(el: HadronElementType): void;
468473
extraGutterWidth?: number;
469-
onAddToQuery?: (field: string, value: unknown) => void;
470-
isInQuery?: (field: string, value: unknown) => boolean;
474+
queryBar?: QueryBarController;
471475
}> = ({
472476
value: element,
473477
editable,
@@ -476,8 +480,7 @@ export const HadronElement: React.FunctionComponent<{
476480
lineNumberSize,
477481
onAddElement,
478482
extraGutterWidth = 0,
479-
onAddToQuery,
480-
isInQuery,
483+
queryBar,
481484
}) => {
482485
const darkMode = useDarkMode();
483486
const autoFocus = useAutoFocusContext();
@@ -504,17 +507,17 @@ export const HadronElement: React.FunctionComponent<{
504507
// Add context menu hook for the field
505508
const fieldContextMenuRef = useContextMenuItems(
506509
() => [
507-
...(onAddToQuery && isInQuery
510+
...(queryBar
508511
? [
509512
{
510-
label: isInQuery(
513+
label: queryBar.isInQuery(
511514
getNestedKeyPathForElement(element),
512515
element.generateObject()
513516
)
514517
? 'Remove from query'
515518
: 'Add to query',
516519
onAction: () => {
517-
onAddToQuery(
520+
queryBar.toggleQueryFilter(
518521
getNestedKeyPathForElement(element),
519522
element.generateObject()
520523
);
@@ -541,7 +544,7 @@ export const HadronElement: React.FunctionComponent<{
541544
]
542545
: []),
543546
],
544-
[element, key.value, value.value, type.value, onAddToQuery, isInQuery]
547+
[element, key.value, value.value, type.value, queryBar]
545548
);
546549

547550
const toggleExpanded = () => {
@@ -831,8 +834,7 @@ export const HadronElement: React.FunctionComponent<{
831834
lineNumberSize={lineNumberSize}
832835
onAddElement={onAddElement}
833836
extraGutterWidth={extraGutterWidth}
834-
onAddToQuery={onAddToQuery}
835-
isInQuery={isInQuery}
837+
queryBar={queryBar}
836838
></HadronElement>
837839
);
838840
})}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,13 @@
1-
export { default as DocumentActionsGroup } from './document-actions-group';
2-
export { default as VisibleFieldsToggle } from './visible-field-toggle';
3-
export { default as Document } from './document';
4-
export { default as DocumentEditActionsFooter } from './document-edit-actions-footer';
1+
import { default as DocumentActionsGroup } from './document-actions-group';
2+
import { default as VisibleFieldsToggle } from './visible-field-toggle';
3+
import { default as Document } from './document';
4+
import { default as DocumentEditActionsFooter } from './document-edit-actions-footer';
5+
6+
export const DocumentList = {
7+
DocumentActionsGroup,
8+
VisibleFieldsToggle,
9+
Document,
10+
DocumentEditActionsFooter,
11+
};
12+
13+
export { type QueryBarController } from './element';

packages/compass-components/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ export { useSortControls, useSortedItems } from './hooks/use-sort';
183183
export * from './hooks/use-formatted-date';
184184
export { fontFamilies } from '@leafygreen-ui/tokens';
185185
export { default as BSONValue } from './components/bson-value';
186-
export * as DocumentList from './components/document-list';
186+
export {
187+
DocumentList,
188+
type QueryBarController,
189+
} from './components/document-list';
187190
export { KeylineCard } from './components/keyline-card';
188191
export { variantColors as codePalette } from '@leafygreen-ui/code';
189192
export { useEffectOnChange } from './hooks/use-effect-on-change';

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

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useMemo, useCallback } from 'react';
1+
import React, { useMemo } from 'react';
22
import PropTypes from 'prop-types';
33
import HadronDocument from 'hadron-document';
44
import type { EditableDocumentProps } from './editable-document';
@@ -11,6 +11,7 @@ import {
1111
useChangeQueryBarQuery,
1212
useQueryBarQuery,
1313
} from '@mongodb-js/compass-query-bar';
14+
import type { QueryBarController } from '@mongodb-js/compass-components';
1415

1516
export type DocumentProps = {
1617
doc: HadronDocument | BSONObject;
@@ -19,7 +20,7 @@ export type DocumentProps = {
1920
} & Omit<EditableDocumentProps, 'doc' | 'expandAll'> &
2021
Pick<ReadonlyDocumentProps, 'copyToClipboard' | 'openInsertDocumentDialog'>;
2122

22-
const Document = (props: DocumentProps) => {
23+
export const Document = (props: DocumentProps) => {
2324
const {
2425
editable,
2526
isTimeSeries,
@@ -37,26 +38,23 @@ const Document = (props: DocumentProps) => {
3738
return new HadronDocument(_doc as Record<string, unknown>);
3839
}, [_doc]);
3940

40-
const changeQuery = useChangeQueryBarQuery();
41+
const queryBarChangeQuery = useChangeQueryBarQuery();
4142
const queryBarQuery = useQueryBarQuery();
4243

43-
const handleAddToQuery = useCallback(
44-
(field: string, value: unknown) => {
45-
changeQuery('toggleDistinctValue', {
46-
field,
47-
value,
48-
});
49-
},
50-
[changeQuery]
51-
);
52-
53-
const isInQuery = useCallback(
54-
(field: string, value: unknown) => {
55-
const filter = queryBarQuery.filter?.[field];
56-
return hasDistinctValue(filter, value);
57-
},
58-
[queryBarQuery]
59-
);
44+
const queryBar = useMemo<QueryBarController>(() => {
45+
return {
46+
isInQuery: (field: string, value: unknown) => {
47+
const filter = queryBarQuery.filter?.[field];
48+
return hasDistinctValue(filter, value);
49+
},
50+
toggleQueryFilter: (field: string, value: unknown) => {
51+
queryBarChangeQuery('toggleDistinctValue', {
52+
field,
53+
value,
54+
});
55+
},
56+
};
57+
}, [queryBarChangeQuery, queryBarQuery]);
6058

6159
if (editable && isTimeSeries) {
6260
return (
@@ -66,29 +64,20 @@ const Document = (props: DocumentProps) => {
6664
openInsertDocumentDialog={(doc, cloned) => {
6765
void openInsertDocumentDialog?.(doc, cloned);
6866
}}
69-
onAddToQuery={handleAddToQuery}
70-
isInQuery={isInQuery}
67+
queryBar={queryBar}
7168
/>
7269
);
7370
}
7471

7572
if (editable) {
76-
return (
77-
<EditableDocument
78-
{...props}
79-
doc={doc}
80-
onAddToQuery={handleAddToQuery}
81-
isInQuery={isInQuery}
82-
/>
83-
);
73+
return <EditableDocument {...props} doc={doc} queryBar={queryBar} />;
8474
}
8575

8676
return (
8777
<ReadonlyDocument
8878
doc={doc}
8979
copyToClipboard={copyToClipboard}
90-
onAddToQuery={handleAddToQuery}
91-
isInQuery={isInQuery}
80+
queryBar={queryBar}
9281
/>
9382
);
9483
};

packages/compass-crud/src/components/editable-document.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import type { Document } from 'hadron-document';
44
import HadronDocument from 'hadron-document';
5-
import { DocumentList, css } from '@mongodb-js/compass-components';
5+
import {
6+
type QueryBarController,
7+
css,
8+
DocumentList,
9+
} from '@mongodb-js/compass-components';
610
import { withPreferences } from 'compass-preferences-model/provider';
711

812
import { documentStyles, documentContentStyles } from './readonly-document';
@@ -21,8 +25,7 @@ export type EditableDocumentProps = {
2125
openInsertDocumentDialog?: CrudActions['openInsertDocumentDialog'];
2226
copyToClipboard?: CrudActions['copyToClipboard'];
2327
showInsights?: boolean;
24-
onAddToQuery?: (field: string, value: unknown) => void;
25-
isInQuery?: (field: string, value: unknown) => boolean;
28+
queryBar?: QueryBarController;
2629
};
2730

2831
type EditableDocumentState = {
@@ -252,8 +255,7 @@ class EditableDocument extends React.Component<
252255
editable
253256
editing={this.state.editing}
254257
onEditStart={this.handleStartEditing.bind(this)}
255-
onAddToQuery={this.props.onAddToQuery}
256-
isInQuery={this.props.isInQuery}
258+
queryBar={this.props.queryBar}
257259
/>
258260
);
259261
}
@@ -319,8 +321,7 @@ class EditableDocument extends React.Component<
319321
openInsertDocumentDialog: PropTypes.func.isRequired,
320322
copyToClipboard: PropTypes.func.isRequired,
321323
showInsights: PropTypes.bool,
322-
onAddToQuery: PropTypes.func,
323-
isInQuery: PropTypes.func,
324+
queryBar: PropTypes.object,
324325
};
325326
}
326327

packages/compass-crud/src/components/readonly-document.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import { DocumentList, css, spacing } from '@mongodb-js/compass-components';
3+
import {
4+
type QueryBarController,
5+
css,
6+
spacing,
7+
DocumentList,
8+
} from '@mongodb-js/compass-components';
49
import type Document from 'hadron-document';
510
import type { TypeCastMap } from 'hadron-type-checker';
611
import { withPreferences } from 'compass-preferences-model/provider';
@@ -29,8 +34,7 @@ export type ReadonlyDocumentProps = {
2934
openInsertDocumentDialog?: (doc: BSONObject, cloned: boolean) => void;
3035
doc: Document;
3136
showInsights?: boolean;
32-
onAddToQuery?: (field: string, value: unknown) => void;
33-
isInQuery?: (field: string, value: unknown) => boolean;
37+
queryBar?: QueryBarController;
3438
};
3539

3640
type ReadonlyDocumentState = {
@@ -139,8 +143,7 @@ class ReadonlyDocument extends React.Component<
139143
value={this.props.doc}
140144
// Provide extra whitespace for the expand button
141145
extraGutterWidth={spacing[900]}
142-
onAddToQuery={this.props.onAddToQuery}
143-
isInQuery={this.props.isInQuery}
146+
queryBar={this.props.queryBar}
144147
/>
145148
</>
146149
);

0 commit comments

Comments
 (0)