Skip to content

Commit

Permalink
Show motion playback on page initially (blakeblackshear#11196)
Browse files Browse the repository at this point in the history
* motion video controls handle current time better

* Make sure state is updated
  • Loading branch information
NickM-27 committed May 2, 2024
1 parent 2e63941 commit 28dd871
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 34 deletions.
10 changes: 6 additions & 4 deletions web/src/hooks/use-overlay-state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export function useOverlayState<S>(
): [S | undefined, (value: S, replace?: boolean) => void] {
const location = useLocation();
const navigate = useNavigate();
const currentLocationState = location.state;

const currentLocationState = useMemo(() => location.state, [location]);

const setOverlayStateValue = useCallback(
(value: S, replace: boolean = false) => {
Expand All @@ -18,7 +19,7 @@ export function useOverlayState<S>(
},
// we know that these deps are correct
// eslint-disable-next-line react-hooks/exhaustive-deps
[key, navigate],
[key, currentLocationState, navigate],
);

const overlayStateValue = useMemo<S | undefined>(
Expand All @@ -39,7 +40,8 @@ export function usePersistedOverlayState<S extends string>(
);
const location = useLocation();
const navigate = useNavigate();
const currentLocationState = location.state;

const currentLocationState = useMemo(() => location.state, [location]);

const setOverlayStateValue = useCallback(
(value: S | undefined, replace: boolean = false) => {
Expand All @@ -50,7 +52,7 @@ export function usePersistedOverlayState<S extends string>(
},
// we know that these deps are correct
// eslint-disable-next-line react-hooks/exhaustive-deps
[key, navigate],
[key, currentLocationState, navigate],
);

const overlayStateValue = useMemo<S | undefined>(
Expand Down
63 changes: 33 additions & 30 deletions web/src/views/events/EventView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,11 @@ function MotionReview({
return;
}

if (nextTimestamp >= timeRange.before - 4) {
setPlaying(false);
return;
}

const handleTimeout = () => {
setCurrentTime(nextTimestamp);
timeoutIdRef.current = setTimeout(handleTimeout, 500 / playbackRate);
Expand All @@ -818,7 +823,7 @@ function MotionReview({
}
};
}
}, [playing, playbackRate, nextTimestamp]);
}, [playing, playbackRate, nextTimestamp, setPlaying, timeRange]);

const { alignStartDateToTimeline } = useTimelineUtils({
segmentDuration,
Expand Down Expand Up @@ -962,37 +967,35 @@ function MotionReview({
)}
</div>

{!scrubbing && (
<VideoControls
className="absolute bottom-16 left-1/2 -translate-x-1/2 bg-secondary"
features={{
volume: false,
seek: true,
playbackRate: true,
}}
isPlaying={playing}
playbackRates={[4, 8, 12, 16]}
playbackRate={playbackRate}
controlsOpen={controlsOpen}
setControlsOpen={setControlsOpen}
onPlayPause={setPlaying}
onSeek={(diff) => {
const wasPlaying = playing;

if (wasPlaying) {
setPlaying(false);
}
<VideoControls
className="absolute bottom-16 left-1/2 -translate-x-1/2 bg-secondary"
features={{
volume: false,
seek: true,
playbackRate: true,
}}
isPlaying={playing}
show={!scrubbing}
playbackRates={[4, 8, 12, 16]}
playbackRate={playbackRate}
controlsOpen={controlsOpen}
setControlsOpen={setControlsOpen}
onPlayPause={setPlaying}
onSeek={(diff) => {
const wasPlaying = playing;

if (wasPlaying) {
setPlaying(false);
}

setCurrentTime(currentTime + diff);
setCurrentTime(currentTime + diff);

if (wasPlaying) {
setTimeout(() => setPlaying(true), 100);
}
}}
onSetPlaybackRate={setPlaybackRate}
show={currentTime < timeRange.before - 4}
/>
)}
if (wasPlaying) {
setTimeout(() => setPlaying(true), 100);
}
}}
onSetPlaybackRate={setPlaybackRate}
/>
</>
);
}

0 comments on commit 28dd871

Please sign in to comment.