'use client'; import { Pause, Volume2 } from 'lucide-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import type { PPTAudioElement } from '@openmaic/dsl'; export interface BaseAudioElementProps { readonly elementInfo: PPTAudioElement; } /** * The audio affordance belongs to the renderer rather than the editor UI so * read-only slide previews can play it directly. Editing mode layers its hit * targets above this button, keeping selection and transform gestures intact. */ export function BaseAudioElement({ elementInfo }: BaseAudioElementProps) { const available = Boolean(elementInfo.src); const audioRef = useRef(null); const [playing, setPlaying] = useState(false); const stop = useCallback(() => { audioRef.current?.pause(); audioRef.current = null; setPlaying(false); }, []); useEffect(() => stop, [elementInfo.id, elementInfo.src, stop]); const togglePlayback = useCallback(async () => { if (!elementInfo.src) return; if (playing) { stop(); return; } const audio = new Audio(elementInfo.src); audio.loop = elementInfo.loop; audio.onended = stop; audio.onerror = stop; audioRef.current = audio; try { await audio.play(); setPlaying(true); } catch { stop(); } }, [elementInfo.loop, elementInfo.src, playing, stop]); return (
); }