Add audio feedback for toast notifications; implement playAudio function in ToastProvider

This commit is contained in:
Md Asif 2024-12-21 20:56:02 +05:30
parent 96784c02c7
commit 2b940c3d43
3 changed files with 13 additions and 0 deletions

BIN
public/audio/error.mp3 Normal file

Binary file not shown.

BIN
public/audio/warning.mp3 Normal file

Binary file not shown.

View File

@ -8,7 +8,20 @@ export const ToastContext = createContext();
export const ToastProvider = ({ children }) => {
const [toast, setToast] = useState({ show: false, message: "", type: "" });
const playAudio = (type) => {
let audioSrc;
if(type === "warning") audioSrc = "/audio/warning.mp3";
else if (type === "error") audioSrc = "/audio/error.mp3";
if (audioSrc) {
const audio = new Audio(audioSrc);
audio.play().catch((error) => console.error("Error playing audio:", error));
}
};
const showToast = (message, type) => {
playAudio(type);
setToast({ show: true, message, type });
setTimeout(() => {
setToast({ show: false, message: "", type: "" });