@sentinel-nvr/web 0.19.1 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ui/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  PlayerController,
3
3
  rlog
4
- } from "../chunk-7W5ORHOB.js";
4
+ } from "../chunk-ZC64YDB2.js";
5
5
  import {
6
6
  SENTINEL_CLASS_PRIORITY,
7
7
  SENTINEL_DAY_MS,
@@ -508,17 +508,59 @@ function EventsStrip({ events, icon }) {
508
508
  }
509
509
 
510
510
  // src/ui/components/CameraGrid.tsx
511
- import { useEffect, useState } from "react";
511
+ import { useCallback, useEffect, useState } from "react";
512
512
  import { Camera, VideoOff, WifiOff } from "lucide-react";
513
513
 
514
514
  // src/ui/snapshot-cache.ts
515
515
  var last = /* @__PURE__ */ new Map();
516
+ var copies = /* @__PURE__ */ new Map();
516
517
  function rememberTileSnapshot(camId, snap) {
517
518
  last.set(camId, snap);
519
+ if (typeof snap === "string") copies.delete(camId);
520
+ else keepCopy(camId, snap);
518
521
  }
519
522
  function lastTileSnapshot(camId) {
520
523
  return last.get(camId);
521
524
  }
525
+ function loaded(img) {
526
+ return img.complete && img.naturalWidth > 0;
527
+ }
528
+ function paint(canvas, src, w, h) {
529
+ try {
530
+ const ctx = canvas.getContext("2d");
531
+ if (!ctx) return false;
532
+ canvas.width = w;
533
+ canvas.height = h;
534
+ ctx.drawImage(src, 0, 0);
535
+ return true;
536
+ } catch {
537
+ return false;
538
+ }
539
+ }
540
+ function keepCopy(camId, img) {
541
+ const copy = () => {
542
+ if (last.get(camId) !== img || !loaded(img)) return;
543
+ const canvas = copies.get(camId) ?? document.createElement("canvas");
544
+ if (paint(canvas, img, img.naturalWidth, img.naturalHeight)) copies.set(camId, canvas);
545
+ };
546
+ if (typeof img.decode === "function") img.decode().then(copy, () => {
547
+ });
548
+ else copy();
549
+ }
550
+ function drawable(camId) {
551
+ const snap = last.get(camId);
552
+ if (snap && typeof snap !== "string" && loaded(snap))
553
+ return { src: snap, w: snap.naturalWidth, h: snap.naturalHeight };
554
+ const copy = copies.get(camId);
555
+ return copy ? { src: copy, w: copy.width, h: copy.height } : null;
556
+ }
557
+ function hasLastTileSnapshot(camId) {
558
+ return drawable(camId) !== null;
559
+ }
560
+ function drawLastTileSnapshot(camId, canvas) {
561
+ const pic = drawable(camId);
562
+ return pic ? paint(canvas, pic.src, pic.w, pic.h) : false;
563
+ }
522
564
 
523
565
  // src/ui/components/CameraGrid.tsx
524
566
  import { jsx as jsx4, jsxs as jsxs2 } from "react/jsx-runtime";
@@ -530,6 +572,11 @@ var RETRY_EVERY = 6;
530
572
  function CameraTile({ cam, tick, n }) {
531
573
  const { client, t, nav } = useSentinelUi();
532
574
  const [failed, setFailed] = useState(0);
575
+ const [last2, setLast] = useState(() => hasLastTileSnapshot(cam.id));
576
+ const drawLast = useCallback(
577
+ (canvas) => void (canvas && drawLastTileSnapshot(cam.id, canvas)),
578
+ [cam.id]
579
+ );
533
580
  useEffect(() => {
534
581
  setFailed(0);
535
582
  }, [client, cam.id]);
@@ -547,6 +594,7 @@ function CameraTile({ cam, tick, n }) {
547
594
  "div",
548
595
  {
549
596
  className: `nvr-card nvr-camtile${rec === "offline" || rec === "stalled" ? " nvr-camtile--badged" : ""}`,
597
+ "data-nvr-camera": cam.id,
550
598
  role: "button",
551
599
  tabIndex: 0,
552
600
  onClick: open,
@@ -558,17 +606,30 @@ function CameraTile({ cam, tick, n }) {
558
606
  },
559
607
  "aria-label": cam.name,
560
608
  children: [
561
- src ? /* @__PURE__ */ jsx4(
562
- "img",
563
- {
564
- className: "nvr-camtile__img",
565
- src,
566
- alt: "",
567
- crossOrigin: "anonymous",
568
- onLoad: (e) => rememberTileSnapshot(cam.id, e.currentTarget),
569
- onError: () => setFailed((f) => Math.min(2, f + 1))
570
- }
571
- ) : /* @__PURE__ */ jsx4("div", { className: "nvr-camtile__placeholder", "aria-hidden": "true", children: /* @__PURE__ */ jsx4(Camera, { size: 32, strokeWidth: 1.5 }) }),
609
+ src ? /* @__PURE__ */ jsxs2("span", { className: "nvr-camtile__pic", children: [
610
+ last2 && /* @__PURE__ */ jsx4("canvas", { className: "nvr-camtile__img nvr-camtile__img--last", ref: drawLast, "aria-hidden": "true" }),
611
+ /* @__PURE__ */ jsx4(
612
+ "img",
613
+ {
614
+ className: "nvr-camtile__img",
615
+ src,
616
+ alt: "",
617
+ crossOrigin: "anonymous",
618
+ onLoad: (e) => {
619
+ const img = e.currentTarget;
620
+ rememberTileSnapshot(cam.id, img);
621
+ if (!last2) return;
622
+ const drop = () => setLast(false);
623
+ if (typeof img.decode === "function") img.decode().then(drop, drop);
624
+ else drop();
625
+ },
626
+ onError: () => {
627
+ if (failed > 0 || !cam.latestThumbId) setLast(false);
628
+ setFailed((f) => Math.min(2, f + 1));
629
+ }
630
+ }
631
+ )
632
+ ] }) : /* @__PURE__ */ jsx4("div", { className: "nvr-camtile__placeholder", "aria-hidden": "true", children: /* @__PURE__ */ jsx4(Camera, { size: 32, strokeWidth: 1.5 }) }),
572
633
  /* @__PURE__ */ jsxs2("span", { className: "nvr-camtile__name", children: [
573
634
  cam.name,
574
635
  cam.recording && /* @__PURE__ */ jsx4("i", { className: `nvr-camtile__dot${rec === "ok" ? "" : " nvr-camtile__dot--off"}`, "aria-hidden": "true" })
@@ -1515,7 +1576,7 @@ function VerticalTimeline(p) {
1515
1576
  }
1516
1577
 
1517
1578
  // src/ui/components/CameraPage.tsx
1518
- import { useCallback as useCallback2, useEffect as useEffect4, useMemo as useMemo2, useRef as useRef3, useState as useState5 } from "react";
1579
+ import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo2, useRef as useRef3, useState as useState5 } from "react";
1519
1580
  import {
1520
1581
  Calendar,
1521
1582
  Camera as Camera2,
@@ -1694,7 +1755,7 @@ function clipErrorKind(status, code) {
1694
1755
  }
1695
1756
 
1696
1757
  // src/ui/components/ClipBar.tsx
1697
- import { useCallback, useEffect as useEffect3, useRef as useRef2, useState as useState4 } from "react";
1758
+ import { useCallback as useCallback2, useEffect as useEffect3, useRef as useRef2, useState as useState4 } from "react";
1698
1759
  import { Share2, X } from "lucide-react";
1699
1760
  import { Fragment as Fragment7, jsx as jsx9, jsxs as jsxs7 } from "react/jsx-runtime";
1700
1761
  function canShareFiles() {
@@ -1719,7 +1780,7 @@ function ClipBar(p) {
1719
1780
  const [edges, setEdges] = useState4({ cur: p.edge, was: null });
1720
1781
  if (edges.cur !== p.edge) setEdges({ cur: p.edge, was: edges.cur });
1721
1782
  const jobRef = useRef2(job);
1722
- const setJob = useCallback((j) => {
1783
+ const setJob = useCallback2((j) => {
1723
1784
  jobRef.current = j;
1724
1785
  setJobState(j);
1725
1786
  }, []);
@@ -1727,13 +1788,13 @@ function ClipBar(p) {
1727
1788
  const alive = useRef2(true);
1728
1789
  const abort = useRef2(null);
1729
1790
  const standalone = isIosHomeScreenApp();
1730
- const release = useCallback(() => {
1791
+ const release = useCallback2(() => {
1731
1792
  abort.current?.abort();
1732
1793
  abort.current = null;
1733
1794
  const j = jobRef.current;
1734
1795
  if (j.s === "ready" && j.blobUrl) URL.revokeObjectURL(j.blobUrl);
1735
1796
  }, []);
1736
- const cancelRunning = useCallback(() => {
1797
+ const cancelRunning = useCallback2(() => {
1737
1798
  const j = jobRef.current;
1738
1799
  if (j.s === "running") void client.cancelExport(j.id);
1739
1800
  }, [client]);
@@ -1757,14 +1818,14 @@ function ClipBar(p) {
1757
1818
  release();
1758
1819
  setJob({ s: "idle" });
1759
1820
  }, [rangeKey, cancelRunning, release, setJob]);
1760
- const fail = useCallback(
1821
+ const fail = useCallback2(
1761
1822
  (kind, vars, extra) => {
1762
1823
  rlog("clip", { ok: false, err: kind, standalone, ...extra });
1763
1824
  setJob(vars ? { s: "error", kind, vars } : { s: "error", kind });
1764
1825
  },
1765
1826
  [setJob, standalone]
1766
1827
  );
1767
- const errorOf = useCallback(
1828
+ const errorOf = useCallback2(
1768
1829
  (e) => {
1769
1830
  const st2 = e instanceof SentinelHttpError ? e.status : 0;
1770
1831
  const code = e instanceof SentinelHttpError ? e.code : void 0;
@@ -1781,7 +1842,7 @@ function ClipBar(p) {
1781
1842
  },
1782
1843
  [fail, locale]
1783
1844
  );
1784
- const finish = useCallback(
1845
+ const finish = useCallback2(
1785
1846
  async (g, d) => {
1786
1847
  const crossOrigin = isCrossOrigin(client.exportFileUrl(d.id));
1787
1848
  const ways = clipWays({ bytes: d.bytes, canShareFiles: canShareFiles(), standalone, crossOrigin });
@@ -1895,7 +1956,7 @@ function ClipBar(p) {
1895
1956
  );
1896
1957
  return () => window.clearTimeout(i);
1897
1958
  }, [expiresAt, release, setJob]);
1898
- const create = useCallback(async () => {
1959
+ const create = useCallback2(async () => {
1899
1960
  const j = jobRef.current;
1900
1961
  if (j.s === "starting" || j.s === "running" || j.s === "loading") return;
1901
1962
  gen.current++;
@@ -1916,7 +1977,7 @@ function ClipBar(p) {
1916
1977
  errorOf(e);
1917
1978
  }
1918
1979
  }, [client, p, release, setJob, errorOf]);
1919
- const cancel = useCallback(() => {
1980
+ const cancel = useCallback2(() => {
1920
1981
  cancelRunning();
1921
1982
  gen.current++;
1922
1983
  release();
@@ -2169,7 +2230,7 @@ function CameraPage(p) {
2169
2230
  const i = setTimeout(() => setPipNote(null), 15e3);
2170
2231
  return () => clearTimeout(i);
2171
2232
  }, [pipNote]);
2172
- const onPip = useCallback2(() => {
2233
+ const onPip = useCallback3(() => {
2173
2234
  const c = ctl.current;
2174
2235
  if (!c) return;
2175
2236
  void c.pip().then((r) => {
@@ -2205,7 +2266,7 @@ function CameraPage(p) {
2205
2266
  const rangeEnd = Math.min(sentinelDayEnd(todayStart()), nowTick + 20 * 6e4);
2206
2267
  return { ...m, rangeStart, rangeEnd };
2207
2268
  }, [days, nowTick]);
2208
- const mergedNow = useCallback2(() => {
2269
+ const mergedNow = useCallback3(() => {
2209
2270
  const m = sentinelMergeDays(daysRef.current);
2210
2271
  return {
2211
2272
  ...m,
@@ -2216,7 +2277,8 @@ function CameraPage(p) {
2216
2277
  useEffect4(() => {
2217
2278
  ctl.current?.setClips(merged.clips, merged.codecs, merged.rangeStart, merged.rangeEnd);
2218
2279
  }, [merged]);
2219
- const fetchDay = useCallback2(
2280
+ const frameServer = useCallback3(() => Object.values(daysRef.current).some((d) => sentinelHasFeature(d, "frame")), []);
2281
+ const fetchDay = useCallback3(
2220
2282
  async (ds) => {
2221
2283
  const d = await client.getJson(
2222
2284
  `api/clips?camera=${encodeURIComponent(camId)}&start=${ds}&end=${sentinelDayEnd(ds)}`
@@ -2228,7 +2290,7 @@ function CameraPage(p) {
2228
2290
  },
2229
2291
  [client, camId]
2230
2292
  );
2231
- const ensureDay = useCallback2(
2293
+ const ensureDay = useCallback3(
2232
2294
  async (ds, force = false) => {
2233
2295
  if (ds > todayStart()) return;
2234
2296
  if (earliest && sentinelAddDays(ds, 1) < sentinelDayOf(earliest)) return;
@@ -2246,7 +2308,7 @@ function CameraPage(p) {
2246
2308
  },
2247
2309
  [fetchDay, earliest, camId]
2248
2310
  );
2249
- const onCenter = useCallback2(
2311
+ const onCenter = useCallback3(
2250
2312
  (ts) => {
2251
2313
  lineTs.current = ts;
2252
2314
  const d = sentinelDayOf(ts);
@@ -2299,16 +2361,20 @@ function CameraPage(p) {
2299
2361
  setClipEdge(null);
2300
2362
  setLoadError(false);
2301
2363
  loading.current.clear();
2302
- if (startAt) {
2303
- if (posterTs) c.posterEvent(posterTs);
2304
- } else {
2364
+ if (!startAt) {
2305
2365
  c.posterFromSnapshot(lastTileSnapshot(camId));
2306
2366
  c.goLive();
2307
2367
  }
2308
2368
  const t0 = todayStart();
2309
2369
  const target = startAt ? sentinelDayOf(startAt) : t0;
2370
+ const first = ensureDay(target);
2371
+ if (startAt && posterTs)
2372
+ first.then(() => {
2373
+ if (c.camId === camId && ctl.current === c) c.posterEvent(posterTs, frameServer() ? startAt : void 0);
2374
+ }).catch(() => {
2375
+ });
2310
2376
  Promise.all([
2311
- ensureDay(target),
2377
+ first,
2312
2378
  ensureDay(sentinelAddDays(target, -1)),
2313
2379
  target !== t0 ? ensureDay(t0) : Promise.resolve(),
2314
2380
  target === t0 ? Promise.resolve() : ensureDay(sentinelAddDays(target, 1))
@@ -2334,7 +2400,7 @@ function CameraPage(p) {
2334
2400
  deepRef.current = key;
2335
2401
  const c = ctl.current;
2336
2402
  c.freezeCurrent();
2337
- if (posterTs) c.posterEvent(posterTs);
2403
+ if (posterTs) c.posterEvent(posterTs, frameServer() ? startAt : void 0);
2338
2404
  goToDay(sentinelDayOf(startAt), startAt).catch(() => setLoadError(true));
2339
2405
  }, [startAt, posterTs]);
2340
2406
  useEffect4(() => {
@@ -2364,19 +2430,23 @@ function CameraPage(p) {
2364
2430
  [merged.events, filterOff]
2365
2431
  );
2366
2432
  const dayCount = useMemo2(() => eventsOnDay(visEvents, centerDay), [visEvents, centerDay]);
2367
- const goLive = useCallback2(() => {
2433
+ const goLive = useCallback3(() => {
2368
2434
  ctl.current?.goLive();
2369
2435
  setJump({ ts: Date.now(), n: Date.now() });
2370
2436
  }, []);
2371
- const playEvent = useCallback2((ev) => {
2372
- const c = ctl.current;
2373
- if (!c) return;
2374
- setTab("tl");
2375
- c.freezeCurrent();
2376
- c.posterEvent(ev.timestamp);
2377
- c.playAt(sentinelEventPlayTs(ev), {});
2378
- }, []);
2379
- const seekTo = useCallback2(
2437
+ const playEvent = useCallback3(
2438
+ (ev) => {
2439
+ const c = ctl.current;
2440
+ if (!c) return;
2441
+ setTab("tl");
2442
+ c.freezeCurrent();
2443
+ const start = sentinelEventPlayTs(ev);
2444
+ c.posterEvent(ev.timestamp, frameServer() ? start : void 0);
2445
+ c.playAt(start, {});
2446
+ },
2447
+ [frameServer]
2448
+ );
2449
+ const seekTo = useCallback3(
2380
2450
  (ts) => {
2381
2451
  const c = ctl.current;
2382
2452
  if (!c) return;
@@ -2389,7 +2459,7 @@ function CameraPage(p) {
2389
2459
  },
2390
2460
  [goLive]
2391
2461
  );
2392
- const jumpEvent = useCallback2(
2462
+ const jumpEvent = useCallback3(
2393
2463
  (dir) => {
2394
2464
  const c = ctl.current;
2395
2465
  if (!c) return;
@@ -2409,7 +2479,7 @@ function CameraPage(p) {
2409
2479
  },
2410
2480
  [visEvents, playEvent]
2411
2481
  );
2412
- const goToDay = useCallback2(
2482
+ const goToDay = useCallback3(
2413
2483
  async (ds, at2) => {
2414
2484
  const c = ctl.current;
2415
2485
  if (!c) return;
@@ -2437,7 +2507,7 @@ function CameraPage(p) {
2437
2507
  );
2438
2508
  const canExport = useMemo2(() => Object.values(days).some((d) => sentinelHasFeature(d, "export")), [days]);
2439
2509
  const oldestRec = earliest ?? merged.clips[0]?.startTime;
2440
- const clipEvent = useCallback2(
2510
+ const clipEvent = useCallback3(
2441
2511
  (ev) => {
2442
2512
  const now = Date.now();
2443
2513
  const r = clipRangeForEvent(ev, now);
@@ -2447,16 +2517,17 @@ function CameraPage(p) {
2447
2517
  const c = ctl.current;
2448
2518
  if (!c) return;
2449
2519
  c.freezeCurrent();
2450
- c.posterEvent(ev.timestamp);
2451
- c.playAt(Math.max(r.from, oldestRec ?? -Infinity), {});
2520
+ const start = Math.max(r.from, oldestRec ?? -Infinity);
2521
+ c.posterEvent(ev.timestamp, frameServer() ? start : void 0);
2522
+ c.playAt(start, {});
2452
2523
  },
2453
- [oldestRec]
2524
+ [oldestRec, frameServer]
2454
2525
  );
2455
- const closeClip = useCallback2(() => {
2526
+ const closeClip = useCallback3(() => {
2456
2527
  setClip(null);
2457
2528
  setClipEdge(null);
2458
2529
  }, []);
2459
- const toggleClip = useCallback2(() => {
2530
+ const toggleClip = useCallback3(() => {
2460
2531
  if (clipRef.current) {
2461
2532
  closeClip();
2462
2533
  return;
@@ -2474,7 +2545,7 @@ function CameraPage(p) {
2474
2545
  setClipEdge(null);
2475
2546
  setTab("tl");
2476
2547
  }, [closeClip, visEvents, oldestRec]);
2477
- const activateEdge = useCallback2((e) => {
2548
+ const activateEdge = useCallback3((e) => {
2478
2549
  setClipEdge(e);
2479
2550
  const r = clipRef.current;
2480
2551
  if (!e || !r) return;
@@ -2487,7 +2558,7 @@ function CameraPage(p) {
2487
2558
  c.playAt(ts, {});
2488
2559
  }
2489
2560
  }, []);
2490
- const onUserCenter = useCallback2((ts) => {
2561
+ const onUserCenter = useCallback3((ts) => {
2491
2562
  lineTs.current = ts;
2492
2563
  const e = clipEdgeRef.current, r = clipRef.current;
2493
2564
  if (!e || !r) return;
@@ -2495,7 +2566,7 @@ function CameraPage(p) {
2495
2566
  setClip({ ...n.range, open: false });
2496
2567
  if (n.edge !== e) setClipEdge(n.edge);
2497
2568
  }, []);
2498
- const setEdgeToLine = useCallback2((e) => {
2569
+ const setEdgeToLine = useCallback3((e) => {
2499
2570
  const r = clipRef.current;
2500
2571
  if (!r) return;
2501
2572
  const s = psRef.current;
@@ -2505,7 +2576,7 @@ function CameraPage(p) {
2505
2576
  setClip({ ...n.range, open: false });
2506
2577
  if (clipEdgeRef.current) setClipEdge(n.edge);
2507
2578
  }, []);
2508
- const onTimelineEvent = useCallback2(
2579
+ const onTimelineEvent = useCallback3(
2509
2580
  (ev) => clipRef.current ? clipEvent(ev) : playEvent(ev),
2510
2581
  [clipEvent, playEvent]
2511
2582
  );
@@ -3083,9 +3154,11 @@ export {
3083
3154
  classLabel,
3084
3155
  createT,
3085
3156
  dateChipNav,
3157
+ drawLastTileSnapshot,
3086
3158
  eventLabel,
3087
3159
  fmtDays,
3088
3160
  fmtRelative,
3161
+ hasLastTileSnapshot,
3089
3162
  hexHue,
3090
3163
  humanBytes,
3091
3164
  interpolate,