mirror of
https://github.com/antebudimir/feishin.git
synced 2026-01-02 19:01:40 +00:00
refactor playerbar slider to separate component
This commit is contained in:
parent
40e7eda882
commit
309b49b46e
1 changed files with 104 additions and 84 deletions
|
|
@ -38,21 +38,17 @@ interface CenterControlsProps {
|
||||||
export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
const [isSeeking, setIsSeeking] = useState(false);
|
|
||||||
const currentSong = useCurrentSong();
|
const currentSong = useCurrentSong();
|
||||||
const skip = useSettingsStore((state) => state.general.skipButtons);
|
const skip = useSettingsStore((state) => state.general.skipButtons);
|
||||||
const buttonSize = useSettingsStore((state) => state.general.buttonSize);
|
const buttonSize = useSettingsStore((state) => state.general.buttonSize);
|
||||||
const playbackType = usePlaybackType();
|
|
||||||
const player1 = playersRef?.current?.player1;
|
const player1 = playersRef?.current?.player1;
|
||||||
const player2 = playersRef?.current?.player2;
|
const player2 = playersRef?.current?.player2;
|
||||||
const status = useCurrentStatus();
|
const status = useCurrentStatus();
|
||||||
const player = useCurrentPlayer();
|
|
||||||
const setCurrentTime = useSetCurrentTime();
|
|
||||||
const repeat = useRepeatStatus();
|
const repeat = useRepeatStatus();
|
||||||
const shuffle = useShuffleStatus();
|
const shuffle = useShuffleStatus();
|
||||||
const { bindings } = useHotkeySettings();
|
const { bindings } = useHotkeySettings();
|
||||||
const { showTimeRemaining } = useAppStore();
|
|
||||||
const { setShowTimeRemaining } = useAppStoreActions();
|
|
||||||
|
|
||||||
const {
|
const {
|
||||||
handleNextTrack,
|
handleNextTrack,
|
||||||
|
|
@ -60,7 +56,6 @@ export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
||||||
handlePlay,
|
handlePlay,
|
||||||
handlePlayPause,
|
handlePlayPause,
|
||||||
handlePrevTrack,
|
handlePrevTrack,
|
||||||
handleSeekSlider,
|
|
||||||
handleSkipBackward,
|
handleSkipBackward,
|
||||||
handleSkipForward,
|
handleSkipForward,
|
||||||
handleStop,
|
handleStop,
|
||||||
|
|
@ -69,32 +64,6 @@ export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
||||||
} = useCenterControls({ playersRef });
|
} = useCenterControls({ playersRef });
|
||||||
const handlePlayQueueAdd = usePlayQueueAdd();
|
const handlePlayQueueAdd = usePlayQueueAdd();
|
||||||
|
|
||||||
const songDuration = currentSong?.duration ? currentSong.duration / 1000 : 0;
|
|
||||||
const currentTime = useCurrentTime();
|
|
||||||
const currentPlayerRef = player === 1 ? player1 : player2;
|
|
||||||
const formattedDuration = formatDuration(songDuration * 1000 || 0);
|
|
||||||
const formattedTimeRemaining = formatDuration((currentTime - songDuration) * 1000 || 0);
|
|
||||||
const formattedTime = formatDuration(currentTime * 1000 || 0);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let interval: ReturnType<typeof setInterval>;
|
|
||||||
|
|
||||||
if (status === PlayerStatus.PLAYING && !isSeeking) {
|
|
||||||
if (!isElectron() || playbackType === PlaybackType.WEB) {
|
|
||||||
// Update twice a second for slightly better performance
|
|
||||||
interval = setInterval(() => {
|
|
||||||
if (currentPlayerRef) {
|
|
||||||
setCurrentTime(currentPlayerRef.getCurrentTime());
|
|
||||||
}
|
|
||||||
}, 500);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return () => clearInterval(interval);
|
|
||||||
}, [currentPlayerRef, isSeeking, setCurrentTime, playbackType, status]);
|
|
||||||
|
|
||||||
const [seekValue, setSeekValue] = useState(0);
|
|
||||||
|
|
||||||
useHotkeys([
|
useHotkeys([
|
||||||
[bindings.playPause.isGlobal ? '' : bindings.playPause.hotkey, handlePlayPause],
|
[bindings.playPause.isGlobal ? '' : bindings.playPause.hotkey, handlePlayPause],
|
||||||
[bindings.play.isGlobal ? '' : bindings.play.hotkey, handlePlay],
|
[bindings.play.isGlobal ? '' : bindings.play.hotkey, handlePlay],
|
||||||
|
|
@ -255,6 +224,58 @@ export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<PlayerSeekSlider player1={player1} player2={player2} playersRef={playersRef} />
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const PlayerSeekSlider = ({
|
||||||
|
player1,
|
||||||
|
player2,
|
||||||
|
playersRef,
|
||||||
|
}: {
|
||||||
|
player1: any;
|
||||||
|
player2: any;
|
||||||
|
playersRef: any;
|
||||||
|
}) => {
|
||||||
|
const { handleSeekSlider } = useCenterControls({ playersRef });
|
||||||
|
|
||||||
|
const player = useCurrentPlayer();
|
||||||
|
const playbackType = usePlaybackType();
|
||||||
|
const setCurrentTime = useSetCurrentTime();
|
||||||
|
const status = useCurrentStatus();
|
||||||
|
|
||||||
|
const currentSong = useCurrentSong();
|
||||||
|
const songDuration = currentSong?.duration ? currentSong.duration / 1000 : 0;
|
||||||
|
const currentTime = useCurrentTime();
|
||||||
|
const currentPlayerRef = player === 1 ? player1 : player2;
|
||||||
|
const [isSeeking, setIsSeeking] = useState(false);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
let interval: ReturnType<typeof setInterval>;
|
||||||
|
|
||||||
|
if (status === PlayerStatus.PLAYING && !isSeeking) {
|
||||||
|
if (!isElectron() || playbackType === PlaybackType.WEB) {
|
||||||
|
// Update twice a second for slightly better performance
|
||||||
|
interval = setInterval(() => {
|
||||||
|
if (currentPlayerRef) {
|
||||||
|
setCurrentTime(currentPlayerRef.getCurrentTime());
|
||||||
|
}
|
||||||
|
}, 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, [currentPlayerRef, isSeeking, setCurrentTime, playbackType, status]);
|
||||||
|
|
||||||
|
const { showTimeRemaining } = useAppStore();
|
||||||
|
const { setShowTimeRemaining } = useAppStoreActions();
|
||||||
|
const formattedDuration = formatDuration(songDuration * 1000 || 0);
|
||||||
|
const formattedTimeRemaining = formatDuration((currentTime - songDuration) * 1000 || 0);
|
||||||
|
const formattedTime = formatDuration(currentTime * 1000 || 0);
|
||||||
|
const [seekValue, setSeekValue] = useState(0);
|
||||||
|
|
||||||
|
return (
|
||||||
<div className={styles.sliderContainer}>
|
<div className={styles.sliderContainer}>
|
||||||
<div className={styles.sliderValueWrapper}>
|
<div className={styles.sliderValueWrapper}>
|
||||||
<Text
|
<Text
|
||||||
|
|
@ -306,6 +327,5 @@ export const CenterControls = ({ playersRef }: CenterControlsProps) => {
|
||||||
</Text>
|
</Text>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue