blakeblackshear.frigate/web/src/components/HistoryViewer/HistoryVideo.tsx

121 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-02-27 15:04:12 +01:00
import { h } from 'preact';
2022-06-24 17:52:17 +02:00
import { useCallback, useEffect, useRef, useState } from 'preact/hooks';
2022-02-27 15:04:12 +01:00
import { useApiHost } from '../../api';
import { isNullOrUndefined } from '../../utils/objectUtils';
2022-06-02 23:05:56 +02:00
import 'video.js/dist/video-js.css';
2023-04-16 14:09:51 +02:00
import videojs from 'video.js';
import type Player from 'video.js/dist/types/player';
2022-06-02 23:05:56 +02:00
2022-02-27 15:04:12 +01:00
interface OnTimeUpdateEvent {
timestamp: number;
isPlaying: boolean;
}
interface HistoryVideoProps {
2022-03-06 05:16:31 +01:00
id?: string;
2022-02-27 15:04:12 +01:00
isPlaying: boolean;
currentTime: number;
onTimeUpdate?: (event: OnTimeUpdateEvent) => void;
onPause: () => void;
onPlay: () => void;
}
export const HistoryVideo = ({
id,
isPlaying: videoIsPlaying,
currentTime,
onTimeUpdate,
onPause,
onPlay,
}: HistoryVideoProps) => {
const apiHost = useApiHost();
2022-06-24 16:35:56 +02:00
const videoRef = useRef<HTMLVideoElement>(null);
2022-02-27 15:04:12 +01:00
2023-04-16 14:09:51 +02:00
const [video, setVideo] = useState<Player>();
2022-06-24 17:52:17 +02:00
2022-02-27 15:04:12 +01:00
useEffect(() => {
2023-04-16 14:09:51 +02:00
let video: Player
2022-06-24 17:52:17 +02:00
if (videoRef.current) {
video = videojs(videoRef.current, {})
setVideo(video)
2022-02-27 15:04:12 +01:00
}
2022-06-24 17:52:17 +02:00
() => video?.dispose()
2022-06-02 23:05:56 +02:00
}, [videoRef]);
2022-02-27 15:04:12 +01:00
useEffect(() => {
2022-06-24 17:52:17 +02:00
if (!video) {
return
}
2022-06-02 23:05:56 +02:00
if (!id) {
2022-06-24 17:52:17 +02:00
video.pause()
return
2022-06-02 23:05:56 +02:00
}
2022-06-24 17:52:17 +02:00
2022-06-02 23:05:56 +02:00
video.src({
2022-08-17 17:28:32 +02:00
src: `${apiHost}/vod/event/${id}/master.m3u8`,
2022-06-02 23:05:56 +02:00
type: 'application/vnd.apple.mpegurl',
});
video.poster(`${apiHost}/api/events/${id}/snapshot.jpg`);
if (videoIsPlaying) {
video.play();
}
Update web deps (#4383) * Bump jest from 27.5.1 to 29.3.1 in /web Bumps [jest](https://github.com/facebook/jest/tree/HEAD/packages/jest) from 27.5.1 to 29.3.1. - [Release notes](https://github.com/facebook/jest/releases) - [Changelog](https://github.com/facebook/jest/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/jest/commits/v29.3.1/packages/jest) --- updated-dependencies: - dependency-name: jest dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump msw from 0.38.2 to 0.48.0 in /web Bumps [msw](https://github.com/mswjs/msw) from 0.38.2 to 0.48.0. - [Release notes](https://github.com/mswjs/msw/releases) - [Changelog](https://github.com/mswjs/msw/blob/main/CHANGELOG.md) - [Commits](https://github.com/mswjs/msw/compare/v0.38.2...v0.48.0) --- updated-dependencies: - dependency-name: msw dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump idb-keyval from 6.1.0 to 6.2.0 in /web Bumps [idb-keyval](https://github.com/jakearchibald/idb-keyval) from 6.1.0 to 6.2.0. - [Release notes](https://github.com/jakearchibald/idb-keyval/releases) - [Changelog](https://github.com/jakearchibald/idb-keyval/blob/main/CHANGELOG.md) - [Commits](https://github.com/jakearchibald/idb-keyval/compare/v6.1.0...v6.2.0) --- updated-dependencies: - dependency-name: idb-keyval dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @babel/preset-typescript from 7.16.7 to 7.18.6 in /web Bumps [@babel/preset-typescript](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-typescript) from 7.16.7 to 7.18.6. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.18.6/packages/babel-preset-typescript) --- updated-dependencies: - dependency-name: "@babel/preset-typescript" dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump typescript from 4.6.2 to 4.8.4 in /web Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.6.2 to 4.8.4. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](https://github.com/Microsoft/TypeScript/compare/v4.6.2...v4.8.4) --- updated-dependencies: - dependency-name: typescript dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @testing-library/user-event from 13.5.0 to 14.4.3 in /web Bumps [@testing-library/user-event](https://github.com/testing-library/user-event) from 13.5.0 to 14.4.3. - [Release notes](https://github.com/testing-library/user-event/releases) - [Changelog](https://github.com/testing-library/user-event/blob/main/CHANGELOG.md) - [Commits](https://github.com/testing-library/user-event/compare/v13.5.0...v14.4.3) --- updated-dependencies: - dependency-name: "@testing-library/user-event" dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump preact-router from 4.0.1 to 4.1.0 in /web Bumps [preact-router](https://github.com/preactjs/preact-router) from 4.0.1 to 4.1.0. - [Release notes](https://github.com/preactjs/preact-router/releases) - [Commits](https://github.com/preactjs/preact-router/compare/4.0.1...4.1.0) --- updated-dependencies: - dependency-name: preact-router dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump axios from 0.26.0 to 1.1.3 in /web Bumps [axios](https://github.com/axios/axios) from 0.26.0 to 1.1.3. - [Release notes](https://github.com/axios/axios/releases) - [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md) - [Commits](https://github.com/axios/axios/compare/v0.26.0...v1.1.3) --- updated-dependencies: - dependency-name: axios dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @preact/preset-vite from 2.1.7 to 2.4.0 in /web Bumps [@preact/preset-vite](https://github.com/preactjs/preset-vite) from 2.1.7 to 2.4.0. - [Release notes](https://github.com/preactjs/preset-vite/releases) - [Commits](https://github.com/preactjs/preset-vite/compare/v2.1.7...v2.4.0) --- updated-dependencies: - dependency-name: "@preact/preset-vite" dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @testing-library/jest-dom from 5.16.2 to 5.16.5 in /web Bumps [@testing-library/jest-dom](https://github.com/testing-library/jest-dom) from 5.16.2 to 5.16.5. - [Release notes](https://github.com/testing-library/jest-dom/releases) - [Changelog](https://github.com/testing-library/jest-dom/blob/main/CHANGELOG.md) - [Commits](https://github.com/testing-library/jest-dom/compare/v5.16.2...v5.16.5) --- updated-dependencies: - dependency-name: "@testing-library/jest-dom" dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * revamp frontend * disable broken tests * disable a few more tests * update typescript * couple docs updates Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-13 17:31:36 +01:00
}, [video, id, apiHost, videoIsPlaying]);
2022-02-27 15:04:12 +01:00
useEffect(() => {
const video = videoRef.current;
const videoExists = !isNullOrUndefined(video);
const hasSeeked = currentTime >= 0;
2022-03-06 05:16:31 +01:00
if (video && videoExists && hasSeeked) {
2022-02-27 15:04:12 +01:00
video.currentTime = currentTime;
}
}, [currentTime, videoRef]);
const onTimeUpdateHandler = useCallback(
(event: Event) => {
const target = event.target as HTMLMediaElement;
const timeUpdateEvent = {
isPlaying: videoIsPlaying,
timestamp: target.currentTime,
};
onTimeUpdate && onTimeUpdate(timeUpdateEvent);
},
[videoIsPlaying, onTimeUpdate]
);
2022-06-24 17:52:17 +02:00
useEffect(() => {
2023-04-16 14:09:51 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (video && (video as any).readyState() >= 1) {
2022-06-24 17:52:17 +02:00
if (videoIsPlaying) {
video.play()
} else {
video.pause()
}
}
}, [video, videoIsPlaying])
const onLoad = useCallback(() => {
2023-04-16 14:09:51 +02:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if (video && (video as any).readyState() >= 1 && videoIsPlaying) {
2022-06-24 17:52:17 +02:00
video.play()
}
}, [video, videoIsPlaying])
Update web deps (#4383) * Bump jest from 27.5.1 to 29.3.1 in /web Bumps [jest](https://github.com/facebook/jest/tree/HEAD/packages/jest) from 27.5.1 to 29.3.1. - [Release notes](https://github.com/facebook/jest/releases) - [Changelog](https://github.com/facebook/jest/blob/main/CHANGELOG.md) - [Commits](https://github.com/facebook/jest/commits/v29.3.1/packages/jest) --- updated-dependencies: - dependency-name: jest dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump msw from 0.38.2 to 0.48.0 in /web Bumps [msw](https://github.com/mswjs/msw) from 0.38.2 to 0.48.0. - [Release notes](https://github.com/mswjs/msw/releases) - [Changelog](https://github.com/mswjs/msw/blob/main/CHANGELOG.md) - [Commits](https://github.com/mswjs/msw/compare/v0.38.2...v0.48.0) --- updated-dependencies: - dependency-name: msw dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump idb-keyval from 6.1.0 to 6.2.0 in /web Bumps [idb-keyval](https://github.com/jakearchibald/idb-keyval) from 6.1.0 to 6.2.0. - [Release notes](https://github.com/jakearchibald/idb-keyval/releases) - [Changelog](https://github.com/jakearchibald/idb-keyval/blob/main/CHANGELOG.md) - [Commits](https://github.com/jakearchibald/idb-keyval/compare/v6.1.0...v6.2.0) --- updated-dependencies: - dependency-name: idb-keyval dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @babel/preset-typescript from 7.16.7 to 7.18.6 in /web Bumps [@babel/preset-typescript](https://github.com/babel/babel/tree/HEAD/packages/babel-preset-typescript) from 7.16.7 to 7.18.6. - [Release notes](https://github.com/babel/babel/releases) - [Changelog](https://github.com/babel/babel/blob/main/CHANGELOG.md) - [Commits](https://github.com/babel/babel/commits/v7.18.6/packages/babel-preset-typescript) --- updated-dependencies: - dependency-name: "@babel/preset-typescript" dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump typescript from 4.6.2 to 4.8.4 in /web Bumps [typescript](https://github.com/Microsoft/TypeScript) from 4.6.2 to 4.8.4. - [Release notes](https://github.com/Microsoft/TypeScript/releases) - [Commits](https://github.com/Microsoft/TypeScript/compare/v4.6.2...v4.8.4) --- updated-dependencies: - dependency-name: typescript dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @testing-library/user-event from 13.5.0 to 14.4.3 in /web Bumps [@testing-library/user-event](https://github.com/testing-library/user-event) from 13.5.0 to 14.4.3. - [Release notes](https://github.com/testing-library/user-event/releases) - [Changelog](https://github.com/testing-library/user-event/blob/main/CHANGELOG.md) - [Commits](https://github.com/testing-library/user-event/compare/v13.5.0...v14.4.3) --- updated-dependencies: - dependency-name: "@testing-library/user-event" dependency-type: direct:development update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump preact-router from 4.0.1 to 4.1.0 in /web Bumps [preact-router](https://github.com/preactjs/preact-router) from 4.0.1 to 4.1.0. - [Release notes](https://github.com/preactjs/preact-router/releases) - [Commits](https://github.com/preactjs/preact-router/compare/4.0.1...4.1.0) --- updated-dependencies: - dependency-name: preact-router dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump axios from 0.26.0 to 1.1.3 in /web Bumps [axios](https://github.com/axios/axios) from 0.26.0 to 1.1.3. - [Release notes](https://github.com/axios/axios/releases) - [Changelog](https://github.com/axios/axios/blob/v1.x/CHANGELOG.md) - [Commits](https://github.com/axios/axios/compare/v0.26.0...v1.1.3) --- updated-dependencies: - dependency-name: axios dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @preact/preset-vite from 2.1.7 to 2.4.0 in /web Bumps [@preact/preset-vite](https://github.com/preactjs/preset-vite) from 2.1.7 to 2.4.0. - [Release notes](https://github.com/preactjs/preset-vite/releases) - [Commits](https://github.com/preactjs/preset-vite/compare/v2.1.7...v2.4.0) --- updated-dependencies: - dependency-name: "@preact/preset-vite" dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * Bump @testing-library/jest-dom from 5.16.2 to 5.16.5 in /web Bumps [@testing-library/jest-dom](https://github.com/testing-library/jest-dom) from 5.16.2 to 5.16.5. - [Release notes](https://github.com/testing-library/jest-dom/releases) - [Changelog](https://github.com/testing-library/jest-dom/blob/main/CHANGELOG.md) - [Commits](https://github.com/testing-library/jest-dom/compare/v5.16.2...v5.16.5) --- updated-dependencies: - dependency-name: "@testing-library/jest-dom" dependency-type: direct:development update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <support@github.com> * revamp frontend * disable broken tests * disable a few more tests * update typescript * couple docs updates Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2022-11-13 17:31:36 +01:00
2022-02-27 15:04:12 +01:00
return (
2022-06-02 23:05:56 +02:00
<div data-vjs-player>
<video
ref={videoRef}
onTimeUpdate={onTimeUpdateHandler}
2022-06-24 17:52:17 +02:00
onLoadedMetadata={onLoad}
2022-06-02 23:05:56 +02:00
onPause={onPause}
onPlay={onPlay}
className="video-js vjs-fluid"
data-setup="{}"
/>
</div>
2022-02-27 15:04:12 +01:00
);
};