From f79e0109fe4e6ae82755c4abae6eb304ee2b18ff Mon Sep 17 00:00:00 2001 From: Federico Rao <157750791+Federicorao@users.noreply.github.com> Date: Fri, 12 Jun 2026 16:05:57 +0200 Subject: [PATCH] Guard against undefined AudioContext in useAudioContext hook (#39397) --- app/javascript/mastodon/hooks/useAudioContext.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/app/javascript/mastodon/hooks/useAudioContext.ts b/app/javascript/mastodon/hooks/useAudioContext.ts index 84acf5ac7f..2cad7ee3a5 100644 --- a/app/javascript/mastodon/hooks/useAudioContext.ts +++ b/app/javascript/mastodon/hooks/useAudioContext.ts @@ -18,7 +18,7 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { const gainNodeRef = useRef(); useEffect(() => { - if (!audioElementRef.current) { + if (!audioElementRef.current || typeof AudioContext === 'undefined') { return; } @@ -43,13 +43,17 @@ export const useAudioContext = ({ audioElementRef }: AudioContextOptions) => { }, [audioElementRef]); const playAudio = useCallback(() => { - void audioElementRef.current?.play(); - void audioContextRef.current?.resume(); + if (audioContextRef.current && audioElementRef.current) { + void audioElementRef.current.play(); + void audioContextRef.current.resume(); + } }, [audioElementRef]); const pauseAudio = useCallback(() => { - audioElementRef.current?.pause(); - void audioContextRef.current?.suspend(); + if (audioContextRef.current && audioElementRef.current) { + audioElementRef.current.pause(); + void audioContextRef.current.suspend(); + } }, [audioElementRef]); return {