From ce5b26dc11e5d539971eafdf2921de413e6c16b2 Mon Sep 17 00:00:00 2001
From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com>
Date: Fri, 20 Nov 2026 09:56:01 -0600
Subject: [PATCH] Add summary timeline to Motion Review and History (#25034)
* add motion summary timeline to motion review and history
The summary strip draws motion as a canvas layer, with each pixel row's opacity set by the mean motion value over its time, so the full day is visible next to the 30s timeline. Dragging now converts strip pixels to timeline pixels directly instead of going through the drag box height, which no longer matches the box once it has borders. The strip is wider on mobile, and the drag box style is shared with the alerts and detections strips.
* hide summary timeline when motion only switch is on
* drag summary timeline from anywhere and keep a minimum box size
The drag box shrank to a few pixels when the timeline was zoomed in, and tapping the summary timeline on mobile read touches[0] on touchend, where the list is always empty. The box now has a minimum height centered on the visible range, and input goes through pointer events: a tap outside the box jumps there, and a press outside the box that moves snaps the box under the pointer and drags it.
---
.../components/timeline/SummarySegment.tsx | 2 +-
.../components/timeline/SummaryTimeline.tsx | 366 ++++++++++--------
web/src/views/events/EventView.tsx | 83 ++--
web/src/views/recording/RecordingView.tsx | 76 ++--
4 files changed, 313 insertions(+), 214 deletions(-)
diff --git a/web/src/components/timeline/SummarySegment.tsx b/web/src/components/timeline/SummarySegment.tsx
index 4945e9b5d1..78b9f7cd37 100644
--- a/web/src/components/timeline/SummarySegment.tsx
+++ b/web/src/components/timeline/SummarySegment.tsx
@@ -28,7 +28,7 @@ export function SummarySegment({
;
timelineStart: number;
timelineEnd: number;
segmentDuration: number;
- events: ReviewSegment[];
- severityType: ReviewSeverity;
+ events?: ReviewSegment[];
+ severityType?: ReviewSeverity;
+ motionData?: MotionData[];
+ motionScale?: number;
};
export function SummaryTimeline({
@@ -27,16 +35,22 @@ export function SummaryTimeline({
timelineStart,
timelineEnd,
segmentDuration,
- events,
+ events = [],
severityType,
+ motionData,
+ motionScale,
}: SummaryTimelineProps) {
const summaryTimelineRef = useRef
(null);
const visibleSectionRef = useRef(null);
+ const motionCanvasRef = useRef(null);
const [isDragging, setIsDragging] = useState(false);
- const [scrollStartPosition, setScrollStartPosition] = useState(0);
- const [initialReviewTimelineScrollTop, setInitialReviewTimelineScrollTop] =
- useState(0);
+ const dragRef = useRef<{
+ startY: number;
+ startScrollTop: number;
+ insideBox: boolean;
+ moved: boolean;
+ } | null>(null);
const observer = useRef(null);
@@ -129,6 +143,79 @@ export function SummaryTimeline({
segmentDuration,
]);
+ // Each motion sample covers motionScale seconds with a 0-100 value, and
+ // bins without motion are absent. Opacity is the bucket's mean value.
+
+ const drawMotion = useCallback(() => {
+ const canvas = motionCanvasRef.current;
+ const ctx = canvas?.getContext("2d");
+
+ if (!canvas || !ctx || !motionData || !motionScale) {
+ return;
+ }
+
+ const { clientWidth: width, clientHeight: height } = canvas;
+ const dpr = window.devicePixelRatio || 1;
+ canvas.width = width * dpr;
+ canvas.height = height * dpr;
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
+ ctx.clearRect(0, 0, width, height);
+
+ const duration = timelineStart - timelineEnd;
+
+ if (duration <= 0 || height <= 0) {
+ return;
+ }
+
+ const rows = Math.max(
+ 1,
+ Math.min(Math.floor(height), Math.ceil(duration / motionScale)),
+ );
+ const rowHeight = height / rows;
+ const rowSeconds = duration / rows;
+ const sums = new Array(rows).fill(0);
+
+ motionData.forEach((item) => {
+ const row = Math.floor(
+ ((timelineStart - item.start_time) / duration) * rows,
+ );
+
+ if (row >= 0 && row < rows) {
+ sums[row] += item.motion ?? 0;
+ }
+ });
+
+ const color = getComputedStyle(document.documentElement)
+ .getPropertyValue("--severity_significant_motion")
+ .trim();
+
+ const samplesPerRow = rowSeconds / motionScale;
+
+ sums.forEach((sum, row) => {
+ if (sum == 0) {
+ return;
+ }
+
+ const intensity = Math.min(1, sum / (100 * samplesPerRow));
+ ctx.fillStyle = `hsl(${color} / ${intensity})`;
+ ctx.fillRect(0, row * rowHeight, width, Math.ceil(rowHeight));
+ });
+ }, [motionData, motionScale, timelineStart, timelineEnd]);
+
+ useEffect(() => {
+ const canvas = motionCanvasRef.current;
+
+ if (!canvas) {
+ return;
+ }
+
+ drawMotion();
+ const resizeObserver = new ResizeObserver(drawMotion);
+ resizeObserver.observe(canvas);
+
+ return () => resizeObserver.disconnect();
+ }, [drawMotion]);
+
const setVisibleSectionStyles = useCallback(() => {
if (
reviewTimelineRef.current &&
@@ -144,13 +231,22 @@ export function SummaryTimeline({
} = content;
const { clientHeight: summaryTimelineVisibleHeight } = summary;
- visibleSectionRef.current.style.top = `${
- summaryTimelineVisibleHeight * (scrolled / reviewTimelineFullHeight)
- }px`;
- visibleSectionRef.current.style.height = `${
- reviewTimelineVisibleHeight *
- (reviewTimelineVisibleHeight / reviewTimelineFullHeight)
- }px`;
+ // The box keeps a minimum height so it stays visible when zoomed in,
+ // centered on the true visible range.
+
+ const trueHeight =
+ summaryTimelineVisibleHeight *
+ (reviewTimelineVisibleHeight / reviewTimelineFullHeight);
+ const trueTop =
+ summaryTimelineVisibleHeight * (scrolled / reviewTimelineFullHeight);
+ const height = Math.max(trueHeight, MIN_BOX_HEIGHT);
+ const top = Math.min(
+ Math.max(trueTop - (height - trueHeight) / 2, 0),
+ summaryTimelineVisibleHeight - height,
+ );
+
+ visibleSectionRef.current.style.top = `${top}px`;
+ visibleSectionRef.current.style.height = `${height}px`;
}
}, [reviewTimelineRef, summaryTimelineRef, visibleSectionRef]);
@@ -184,178 +280,137 @@ export function SummaryTimeline({
segmentDuration,
]);
- const timelineClick = useCallback(
- (
- e: React.MouseEvent | React.TouchEvent,
- ) => {
- // prevent default only for mouse events
- // to avoid chrome/android issues
- if (e.nativeEvent instanceof MouseEvent) {
- e.preventDefault();
+ const scrollTopForPosition = useCallback(
+ (clientY: number) => {
+ const timeline = reviewTimelineRef.current;
+ const summary = summaryTimelineRef.current;
+
+ if (!timeline || !summary) {
+ return 0;
}
- e.stopPropagation();
- let clientY;
- if ("TouchEvent" in window && e.nativeEvent instanceof TouchEvent) {
- clientY = e.nativeEvent.touches[0].clientY;
- } else if (e.nativeEvent instanceof MouseEvent) {
- clientY = e.nativeEvent.clientY;
- }
- if (
- clientY &&
- reviewTimelineRef.current &&
- summaryTimelineRef.current &&
- visibleSectionRef.current
- ) {
- const { clientHeight: summaryTimelineVisibleHeight } =
- summaryTimelineRef.current;
+ const rect = summary.getBoundingClientRect();
+ const ratio = (clientY - rect.top) / rect.height;
+ return ratio * timeline.scrollHeight - timeline.clientHeight / 2;
+ },
+ [reviewTimelineRef],
+ );
- const rect = summaryTimelineRef.current.getBoundingClientRect();
- const summaryTimelineTop = rect.top;
+ const scrollTimelineTo = useCallback(
+ (top: number, behavior: ScrollBehavior = "auto") => {
+ const timeline = reviewTimelineRef.current;
- const { scrollHeight: reviewTimelineHeight } =
- reviewTimelineRef.current;
-
- const { clientHeight: visibleSectionHeight } =
- visibleSectionRef.current;
-
- const visibleSectionOffset = -(visibleSectionHeight / 2);
-
- const clickPercentage =
- (clientY - summaryTimelineTop + visibleSectionOffset) /
- summaryTimelineVisibleHeight;
-
- reviewTimelineRef.current.scrollTo({
- top: Math.floor(reviewTimelineHeight * clickPercentage),
- behavior: "smooth",
+ if (timeline) {
+ const maxScrollTop = timeline.scrollHeight - timeline.clientHeight;
+ timeline.scrollTo({
+ top: Math.min(Math.max(top, 0), maxScrollTop),
+ behavior,
});
}
},
- [reviewTimelineRef, summaryTimelineRef, visibleSectionRef],
+ [reviewTimelineRef],
);
- const handleMouseDown = useCallback(
- (
- e: React.MouseEvent | React.TouchEvent,
- ) => {
- // prevent default only for mouse events
- // to avoid chrome/android issues
- if (e.nativeEvent instanceof MouseEvent) {
- e.preventDefault();
- }
- e.stopPropagation();
- setIsDragging(true);
+ // A press outside the box jumps there on release, or snaps the box under
+ // the pointer once it moves and then drags it.
- let clientY;
- if ("TouchEvent" in window && e.nativeEvent instanceof TouchEvent) {
- clientY = e.nativeEvent.touches[0].clientY;
- } else if (e.nativeEvent instanceof MouseEvent) {
- clientY = e.nativeEvent.clientY;
+ const handlePointerDown = useCallback(
+ (e: React.PointerEvent) => {
+ if (
+ e.button != 0 ||
+ !reviewTimelineRef.current ||
+ !visibleSectionRef.current
+ ) {
+ return;
}
- if (clientY && summaryTimelineRef.current && reviewTimelineRef.current) {
- setScrollStartPosition(clientY);
- setInitialReviewTimelineScrollTop(reviewTimelineRef.current.scrollTop);
- }
- },
- [setIsDragging, summaryTimelineRef, reviewTimelineRef],
- );
- const handleMouseUp = useCallback(
- (e: MouseEvent | TouchEvent) => {
e.preventDefault();
e.stopPropagation();
- if (isDragging) {
- setIsDragging(false);
- }
+ e.currentTarget.setPointerCapture(e.pointerId);
+
+ const box = visibleSectionRef.current.getBoundingClientRect();
+ dragRef.current = {
+ startY: e.clientY,
+ startScrollTop: reviewTimelineRef.current.scrollTop,
+ insideBox: e.clientY >= box.top && e.clientY <= box.bottom,
+ moved: false,
+ };
},
- [isDragging, setIsDragging],
+ [reviewTimelineRef],
);
- const handleMouseMove = useCallback(
- (e: MouseEvent | TouchEvent) => {
- if (
- summaryTimelineRef.current &&
- reviewTimelineRef.current &&
- visibleSectionRef.current
- ) {
- // prevent default only for mouse events
- // to avoid chrome/android issues
- if (e instanceof MouseEvent) {
- e.preventDefault();
+ const handlePointerMove = useCallback(
+ (e: React.PointerEvent) => {
+ const drag = dragRef.current;
+ const timeline = reviewTimelineRef.current;
+ const summary = summaryTimelineRef.current;
+
+ if (!drag || !timeline || !summary) {
+ return;
+ }
+
+ const deltaY = e.clientY - drag.startY;
+
+ if (!drag.moved) {
+ if (Math.abs(deltaY) < DRAG_THRESHOLD) {
+ return;
}
- e.stopPropagation();
- let clientY;
- if ("TouchEvent" in window && e instanceof TouchEvent) {
- clientY = e.touches[0].clientY;
- } else if (e instanceof MouseEvent) {
- clientY = e.clientY;
- }
- if (isDragging && clientY) {
- const { clientHeight: summaryTimelineVisibleHeight } =
- summaryTimelineRef.current;
- const {
- scrollHeight: reviewTimelineHeight,
- clientHeight: reviewTimelineVisibleHeight,
- } = reviewTimelineRef.current;
+ drag.moved = true;
+ setIsDragging(true);
- const { clientHeight: visibleSectionHeight } =
- visibleSectionRef.current;
-
- const deltaY =
- (clientY - scrollStartPosition) *
- (summaryTimelineVisibleHeight / visibleSectionHeight);
-
- const newScrollTop = Math.min(
- initialReviewTimelineScrollTop + deltaY,
- reviewTimelineHeight - reviewTimelineVisibleHeight,
- );
-
- reviewTimelineRef.current.scrollTop = newScrollTop;
+ if (!drag.insideBox) {
+ drag.startScrollTop = scrollTopForPosition(drag.startY);
}
}
+
+ scrollTimelineTo(
+ drag.startScrollTop +
+ deltaY * (timeline.scrollHeight / summary.clientHeight),
+ );
},
- [
- initialReviewTimelineScrollTop,
- isDragging,
- reviewTimelineRef,
- scrollStartPosition,
- ],
+ [reviewTimelineRef, scrollTopForPosition, scrollTimelineTo],
);
- const documentRef = useRef(document);
- useEffect(() => {
- const documentInstance = documentRef.current;
+ const handlePointerUp = useCallback(
+ (e: React.PointerEvent) => {
+ const drag = dragRef.current;
+ dragRef.current = null;
+ setIsDragging(false);
- if (isDragging) {
- documentInstance?.addEventListener("mousemove", handleMouseMove);
- documentInstance?.addEventListener("touchmove", handleMouseMove);
- documentInstance?.addEventListener("mouseup", handleMouseUp);
- documentInstance?.addEventListener("touchend", handleMouseUp);
- } else {
- documentInstance?.removeEventListener("mousemove", handleMouseMove);
- documentInstance?.removeEventListener("touchmove", handleMouseMove);
- documentInstance?.removeEventListener("mouseup", handleMouseUp);
- documentInstance?.removeEventListener("touchend", handleMouseUp);
- }
- return () => {
- documentInstance?.removeEventListener("mousemove", handleMouseMove);
- documentInstance?.removeEventListener("touchmove", handleMouseMove);
- documentInstance?.removeEventListener("mouseup", handleMouseUp);
- documentInstance?.removeEventListener("touchend", handleMouseUp);
- };
- }, [handleMouseMove, handleMouseUp, isDragging]);
+ if (drag && !drag.moved && !drag.insideBox) {
+ scrollTimelineTo(scrollTopForPosition(e.clientY), "smooth");
+ }
+ },
+ [scrollTopForPosition, scrollTimelineTo],
+ );
+
+ const handlePointerCancel = useCallback(() => {
+ dragRef.current = null;
+ setIsDragging(false);
+ }, []);
return (
+ {motionData && (
+
+ )}
{consolidatedSegments.map((segment, index) => (
);
diff --git a/web/src/views/events/EventView.tsx b/web/src/views/events/EventView.tsx
index 54ea2fbef2..8dc01e9b0a 100644
--- a/web/src/views/events/EventView.tsx
+++ b/web/src/views/events/EventView.tsx
@@ -923,7 +923,7 @@ function DetectionReview({
)}
-
+
{loading ? (
@@ -951,7 +951,7 @@ function DetectionReview({
/>
)}
-
+
{loading ? (
) : (
@@ -1013,6 +1013,7 @@ function MotionReview({
const segmentDuration = 30;
const { data: config } = useSWR("config");
const allowedCameras = useAllowedCameras();
+ const motionTimelineRef = useRef(null);
const reviewCameras = useMemo(() => {
if (!config) {
@@ -1700,34 +1701,58 @@ function MotionReview({
{!selectedMotionPreviewCamera && (
-
- {motionData ? (
-
{
- if (playing && scrubbing) {
- setPlaying(false);
- }
+
+
+ {motionData ? (
+ {
+ if (playing && scrubbing) {
+ setPlaying(false);
+ }
- setScrubbing(scrubbing);
- }}
- dense={isMobileOnly}
- isZooming={false}
- zoomDirection={null}
- alwaysShowMotionLine={true}
- />
- ) : (
-
+ setScrubbing(scrubbing);
+ }}
+ dense={isMobileOnly}
+ isZooming={false}
+ zoomDirection={null}
+ alwaysShowMotionLine={true}
+ />
+ ) : (
+
+ )}
+
+ {!motionOnly && (
+
+ {motionData ? (
+
+ ) : (
+
+ )}
+
)}
)}
diff --git a/web/src/views/recording/RecordingView.tsx b/web/src/views/recording/RecordingView.tsx
index 1d9657f705..8d4c049c4b 100644
--- a/web/src/views/recording/RecordingView.tsx
+++ b/web/src/views/recording/RecordingView.tsx
@@ -70,6 +70,7 @@ import { cn } from "@/lib/utils";
import { useFullscreen } from "@/hooks/use-fullscreen";
import { useTimezone } from "@/hooks/use-date-utils";
import { useTimelineZoom } from "@/hooks/use-timeline-zoom";
+import SummaryTimeline from "@/components/timeline/SummaryTimeline";
import { useTranslation } from "react-i18next";
import { useTimelineUtils } from "@/hooks/use-timeline-utils";
import {
@@ -1317,14 +1318,14 @@ function Timeline({
isDesktop
? cn(
timelineType == "timeline"
- ? "w-[100px] flex-shrink-0"
+ ? "w-[110px] flex-shrink-0"
: timelineType == "detail"
? "min-w-[20rem] max-w-[30%] flex-shrink-0 flex-grow-0 basis-[30rem] md:min-w-[20rem] md:max-w-[25%] lg:min-w-[30rem] lg:max-w-[33%]"
: "w-80 flex-shrink-0",
)
: cn(
timelineType == "timeline"
- ? "portrait:flex-grow landscape:w-[100px] landscape:flex-shrink-0"
+ ? "portrait:flex-grow landscape:w-[128px] landscape:flex-shrink-0"
: timelineType == "detail"
? "portrait:flex-grow landscape:w-[19rem] landscape:flex-shrink-0"
: "portrait:flex-grow landscape:w-[19rem] landscape:flex-shrink-0",
@@ -1345,32 +1346,51 @@ function Timeline({
)}
{timelineType == "timeline" ? (
!isLoading ? (
-
+
) : (
)