Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | 19x 19x 19x 19x | import { ReactElement, useEffect, useState } from "react"; import { Slider } from "components"; import { useAuth, useSpotify, useToast, useTranslations } from "hooks"; import type { AudioPlayer } from "hooks/useSpotifyPlayer"; import { formatTime } from "utils"; export default function ProgressBar(): ReactElement { const { currentlyPlayingDuration, currentlyPlayingPosition, isPlaying, player, currentlyPlaying, setUpdateLyricLine, } = useSpotify(); const [progressSeconds, setProgressSeconds] = useState(0); const [progressFromSpotify, setProgressFromSpotify] = useState(0); const [labelSeconds, setLabelSeconds] = useState(0); const { isPremium } = useAuth(); const { addToast } = useToast(); const { translations } = useTranslations(); const durationInSecondsPremium = currentlyPlayingDuration ? currentlyPlayingDuration / 1000 : 0; const durationInSecondsNoPremium = currentlyPlayingDuration ?? 0; const durationInSeconds = isPremium ? durationInSecondsPremium : durationInSecondsNoPremium; useEffect(() => { Iif (!isPremium || !player) return; (player as Spotify.Player).getCurrentState().then((playbackState) => { Iif (playbackState) { setProgressFromSpotify( (playbackState?.position / playbackState?.duration) * 100 ); setProgressSeconds(playbackState?.position / 1000); } }); }, [isPremium, player]); useEffect(() => { Iif (!isPremium || !player) return; (player as Spotify.Player)?.on("player_state_changed", (playbackState) => { setProgressFromSpotify( (playbackState?.position / playbackState?.duration) * 100 ); setProgressSeconds(playbackState?.position / 1000); }); }, [isPremium, player]); useEffect(() => { Iif (!currentlyPlayingDuration || !isPremium) return; setProgressFromSpotify( ((progressSeconds * 1000) / currentlyPlayingDuration) * 100 ); }, [currentlyPlayingDuration, isPremium, progressSeconds]); useEffect(() => { Iif ( isPremium || currentlyPlayingPosition === undefined || !currentlyPlayingDuration || (player as AudioPlayer).sliderBusy ) return; setProgressFromSpotify( (currentlyPlayingPosition / currentlyPlayingDuration) * 100 ); setProgressSeconds( currentlyPlayingPosition > currentlyPlayingDuration ? currentlyPlayingDuration : currentlyPlayingPosition ); }, [currentlyPlayingDuration, currentlyPlayingPosition, isPremium, player]); useEffect(() => { setLabelSeconds(progressSeconds); }, [progressSeconds]); return ( <div className="progressBar"> <div className="timeTag"> {formatTime( progressSeconds > durationInSeconds ? durationInSeconds : labelSeconds )} </div> <Slider title="Control the progress of the playback" updateProgress={progressFromSpotify} intervalUpdateAction={{ steps: 100 / durationInSeconds, labelUpdateValue: 1, ms: 1000, shouldUpdate: isPlaying, }} onDragging={(isDragging, progressPercent) => { Iif (!isPremium && player) { (player as AudioPlayer).sliderBusy = isDragging; } const progressSeconds = (progressPercent * durationInSeconds) / 100; Iif (isDragging) { setLabelSeconds(progressSeconds); } }} setLabelValue={setProgressSeconds} valueText={`${formatTime(progressSeconds)}/${formatTime( durationInSeconds )}`} initialValuePercent={0} action={(progressPercent) => { Iif (!currentlyPlaying) { addToast({ variant: "error", message: translations.toastMessages.nothingPlaying, }); return; } player?.seek( (progressPercent * (currentlyPlayingDuration ?? 0)) / 100 ); setUpdateLyricLine.on(); }} /> <div className="timeTag">{formatTime(durationInSeconds)}</div> <style jsx>{` .progressBar { display: flex; align-items: center; flex-direction: row; justify-content: space-between; width: 100%; max-width: 540px; margin-top: 11px; } .timeTag { min-width: 40px; text-align: center; font-size: 11px; font-weight: 400; letter-spacing: normal; line-height: 16px; user-select: none; } `}</style> </div> ); } |