diff --git a/apps/web/src/lib/i18n/en.ts b/apps/web/src/lib/i18n/en.ts index c032c89..cdbe555 100644 --- a/apps/web/src/lib/i18n/en.ts +++ b/apps/web/src/lib/i18n/en.ts @@ -270,6 +270,9 @@ export const en: Catalog = { checkedAt: "checked {{time}}", }, camera: { + expand: "Show larger", + snapshot: "Snapshot", + liveOnDemand: "Live on request", live: "Live", showTile: "Show this camera", hideTile: "Hide this camera", diff --git a/apps/web/src/lib/i18n/sq.ts b/apps/web/src/lib/i18n/sq.ts index f81d0b0..60b4431 100644 --- a/apps/web/src/lib/i18n/sq.ts +++ b/apps/web/src/lib/i18n/sq.ts @@ -272,6 +272,9 @@ export const sq = { checkedAt: "kontrolluar {{time}}", }, camera: { + expand: "Shfaq më të madhe", + snapshot: "Foto", + liveOnDemand: "Live me kërkesë", live: "Live", showTile: "Shfaq këtë kamerë", hideTile: "Fshih këtë kamerë", diff --git a/apps/web/src/ui/CameraDock.tsx b/apps/web/src/ui/CameraDock.tsx index afb8896..418a828 100644 --- a/apps/web/src/ui/CameraDock.tsx +++ b/apps/web/src/ui/CameraDock.tsx @@ -1,4 +1,5 @@ -import { useEffect, useRef, useState } from "react"; +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"; @@ -11,9 +12,12 @@ import { createPlatformSocket } from "../lib/platform-ws.js"; // 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). Live frames come over their own socket as JPEG -// binaries; only the newest frame is shown. Advisory viewing — nothing here gates -// anything. +// 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" } @@ -69,20 +73,42 @@ function CameraTile({ 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); - const live = useLiveFrames(deviceId, active); + /** 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 = active && live.hasFrame; + 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 (
- {showLive ? ( - - - {t("camera.live")} - - ) : null} {title}
-
- {/* Live frames are painted straight into this (no re-render per frame). */} - - {!showLive && snapshot.url && ( - - )} - {!showLive && !snapshot.url && ( -
- {canSeeSnapshots ? t("camera.noSnapshot") : t("camera.idle")} -
- )} -
- {active && !showLive - ? live.status.state === "unavailable" || live.status.state === "retrying" - ? `${t("camera.liveUnavailable")}: ${live.status.reason}` - : t("camera.connecting") - : !showLive && snapshot.meta - ? `${snapshot.meta.direction === "entry" ? t("pay.snapEntry") : t("pay.snapExit")} · ${formatClock(snapshot.meta.capturedAt)}` - : showLive - ? t("camera.vehicleInView") - : ""} -
-
+ {/* 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 ( +
+ + {!showLive && snapshotUrl && ( + + )} + {!showLive && !snapshotUrl && ( +
+ {placeholder} +
+ )} + {showLive && } + {caption && ( +
+ {caption} +
+ )} +
+ ); +} + +/** "● LIVE" with a pulsing green dot, over the video's top-left corner. */ +function LiveBadge({ large }: { large: boolean }) { + const { t } = useTranslation(); + return ( + + + + + + {t("camera.live")} + + ); +} + +/** Large view of one camera with a Live / Snapshot switch. Closes on ✕, Escape or a + * backdrop click. */ +function CameraModal({ + title, + mode, + onMode, + onClose, + children, +}: { + title: string; + mode: "live" | "snapshot"; + onMode: (m: "live" | "snapshot") => void; + onClose: () => void; + children: ReactNode; +}) { + const { t } = useTranslation(); + useEffect(() => { + const onKey = (e: KeyboardEvent) => e.key === "Escape" && onClose(); + document.addEventListener("keydown", onKey); + return () => document.removeEventListener("keydown", onKey); + }, [onClose]); + const seg = (m: "live" | "snapshot", label: string) => ( + + ); + // Portalled to : the dock is a fixed, z-indexed, pointer-events-none layer that + // would otherwise trap the modal inside its stacking context. + return createPortal( +
+
e.stopPropagation()} + > +
+ {title} +
+ {seg("live", t("camera.live"))} + {seg("snapshot", t("camera.snapshot"))} +
+ +
+ {children} +
+
, + document.body, + ); +} + /** Newest snapshot for the tile's resting picture, as an object URL (revoked on change). */ function useLatestSnapshot(deviceId: string, enabled: boolean) { const meta = useQuery({ @@ -160,13 +307,27 @@ function useLatestSnapshot(deviceId: string, enabled: boolean) { } /** - * While `active`, hold a live socket to the camera and paint each JPEG into `imgRef`. + * While `active` (wanted: camera activity, or the user's choice in the large view), hold a live socket to the camera and paint each JPEG into `imgRef`. * Closed (and the last frame dropped) as soon as the camera goes idle or the tile * unmounts; reconnects after a drop while still active. The server shares one camera * session between every viewer and skips frames for a slow one. */ function useLiveFrames(deviceId: string, active: boolean) { - const imgRef = useRef(null); + // Every showing this camera (the tile, and the large view while open) gets each + // frame; a view that mounts between frames is given the newest one at once. + const imgs = useRef(new Map()); + const latest = useRef(null); + const imgRefs = useMemo(() => { + const slot = (name: string) => (el: HTMLImageElement | null) => { + if (el) { + imgs.current.set(name, el); + if (latest.current) el.src = latest.current; + } else { + imgs.current.delete(name); + } + }; + return { tile: slot("tile"), modal: slot("modal") }; + }, []); const [status, setStatus] = useState({ state: "connecting" }); const [hasFrame, setHasFrame] = useState(false); @@ -182,12 +343,12 @@ function useLiveFrames(deviceId: string, active: boolean) { let sock: ReturnType | null = null; const paint = (buf: ArrayBuffer) => { - const img = imgRef.current; - if (closed || !img) return; // a frame landing after cleanup must not resurrect the view + if (closed) return; // a frame landing after cleanup must not resurrect the view const next = URL.createObjectURL(new Blob([buf], { type: "image/jpeg" })); const prev = currentUrl; currentUrl = next; - img.src = next; + latest.current = next; + for (const img of imgs.current.values()) img.src = next; if (prev) URL.revokeObjectURL(prev); setHasFrame(true); }; @@ -222,9 +383,10 @@ function useLiveFrames(deviceId: string, active: boolean) { if (retry) window.clearTimeout(retry); sock?.close(); if (currentUrl) URL.revokeObjectURL(currentUrl); - if (imgRef.current) imgRef.current.removeAttribute("src"); + latest.current = null; + for (const img of imgs.current.values()) img.removeAttribute("src"); }; }, [deviceId, active]); - return { imgRef, status, hasFrame }; + return { imgRefs, status, hasFrame }; }