feat(notifications): add notification sound playback for new alerts and include sound file documentation
All checks were successful
Build & Push Docker Image / build (push) Successful in 6m27s

This commit is contained in:
2026-03-02 18:32:47 +01:00
parent dc7a78badb
commit 9be3be7712
3 changed files with 34 additions and 0 deletions

View File

@@ -3,6 +3,24 @@ import toast from 'react-hot-toast';
import { useAuth } from './AuthContext';
import api from '../services/api';
// Lazily created Audio instance — reused across calls to avoid memory churn
let _audio = null;
function playNotificationSound() {
try {
if (!_audio) {
_audio = new Audio('/sounds/notification.mp3');
_audio.volume = 0.5;
}
// Reset to start so rapid arrivals always play from beginning
_audio.currentTime = 0;
_audio.play().catch(() => {
// Autoplay blocked (user hasn't interacted yet) or file missing — silent fail
});
} catch {
// Ignore any other errors (e.g. unsupported format)
}
}
const NotificationContext = createContext();
export function NotificationProvider({ children }) {
@@ -30,6 +48,9 @@ export function NotificationProvider({ children }) {
// Subsequent fetches: toast new unread notifications
const newItems = incoming.filter(n => !n.read && !seenIds.current.has(n.id));
if (newItems.length > 0) {
playNotificationSound();
}
newItems.forEach(n => {
seenIds.current.add(n.id);
const icon = notificationIcon(n.type);