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

246 lines
7.7 KiB
TypeScript
Raw Normal View History

import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { ReducedSightingType, SightingType } from "../../types/types";
import { BLANK_IMG, getSoundFileURL } 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 notification from "../../assets/sounds/ui/notification.mp3";
import { useSound } from "react-sounds";
import { useNPEDContext } from "../../context/NPEDUserContext";
import { useSoundContext } from "../../context/SoundContext";
import Loading from "../UI/Loading";
import { checkIsHotListHit } from "../../utils/utils";
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 { state } = useSoundContext();
const soundSrcNped = useMemo(() => {
return getSoundFileURL(state.NPEDsound) ?? popup;
}, [state.NPEDsound]);
const soundSrcHotlist = useMemo(() => {
return getSoundFileURL(state?.hotlists?.[0]?.sound) ?? notification;
}, [state.hotlists]);
const { play: npedSound } = useSound(soundSrcNped);
const { play: hotlistsound } = useSound(soundSrcHotlist);
2025-09-12 08:21:52 +01:00
const {
sightings,
setSelectedSighting,
setSightingModalOpen,
isSightingModalOpen,
selectedSighting,
mostRecent,
isLoading,
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();
const reduceObject = (obj: SightingType): ReducedSightingType => {
return {
vrm: obj.vrm,
metadata: obj?.metadata,
};
};
useEffect(() => {
if (sessionStarted) {
if (!mostRecent) return;
const reducedMostRecent = reduceObject(mostRecent);
setSessionList([...sessionList, reducedMostRecent]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [mostRecent, sessionStarted, setSessionList]);
const hasAutoOpenedRef = useRef(false);
const npedRef = 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";
const isNPEDHitD = obj?.metadata?.npedJSON?.["NPED CATEGORY"] === "D";
if (isNPEDHitA || isNPEDHitB || isNPEDHitC || isNPEDHitD) {
2025-09-22 09:26:45 +01:00
dispatch({
type: "ADD",
payload: obj,
});
}
});
}, [dispatch]);
useEffect(() => {
if (hasAutoOpenedRef.current || npedRef.current) return;
const firstNPED = rows.find((r) => {
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";
const isNPEDHitD = r?.metadata?.npedJSON?.["NPED CATEGORY"] === "D";
return isNPEDHitA || isNPEDHitB || isNPEDHitC || isNPEDHitD;
});
const firstHot = rows?.find((r) => {
const isHotListHit = checkIsHotListHit(r);
return isHotListHit;
2025-09-22 09:26:45 +01:00
});
if (firstNPED) {
setSelectedSighting(firstNPED);
console.log("first");
npedSound();
setSightingModalOpen(true);
npedRef.current = true;
}
if (firstHot) {
setSelectedSighting(firstHot);
hotlistsound();
setSightingModalOpen(true);
2025-09-22 09:26:45 +01:00
hasAutoOpenedRef.current = true;
}
}, [
hotlistsound,
npedSound,
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 ">
{isLoading && (
<div className="my-50 h-[50%]">
<Loading message="Loading Sightings" />
</div>
)}
2025-09-12 08:21:52 +01:00
{/* 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";
2025-09-12 08:21:52 +01:00
const motionAway = (obj?.motion ?? "").toUpperCase() === "AWAY";
const isHotListHit = checkIsHotListHit(obj);
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
className={`flex items-center gap-3 mt-2 justify-between `}
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
);
}