frontend: implements volume slider

This commit is contained in:
2025-02-15 21:16:04 -08:00
parent 9a8f7b9ac5
commit a06bc3960e
3 changed files with 70 additions and 26 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react';
import React, { useState, useEffect, useCallback } from 'react';
import SongTable from './SongTable';
import NowPlaying from './NowPlaying';
import AddSongPanel from './AddSongPanel';
@@ -8,20 +8,26 @@ const App: React.FC = () => {
const [isPlaying, setIsPlaying] = useState(false);
const [nowPlayingSong, setNowPlayingSong] = useState<string | null>(null);
const [nowPlayingFileName, setNowPlayingFileName] = useState<string | null>(null);
const [volume, setVolume] = useState(100);
const [volumeSettingIsLocked, setVolumeSettingIsLocked] = useState(false);
const [songs, setSongs] = useState<PlaylistItem[]>([]);
const [ws, setWs] = useState<WebSocket | null>(null);
const [_, setWs] = useState<WebSocket | null>(null);
const fetchPlaylist = async () => {
const fetchPlaylist = useCallback(async () => {
const playlist = await API.getPlaylist();
setSongs(playlist);
};
}, []);
const fetchNowPlaying = async () => {
const fetchNowPlaying = useCallback(async () => {
const nowPlaying = await API.getNowPlaying();
setNowPlayingSong(nowPlaying.nowPlaying);
setNowPlayingFileName(nowPlaying.currentFile);
setIsPlaying(!nowPlaying.isPaused);
};
if (!volumeSettingIsLocked) {
setVolume(nowPlaying.volume);
}
}, [volumeSettingIsLocked]);
const handleAddURL = (url: string) => {
const urlToAdd = url.trim();
@@ -71,36 +77,43 @@ const App: React.FC = () => {
fetchNowPlaying();
};
const watchForEvents = () => {
const ws = API.subscribeToEvents((event) => {
switch (event.event) {
case 'user_modify':
case 'end-file':
case 'playback-restart':
fetchPlaylist();
fetchNowPlaying();
break;
}
});
return ws;
const handleVolumeSettingChange = async (volume: number) => {
setVolume(volume);
await API.setVolume(volume);
};
const handleWebSocketEvent = useCallback((event: any) => {
switch (event.event) {
case 'user_modify':
case 'end-file':
case 'playback-restart':
fetchPlaylist();
fetchNowPlaying();
break;
}
}, [fetchPlaylist, fetchNowPlaying]);
// Initial data fetch
useEffect(() => {
fetchPlaylist();
fetchNowPlaying();
setWs(watchForEvents());
}, [fetchPlaylist, fetchNowPlaying]);
// WebSocket connection
useEffect(() => {
const ws = API.subscribeToEvents(handleWebSocketEvent);
setWs(ws);
return () => {
if (ws) {
ws.close();
}
};
}, []);
}, [handleWebSocketEvent]);
return (
<div className="flex items-center justify-center h-screen w-screen bg-black py-10">
<div className="bg-violet-900 w-full md:max-w-xl h-full md:max-h-xl md:border md:rounded-2xl flex flex-col">
<div className="flex items-center justify-center h-screen w-screen bg-black md:py-10">
<div className="bg-violet-900 w-full md:max-w-2xl h-full md:max-h-xl md:border md:rounded-2xl flex flex-col">
<NowPlaying
className="flex flex-row md:rounded-t-2xl"
songName={nowPlayingSong || "(Not Playing)"}
@@ -109,6 +122,10 @@ const App: React.FC = () => {
onPlayPause={togglePlayPause}
onSkip={handleSkip}
onPrevious={handlePrevious}
volume={volume}
onVolumeSettingChange={handleVolumeSettingChange}
onVolumeWillChange={() => setVolumeSettingIsLocked(true)}
onVolumeDidChange={() => setVolumeSettingIsLocked(false)}
/>
{songs.length > 0 ? (