Skip to content

Commit 96757c6

Browse files
committed
areFieldPathsEqual
1 parent 7078026 commit 96757c6

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

packages/compass-data-modeling/src/components/drawer/field-drawer-content.tsx

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
import { useChangeOnBlur } from './use-change-on-blur';
2626
import { RelationshipsSection } from './relationships-section';
2727
import { getFieldFromSchema } from '../../utils/schema-traversal';
28+
import { areFieldPathsEqual } from '../../utils/utils';
2829

2930
type FieldDrawerContentProps = {
3031
namespace: string;
@@ -73,10 +74,7 @@ export function getIsFieldNameValid(
7374
}
7475

7576
const fieldsNamesWithoutCurrent = existingFields
76-
.filter(
77-
(fieldPath) =>
78-
JSON.stringify(fieldPath) !== JSON.stringify(currentFieldPath)
79-
)
77+
.filter((fieldPath) => !areFieldPathsEqual(fieldPath, currentFieldPath))
8078
.map((fieldPath) => fieldPath[fieldPath.length - 1]);
8179

8280
const isDuplicate = fieldsNamesWithoutCurrent.some(
@@ -169,7 +167,8 @@ const FieldDrawerContent: React.FunctionComponent<FieldDrawerContentProps> = ({
169167
getRelationshipLabel={([local, foreign]) => {
170168
const labelField =
171169
local.ns === namespace &&
172-
JSON.stringify(local.fields) === JSON.stringify(fieldPath)
170+
local.fields &&
171+
areFieldPathsEqual(local.fields, fieldPath)
173172
? foreign
174173
: local;
175174
return [
@@ -210,11 +209,11 @@ export default connect(
210209
const [local, foreign] = r.relationship;
211210
return (
212211
(local.ns === ownProps.namespace &&
213-
JSON.stringify(local.fields) ===
214-
JSON.stringify(ownProps.fieldPath)) ||
212+
local.fields &&
213+
areFieldPathsEqual(local.fields, ownProps.fieldPath)) ||
215214
(foreign.ns === ownProps.namespace &&
216-
JSON.stringify(foreign.fields) ===
217-
JSON.stringify(ownProps.fieldPath))
215+
foreign.fields &&
216+
areFieldPathsEqual(foreign.fields, ownProps.fieldPath))
218217
);
219218
}),
220219
};

packages/compass-data-modeling/src/utils/nodes-and-edges.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
Relationship,
1111
} from '../services/data-model-storage';
1212
import { traverseSchema } from './schema-traversal';
13+
import { areFieldPathsEqual } from './utils';
1314

1415
function getBsonTypeName(bsonType: string) {
1516
switch (bsonType) {
@@ -100,12 +101,11 @@ export const getFieldsFromSchema = ({
100101
? ['key']
101102
: [],
102103
selectable: true,
103-
selected: JSON.stringify(fieldPath) === JSON.stringify(selectedField),
104+
selected: areFieldPathsEqual(fieldPath, selectedField ?? []),
104105
variant:
105106
highlightedFields.length &&
106-
highlightedFields.some(
107-
(highlightedField) =>
108-
JSON.stringify(fieldPath) === JSON.stringify(highlightedField)
107+
highlightedFields.some((highlightedField) =>
108+
areFieldPathsEqual(fieldPath, highlightedField)
109109
)
110110
? 'preview'
111111
: undefined,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import type { FieldPath } from '../services/data-model-storage';
2+
3+
export function areFieldPathsEqual(
4+
fieldA: FieldPath,
5+
fieldB: FieldPath
6+
): boolean {
7+
return JSON.stringify(fieldA) === JSON.stringify(fieldB);
8+
}

0 commit comments

Comments
 (0)