Handle play() and load() promises for audio elements
Updated audio playback and preloading logic to check for and handle returned promises from play() and load() methods. This prevents uncaught promise rejections in browsers where these methods may return undefined, improving reliability and error handling for notification sounds.
This commit is contained in:
@@ -438,7 +438,7 @@ export default function AdminAnalytics() {
|
|||||||
<span>Today: {analyticsData?.orders?.totalToday || 0}</span>
|
<span>Today: {analyticsData?.orders?.totalToday || 0}</span>
|
||||||
<TrendIndicator
|
<TrendIndicator
|
||||||
current={analyticsData?.orders?.totalToday || 0}
|
current={analyticsData?.orders?.totalToday || 0}
|
||||||
previous={(analyticsData?.orders?.total || 0) / 30} // Rough estimate
|
previous={(analyticsData?.orders?.total || 0) / 30}
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -12,9 +12,16 @@ export function AudioPreloader() {
|
|||||||
audio.preload = 'auto'
|
audio.preload = 'auto'
|
||||||
|
|
||||||
// Try to load it immediately
|
// Try to load it immediately
|
||||||
audio.load().catch(err => {
|
try {
|
||||||
|
const loadPromise = audio.load();
|
||||||
|
if (loadPromise !== undefined) {
|
||||||
|
loadPromise.catch(err => {
|
||||||
console.log('Audio preload failed (non-critical):', err)
|
console.log('Audio preload failed (non-critical):', err)
|
||||||
})
|
});
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
console.log('Audio preload failed (non-critical):', err)
|
||||||
|
}
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
// Cleanup
|
// Cleanup
|
||||||
|
|||||||
@@ -153,7 +153,9 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
|
|||||||
const playNotificationSound = () => {
|
const playNotificationSound = () => {
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
audioRef.current.currentTime = 0;
|
audioRef.current.currentTime = 0;
|
||||||
audioRef.current.play().catch(err => {
|
const playPromise = audioRef.current.play();
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch(err => {
|
||||||
console.log('Error playing sound:', err);
|
console.log('Error playing sound:', err);
|
||||||
// Fallback to simple beep if audio file fails
|
// Fallback to simple beep if audio file fails
|
||||||
try {
|
try {
|
||||||
@@ -168,6 +170,7 @@ export default function ChatDetail({ chatId }: { chatId: string }) {
|
|||||||
console.error('Could not play fallback audio', e);
|
console.error('Could not play fallback audio', e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Fallback to simple beep if audio element is not available
|
// Fallback to simple beep if audio element is not available
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -92,10 +92,13 @@ export default function ChatTable() {
|
|||||||
// Play notification sound
|
// Play notification sound
|
||||||
const playNotificationSound = () => {
|
const playNotificationSound = () => {
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
audioRef.current.play().catch(e => {
|
const playPromise = audioRef.current.play();
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch(e => {
|
||||||
console.log("Failed to play notification sound:", e);
|
console.log("Failed to play notification sound:", e);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get vendor ID from JWT token
|
// Get vendor ID from JWT token
|
||||||
|
|||||||
@@ -51,7 +51,9 @@ export default function OrderNotifications() {
|
|||||||
const playNotificationSound = () => {
|
const playNotificationSound = () => {
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
audioRef.current.currentTime = 0;
|
audioRef.current.currentTime = 0;
|
||||||
audioRef.current.play().catch(err => {
|
const playPromise = audioRef.current.play();
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch(err => {
|
||||||
console.log('Error playing sound:', err);
|
console.log('Error playing sound:', err);
|
||||||
// Fallback to simple beep if audio file fails
|
// Fallback to simple beep if audio file fails
|
||||||
try {
|
try {
|
||||||
@@ -67,6 +69,7 @@ export default function OrderNotifications() {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@@ -147,7 +147,9 @@ export function NotificationProvider({ children }: NotificationProviderProps) {
|
|||||||
const playNotificationSound = () => {
|
const playNotificationSound = () => {
|
||||||
if (audioRef.current) {
|
if (audioRef.current) {
|
||||||
audioRef.current.currentTime = 0;
|
audioRef.current.currentTime = 0;
|
||||||
audioRef.current.play().catch(err => {
|
const playPromise = audioRef.current.play();
|
||||||
|
if (playPromise !== undefined) {
|
||||||
|
playPromise.catch(err => {
|
||||||
console.log('Error playing sound:', err);
|
console.log('Error playing sound:', err);
|
||||||
// Fallback beep if audio file fails
|
// Fallback beep if audio file fails
|
||||||
try {
|
try {
|
||||||
@@ -163,6 +165,7 @@ export function NotificationProvider({ children }: NotificationProviderProps) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Check for new paid orders
|
// Check for new paid orders
|
||||||
|
|||||||
Reference in New Issue
Block a user