feat: add soundboard and admin sounds management pages with routing
This commit is contained in:
205
src/pages/SoundboardPage.tsx
Normal file
205
src/pages/SoundboardPage.tsx
Normal file
@@ -0,0 +1,205 @@
|
||||
import React, { useState, useEffect } from 'react';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Play, Square, Volume2 } from 'lucide-react';
|
||||
import { apiService } from '@/services/api';
|
||||
|
||||
interface Sound {
|
||||
id: number;
|
||||
name: string;
|
||||
filename: string;
|
||||
type: string;
|
||||
duration: number;
|
||||
play_count: number;
|
||||
is_normalized: boolean;
|
||||
normalized_filename?: string;
|
||||
}
|
||||
|
||||
interface SoundCardProps {
|
||||
sound: Sound;
|
||||
onPlay: (soundId: number) => void;
|
||||
isPlaying: boolean;
|
||||
}
|
||||
|
||||
const SoundCard: React.FC<SoundCardProps> = ({ sound, onPlay, isPlaying }) => {
|
||||
const handlePlay = () => {
|
||||
onPlay(sound.id);
|
||||
};
|
||||
|
||||
const formatDuration = (seconds: number) => {
|
||||
const mins = Math.floor(seconds / 60);
|
||||
const secs = seconds % 60;
|
||||
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
||||
};
|
||||
|
||||
return (
|
||||
<Card className="transition-all duration-200 hover:shadow-lg cursor-pointer group">
|
||||
<CardHeader className="pb-3">
|
||||
<CardTitle className="text-sm font-medium truncate" title={sound.name}>
|
||||
{sound.name}
|
||||
</CardTitle>
|
||||
</CardHeader>
|
||||
<CardContent className="pt-0">
|
||||
<div className="flex items-center justify-between mb-2">
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground">
|
||||
<Volume2 size={12} />
|
||||
<span>{formatDuration(sound.duration)}</span>
|
||||
</div>
|
||||
<div className="text-xs text-muted-foreground">
|
||||
{sound.play_count} plays
|
||||
</div>
|
||||
</div>
|
||||
<Button
|
||||
onClick={handlePlay}
|
||||
className="w-full"
|
||||
variant={isPlaying ? "secondary" : "default"}
|
||||
size="sm"
|
||||
>
|
||||
<Play size={16} className="mr-2" />
|
||||
{isPlaying ? 'Playing...' : 'Play'}
|
||||
</Button>
|
||||
{sound.is_normalized && (
|
||||
<div className="mt-2 text-xs text-green-600 text-center">
|
||||
Normalized
|
||||
</div>
|
||||
)}
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
};
|
||||
|
||||
export default function SoundboardPage() {
|
||||
const [sounds, setSounds] = useState<Sound[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [playingSound, setPlayingSound] = useState<number | null>(null);
|
||||
const [searchTerm, setSearchTerm] = useState('');
|
||||
|
||||
useEffect(() => {
|
||||
fetchSounds();
|
||||
}, []);
|
||||
|
||||
const fetchSounds = async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await apiService.get('/api/soundboard/sounds?type=SDB');
|
||||
const data = await response.json();
|
||||
setSounds(data.sounds || []);
|
||||
} catch (err) {
|
||||
setError('Failed to load sounds');
|
||||
console.error('Error fetching sounds:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
const handlePlaySound = async (soundId: number) => {
|
||||
try {
|
||||
setPlayingSound(soundId);
|
||||
await apiService.post(`/api/soundboard/sounds/${soundId}/play`);
|
||||
|
||||
// Reset playing state after a short delay
|
||||
setTimeout(() => {
|
||||
setPlayingSound(null);
|
||||
}, 1000);
|
||||
} catch (err) {
|
||||
setError('Failed to play sound');
|
||||
console.error('Error playing sound:', err);
|
||||
setPlayingSound(null);
|
||||
}
|
||||
};
|
||||
|
||||
const handleStopAll = async () => {
|
||||
try {
|
||||
await apiService.post('/api/soundboard/stop-all');
|
||||
setPlayingSound(null);
|
||||
} catch (err) {
|
||||
setError('Failed to stop sounds');
|
||||
console.error('Error stopping sounds:', err);
|
||||
}
|
||||
};
|
||||
|
||||
const handleForceStopAll = async () => {
|
||||
try {
|
||||
const response = await apiService.post('/api/soundboard/force-stop');
|
||||
const data = await response.json();
|
||||
setPlayingSound(null);
|
||||
alert(`Force stopped ${data.stopped_count} sound instances`);
|
||||
} catch (err) {
|
||||
setError('Failed to force stop sounds');
|
||||
console.error('Error force stopping sounds:', err);
|
||||
}
|
||||
};
|
||||
|
||||
const filteredSounds = sounds.filter(sound =>
|
||||
sound.name.toLowerCase().includes(searchTerm.toLowerCase())
|
||||
);
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<div className="text-lg">Loading sounds...</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
if (error) {
|
||||
return (
|
||||
<div className="flex justify-center items-center h-64">
|
||||
<div className="text-lg text-red-500">{error}</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Soundboard</h1>
|
||||
<div className="flex gap-2">
|
||||
<Button onClick={handleStopAll} variant="outline" size="sm">
|
||||
<Square size={16} className="mr-2" />
|
||||
Stop All
|
||||
</Button>
|
||||
<Button onClick={handleForceStopAll} variant="outline" size="sm" className="text-red-600">
|
||||
<Square size={16} className="mr-2" />
|
||||
Force Stop
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-4">
|
||||
<div className="flex-1">
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search sounds..."
|
||||
value={searchTerm}
|
||||
onChange={(e) => setSearchTerm(e.target.value)}
|
||||
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="text-sm text-muted-foreground">
|
||||
{filteredSounds.length} of {sounds.length} sounds
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4 xl:grid-cols-5 gap-4">
|
||||
{filteredSounds.map((sound) => (
|
||||
<SoundCard
|
||||
key={sound.id}
|
||||
sound={sound}
|
||||
onPlay={handlePlaySound}
|
||||
isPlaying={playingSound === sound.id}
|
||||
/>
|
||||
))}
|
||||
</div>
|
||||
|
||||
{filteredSounds.length === 0 && (
|
||||
<div className="text-center py-12">
|
||||
<div className="text-lg text-muted-foreground">
|
||||
{searchTerm ? 'No sounds found matching your search.' : 'No sounds available.'}
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user