import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
import { createPortal } from "react-dom";
import { useTranslation } from "react-i18next";
import { useQuery } from "@tanstack/react-query";
import { fetchLatestCameraSnapshot, fetchSnapshotObjectUrl, type DeviceStatus } from "../api.js";
import { formatClock } from "../lib/format.js";
import { useLiveStore } from "../lib/live-store.js";
import { wsUrl } from "../lib/origin.js";
import { createPlatformSocket } from "../lib/platform-ws.js";
// Booth camera tiles (wiki/decisions/camera-live-view.md). Opened from the camera
// chips in the device footer; docked bottom-right, the first opened rightmost and each
// next one to its left. A tile rests on the camera's newest entry/exit snapshot and
// switches to live video while that camera is seeing a vehicle (the server's per-camera
// activity, pushed over /api/ws). Clicking the picture opens a large view that streams
// LIVE ON DEMAND (car or no car) with a Live / Snapshot switch; closing it returns the
// tile to automatic — a stream the user started stops, one the camera started carries
// on. Live frames come over their own socket as JPEG binaries; only the newest frame is
// shown, and the tile and the large view share that one socket. Advisory viewing —
// nothing here gates anything.
type LiveState =
| { state: "connecting" }
| { state: "live" }
| { state: "retrying"; reason: string }
| { state: "unavailable"; reason: string };
/** Delay before re-opening a live socket that dropped while the camera is still active. */
const RECONNECT_MS = 2000;
export function CameraDock({
openIds,
cameras,
label,
canSeeSnapshots,
onClose,
}: {
openIds: string[];
/** Configured cameras (for labels, and to skip a tile whose camera was removed). */
cameras: DeviceStatus[];
label: (d: DeviceStatus) => string;
canSeeSnapshots: boolean;
onClose: (deviceId: string) => void;
}) {
const byId = new Map(cameras.map((c) => [c.deviceId, c]));
const shown = openIds.filter((id) => byId.has(id));
if (shown.length === 0) return null;
return (
// Above the footer; first opened rightmost (row-reverse), the next to its left.
{shown.map((id) => (
onClose(id)}
/>
))}
);
}
function CameraTile({
deviceId,
title,
canSeeSnapshots,
onClose,
}: {
deviceId: string;
title: string;
canSeeSnapshots: boolean;
onClose: () => void;
}) {
const { t } = useTranslation();
/** The camera is reporting a vehicle (server-side activity, 30 s after its last push). */
const active = useLiveStore((s) => s.activeCameras[deviceId] === true);
/** Large view open, and what the user picked in it (opens on "live"). */
const [expanded, setExpanded] = useState(false);
const [modalMode, setModalMode] = useState<"live" | "snapshot">("live");
// Modal open → the user's choice rules; closed → automatic (live while a vehicle is in view).
const wantLive = expanded ? modalMode === "live" : active;
const live = useLiveFrames(deviceId, wantLive);
const snapshot = useLatestSnapshot(deviceId, canSeeSnapshots);
const showLive = wantLive && live.hasFrame;
const caption = wantLive
? showLive
? active
? t("camera.vehicleInView")
: t("camera.liveOnDemand")
: live.status.state === "unavailable" || live.status.state === "retrying"
? `${t("camera.liveUnavailable")}: ${live.status.reason}`
: t("camera.connecting")
: snapshot.meta
? `${snapshot.meta.direction === "entry" ? t("pay.snapEntry") : t("pay.snapExit")} · ${formatClock(snapshot.meta.capturedAt)}`
: "";
const view = (slot: "tile" | "modal") => (
);
return (
{title}
{/* The picture is the expand control: a click opens the large view, live. */}
{expanded && (
setExpanded(false)}>
{view("modal")}
)}
);
}
/** The picture area shared by the tile and the large view: live frames (painted
* straight into the , no re-render per frame) or the resting snapshot. */
function CameraView({
imgRef,
showLive,
snapshotUrl,
placeholder,
caption,
large,
}: {
imgRef: (el: HTMLImageElement | null) => void;
showLive: boolean;
snapshotUrl: string | null;
placeholder: string;
caption: string;
large: boolean;
}) {
return (