added sound setting
This commit is contained in:
parent
a00bdba56b
commit
891dde40b6
|
|
@ -4,6 +4,7 @@ export const userSettings = z.object({
|
||||||
placeQuestionMark: z.boolean().default(false),
|
placeQuestionMark: z.boolean().default(false),
|
||||||
longPressOnDesktop: z.boolean().default(false),
|
longPressOnDesktop: z.boolean().default(false),
|
||||||
showRevealAnimation: z.boolean().default(true),
|
showRevealAnimation: z.boolean().default(true),
|
||||||
|
soundEnabled: z.boolean().default(true),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type UserSettings = z.infer<typeof userSettings>;
|
export type UserSettings = z.infer<typeof userSettings>;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import { useCallback, useRef } from 'react';
|
import { useCallback, useRef } from 'react';
|
||||||
|
import { useWSQuery } from '../hooks';
|
||||||
|
|
||||||
interface AudioOptions {
|
interface AudioOptions {
|
||||||
volume?: number;
|
volume?: number;
|
||||||
|
|
@ -7,8 +8,14 @@ interface AudioOptions {
|
||||||
|
|
||||||
export const useAudio = (src: string, options: AudioOptions = {}) => {
|
export const useAudio = (src: string, options: AudioOptions = {}) => {
|
||||||
const audioRef = useRef<HTMLAudioElement | null>(null);
|
const audioRef = useRef<HTMLAudioElement | null>(null);
|
||||||
|
const { data: settings } = useWSQuery("user.getSettings", null);
|
||||||
|
|
||||||
const play = useCallback(() => {
|
const play = useCallback(() => {
|
||||||
|
// Check if sound is disabled in settings
|
||||||
|
if (settings && settings.soundEnabled === false) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!audioRef.current) {
|
if (!audioRef.current) {
|
||||||
audioRef.current = new Audio(src);
|
audioRef.current = new Audio(src);
|
||||||
audioRef.current.volume = options.volume ?? 1;
|
audioRef.current.volume = options.volume ?? 1;
|
||||||
|
|
@ -20,7 +27,7 @@ export const useAudio = (src: string, options: AudioOptions = {}) => {
|
||||||
audioRef.current.play().catch((error) => {
|
audioRef.current.play().catch((error) => {
|
||||||
console.warn('Audio play failed:', error);
|
console.warn('Audio play failed:', error);
|
||||||
});
|
});
|
||||||
}, [src, options.volume, options.loop]);
|
}, [src, options.volume, options.loop, settings]);
|
||||||
|
|
||||||
const pause = useCallback(() => {
|
const pause = useCallback(() => {
|
||||||
audioRef.current?.pause();
|
audioRef.current?.pause();
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,15 @@ const Settings = () => {
|
||||||
refetch();
|
refetch();
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
<BoolSetting
|
||||||
|
label="Sound Effects"
|
||||||
|
description={<>Enable or disable sound effects in the game.</>}
|
||||||
|
value={settings?.soundEnabled ?? true}
|
||||||
|
onChange={async (value) => {
|
||||||
|
await updateSettings.mutateAsync({ soundEnabled: value });
|
||||||
|
refetch();
|
||||||
|
}}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue