fix: optimize component sorting logic to ensure recalculation on changes. Fixes the bug on showing the component from one subcontainer to another

This commit is contained in:
Kavin Venkatachalam 2025-07-09 12:22:28 +05:30
parent 68fa104582
commit d931b7ecc6

View file

@ -22,10 +22,10 @@ const useSortedComponents = (components, currentLayout, id, moduleId) => {
const sortedComponents = useMemo(() => {
const { triggerUpdate, shouldReorder } = reorderContainerChildren;
// If this container is not the target of the reorder, return cached order
if (!shouldReorder) {
return prevComponentsOrder.current;
}
// Always recalculate if components array has changed (new component added/removed)
const componentsChanged =
prevComponentsOrder.current.length !== components.length ||
!components.every((comp) => prevComponentsOrder.current.includes(comp));
// If a forced update occurred for this container, recalculate order
const isForcedUpdate = prevForceUpdateRef.current !== triggerUpdate;
@ -33,6 +33,14 @@ const useSortedComponents = (components, currentLayout, id, moduleId) => {
prevForceUpdateRef.current = triggerUpdate;
}
// Skip recalculation only if:
// 1. This container is not the target of reorder
// 2. Components haven't changed
// 3. No forced update occurred
if (!shouldReorder && !componentsChanged && !isForcedUpdate) {
return prevComponentsOrder.current;
}
const currentPageComponents = getCurrentPageComponents();
const newComponentsOrder = [...components].sort((a, b) => {