Files
Mav-Mobile-UI/src/components/SightingsWidget/SightingWidget.tsx

200 lines
6.4 KiB
TypeScript
Raw Normal View History

import { useCallback, useEffect, useMemo, useRef, useState } from "react";
2025-09-16 14:20:38 +01:00
import type { SightingType } from "../../types/types";
import { BLANK_IMG } from "../../utils/utils";
2025-08-20 08:27:05 +01:00
import NumberPlate from "../PlateStack/NumberPlate";
import Card from "../UI/Card";
import CardHeader from "../UI/CardHeader";
import clsx from "clsx";
import { useSightingFeedContext } from "../../context/SightingFeedContext";
2025-09-12 08:21:52 +01:00
import SightingModal from "../SightingModal/SightingModal";
2025-09-22 09:26:45 +01:00
import { useAlertHitContext } from "../../context/AlertHitContext";
2025-09-19 11:22:09 +01:00
import HotListImg from "/Hotlist_Hit.svg";
2025-09-22 09:26:45 +01:00
import NPED_CAT_A from "/NPED_Cat_A.svg";
import NPED_CAT_B from "/NPED_Cat_B.svg";
import NPED_CAT_C from "/NPED_Cat_C.svg";
import popup from "../../assets/sounds/ui/popup_open.mp3";
import { useSound } from "react-sounds";
import { useNPEDContext } from "../../context/NPEDUserContext";
2025-08-20 08:27:05 +01:00
function useNow(tickMs = 1000) {
const [, setNow] = useState(() => Date.now());
useEffect(() => {
const id = setInterval(() => setNow(Date.now()), tickMs);
return () => clearInterval(id);
}, [tickMs]);
2025-09-22 09:26:45 +01:00
return null;
2025-08-20 08:27:05 +01:00
}
2025-09-15 10:27:31 +01:00
type SightingHistoryProps = {
baseUrl?: string;
2025-09-22 09:26:45 +01:00
entries?: number;
pollMs?: number;
2025-08-20 08:27:05 +01:00
autoSelectLatest?: boolean;
2025-09-15 10:27:31 +01:00
title: string;
className?: string;
2025-08-20 08:27:05 +01:00
};
export default function SightingHistoryWidget({
className,
2025-09-15 10:27:31 +01:00
title,
}: SightingHistoryProps) {
2025-08-20 08:27:05 +01:00
useNow(1000);
const { play } = useSound(popup);
2025-09-12 08:21:52 +01:00
const {
sightings,
setSelectedSighting,
setSightingModalOpen,
isSightingModalOpen,
selectedSighting,
mostRecent,
2025-09-12 08:21:52 +01:00
} = useSightingFeedContext();
2025-08-20 08:27:05 +01:00
2025-09-22 09:26:45 +01:00
const { dispatch } = useAlertHitContext();
const { sessionStarted, setSessionList, sessionList } = useNPEDContext();
useEffect(() => {
if (sessionStarted) {
if (!mostRecent) return;
setSessionList([...sessionList, mostRecent]);
}
}, [mostRecent, sessionStarted, setSessionList]);
const hasAutoOpenedRef = useRef(false);
2025-08-20 08:27:05 +01:00
const onRowClick = useCallback(
2025-09-16 14:20:38 +01:00
(sighting: SightingType) => {
2025-09-12 08:21:52 +01:00
if (!sighting) return;
2025-09-22 09:26:45 +01:00
setSightingModalOpen(true);
2025-09-12 08:21:52 +01:00
setSelectedSighting(sighting);
2025-08-20 08:27:05 +01:00
},
2025-09-22 09:26:45 +01:00
[setSelectedSighting, setSightingModalOpen]
2025-08-20 08:27:05 +01:00
);
2025-08-20 08:27:05 +01:00
const rows = useMemo(
2025-09-16 14:20:38 +01:00
() => sightings?.filter(Boolean) as SightingType[],
2025-08-22 10:38:28 +01:00
[sightings]
2025-08-20 08:27:05 +01:00
);
2025-09-22 09:26:45 +01:00
useEffect(() => {
rows?.forEach((obj) => {
const isNPEDHitA = obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "A";
const isNPEDHitB = obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "B";
const isNPEDHitC = obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "C";
2025-09-22 09:26:45 +01:00
if (isNPEDHitA || isNPEDHitB || isNPEDHitC) {
dispatch({
type: "ADD",
payload: obj,
});
}
});
}, [dispatch, rows]);
useEffect(() => {
if (hasAutoOpenedRef.current) return;
2025-09-22 09:26:45 +01:00
const firstHot = rows?.find((r) => {
const isHotListHit = r?.metadata?.hotlistMatches?.Hotlist0 === true;
2025-09-22 09:26:45 +01:00
const isNPEDHitA = r?.metadata?.npedJSON?.["NPED CATEGORY"] === "A";
const isNPEDHitB = r?.metadata?.npedJSON?.["NPED CATEGORY"] === "B";
const isNPEDHitC = r?.metadata?.npedJSON?.["NPED CATEGORY"] === "C";
return isNPEDHitA || isNPEDHitB || isNPEDHitC || isHotListHit;
2025-09-22 09:26:45 +01:00
});
if (firstHot) {
setSelectedSighting(firstHot);
play();
setSightingModalOpen(true);
2025-09-22 09:26:45 +01:00
hasAutoOpenedRef.current = true;
}
}, [play, rows, setSelectedSighting, setSightingModalOpen]);
2025-09-12 08:21:52 +01:00
const handleClose = () => {
setSightingModalOpen(false);
};
2025-08-20 08:27:05 +01:00
return (
2025-09-12 08:21:52 +01:00
<>
<Card
className={clsx(
"overflow-y-auto min-h-[40vh] md:min-h-[60vh] max-h-[80vh] lg:w-[40%] p-4",
className
)}
>
2025-09-15 10:27:31 +01:00
<CardHeader title={title} />
2025-09-12 08:21:52 +01:00
<div className="flex flex-col gap-3 ">
{/* Rows */}
<div className="flex flex-col">
2025-09-22 09:26:45 +01:00
{rows?.map((obj) => {
const isNPEDHitA =
obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "A";
const isNPEDHitB =
obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "B";
const isNPEDHitC =
obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "C";
const isNPEDHitD =
obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "D";
2025-09-12 08:21:52 +01:00
const motionAway = (obj?.motion ?? "").toUpperCase() === "AWAY";
2025-09-19 11:22:09 +01:00
const isHotListHit =
obj?.metadata?.hotlistMatches?.Hotlist0 === true;
2025-09-12 08:21:52 +01:00
return (
2025-08-29 14:55:37 +01:00
<div
2025-09-22 09:26:45 +01:00
key={obj.ref}
className={`border border-gray-700 rounded-md mb-2 p-2 cursor-pointer `}
2025-09-12 08:21:52 +01:00
onClick={() => onRowClick(obj)}
2025-08-29 14:55:37 +01:00
>
2025-08-20 08:27:05 +01:00
<div
2025-09-22 09:26:45 +01:00
className={`flex items-center gap-3 mt-2 justify-between ${
isNPEDHitD ? " border-amber-600" : ""
2025-09-22 09:26:45 +01:00
}`}
2025-08-20 08:27:05 +01:00
>
<div className={`border p-1 `}>
2025-09-12 08:21:52 +01:00
<img
src={obj?.plateUrlColour || BLANK_IMG}
height={48}
width={200}
2025-09-12 08:21:52 +01:00
alt="colour patch"
/>
</div>
2025-09-19 11:22:09 +01:00
{isHotListHit && (
<img
src={HotListImg}
alt="hotlistHit"
className="h-20 object-contain rounded-md"
/>
)}
2025-09-22 09:26:45 +01:00
{isNPEDHitA && (
<img
src={NPED_CAT_A}
alt="hotlistHit"
className="h-20 object-contain rounded-md"
/>
)}
{isNPEDHitB && (
<img
src={NPED_CAT_B}
alt="hotlistHit"
className="h-20 object-contain rounded-md"
/>
)}
{isNPEDHitC && (
<img
src={NPED_CAT_C}
alt="hotlistHit"
className="h-20 object-contain rounded-md"
/>
)}
2025-09-12 08:21:52 +01:00
<NumberPlate motion={motionAway} vrm={obj?.vrm} />
2025-08-20 08:27:05 +01:00
</div>
</div>
2025-09-12 08:21:52 +01:00
);
})}
</div>
2025-08-20 08:27:05 +01:00
</div>
2025-09-12 08:21:52 +01:00
</Card>
<SightingModal
isSightingModalOpen={isSightingModalOpen}
handleClose={handleClose}
sighting={selectedSighting}
/>
</>
2025-08-20 08:27:05 +01:00
);
}