2025-09-19 10:09:14 +01:00
|
|
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
2025-09-16 14:20:38 +01:00
|
|
|
import type { SightingType } from "../../types/types";
|
2025-09-16 11:07:35 +01:00
|
|
|
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-16 11:07:35 +01:00
|
|
|
import InfoBar from "./InfoBar";
|
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";
|
2025-09-23 13:03:54 +01:00
|
|
|
import popup from "../../assets/sounds/ui/popup_open.mp3";
|
2025-09-22 11:18:14 +01:00
|
|
|
import { useSound } from "react-sounds";
|
2025-09-25 10:38:49 +01:00
|
|
|
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;
|
2025-09-22 09:26:45 +01:00
|
|
|
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);
|
2025-09-23 13:03:54 +01:00
|
|
|
const { play } = useSound(popup);
|
2025-09-12 08:21:52 +01:00
|
|
|
const {
|
|
|
|
|
sightings,
|
|
|
|
|
setSelectedSighting,
|
|
|
|
|
setSightingModalOpen,
|
|
|
|
|
isSightingModalOpen,
|
|
|
|
|
selectedSighting,
|
2025-09-25 10:38:49 +01:00
|
|
|
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();
|
2025-09-25 10:38:49 +01:00
|
|
|
const { sessionStarted, setSessionList, sessionList } = useNPEDContext();
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (sessionStarted) {
|
|
|
|
|
if (!mostRecent) return;
|
|
|
|
|
setSessionList([...sessionList, mostRecent]);
|
|
|
|
|
}
|
|
|
|
|
}, [mostRecent, sessionStarted, setSessionList]);
|
2025-09-16 11:07:35 +01:00
|
|
|
|
2025-09-19 10:09:14 +01:00
|
|
|
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-09-19 10:09:14 +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-16 11:07:35 +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-16 11:07:35 +01:00
|
|
|
|
2025-09-22 09:26:45 +01:00
|
|
|
if (isNPEDHitA || isNPEDHitB || isNPEDHitC) {
|
|
|
|
|
dispatch({
|
|
|
|
|
type: "ADD",
|
|
|
|
|
payload: obj,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
2025-09-23 13:03:54 +01:00
|
|
|
}, [dispatch, rows]);
|
2025-09-16 11:07:35 +01:00
|
|
|
|
2025-09-19 10:09:14 +01:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (hasAutoOpenedRef.current) return;
|
2025-09-22 09:26:45 +01:00
|
|
|
const firstHot = rows?.find((r) => {
|
2025-09-25 10:38:49 +01:00
|
|
|
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";
|
2025-09-25 10:38:49 +01:00
|
|
|
return isNPEDHitA || isNPEDHitB || isNPEDHitC || isHotListHit;
|
2025-09-22 09:26:45 +01:00
|
|
|
});
|
2025-09-19 10:09:14 +01:00
|
|
|
if (firstHot) {
|
|
|
|
|
setSelectedSighting(firstHot);
|
2025-09-22 11:18:14 +01:00
|
|
|
play();
|
2025-09-19 10:09:14 +01:00
|
|
|
setSightingModalOpen(true);
|
2025-09-22 09:26:45 +01:00
|
|
|
hasAutoOpenedRef.current = true;
|
2025-09-19 10:09:14 +01:00
|
|
|
}
|
2025-09-22 11:18:14 +01:00
|
|
|
}, [play, rows, setSelectedSighting, setSightingModalOpen]);
|
2025-09-19 10:09:14 +01:00
|
|
|
|
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 h-100", 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";
|
|
|
|
|
const primaryIsColour = obj?.srcCam === 1;
|
|
|
|
|
const secondaryMissing = (obj?.vrmSecondary ?? "") === "";
|
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-neutral-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-09-16 11:07:35 +01:00
|
|
|
<InfoBar obj={obj} />
|
2025-09-12 08:21:52 +01:00
|
|
|
{/* Patch row */}
|
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 border-amber-600" : ""
|
|
|
|
|
}`}
|
2025-08-20 08:27:05 +01:00
|
|
|
>
|
2025-09-12 08:21:52 +01:00
|
|
|
<div
|
|
|
|
|
className={`border p-1 ${
|
|
|
|
|
primaryIsColour ? "ring-2 ring-lime-400" : ""
|
2025-09-19 11:38:26 +01:00
|
|
|
} ${secondaryMissing && primaryIsColour ? "" : ""}`}
|
2025-09-12 08:21:52 +01:00
|
|
|
>
|
|
|
|
|
<img
|
|
|
|
|
src={obj?.plateUrlColour || BLANK_IMG}
|
|
|
|
|
height={48}
|
|
|
|
|
alt="colour patch"
|
2025-09-19 11:38:26 +01:00
|
|
|
className={primaryIsColour ? "" : ""}
|
2025-09-12 08:21:52 +01:00
|
|
|
/>
|
|
|
|
|
</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
|
|
|
);
|
|
|
|
|
}
|