import { useCallback, useEffect, useMemo, useState } from "react"; import type { 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"; 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 { items, selectedRef, setSelectedRef } = useSightingFeedContext(); const onRowClick = useCallback( (ref: number) => { setSelectedRef(ref); }, [setSelectedRef] ); const rows = useMemo( () => items.filter(Boolean) as SightingWidgetType[], [items] ); return (
{/* Rows */}
{rows.map((obj, idx) => { const isSelected = obj?.ref === selectedRef; const motionAway = (obj?.motion ?? "").toUpperCase() === "AWAY"; const primaryIsColour = obj?.srcCam === 1; const secondaryMissing = (obj?.vrmSecondary ?? "") === ""; return (
onRowClick(obj.ref)} > {/* Info bar */}
CH: {obj ? obj.charHeight : "—"}
Seen: {obj ? obj.seenCount : "—"}
{obj ? capitalize(obj.motion) : "—"}
{obj ? formatAge(obj.timeStampMillis) : "—"}
{/* Patch row */}
infrared patch
colour patch
); })}
); }