2025-03-30 19:04:24 -03:00
|
|
|
import React, { createContext, useContext, useCallback } from 'react';
|
|
|
|
import { invoke } from '@tauri-apps/api/tauri';
|
2025-03-30 16:42:51 -03:00
|
|
|
|
|
|
|
interface SoundContextType {
|
|
|
|
playSound: (sound: string) => void;
|
|
|
|
setEnabled: (enabled: boolean) => void;
|
|
|
|
}
|
|
|
|
|
2025-03-30 19:04:24 -03:00
|
|
|
const SoundContext = createContext<SoundContextType | undefined>(undefined);
|
2025-03-30 16:42:51 -03:00
|
|
|
|
|
|
|
export function SoundProvider({ children }: { children: React.ReactNode }) {
|
2025-03-30 19:04:24 -03:00
|
|
|
const [enabled, setEnabled] = React.useState(true);
|
2025-03-30 16:42:51 -03:00
|
|
|
|
2025-03-30 19:04:24 -03:00
|
|
|
const playSound = useCallback(async (sound: string) => {
|
|
|
|
if (!enabled) return;
|
|
|
|
try {
|
|
|
|
await invoke('play_sound', { sound });
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Failed to play sound:', error);
|
|
|
|
}
|
|
|
|
}, [enabled]);
|
2025-03-30 16:42:51 -03:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|