Files
Alberto-Audrix 32a36cceff
CI / lint-and-test (push) Canceled after 0s
first commit
2026-07-28 08:55:05 +07:00

119 lines
4.0 KiB
TypeScript

import { useMemo } from 'react';
import { useDeviceStore } from '../stores/useDeviceStore.ts';
import { useStaticDataStore } from '../stores/useStaticDataStore.ts';
import { useLocationStore } from '../stores/useLocationStore.ts';
import { AggregatedCountLocation, LiveCount, NetworkDevice } from '../../types.ts';
export const useDerivedState = (
deviceStore: ReturnType<typeof useDeviceStore>,
staticDataStore: ReturnType<typeof useStaticDataStore>,
locationStore: ReturnType<typeof useLocationStore>
) => {
const { cameras } = deviceStore;
const { liveCounts } = staticDataStore;
const { locations, coops, floors } = locationStore;
const aggregatedCounts = useMemo<AggregatedCountLocation[]>(() => {
const cameraMap = new Map<string, LiveCount>();
liveCounts.forEach((lc) => cameraMap.set(lc.cameraId, lc));
const floorMap = new Map<
string,
{ floorName: string; coopId: string; count: number; cameras: LiveCount[] }
>();
cameras.forEach((camera) => {
const liveCount = cameraMap.get(camera.id);
if (camera.floorId && liveCount && liveCount.status !== 'offline') {
if (!floorMap.has(camera.floorId)) {
const floor = floors.find((f) => f.id === camera.floorId);
floorMap.set(camera.floorId, {
floorName: floor?.name || 'Unknown Floor',
coopId: floor?.coopId || 'unknown-coop',
count: 0,
cameras: [],
});
}
const floorData = floorMap.get(camera.floorId)!;
floorData.count += liveCount.count;
floorData.cameras.push(liveCount);
}
});
const coopMap = new Map<
string,
{ coopName: string; locationId: string; totalCount: number; floors: any[] }
>();
floorMap.forEach((data, floorId) => {
if (!coopMap.has(data.coopId)) {
const coop = coops.find((c) => c.id === data.coopId);
coopMap.set(data.coopId, {
coopName: coop?.name || 'Unknown Coop',
locationId: coop?.locationId || 'unknown-loc',
totalCount: 0,
floors: [],
});
}
const coopData = coopMap.get(data.coopId)!;
coopData.totalCount += data.count;
coopData.floors.push({
floorId,
floorName: data.floorName,
count: data.count,
cameras: data.cameras,
});
});
const locationMap = new Map<
string,
{ locationName: string; totalCount: number; coops: any[] }
>();
coopMap.forEach((data, coopId) => {
if (!locationMap.has(data.locationId)) {
const loc = locations.find((l) => l.id === data.locationId);
locationMap.set(data.locationId, {
locationName: loc?.name || 'Unknown Location',
totalCount: 0,
coops: [],
});
}
const locData = locationMap.get(data.locationId)!;
locData.totalCount += data.totalCount;
locData.coops.push({
coopId,
coopName: data.coopName,
totalCount: data.totalCount,
floors: data.floors,
});
});
return Array.from(locationMap.entries()).map(([locationId, data]) => ({ locationId, ...data }));
}, [liveCounts, cameras, locations, coops, floors]);
const getNetworkDeviceLocationName = (device: NetworkDevice): string => {
if (!device.locationId) return 'Global';
const floor = floors.find((f) => f.id === device.locationId);
if (floor) {
const coop = coops.find((c) => c.id === floor.coopId);
const location = locations.find((l) => l.id === coop?.locationId);
return `${location?.name || ''} / ${coop?.name || ''} / ${floor.name}`;
}
const coop = coops.find((c) => c.id === device.locationId);
if (coop) {
const location = locations.find((l) => l.id === coop.locationId);
return `${location?.name || ''} / ${coop.name}`;
}
const location = locations.find((l) => l.id === device.locationId);
if (location) return location.name;
return 'Lokasi Tidak Dikenal';
};
return {
aggregatedCounts,
getNetworkDeviceLocationName,
};
};