From 4bc8106cb37c4f5e34cd6ea25a3f72bde5857988 Mon Sep 17 00:00:00 2001 From: mrjvs Date: Sun, 23 Jul 2023 16:30:22 +0200 Subject: [PATCH] basics of new video player state --- .eslintrc.js | 1 + package.json | 1 + src/components/player/Player.tsx | 3 ++ src/components/player/base/Container.tsx | 9 +++- src/components/player/hooks/usePlayer.ts | 20 +++++++++ .../player/internals/VideoContainer.tsx | 13 +++++- src/stores/player/slices/interface.ts | 28 ++++++++++++ src/stores/player/slices/playing.ts | 43 +++++++++++++++++++ src/stores/player/slices/progress.ts | 19 ++++++++ src/stores/player/slices/source.ts | 39 +++++++++++++++++ src/stores/player/slices/types.ts | 17 ++++++++ src/stores/player/store.ts | 17 ++++++++ src/stores/player/types.ts | 22 ++++++++++ src/stores/video.ts | 5 +++ src/utils/typeguard.ts | 2 + src/views/developer/VideoTesterView.tsx | 13 ++++++ yarn.lock | 5 +++ 17 files changed, 255 insertions(+), 2 deletions(-) create mode 100644 src/components/player/Player.tsx create mode 100644 src/components/player/hooks/usePlayer.ts create mode 100644 src/stores/player/slices/interface.ts create mode 100644 src/stores/player/slices/playing.ts create mode 100644 src/stores/player/slices/progress.ts create mode 100644 src/stores/player/slices/source.ts create mode 100644 src/stores/player/slices/types.ts create mode 100644 src/stores/player/store.ts create mode 100644 src/stores/player/types.ts create mode 100644 src/stores/video.ts diff --git a/.eslintrc.js b/.eslintrc.js index c1a2b2a7..a2da2b2a 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -52,6 +52,7 @@ module.exports = { "no-await-in-loop": "off", "no-nested-ternary": "off", "prefer-destructuring": "off", + "no-param-reassign": "off", "@typescript-eslint/no-unused-vars": ["warn", { argsIgnorePattern: "^_" }], "react/jsx-filename-extension": [ "error", diff --git a/package.json b/package.json index 22fa2e07..ad03942a 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "hls.js": "^1.0.7", "i18next": "^22.4.5", "i18next-browser-languagedetector": "^7.0.1", + "immer": "^10.0.2", "json5": "^2.2.0", "lodash.throttle": "^4.1.1", "nanoid": "^4.0.0", diff --git a/src/components/player/Player.tsx b/src/components/player/Player.tsx new file mode 100644 index 00000000..4cd0f48e --- /dev/null +++ b/src/components/player/Player.tsx @@ -0,0 +1,3 @@ +import { ReactNode } from "react"; +export * as Atoms from "./atoms/index"; +export \ No newline at end of file diff --git a/src/components/player/base/Container.tsx b/src/components/player/base/Container.tsx index 5b682393..d8e3cb93 100644 --- a/src/components/player/base/Container.tsx +++ b/src/components/player/base/Container.tsx @@ -1,9 +1,16 @@ import { ReactNode } from "react"; +import { VideoContainer } from "@/components/player/internals/VideoContainer"; + export interface PlayerProps { children?: ReactNode; } export function Container(props: PlayerProps) { - return
{props.children}
; + return ( +
+ + {props.children} +
+ ); } diff --git a/src/components/player/hooks/usePlayer.ts b/src/components/player/hooks/usePlayer.ts new file mode 100644 index 00000000..0db61398 --- /dev/null +++ b/src/components/player/hooks/usePlayer.ts @@ -0,0 +1,20 @@ +import { MWStreamType } from "@/backend/helpers/streams"; +import { playerStatus } from "@/stores/player/slices/source"; +import { usePlayerStore } from "@/stores/player/store"; + +export interface Source { + url: string; + type: MWStreamType; +} + +export function usePlayer() { + const setStatus = usePlayerStore((s) => s.setStatus); + const setSource = usePlayerStore((s) => s.setSource); + + return { + playMedia(source: Source) { + setSource(source.url, source.type); + setStatus(playerStatus.PLAYING); + }, + }; +} diff --git a/src/components/player/internals/VideoContainer.tsx b/src/components/player/internals/VideoContainer.tsx index 53907341..1cd28f51 100644 --- a/src/components/player/internals/VideoContainer.tsx +++ b/src/components/player/internals/VideoContainer.tsx @@ -1,3 +1,14 @@ +import { useEffect, useRef } from "react"; + +import { usePlayerStore } from "@/stores/player/store"; + export function VideoContainer() { - return
; + const player = usePlayerStore(); + const videoEl = useRef(null); + + useEffect(() => { + if (videoEl.current) videoEl.current.src = player.source?.url ?? ""; + }, [player.source?.url]); + + return