onedrive/components/previews/AudioPreview.tsx

112 lines
4.2 KiB
TypeScript
Raw Normal View History

2022-02-02 08:41:30 +00:00
import type { OdFileObject } from '../../types'
import { FC, useEffect, useRef, useState } from 'react'
2022-01-16 12:19:52 +00:00
import ReactAudioPlayer from 'react-audio-player'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { useTranslation } from 'next-i18next'
import { useRouter } from 'next/router'
2021-06-24 13:54:59 +00:00
import DownloadButtonGroup from '../DownloadBtnGtoup'
import { DownloadBtnContainer, PreviewContainer } from './Containers'
import { LoadingIcon } from '../Loading'
import { formatModifiedDateTime } from '../../utils/fileDetails'
2021-06-25 14:15:00 +00:00
enum PlayerState {
Loading,
Ready,
Playing,
Paused,
}
2022-01-16 12:19:52 +00:00
const AudioPreview: FC<{ file: OdFileObject }> = ({ file }) => {
const { t } = useTranslation()
const { asPath } = useRouter()
const rapRef = useRef<ReactAudioPlayer>(null)
const [playerStatus, setPlayerStatus] = useState(PlayerState.Loading)
// Render audio thumbnail, and also check for broken thumbnails
const thumbnail = `/api/thumbnail?path=${asPath}&size=medium`
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 as ReactAudioPlayer).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)
}
}, [])
2021-06-24 13:54:59 +00:00
return (
2021-06-25 14:15:00 +00:00
<>
<PreviewContainer>
<div className="flex flex-col space-y-4 md:flex-row md:space-x-4">
<div className="relative flex aspect-square w-full items-center justify-center rounded bg-gray-100 transition-all duration-75 dark:bg-gray-700 md:w-48">
<div
className={`absolute z-20 flex h-full w-full items-center justify-center transition-all duration-300 ${
playerStatus === PlayerState.Loading
? 'bg-white opacity-80 dark:bg-gray-800'
: 'bg-transparent opacity-0'
}`}
>
<LoadingIcon className="z-10 inline-block h-5 w-5 animate-spin" />
</div>
{!brokenThumbnail ? (
<div className="absolute m-4 rounded-full shadow-lg">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
className={`h-full w-full rounded-full object-cover object-top ${
playerStatus === PlayerState.Playing ? 'animate-spin-slow' : ''
}`}
src={thumbnail}
alt={file.name}
onError={() => setBrokenThumbnail(true)}
/>
</div>
2021-06-25 14:15:00 +00:00
) : (
<FontAwesomeIcon
className={`z-10 h-5 w-5 ${playerStatus === PlayerState.Playing ? 'animate-spin' : ''}`}
2021-06-25 14:15:00 +00:00
icon="music"
size="2x"
/>
)}
</div>
<div className="flex w-full flex-col justify-between">
<div>
<div className="mb-2 font-medium">{file.name}</div>
<div className="mb-4 text-sm text-gray-500">
{t('Last modified:') + ' ' + formatModifiedDateTime(file.lastModifiedDateTime)}
</div>
</div>
<ReactAudioPlayer
className="h-11 w-full"
src={file['@microsoft.graph.downloadUrl']}
ref={rapRef}
2021-06-25 14:15:00 +00:00
controls
preload="auto"
/>
</div>
</div>
</PreviewContainer>
<DownloadBtnContainer>
<DownloadButtonGroup downloadUrl={file['@microsoft.graph.downloadUrl']} />
</DownloadBtnContainer>
2021-06-25 14:15:00 +00:00
</>
2021-06-24 13:54:59 +00:00
)
}
export default AudioPreview