fix(segmentation): Fixed the manual scrolling of the segments list and implemented automatic scrolling to the active segment. (#5510)
The height of the list container is now calculated based on a parent div instead of itself. DataRow component now has a forward ref to allow for automatic scrolling. Added better detection to the useDynamicMaxHeight hook for when to recalculate the max height using intersection observers. The useEffect for scrolling a segment into view used to be excessively called to the point where it was auto scrolling after the user manually scrolled. The auto scroll useEffect is now dependent on the segment index changing which should only happen once per active segment change.
This commit is contained in:
1 parent
cf0b561a49
commit
1df4f843fe
3 files changed
+472
-359
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, { useEffect } from 'react';
|
||||
import { ScrollArea, DataRow } from '../../components';
|
||||
import { HoverCard, HoverCardTrigger, HoverCardContent } from '../../components/HoverCard';
|
||||
import { useSegmentationTableContext, useSegmentationExpanded } from './contexts';
|
||||
@@ -23,6 +23,11 @@ export const SegmentationSegments = ({ children = null }: { children?: React.Rea
|
||||
let segmentation;
|
||||
let representation;
|
||||
|
||||
const activeSegmentRef = React.useRef<{
|
||||
element: HTMLElement | null;
|
||||
index: number | null;
|
||||
}>({ element: null, index: null });
|
||||
|
||||
try {
|
||||
// Try to use the SegmentationExpanded context if available
|
||||
const segmentationInfo = useSegmentationExpanded('SegmentationSegments');
|
||||
@@ -38,108 +43,163 @@ export const SegmentationSegments = ({ children = null }: { children?: React.Rea
|
||||
}
|
||||
|
||||
const segments = Object.values(representation.segments);
|
||||
|
||||
// Find the active segment to scroll to it when it changes
|
||||
const activeSegment = segments.find(segment => {
|
||||
if (!segment) {
|
||||
return false;
|
||||
}
|
||||
const segmentFromSegmentation = segmentation.segments[segment.segmentIndex];
|
||||
return segmentFromSegmentation?.active;
|
||||
});
|
||||
|
||||
const isActiveSegmentation = segmentation.segmentationId === activeSegmentationId;
|
||||
|
||||
const { ref: scrollableContainerRef, maxHeight } = useDynamicMaxHeight(segments);
|
||||
|
||||
useEffect(() => {
|
||||
const activeSegmentIndex = activeSegmentRef.current.index;
|
||||
if (!activeSegmentIndex || activeSegmentIndex !== activeSegment?.segmentIndex) {
|
||||
return;
|
||||
}
|
||||
|
||||
const activeSegmentElement = activeSegmentRef.current.element;
|
||||
|
||||
if (!activeSegmentElement) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if the active segment is already visible.
|
||||
const activeSegmentElementBounds = activeSegmentElement.getBoundingClientRect();
|
||||
const scrollableContainerRect = scrollableContainerRef.current.getBoundingClientRect();
|
||||
if (
|
||||
activeSegmentElementBounds.top > scrollableContainerRect.top &&
|
||||
activeSegmentElementBounds.bottom < scrollableContainerRect.bottom
|
||||
) {
|
||||
// The active segment is already visible, so we don't need to scroll.
|
||||
return;
|
||||
}
|
||||
|
||||
activeSegmentElement.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
|
||||
}, [activeSegment?.segmentIndex, scrollableContainerRef]);
|
||||
|
||||
if (!representation || !segmentation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return (
|
||||
<ScrollArea
|
||||
className={`bg-bkg-low space-y-px`}
|
||||
showArrows={true}
|
||||
>
|
||||
<div
|
||||
ref={scrollableContainerRef}
|
||||
style={{ maxHeight: maxHeight }}
|
||||
<div ref={scrollableContainerRef}>
|
||||
<ScrollArea
|
||||
className={`bg-bkg-low space-y-px`}
|
||||
showArrows={
|
||||
scrollableContainerRef?.current
|
||||
? scrollableContainerRef?.current?.offsetHeight >= parseFloat(maxHeight)
|
||||
: false
|
||||
}
|
||||
>
|
||||
{segments.map(segment => {
|
||||
if (!segment) {
|
||||
return null;
|
||||
}
|
||||
const { segmentIndex, color, visible } = segment as {
|
||||
segmentIndex: number;
|
||||
color: number[];
|
||||
visible: boolean;
|
||||
};
|
||||
const segmentFromSegmentation = segmentation.segments[segmentIndex];
|
||||
<div style={{ maxHeight: maxHeight }}>
|
||||
{segments.map(segment => {
|
||||
if (!segment) {
|
||||
return null;
|
||||
}
|
||||
const { segmentIndex, color, visible } = segment as {
|
||||
segmentIndex: number;
|
||||
color: number[];
|
||||
visible: boolean;
|
||||
};
|
||||
const segmentFromSegmentation = segmentation.segments[segmentIndex];
|
||||
|
||||
if (!segmentFromSegmentation) {
|
||||
return null;
|
||||
}
|
||||
if (!segmentFromSegmentation) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { locked, active, label, displayText } = segmentFromSegmentation;
|
||||
const cssColor = `rgb(${color[0]},${color[1]},${color[2]})`;
|
||||
const { locked, active, label, displayText } = segmentFromSegmentation;
|
||||
const cssColor = `rgb(${color[0]},${color[1]},${color[2]})`;
|
||||
|
||||
const hasStats = segmentFromSegmentation.cachedStats?.namedStats;
|
||||
const DataRowComponent = (
|
||||
<DataRow
|
||||
key={segmentIndex}
|
||||
number={showSegmentIndex ? segmentIndex : null}
|
||||
title={label}
|
||||
// details={displayText}
|
||||
description={displayText}
|
||||
colorHex={cssColor}
|
||||
isSelected={active}
|
||||
isVisible={visible}
|
||||
isLocked={locked}
|
||||
disableEditing={disableEditing}
|
||||
className={!isActiveSegmentation ? 'opacity-80' : ''}
|
||||
onColor={() => onSegmentColorClick(segmentation.segmentationId, segmentIndex)}
|
||||
onToggleVisibility={() =>
|
||||
onToggleSegmentVisibility(
|
||||
segmentation.segmentationId,
|
||||
segmentIndex,
|
||||
representation.type
|
||||
)
|
||||
const hasStats = segmentFromSegmentation.cachedStats?.namedStats;
|
||||
|
||||
const segmentRowRef = (element: HTMLElement) => {
|
||||
if (!active) {
|
||||
return;
|
||||
}
|
||||
onToggleLocked={() => onToggleSegmentLock(segmentation.segmentationId, segmentIndex)}
|
||||
onSelect={() => onSegmentClick(segmentation.segmentationId, segmentIndex)}
|
||||
onRename={() => onSegmentEdit(segmentation.segmentationId, segmentIndex)}
|
||||
onDelete={() => onSegmentDelete(segmentation.segmentationId, segmentIndex)}
|
||||
/>
|
||||
);
|
||||
|
||||
return hasStats ? (
|
||||
<HoverCard
|
||||
key={`hover-${segmentIndex}`}
|
||||
openDelay={300}
|
||||
>
|
||||
<HoverCardTrigger asChild>
|
||||
<div>{DataRowComponent}</div>
|
||||
</HoverCardTrigger>
|
||||
<HoverCardContent
|
||||
side="left"
|
||||
align="start"
|
||||
className="w-72 border"
|
||||
>
|
||||
<div className="mb-4 flex items-center space-x-2">
|
||||
<div
|
||||
className="h-2.5 w-2.5 flex-shrink-0 rounded-full"
|
||||
style={{ backgroundColor: cssColor }}
|
||||
></div>
|
||||
<h3 className="text-muted-foreground break-words font-semibold">{label}</h3>
|
||||
</div>
|
||||
if (element) {
|
||||
activeSegmentRef.current = { element, index: segmentIndex };
|
||||
} else {
|
||||
activeSegmentRef.current = { element: null, index: null };
|
||||
}
|
||||
};
|
||||
|
||||
<SegmentStatistics
|
||||
segment={{
|
||||
...segmentFromSegmentation,
|
||||
const DataRowComponent = (
|
||||
<DataRow
|
||||
ref={segmentRowRef}
|
||||
key={segmentIndex}
|
||||
number={showSegmentIndex ? segmentIndex : null}
|
||||
title={label}
|
||||
// details={displayText}
|
||||
description={displayText}
|
||||
colorHex={cssColor}
|
||||
isSelected={active}
|
||||
isVisible={visible}
|
||||
isLocked={locked}
|
||||
disableEditing={disableEditing}
|
||||
className={!isActiveSegmentation ? 'opacity-80' : ''}
|
||||
onColor={() => onSegmentColorClick(segmentation.segmentationId, segmentIndex)}
|
||||
onToggleVisibility={() =>
|
||||
onToggleSegmentVisibility(
|
||||
segmentation.segmentationId,
|
||||
segmentIndex,
|
||||
}}
|
||||
segmentationId={segmentation.segmentationId}
|
||||
representation.type
|
||||
)
|
||||
}
|
||||
onToggleLocked={() =>
|
||||
onToggleSegmentLock(segmentation.segmentationId, segmentIndex)
|
||||
}
|
||||
onSelect={() => onSegmentClick(segmentation.segmentationId, segmentIndex)}
|
||||
onRename={() => onSegmentEdit(segmentation.segmentationId, segmentIndex)}
|
||||
onDelete={() => onSegmentDelete(segmentation.segmentationId, segmentIndex)}
|
||||
/>
|
||||
);
|
||||
|
||||
return hasStats ? (
|
||||
<HoverCard
|
||||
key={`hover-${segmentIndex}`}
|
||||
openDelay={300}
|
||||
>
|
||||
<HoverCardTrigger asChild>
|
||||
<div>{DataRowComponent}</div>
|
||||
</HoverCardTrigger>
|
||||
<HoverCardContent
|
||||
side="left"
|
||||
align="start"
|
||||
className="w-72 border"
|
||||
>
|
||||
{children}
|
||||
</SegmentStatistics>
|
||||
</HoverCardContent>
|
||||
</HoverCard>
|
||||
) : (
|
||||
DataRowComponent
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
<div className="mb-4 flex items-center space-x-2">
|
||||
<div
|
||||
className="h-2.5 w-2.5 flex-shrink-0 rounded-full"
|
||||
style={{ backgroundColor: cssColor }}
|
||||
></div>
|
||||
<h3 className="text-muted-foreground break-words font-semibold">{label}</h3>
|
||||
</div>
|
||||
|
||||
<SegmentStatistics
|
||||
segment={{
|
||||
...segmentFromSegmentation,
|
||||
segmentIndex,
|
||||
}}
|
||||
segmentationId={segmentation.segmentationId}
|
||||
>
|
||||
{children}
|
||||
</SegmentStatistics>
|
||||
</HoverCardContent>
|
||||
</HoverCard>
|
||||
) : (
|
||||
DataRowComponent
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user