import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { CROSSING_MIN_LENGTH, type CrossingDirection, type CrossingLine, type FramePoint, } from "@parking/shared"; import { useLiveFrames } from "./CameraDock.js"; // Draws a camera's crossing line over its live picture — our own version of a Hikvision // Line Crossing rule, for cameras that have none: a segment, sides A and B, and which // crossing counts (A→B, B→A, A↔B). Points are normalised to the frame, so the line holds // at any display size. Side A is the screen-left of p1→p2 (see crossingSide in shared). type Drag = { end: "p1" | "p2" } | null; const HANDLE_PX = 14; // grab radius around an end point export function CrossingLineEditor({ deviceId, value, onChange, }: { deviceId: string; value: CrossingLine | null; onChange: (next: CrossingLine | null) => void; }) { const { t } = useTranslation(); const live = useLiveFrames(deviceId, true); const surface = useRef(null); const [size, setSize] = useState({ w: 0, h: 0 }); const [drag, setDrag] = useState(null); // While drawing a new line it lives here, so a click-length line never reaches the form. const [draft, setDraft] = useState(null); const line = draft ?? value; // The overlay sits exactly on the picture (the wrapper is sized by the ), so its // rectangle IS the frame: no letterbox offset to correct for. useEffect(() => { const el = surface.current; if (!el) return; const ro = new ResizeObserver(([e]) => e && setSize({ w: e.contentRect.width, h: e.contentRect.height })); ro.observe(el); return () => ro.disconnect(); }, [live.hasFrame]); const toFrame = (ev: React.PointerEvent): FramePoint => { const r = surface.current!.getBoundingClientRect(); const clamp = (v: number) => Math.min(1, Math.max(0, v)); return { x: clamp((ev.clientX - r.left) / r.width), y: clamp((ev.clientY - r.top) / r.height) }; }; const px = (p: FramePoint) => ({ x: p.x * size.w, y: p.y * size.h }); function onDown(ev: React.PointerEvent) { if (!live.hasFrame) return; ev.currentTarget.setPointerCapture(ev.pointerId); const at = toFrame(ev); if (value) { for (const end of ["p1", "p2"] as const) { const e = px(value[end]); const a = px(at); if (Math.hypot(e.x - a.x, e.y - a.y) <= HANDLE_PX) { setDrag({ end }); return; } } } // Anywhere else: draw a new line from here, keeping the chosen direction. setDraft({ p1: at, p2: at, direction: value?.direction ?? "a-to-b" }); setDrag({ end: "p2" }); } function onMove(ev: React.PointerEvent) { if (!drag) return; const at = toFrame(ev); if (draft) setDraft({ ...draft, [drag.end]: at }); else if (value) onChange({ ...value, [drag.end]: at }); } function onUp() { if (draft && Math.hypot(draft.p2.x - draft.p1.x, draft.p2.y - draft.p1.y) >= CROSSING_MIN_LENGTH) { onChange(draft); } setDraft(null); setDrag(null); } const directions: [CrossingDirection, string][] = [ ["a-to-b", "A → B"], ["b-to-a", "B → A"], ["both", "A ↔ B"], ]; return (
{t("setup.crossingTitle")}

{t("setup.crossingHint")}

{!live.hasFrame && (
{live.status.state === "unavailable" || live.status.state === "retrying" ? live.status.reason : t("setup.crossingWaiting")}
)} {live.hasFrame && ( {line && size.w > 0 && } )}
{directions.map(([d, label]) => ( ))}
{!value &&

{t("setup.crossingNone")}

}
); } /** The segment, its end handles, the A/B labels either side, and an arrow across the * middle pointing the way that counts (double-headed for A↔B). Pixel space. */ function LineOverlay({ line, p1, p2 }: { line: CrossingLine; p1: FramePoint; p2: FramePoint }) { const dx = p2.x - p1.x; const dy = p2.y - p1.y; const len = Math.hypot(dx, dy) || 1; // Unit normal toward side A: the screen-left of p1→p2 (y down) is (dy, -dx). const nA = { x: dy / len, y: -dx / len }; const mid = { x: (p1.x + p2.x) / 2, y: (p1.y + p2.y) / 2 }; const at = (k: number) => ({ x: mid.x + nA.x * k, y: mid.y + nA.y * k }); const aSide = at(22); const bSide = at(-22); const labelA = at(40); const labelB = at(-40); // Arrow runs from the side crossings start on to the side they end on. const [from, to] = line.direction === "b-to-a" ? [bSide, aSide] : [aSide, bSide]; const both = line.direction === "both"; const stroke = "rgb(250 204 21)"; // amber, readable over daylight and night pictures return ( {[p1, p2].map((p, i) => ( ))} {( [ ["A", labelA], ["B", labelB], ] as const ).map(([txt, p]) => ( {txt} ))} ); }