import type { OdFileObject } from '../../types' import { FC, useEffect, useRef, useState } from 'react' import ReactAudioPlayer from 'react-audio-player' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { useTranslation } from 'next-i18next' import { useRouter } from 'next/router' import DownloadButtonGroup from '../DownloadBtnGtoup' import { DownloadBtnContainer, PreviewContainer } from './Containers' import { LoadingIcon } from '../Loading' import { formatModifiedDateTime } from '../../utils/fileDetails' import { getStoredToken } from '../../utils/protectedRouteHandler' enum PlayerState { Loading, Ready, Playing, Paused, } const AudioPreview: FC<{ file: OdFileObject }> = ({ file }) => { const { t } = useTranslation() const { asPath } = useRouter() const hashedToken = getStoredToken(asPath) const rapRef = useRef(null) const [playerStatus, setPlayerStatus] = useState(PlayerState.Loading) const [playerVolume, setPlayerVolume] = useState(1) // Render audio thumbnail, and also check for broken thumbnails const thumbnail = `/api/thumbnail/?path=${asPath}&size=medium${hashedToken ? `&odpt=${hashedToken}` : ''}` const [brokenThumbnail, setBrokenThumbnail] = useState(false) useEffect(() => { // Manually get the HTML audio element and set onplaying event. // - As the default event callbacks provided by the React component does not guarantee playing state to be set // - properly when the user seeks through the timeline or the audio is buffered. const rap = rapRef.current?.audioEl.current if (rap) { rap.oncanplay = () => setPlayerStatus(PlayerState.Ready) rap.onended = () => setPlayerStatus(PlayerState.Paused) rap.onpause = () => setPlayerStatus(PlayerState.Paused) rap.onplay = () => setPlayerStatus(PlayerState.Playing) rap.onplaying = () => setPlayerStatus(PlayerState.Playing) rap.onseeking = () => setPlayerStatus(PlayerState.Loading) rap.onwaiting = () => setPlayerStatus(PlayerState.Loading) rap.onerror = () => setPlayerStatus(PlayerState.Paused) rap.onvolumechange = () => setPlayerVolume(rap.volume) } }, []) return ( <>
{!brokenThumbnail ? (
{/* eslint-disable-next-line @next/next/no-img-element */} {file.name} setBrokenThumbnail(true)} />
) : ( )}
{file.name}
{t('Last modified:') + ' ' + formatModifiedDateTime(file.lastModifiedDateTime)}
) } export default AudioPreview