fixed audio effects issue
This commit is contained in:
parent
0d39e916f6
commit
58ebeea23b
@ -83,6 +83,10 @@ export function useAudioEffects({
|
|||||||
const wasHoveredRef = useRef(false);
|
const wasHoveredRef = useRef(false);
|
||||||
const wasActiveRef = useRef(false);
|
const wasActiveRef = useRef(false);
|
||||||
|
|
||||||
|
// Track if initial state sync is complete (skip audio on first render)
|
||||||
|
// User can't hover during mount, so any true state is stale
|
||||||
|
const didInitRef = useRef(false);
|
||||||
|
|
||||||
// Track which URLs we've already fetched to prevent duplicate fetches
|
// Track which URLs we've already fetched to prevent duplicate fetches
|
||||||
const lastFetchedHoverUrlRef = useRef<string | null>(null);
|
const lastFetchedHoverUrlRef = useRef<string | null>(null);
|
||||||
const lastFetchedClickUrlRef = useRef<string | null>(null);
|
const lastFetchedClickUrlRef = useRef<string | null>(null);
|
||||||
@ -238,6 +242,14 @@ export function useAudioEffects({
|
|||||||
|
|
||||||
// Play hover audio when hover starts (plays to completion, not interrupted)
|
// Play hover audio when hover starts (plays to completion, not interrupted)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Skip audio on initial render - sync state only
|
||||||
|
// User can't hover during mount, so any true state is stale
|
||||||
|
if (!didInitRef.current) {
|
||||||
|
didInitRef.current = true;
|
||||||
|
wasHoveredRef.current = isHovered;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isHovered &&
|
isHovered &&
|
||||||
!wasHoveredRef.current &&
|
!wasHoveredRef.current &&
|
||||||
@ -257,6 +269,13 @@ export function useAudioEffects({
|
|||||||
|
|
||||||
// Play click audio when active state begins
|
// Play click audio when active state begins
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
// Skip audio on initial render - sync state only
|
||||||
|
// Note: didInitRef is already set by hover effect due to effect ordering
|
||||||
|
if (!didInitRef.current) {
|
||||||
|
wasActiveRef.current = isActive;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
isActive &&
|
isActive &&
|
||||||
!wasActiveRef.current &&
|
!wasActiveRef.current &&
|
||||||
@ -293,11 +312,12 @@ export function useAudioEffects({
|
|||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Reset audio on key change (e.g., page navigation)
|
// Reset audio on key change (e.g., page navigation or element change)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
stopAll();
|
stopAll();
|
||||||
wasHoveredRef.current = false;
|
wasHoveredRef.current = false;
|
||||||
wasActiveRef.current = false;
|
wasActiveRef.current = false;
|
||||||
|
didInitRef.current = false; // Reset for new element
|
||||||
}, [resetKey, stopAll]);
|
}, [resetKey, stopAll]);
|
||||||
|
|
||||||
return { playClickAudio, stopAll };
|
return { playClickAudio, stopAll };
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user