Compare commits
2 Commits
3f19a4a090
...
f6eb815240
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f6eb815240 | ||
|
|
0e88eed4f8 |
@@ -1,6 +1,5 @@
|
||||
import type { User } from "@/services/auth"
|
||||
import { SidebarMenu, SidebarMenuButton, SidebarMenuItem } from "../ui/sidebar"
|
||||
import { useSocket } from "@/contexts/SocketContext"
|
||||
import NumberFlow from '@number-flow/react'
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
@@ -10,7 +9,6 @@ interface NavPlanProps {
|
||||
|
||||
export function NavPlan({ user }: NavPlanProps) {
|
||||
const [credits, setCredits] = useState(0)
|
||||
const { socket, isConnected } = useSocket()
|
||||
|
||||
useEffect(() => {
|
||||
setCredits(user.credits)
|
||||
@@ -18,19 +16,19 @@ export function NavPlan({ user }: NavPlanProps) {
|
||||
|
||||
// Listen for real-time credits updates
|
||||
useEffect(() => {
|
||||
if (!socket || !isConnected) return
|
||||
|
||||
const handleCreditsChanged = (data: { credits: number }) => {
|
||||
setCredits(data.credits)
|
||||
const handleCreditsChanged = (/*data: { credits: number }*/ event: CustomEvent) => {
|
||||
const { credits } = event.detail
|
||||
setCredits(credits)
|
||||
}
|
||||
|
||||
socket.on("credits_changed", handleCreditsChanged)
|
||||
// Listen for the custom event
|
||||
window.addEventListener('credits_changed', handleCreditsChanged as EventListener);
|
||||
|
||||
// Cleanup listener on unmount
|
||||
// Cleanup
|
||||
return () => {
|
||||
socket.off("credits_changed", handleCreditsChanged)
|
||||
}
|
||||
}, [socket, isConnected])
|
||||
window.removeEventListener('credits_changed', handleCreditsChanged as EventListener);
|
||||
};
|
||||
}, [])
|
||||
|
||||
return (
|
||||
<SidebarMenu>
|
||||
|
||||
@@ -68,13 +68,22 @@ export const SocketProvider: React.FC<SocketProviderProps> = ({ children }) => {
|
||||
setIsConnected(false);
|
||||
});
|
||||
|
||||
// Global socket event listeners for toasts
|
||||
// Global events
|
||||
newSocket.on("error", (data) => {
|
||||
toast.error(data.message || "An error occurred");
|
||||
});
|
||||
|
||||
newSocket.on("credits_required", (data) => {
|
||||
toast.error(`Insufficient credits. Need ${data.credits_needed} credits.`);
|
||||
});
|
||||
|
||||
newSocket.on("error", (data) => {
|
||||
toast.error(data.message || "An error occurred");
|
||||
// Page or component events
|
||||
newSocket.on("credits_changed", (data) => {
|
||||
window.dispatchEvent(new CustomEvent('credits_changed', { detail: data }));
|
||||
});
|
||||
|
||||
newSocket.on("sound_play_count_changed", (data) => {
|
||||
window.dispatchEvent(new CustomEvent('sound_play_count_changed', { detail: data }));
|
||||
});
|
||||
|
||||
setSocket(newSocket);
|
||||
|
||||
@@ -53,8 +53,7 @@ export function AdminSoundsPage() {
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Sound Management</h1>
|
||||
<div className="flex items-center">
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={handleScanSounds}
|
||||
|
||||
@@ -6,6 +6,6 @@ export function DashboardPage() {
|
||||
if (!user) return null
|
||||
|
||||
return (
|
||||
<div>Dashboard</div>
|
||||
<div></div>
|
||||
)
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import { AddUrlDialog } from '@/components/AddUrlDialog'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Card, CardContent } from '@/components/ui/card'
|
||||
import { useSocket } from '@/contexts/SocketContext'
|
||||
// import { useSocket } from '@/contexts/SocketContext'
|
||||
import { useAddUrlShortcut } from '@/hooks/use-keyboard-shortcuts'
|
||||
import { useTheme } from '@/hooks/use-theme'
|
||||
import { formatDuration } from '@/lib/format-duration'
|
||||
@@ -106,7 +106,7 @@ export function SoundboardPage() {
|
||||
const [searchTerm, setSearchTerm] = useState('')
|
||||
const [addUrlDialogOpen, setAddUrlDialogOpen] = useState(false)
|
||||
const [currentColors, setCurrentColors] = useState<string[]>(lightModeColors)
|
||||
const { socket, isConnected } = useSocket()
|
||||
// const { socket, isConnected } = useSocket()
|
||||
|
||||
// Setup keyboard shortcut for CTRL+U
|
||||
useAddUrlShortcut(() => setAddUrlDialogOpen(true))
|
||||
@@ -115,6 +115,30 @@ export function SoundboardPage() {
|
||||
fetchSounds()
|
||||
}, [])
|
||||
|
||||
// Listen for sound_play_count_changed events from socket
|
||||
useEffect(() => {
|
||||
const handleSoundPlayCountChanged = (event: CustomEvent) => {
|
||||
const { sound_id, play_count } = event.detail;
|
||||
|
||||
// Update the sound in the local state
|
||||
setSounds(prevSounds =>
|
||||
prevSounds.map(sound =>
|
||||
sound.id === sound_id
|
||||
? { ...sound, play_count }
|
||||
: sound
|
||||
)
|
||||
);
|
||||
};
|
||||
|
||||
// Listen for the custom event
|
||||
window.addEventListener('sound_play_count_changed', handleSoundPlayCountChanged as EventListener);
|
||||
|
||||
// Cleanup
|
||||
return () => {
|
||||
window.removeEventListener('sound_play_count_changed', handleSoundPlayCountChanged as EventListener);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const { theme } = useTheme()
|
||||
|
||||
useEffect(() => {
|
||||
@@ -146,11 +170,11 @@ export function SoundboardPage() {
|
||||
|
||||
const handlePlaySound = async (soundId: number) => {
|
||||
try {
|
||||
// Try socket.io first if connected
|
||||
if (socket && isConnected) {
|
||||
socket.emit('play_sound', { soundId })
|
||||
return
|
||||
}
|
||||
// // Try socket.io first if connected
|
||||
// if (socket && isConnected) {
|
||||
// socket.emit('play_sound', { soundId })
|
||||
// return
|
||||
// }
|
||||
|
||||
// Fallback to API request
|
||||
await apiService.post(`/api/soundboard/sounds/${soundId}/play`)
|
||||
@@ -203,8 +227,7 @@ export function SoundboardPage() {
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex items-center justify-between">
|
||||
<h1 className="text-2xl font-bold">Soundboard</h1>
|
||||
<div className="flex items-center">
|
||||
<div className="flex gap-2">
|
||||
<Button
|
||||
onClick={() => setAddUrlDialogOpen(true)}
|
||||
|
||||
Reference in New Issue
Block a user