import { createContext, useState } from "react"; import { CircleAlert, X, CircleX, Check } from "lucide-react"; import { toTitleCase } from "../util/util"; import PropTypes from "prop-types"; 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: "" }); }, 7000); }; let toastIcon; let surfaceColor; let borderColor; if(toast.type === "warning") { toastIcon = ; surfaceColor = "bg-warning-surface"; borderColor = "border-warning"; } else if(toast.type === "success") { toastIcon = ; surfaceColor = "bg-success-surface"; borderColor = "border-success"; } else if (toast.type === "error") { toastIcon = ; surfaceColor = "bg-error-surface"; borderColor = "border-error"; } return ( {children} {toast.show && ( {toastIcon} {toTitleCase(toast.type)} {toast.message} setToast({ show: false, message: "", type: "" })}/> )} ); }; ToastProvider.propTypes = { children: PropTypes.node.isRequired, };