fix: Status defaults and stepping

This commit is contained in:
Mateusz Pstrucha committed 2021-09-10 20:27:46 +02:00
1 parent 82a81046b0
commit d464c721f3
2 files changed
+36 -17

No files matched your search

+5 -1
View File
@@ -47,7 +47,7 @@
bottom: 0;
border-bottom: 2px solid white;
border-radius: 2px;
transition: 1s linear;
transition: 0.5s linear width, 0.3s ease-out opacity;
}
.status-progress:after {
@@ -60,3 +60,7 @@
background-color: white;
border-radius: 100%;
}
.status.paused > .status-progress {
opacity: 0;
}
+31 -16
View File
@@ -5,43 +5,58 @@ import PlayingContext from '../../contexts/Playing'
import './Status.css'
function Status(props) {
function Status({ progress, paused, onEnd }) {
const {
id,
title = 'Tytuł',
artists = ['Autor'],
title = '-',
artists = ['-'],
album = {},
duration,
} = useContext(PlayingContext),
[progress, setProgress] = useState(0)
[elapsed, setElapsed] = useState()
const step = 1 / duration
const step = 500,
tread = step / duration
useEffect(() => {
setElapsed(progress ? progress / duration : 0)
}, [progress, duration])
useEffect(
(anim) => {
if (progress < 1) {
anim = setTimeout(() => {
setProgress(progress + step)
}, 1e3)
} else if (progress > 1) {
setProgress(1) // if FPU decides that 1 / duration * duration !== 1
if (!paused) {
if (elapsed < 1) {
anim = setTimeout(() => {
setElapsed(elapsed + tread)
}, step)
} else if (elapsed > 1) {
setElapsed(1) // if FPU decides that 1 / duration * duration !== 1
}
}
return () => {
clearInterval(anim)
clearTimeout(anim)
}
},
[progress, step]
[elapsed, tread, paused]
)
return (
<Link className="status" to={`/utwor/${id}`}>
<img className="status-image" alt="album cover" src={album.art} />
<Link
className={['status', paused ? 'paused' : ''].join(' ')}
to={`/utwor/${id}`}
>
<img
className="status-image"
alt="album cover"
src={album.art ?? '/media/default.png'}
/>
<span className="status-track">{title}</span>
<span className="status-artist">{artists.join(', ')}</span>
<i className="icon-info"></i>
<div
className="status-progress"
style={{ width: `calc(var(--max) * ${progress})` }}
style={{ width: `calc(var(--max) * ${elapsed})` }}
></div>
</Link>
)