Skip to content

Commit 75affe7

Browse files
committed
truncated long node names with ellipses
1 parent e96421e commit 75affe7

File tree

2 files changed

+59
-11
lines changed

2 files changed

+59
-11
lines changed

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

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -325,16 +325,60 @@ export default function ComponentMap({
325325
})}
326326

327327
{tree.descendants().map((node, key) => {
328-
const widthFunc: number = (name) => {
329-
//returns a number that is related to the length of the name. Used for determining the node width.
330-
const nodeLength = name.length;
331-
// return nodeLength * 7 + 20; //uncomment this line if we want each node to be directly proportional to the name.length (instead of nodes of similar sizes to snap to the same width)
328+
const calculateNodeWidth = (text: string): number => {
329+
const nodeLength = text.length;
332330
if (nodeLength <= 5) return nodeLength + 50;
333331
if (nodeLength <= 10) return nodeLength + 120;
334332
return nodeLength + 140;
335333
};
336334

337-
const width: number = widthFunc(node.data.name); // the width is determined by the length of the node.name
335+
// Find the maximum width for any node
336+
const findMaxNodeWidth = (nodeData: any): number => {
337+
// If no children, return current node width
338+
if (!nodeData.children) {
339+
return calculateNodeWidth(nodeData.name);
340+
}
341+
342+
// Get width of current node
343+
const currentWidth = calculateNodeWidth(nodeData.name);
344+
345+
// Get max width from children
346+
const childrenWidths = nodeData.children.map((child) =>
347+
findMaxNodeWidth(child),
348+
);
349+
350+
// Return the maximum width found
351+
return Math.max(currentWidth, ...childrenWidths);
352+
};
353+
354+
// Truncate text for nodes that exceed a certain length
355+
const truncateText = (text: string, width: number, maxWidth: number): string => {
356+
// Calculate approximate text width
357+
const estimatedTextWidth = text.length * 8;
358+
359+
// If this node's width is close to the max width (within 10%), truncate it
360+
if (width >= maxWidth * 0.9) {
361+
const maxChars = Math.floor((width - 30) / 8); // -30 for padding + ellipsis
362+
return `${text.slice(0, maxChars)}...`;
363+
}
364+
365+
return text;
366+
};
367+
368+
const getNodeDimensions = (
369+
name: string,
370+
rootNode: any,
371+
): { width: number; displayText: string } => {
372+
const width = calculateNodeWidth(name);
373+
const maxWidth = findMaxNodeWidth(rootNode);
374+
const displayText = truncateText(name, width, maxWidth);
375+
376+
return { width, displayText };
377+
};
378+
379+
// Usage in your render function:
380+
const { width, displayText } = getNodeDimensions(node.data.name, startNode);
381+
338382
const height: number = 35;
339383
let top: number;
340384
let left: number;
@@ -432,9 +476,13 @@ export default function ComponentMap({
432476
return (
433477
<Group top={top} left={left} key={key} className='rect'>
434478
{node.depth === 0 && (
435-
<circle
479+
<rect
436480
className='compMapRoot'
437-
r={25}
481+
height={height}
482+
width={width}
483+
y={-height / 2}
484+
x={-width / 2}
485+
rx={10}
438486
onClick={() => {
439487
dispatch(toggleExpanded(node.data));
440488
hideTooltip();
@@ -451,7 +499,7 @@ export default function ComponentMap({
451499
width={width}
452500
y={-height / 2}
453501
x={-width / 2}
454-
rx={node.children ? 4 : 10}
502+
rx={10}
455503
onClick={() => {
456504
dispatch(toggleExpanded(node.data));
457505
hideTooltip();
@@ -490,7 +538,7 @@ export default function ComponentMap({
490538
textAnchor='middle'
491539
style={{ pointerEvents: 'none' }}
492540
>
493-
{node.data.name}
541+
{displayText}
494542
</text>
495543
</Group>
496544
);

src/app/components/StateRoute/PerformanceVisx/BarGraph.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ const BarGraph = (props: BarGraphProps): JSX.Element => {
263263
</Text>
264264
<br />
265265
{snapshot === 'All Snapshots' ? (
266-
<Text x={xMax / 2 + 15} y={yMax + 65} fontSize={16} fill={axisLabelColor}>
266+
<Text x={xMax / 2 + 15} y={yMax + 62} fontSize={16} fill={axisLabelColor}>
267267
Snapshot ID
268268
</Text>
269269
) : (
270-
<Text x={xMax / 2 + 15} y={yMax + 65} fontSize={16} fill={axisLabelColor}>
270+
<Text x={xMax / 2 + 15} y={yMax + 62} fontSize={16} fill={axisLabelColor}>
271271
Components
272272
</Text>
273273
)}

0 commit comments

Comments
 (0)