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

142 lines
4.9 KiB
TypeScript
Raw Normal View History

2025-08-20 08:27:05 +01:00
import { useCallback, useEffect, useMemo, useState } from "react";
2025-09-12 08:21:52 +01:00
import type { SightingType, SightingWidgetType } from "../../types/types";
2025-08-20 08:27:05 +01:00
import { BLANK_IMG, capitalize, formatAge } from "../../utils/utils";
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-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]);
return undefined;
}
export type SightingHistoryProps = {
baseUrl: string;
entries?: number; // number of rows to show
pollMs?: number; // poll frequency
autoSelectLatest?: boolean;
};
type SightingHistoryWidgetProps = React.HTMLAttributes<HTMLDivElement>;
export default function SightingHistoryWidget({
className,
}: SightingHistoryWidgetProps) {
useNow(1000);
2025-09-12 08:21:52 +01:00
const {
sightings,
setSelectedSighting,
setSightingModalOpen,
isSightingModalOpen,
selectedSighting,
} = useSightingFeedContext();
2025-08-20 08:27:05 +01:00
const onRowClick = useCallback(
2025-09-12 08:21:52 +01:00
(sighting: SightingType) => {
if (!sighting) return;
setSightingModalOpen(!isSightingModalOpen);
setSelectedSighting(sighting);
2025-08-20 08:27:05 +01:00
},
2025-09-12 08:21:52 +01:00
[isSightingModalOpen, setSelectedSighting, setSightingModalOpen]
2025-08-20 08:27:05 +01:00
);
const rows = useMemo(
2025-08-22 10:38:28 +01:00
() => sightings?.filter(Boolean) as SightingWidgetType[],
[sightings]
2025-08-20 08:27:05 +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)}>
<CardHeader title="Front Camera Sightings" />
<div className="flex flex-col gap-3 ">
{/* Rows */}
<div className="flex flex-col">
{rows?.map((obj, idx) => {
const isNPEDHit = obj?.metadata?.npedJSON?.status_code === 201;
const motionAway = (obj?.motion ?? "").toUpperCase() === "AWAY";
const primaryIsColour = obj?.srcCam === 1;
const secondaryMissing = (obj?.vrmSecondary ?? "") === "";
console.log(obj);
return (
2025-08-29 14:55:37 +01:00
<div
2025-09-12 08:21:52 +01:00
key={idx}
className={`border border-neutral-700 rounded-md mb-2 p-2 cursor-pointer`}
onClick={() => onRowClick(obj)}
2025-08-29 14:55:37 +01:00
>
2025-09-12 08:21:52 +01:00
{/* Info bar */}
<div className="flex items-center gap-3 text-xs bg-neutral-900 px-2 py-1 rounded">
<div className="min-w-14">
CH: {obj ? obj.charHeight : "—"}
</div>
<div className="min-w-14">
Seen: {obj ? obj.seenCount : "—"}
</div>
<div className="min-w-20">
{obj ? capitalize(obj.motion) : "—"}
</div>
<div className="min-w-14 opacity-80">
{obj ? formatAge(obj.timeStampMillis) : "—"}
</div>
2025-08-20 08:27:05 +01:00
</div>
2025-09-12 08:21:52 +01:00
{/* Patch row */}
2025-08-20 08:27:05 +01:00
<div
2025-09-12 08:21:52 +01:00
className={`flex items-center gap-3 mt-2
${isNPEDHit ? "border border-red-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"
} ${!obj ? "opacity-30" : ""}`}
>
<img
src={obj?.plateUrlInfrared || BLANK_IMG}
height={48}
alt="infrared patch"
className={!primaryIsColour ? "" : "opacity-60"}
/>
</div>
<div
className={`border p-1 ${
primaryIsColour ? "ring-2 ring-lime-400" : ""
} ${
secondaryMissing && primaryIsColour
? "opacity-30 grayscale"
: ""
}`}
>
<img
src={obj?.plateUrlColour || BLANK_IMG}
height={48}
alt="colour patch"
className={primaryIsColour ? "" : "opacity-60"}
/>
</div>
<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
);
}