Skip to content

Commit ad291b1

Browse files
mcasimirgribnoysup
andauthored
fix: speed up document json view COMPASS-6365 (#3901)
* fix: speed up document json view COMPASS-6365 * tmp: first pass at adding codemirror instead of ace-editor todo: - finish theme implementation - allow to disable background in the json view in document list - line numbers shouldn't be visible when readonly is true * chore(compass-editor): add support for controlled behavior; finalize styles adjustments * chore(compass-editor): better padding for gutter, align folding with the old behavior; reuse effectOnChange hook * chore(e2e): adjust e2e tests for the new json editor * chore(e2e): adjust regex Co-authored-by: Sergey Petushkov <[email protected]>
1 parent e8cae50 commit ad291b1

File tree

16 files changed

+1127
-420
lines changed

16 files changed

+1127
-420
lines changed

package-lock.json

Lines changed: 296 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { RefObject } from 'react';
2+
import { useRef, useEffect } from 'react';
3+
4+
const kValueChanged = Symbol('Value changed');
5+
6+
/**
7+
* Runs an effect, but only after the value changes for the first time (skipping
8+
* the first "onMount" effect)
9+
*
10+
* Returns a ref for the initial value
11+
*/
12+
export function useEffectOnChange<T>(
13+
fn: React.EffectCallback,
14+
val: T
15+
): RefObject<T> {
16+
// Keep the initial value as a ref so we can check against it in effect when
17+
// the current value changes
18+
const initial = useRef<T | symbol>(val);
19+
const effect = useRef(fn);
20+
effect.current = fn;
21+
useEffect(() => {
22+
// We check if value doesn't match the initial one to avoid running effect
23+
// for the first mount
24+
if (val !== initial.current) {
25+
// After we detected at least one change in value, we set the initial to a
26+
// symbol so that the current value is never equal to it anymore
27+
initial.current = kValueChanged;
28+
return effect.current();
29+
}
30+
}, [val]);
31+
// Return a readonly ref value
32+
return useRef(val);
33+
}

packages/compass-components/src/hooks/use-theme.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ enum Theme {
88
Dark = 'Dark',
99
}
1010

11-
export function useDarkMode(): boolean | undefined {
12-
const darkMode = useLeafyGreenDarkMode();
13-
11+
export function useDarkMode(localDarkMode?: boolean): boolean | undefined {
12+
const darkMode = useLeafyGreenDarkMode(localDarkMode);
1413
return darkMode.darkMode;
1514
}
1615

packages/compass-components/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ export { useDefaultAction } from './hooks/use-default-action';
148148
export { useSortControls, useSortedItems } from './hooks/use-sort';
149149
export { useFormattedDate } from './hooks/use-formatted-date';
150150
export { fontFamilies } from '@leafygreen-ui/tokens';
151-
152151
export { default as BSONValue } from './components/bson-value';
153152
export * as DocumentList from './components/document-list';
154153
export { KeylineCard } from './components/keyline-card';
154+
export { variantColors as codePalette } from '@leafygreen-ui/code';
155+
export { useEffectOnChange } from './hooks/use-effect-on-change';

packages/compass-crud/src/components/document-json-view.less

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,12 @@
77

88
.document-json-item {
99
position: relative;
10-
margin-bottom: 5px;
10+
margin-bottom: 8px;
1111

1212
.json-document-is-editing {
1313
border-radius: inherit;
1414
}
1515

16-
.json-ace-editor {
17-
padding: 10px 10px 10px 0;
18-
overflow: auto;
19-
flex-basis: auto;
20-
flex-shrink: 1;
21-
flex-grow: 1;
22-
23-
.ace-mongodb {
24-
background: transparent;
25-
26-
.ace_gutter {
27-
background: transparent;
28-
z-index: 0;
29-
}
30-
}
31-
}
32-
3316
&:last-child {
3417
margin-bottom: 0;
3518
border-bottom: 0 solid transparent;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import PropTypes from 'prop-types';
33
import { cx, KeylineCard } from '@mongodb-js/compass-components';
44

5-
import type { EditableJsonProps } from './json-editor';
5+
import type { JsonEditorProps } from './json-editor';
66
import JsonEditor from './json-editor';
77
import type Document from 'hadron-document';
88

@@ -26,7 +26,7 @@ export type DocumentJsonViewProps = {
2626
isEditable: boolean;
2727
className?: string;
2828
} & Pick<
29-
EditableJsonProps,
29+
JsonEditorProps,
3030
| 'isTimeSeries'
3131
| 'copyToClipboard'
3232
| 'removeDocument'

0 commit comments

Comments
 (0)