38 lines
830 B
TypeScript
38 lines
830 B
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo } from "react";
|
|
|
|
import { audioService } from "@/services/audio";
|
|
import { AudioUrls } from "@/shared/constants/audio";
|
|
|
|
interface UseAudioOptions {
|
|
enabled?: boolean;
|
|
preload?: AudioUrls[];
|
|
}
|
|
|
|
export const useAudio = (options: UseAudioOptions = {}) => {
|
|
const { enabled = true, preload = [] } = options;
|
|
|
|
useEffect(() => {
|
|
audioService.setEnabled(enabled);
|
|
}, [enabled]);
|
|
|
|
useEffect(() => {
|
|
if (preload.length > 0) {
|
|
audioService.preload(preload);
|
|
}
|
|
}, [preload]);
|
|
|
|
const playNewMessageNotification = useCallback(() => {
|
|
audioService.play(AudioUrls.NewMessage);
|
|
}, []);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
playNewMessageNotification,
|
|
isEnabled: audioService.isAudioEnabled(),
|
|
}),
|
|
[playNewMessageNotification]
|
|
);
|
|
};
|