Skip to content

Commit

Permalink
Remove effect from useMonitorsSortedByStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Fernández Gómez committed Nov 29, 2022
1 parent bc23cd6 commit a7b35cf
Showing 1 changed file with 33 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* 2.0.
*/

import { useEffect, useMemo, useState, useRef } from 'react';
import { isEqual } from 'lodash';
import { useMemo, useRef } from 'react';
import { useSelector } from 'react-redux';
import { MonitorOverviewItem } from '../../../../common/runtime_types';
import { selectOverviewState } from '../state/overview';
Expand All @@ -20,17 +19,19 @@ export function useMonitorsSortedByStatus() {
data: { monitors },
status,
} = useSelector(selectOverviewState);
const [monitorsSortedByStatus, setMonitorsSortedByStatus] = useState<
Record<string, MonitorOverviewItem[]>
>({ up: [], down: [], disabled: [] });

const downMonitors = useRef<Record<string, string[]> | null>(null);
const currentMonitors = useRef<MonitorOverviewItem[] | null>(monitors);
const locationNames = useLocationNames();

useEffect(() => {
const monitorsSortedByStatus = useMemo(() => {
if (!status) {
return;
return {
down: [],
up: [],
disabled: [],
};
}

const { downConfigs } = status;
const downMonitorMap: Record<string, string[]> = {};
downConfigs.forEach(({ location, configId }) => {
Expand All @@ -41,34 +42,30 @@ export function useMonitorsSortedByStatus() {
}
});

if (
!isEqual(downMonitorMap, downMonitors.current) ||
!isEqual(monitors, currentMonitors.current)
) {
const orderedDownMonitors: MonitorOverviewItem[] = [];
const orderedUpMonitors: MonitorOverviewItem[] = [];
const orderedDisabledMonitors: MonitorOverviewItem[] = [];
monitors.forEach((monitor) => {
const monitorLocation = locationNames[monitor.location.id];
if (!monitor.isEnabled) {
orderedDisabledMonitors.push(monitor);
} else if (
Object.keys(downMonitorMap).includes(monitor.id) &&
downMonitorMap[monitor.id].includes(monitorLocation)
) {
orderedDownMonitors.push(monitor);
} else {
orderedUpMonitors.push(monitor);
}
});
downMonitors.current = downMonitorMap;
currentMonitors.current = monitors;
setMonitorsSortedByStatus({
down: orderedDownMonitors,
up: orderedUpMonitors,
disabled: orderedDisabledMonitors,
});
}
const orderedDownMonitors: MonitorOverviewItem[] = [];
const orderedUpMonitors: MonitorOverviewItem[] = [];
const orderedDisabledMonitors: MonitorOverviewItem[] = [];

monitors.forEach((monitor) => {
const monitorLocation = locationNames[monitor.location.id];
if (!monitor.isEnabled) {
orderedDisabledMonitors.push(monitor);
} else if (
monitor.id in downMonitorMap &&
downMonitorMap[monitor.id].includes(monitorLocation)
) {
orderedDownMonitors.push(monitor);
} else {
orderedUpMonitors.push(monitor);
}
});
downMonitors.current = downMonitorMap;

return {
down: orderedDownMonitors,
up: orderedUpMonitors,
disabled: orderedDisabledMonitors,
};
}, [monitors, locationNames, downMonitors, status]);

return useMemo(() => {
Expand Down

0 comments on commit a7b35cf

Please sign in to comment.