import React, { useEffect, useMemo, useRef, useState } from 'react'; import classNames from 'classnames'; import { t } from 'i18next'; import { Transition } from 'stremio/components'; import { useBinaryState } from 'stremio/common'; import styles from './Indicator.less'; type Property = { label: string, format: (value: number | string) => string, }; const VIDEO_SCALE_LABELS: Record = { 'contain': 'Fit', 'cover': 'Crop', 'fill': 'Stretch', }; const PROPERTIES: Record = { 'extraSubtitlesDelay': { label: 'SUBTITLES_DELAY', format: (value) => `${(value / 1000).toFixed(2)}s`, }, 'videoScale': { label: 'VIDEO_SCALE', format: (value) => VIDEO_SCALE_LABELS[String(value)] || String(value), }, }; type VideoState = Record; type Props = { className: string, videoState: VideoState, disabled: boolean, }; const Indicator = ({ className, videoState, disabled }: Props) => { const timeout = useRef(null); const prevVideoState = useRef(videoState); const [shown, show, hide] = useBinaryState(false); const [current, setCurrent] = useState(null); const label = useMemo(() => { const property = current && PROPERTIES[current]; return property && t(property.label); }, [current]); const value = useMemo(() => { const property = current && PROPERTIES[current]; const value = current && videoState[current]; return property && value && property.format(value); }, [current, videoState]); useEffect(() => { for (const property of Object.keys(PROPERTIES)) { const prev = prevVideoState.current[property]; const next = videoState[property]; if (next && next !== prev) { setCurrent(property); show(); timeout.current && clearTimeout(timeout.current); timeout.current = setTimeout(hide, 1000); } } prevVideoState.current = videoState; }, [videoState]); return (
{label} {value}
); }; export default Indicator;