| | import React, { useReducer } from 'react'; |
| | import { qualityKey, videoOption } from 'types'; |
| | export interface VideoStateType<K = boolean, T = number> { |
| | |
| | |
| | |
| | isControl: K; |
| | |
| | |
| | |
| | progressSliderChangeVal: T; |
| | |
| | |
| | |
| | progressMouseUpChangeVal: T; |
| | |
| | |
| | |
| | quality: qualityKey | undefined; |
| | } |
| | |
| | |
| | |
| | export interface contextType { |
| | videoRef: HTMLVideoElement | null; |
| | videoContainerRef: HTMLElement | null; |
| | lightOffMaskRef: HTMLElement | null; |
| | dispatch?: React.Dispatch<mergeAction>; |
| | videoFlow: VideoStateType; |
| | propsAttributes?: videoOption; |
| | } |
| | |
| | |
| | |
| | export const initialState = { |
| | isControl: false, |
| | progressSliderChangeVal: 0, |
| | progressMouseUpChangeVal: 0, |
| | quality: undefined, |
| | }; |
| |
|
| | export const defaultValue = { |
| | videoRef: null, |
| | videoContainerRef: null, |
| | lightOffMaskRef: null, |
| | videoFlow: initialState, |
| | }; |
| |
|
| | export const FlowContext = React.createContext<contextType>(defaultValue); |
| | export interface isControlActionType { |
| | type: 'isControl'; |
| | data: VideoStateType['isControl']; |
| | } |
| | export interface progressSliderChangeValActionType { |
| | type: 'progressSliderChangeVal'; |
| | data: VideoStateType['progressSliderChangeVal']; |
| | } |
| | export interface progressMouseUpChangeValValActionType { |
| | type: 'progressMouseUpChangeVal'; |
| | data: VideoStateType['progressMouseUpChangeVal']; |
| | } |
| | export interface qualityActionType { |
| | type: 'quality'; |
| | data: VideoStateType['quality']; |
| | } |
| | export type mergeAction = |
| | | isControlActionType |
| | | progressSliderChangeValActionType |
| | | progressMouseUpChangeValValActionType |
| | | qualityActionType; |
| |
|
| | export const useVideoFlow = () => { |
| | const reducer = (state: VideoStateType, action: mergeAction) => { |
| | switch (action.type) { |
| | case 'isControl': |
| | return { ...state, isControl: action.data }; |
| | case 'progressSliderChangeVal': |
| | return { ...state, progressSliderChangeVal: action.data }; |
| | case 'progressMouseUpChangeVal': |
| | return { ...state, progressMouseUpChangeVal: action.data }; |
| | case 'quality': |
| | return { ...state, quality: action.data }; |
| | default: |
| | return state; |
| | } |
| | }; |
| | const [videoFlow, dispatch] = useReducer(reducer, initialState); |
| | return { videoFlow, dispatch }; |
| | }; |
| |
|