gbclient/app/chat/providers/sound-provider.tsx

29 lines
748 B
TypeScript
Raw Normal View History

"use client";
import React, { createContext, useContext, useCallback } from 'react';
const SoundContext = createContext(undefined);
export function SoundProvider({ children }) {
const [enabled, setEnabled] = React.useState(true);
const playSound = useCallback((sound) => {
if (!enabled) return;
const audio = new Audio();
audio.play().catch((err) => console.error('Failed to play sound:', err));
}, [enabled]);
return (
<SoundContext.Provider value={{ playSound, setEnabled }}>
{children}
</SoundContext.Provider>
);
}
export function useSound() {
const context = React.useContext(SoundContext);
if (!context) {
throw new Error('useSound must be used within SoundProvider');
}
return context;
}