import {useState, useEffect, useRef} from 'react' import PropTypes from 'prop-types' import {styled, useTheme} from '@mui/material/styles' import Box from '@mui/material/Box' import Typography from '@mui/material/Typography' import Slider from '@mui/material/Slider' import IconButton from '@mui/material/IconButton' import Stack from '@mui/material/Stack' import PauseRounded from '@mui/icons-material/PauseRounded' import PlayArrowRounded from '@mui/icons-material/PlayArrowRounded' import VolumeUpRounded from '@mui/icons-material/VolumeUpRounded' import VolumeDownRounded from '@mui/icons-material/VolumeDownRounded' import Image from 'next/image' import {grey} from '@mui/material/colors' import {Link} from '@mui/material' const apiUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:1337' const Widget = styled('div')(({theme}) => ({ padding: 16, borderRadius: 16, width: 343, maxWidth: '100%', margin: 'auto', position: 'relative', zIndex: 1, backgroundColor: theme.palette.mode === 'dark' ? 'rgba(0,0,0,0.6)' : grey[200], backdropFilter: 'blur(40px)', })) const CoverImage = styled('div')({ width: 100, height: 100, objectFit: 'cover', overflow: 'hidden', flexShrink: 0, borderRadius: 8, backgroundColor: 'rgba(0,0,0,0.08)', '& > img': { width: '100%', }, }) const TinyText = styled(Typography)({ fontSize: '0.75rem', opacity: 0.38, fontWeight: 500, letterSpacing: 0.2, }) export default function Lekte({audio, url, teks}) { const audioRef = useRef(new Audio(audio)) const intervalRef = useRef() const isReady = useRef(false) const {duration} = audioRef.current const theme = useTheme() const [position, setPosition] = useState(0) const [volume, setVolume] = useState(100) const [isPlaying, setIsPlaying] = useState(false) const awtis = teks.awtis.map(({alias}) => alias) function formatDuration(value) { const minute = Math.floor(value / 60) const secondLeft = value - (minute * 60) if (Number.isNaN(minute) || Number.isNaN(secondLeft)) { return 'Information indisponible' } return `${minute}:${secondLeft <= 9 ? `0${secondLeft}` : secondLeft}` } const mainIconColor = theme.palette.mode === 'dark' ? '#fff' : '#000' const lightIconColor = theme.palette.mode === 'dark' ? 'rgba(255,255,255,0.4)' : grey[900] const startTimer = () => { clearInterval(intervalRef.current) intervalRef.current = setInterval(() => { setPosition(Math.round(audioRef.current.currentTime)) }, [1000]) } useEffect(() => { if (isReady.current) { audioRef.current.play() setIsPlaying(true) startTimer() } else { isReady.current = true } }, []) useEffect(() => { if (isPlaying) { audioRef.current.play() } else { audioRef.current.pause() } }, [isPlaying]) useEffect(() => () => { audioRef.current.pause() clearInterval(intervalRef.current) } , []) useEffect(() => { if (isPlaying) { audioRef.current.play() startTimer() } else { clearInterval(intervalRef.current) audioRef.current.pause() } }, [isPlaying]) useEffect(() => { audioRef.current.pause() audioRef.current = new Audio(audio) setIsPlaying(false) setVolume(100) setPosition(0) }, [audio]) const handleChangePosition = value => { setPosition(value) audioRef.current.currentTime = value } const handleChangeVolume = value => { setVolume(value) audioRef.current.volume = value / 100 } return ( {teks.tit} {new Intl.ListFormat('fr').format(awtis)} {teks.tit} {teks.lanne} handleChangePosition(value)} /> {formatDuration(position)} -{formatDuration(Math.round(duration) - position)} setIsPlaying(!isPlaying)} > {isPlaying ? ( ) : ( )} handleChangeVolume(value)} /> ) } Lekte.propTypes = { audio: PropTypes.string.isRequired, url: PropTypes.string.isRequired, teks: PropTypes.object.isRequired }