Skip to content

Commit 1c0cabe

Browse files
committed
Changed how duplicate components are named
1 parent 9831da1 commit 1c0cabe

File tree

3 files changed

+49
-37
lines changed

3 files changed

+49
-37
lines changed

src/app/components/StateRoute/ComponentMap/ComponentMap.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ export default function ComponentMap({
348348
<strong>{tooltipData.name}</strong>
349349
</div>
350350
<div className='tooltipKey'>
351-
key: {tooltipData.componentData.key !== null ? tooltipData.componentData.key : 'null'}
351+
Key: {tooltipData.componentData.key !== null ? tooltipData.componentData.key : 'null'}
352352
</div>
353353
<div> Render time: {formatRenderTime(tooltipData.componentData.actualDuration)} </div>
354354

src/backend/__tests__/masterTree.test.tsx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,11 @@ describe('master tree tests', () => {
243243
// Construct result tree (root => FiberTree => childTree1 => childTree2 & siblingTree1)
244244
(mockChildTree.componentData as ComponentData).props = props;
245245
const childTree1 = deepCopy(mockChildTree);
246-
childTree1.name = 'IncrementClass';
246+
childTree1.name = 'IncrementClass1';
247247
(childTree1.componentData as ComponentData).props.name = 'child1';
248248
(childTree1.componentData as ComponentData).index = 0;
249249
const childTree2 = deepCopy(mockChildTree);
250-
childTree2.name = 'IncrementClass1';
250+
childTree2.name = 'IncrementClass2';
251251
(childTree2.componentData as ComponentData).props.name = 'child2';
252252
(childTree2.componentData as ComponentData).index = 1;
253253
(mockSiblingTree.componentData as ComponentData).props = props;
@@ -340,13 +340,13 @@ describe('master tree tests', () => {
340340
(mockChildTree.componentData as ComponentData).state = classState;
341341
mockChildTree.state = classState;
342342
const childTree1 = deepCopy(mockChildTree);
343-
childTree1.name = 'IncrementClass';
343+
childTree1.name = 'IncrementClass1';
344344
(childTree1.componentData as ComponentData).index = 0;
345345
const childTree2 = deepCopy(mockChildTree);
346-
childTree2.name = 'IncrementClass1';
346+
childTree2.name = 'IncrementClass2';
347347
(childTree2.componentData as ComponentData).index = 1;
348348
const childTree3 = deepCopy(mockChildTree);
349-
childTree3.name = 'IncrementClass2';
349+
childTree3.name = 'IncrementClass3';
350350
(childTree3.componentData as ComponentData).index = 2;
351351
mockFiberTree.children[0].children = [childTree1];
352352
childTree1.children.push(childTree2, childTree3);
@@ -404,10 +404,10 @@ describe('master tree tests', () => {
404404
mockSiblingTree.state = functionalState;
405405
(mockSiblingTree.componentData as ComponentData).hooksState = functionalState;
406406
const siblingTree1 = deepCopy(mockSiblingTree);
407-
siblingTree1.name = 'IncrementFunc';
407+
siblingTree1.name = 'IncrementFunc1';
408408
(siblingTree1.componentData as ComponentData).hooksIndex = [0];
409409
const siblingTree2 = deepCopy(mockSiblingTree);
410-
siblingTree2.name = 'IncrementFunc1';
410+
siblingTree2.name = 'IncrementFunc2';
411411
(siblingTree2.componentData as ComponentData).hooksIndex = [1];
412412
const siblingTree3 = deepCopy(mockSiblingTree);
413413
siblingTree3.name = 'IncrementFuncMultiStates';
@@ -526,8 +526,8 @@ describe('master tree tests', () => {
526526
expect(child.children).toHaveLength(2);
527527
expect(child.children[0]).toBe(nextChild1);
528528
expect(child.children[1]).toBe(nextChild2);
529-
expect(nextChild1.name).toBe('child1');
530-
expect(nextChild2.name).toBe('child2');
529+
expect(nextChild1.name).toBe('child2');
530+
expect(nextChild2.name).toBe('child3');
531531
});
532532

533533
xit('should be able to add multiple children and sibilings', () => {});

src/backend/models/tree.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ComponentData } from '../types/backendTypes';
33

44
/** ComponentNames is used to store a mapping between a component's unique identifier and its name. This mapping is used to reconstruct the component instances during deserialization.*/
55
let componentNames = {};
6+
let rootTree;
67

78
// Making a deep clone of state becuase we want to make a copy
89
/**
@@ -20,6 +21,25 @@ export function serializeState(state) {
2021
}
2122
}
2223

24+
/**
25+
* Tree.name's come from the name of the component, but these names should be unique. When a second component
26+
* is used in a single tree, this function is called in checkForDuplicates to the change the Tree.name of the
27+
* first component to have a "1" at the end.
28+
*
29+
* @param tree A tree class instance
30+
* @param name The name being checked
31+
*/
32+
function changeFirstInstanceName(tree: Tree, name: string): void {
33+
for (const child of tree.children) {
34+
if (child.name === name) {
35+
child.name += 1;
36+
return;
37+
} else {
38+
changeFirstInstanceName(child, name)
39+
}
40+
}
41+
}
42+
2343
/**
2444
* This is the current snapshot that is being sent to the snapshots array.
2545
* Creates a Tree
@@ -71,40 +91,32 @@ class Tree {
7191
// Returns a unique name ready to be used for when new components gets added to the tree
7292
/**
7393
* @function checkForDuplicates - Generates a unique name for a component that is being added to the component tree
74-
* @param name
75-
* @returns
94+
* @param name Component name
95+
* @returns Unique name for Tree.name
7696
*/
7797
checkForDuplicates(name: string): string {
98+
if (this.name === 'root') {
99+
componentNames = {};
100+
rootTree = this;
101+
}
102+
78103
/**
79-
* The condition for check empty name does not seem to ever be reached
80-
* Commented and did not remove for potential future use
104+
* If a duplicate name is found, adds a number to the end of the name so it'll show up uniquely
105+
* in the component map. For example, if only one "Box" component is found, it's name will be "Box".
106+
* However, after a second "Box" component is found, the first box will be renamed "Box1" and the
107+
* second box will be named "Box2". When the third box is found it'll be named "Box3", and so on.
81108
*/
82-
// check for empty name
83-
// if (name === '' && typeof this.rtid === 'string') {
84-
// name = this.rtid.replace('fromLinkFiber', '');
85-
// }
86-
// if parent node is root, initialize the componentNames object
87-
if (this.name === 'root') componentNames = {};
88-
89-
//Original code, left for group review
90-
// Numerize the component name if found duplicate
91-
// Ex: A board has 3 rows => Row, Row1, Row2
92-
// Ex: Each row has 3 boxes => Box, Box1, Box2, ..., Box8
93-
componentNames[name] = componentNames[name] + 1 || 0;
94-
name += componentNames[name] ? componentNames[name] : '';
95-
96-
/**
97-
* Mark's notes: I know we start at 0 for arrays but I found it weird
98-
* that you'd see a component 'Box' and then the second one would be labeled
99-
* 'Box1'. So I changed the second one to be 'Box2'. What I'd like to do is iterate
100-
* over the the componentNames array and if any component name is > 1, find the first
101-
* one and make that one 'Box1', but one thing at a time
102-
*/
103-
// componentNames[name] = componentNames[name] + 1 || 1;
104-
// if (componentNames[name] > 1) name += componentNames[name]
109+
componentNames[name] = componentNames[name] + 1 || 1;
110+
111+
if (componentNames[name] === 2) {
112+
changeFirstInstanceName(rootTree, name);
113+
}
114+
115+
if (componentNames[name] > 1) name += componentNames[name];
105116

106117
return name;
107118
}
119+
108120
/**
109121
*
110122
* @param state - string if root, serialized state otherwise

0 commit comments

Comments
 (0)