import { useCallback, useEffect, useMemo, useState } from "react"; import type { SightingType, SightingWidgetType } from "../../types/types"; 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"; import SightingModal from "../SightingModal/SightingModal"; 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; export default function SightingHistoryWidget({ className, }: SightingHistoryWidgetProps) { useNow(1000); const { sightings, setSelectedSighting, setSightingModalOpen, isSightingModalOpen, selectedSighting, } = useSightingFeedContext(); const onRowClick = useCallback( (sighting: SightingType) => { if (!sighting) return; setSightingModalOpen(!isSightingModalOpen); setSelectedSighting(sighting); }, [isSightingModalOpen, setSelectedSighting, setSightingModalOpen] ); const rows = useMemo( () => sightings?.filter(Boolean) as SightingWidgetType[], [sightings] ); const handleClose = () => { setSightingModalOpen(false); }; return ( <>
{/* Rows */}
{rows?.map((obj, idx) => { const isNPEDHit = obj?.metadata?.npedJSON?.status_code === 404; const motionAway = (obj?.motion ?? "").toUpperCase() === "AWAY"; const primaryIsColour = obj?.srcCam === 1; const secondaryMissing = (obj?.vrmSecondary ?? "") === ""; return (
onRowClick(obj)} > {/* Info bar */}
CH: {obj ? obj.charHeight : "—"}
Seen: {obj ? obj.seenCount : "—"}
{obj ? capitalize(obj.motion) : "—"}
{obj ? formatAge(obj.timeStampMillis) : "—"}
{/* Patch row */}
infrared patch
colour patch
); })}
); }