"use client"; import React, { useEffect, useRef, useState } from 'react'; import 'aframe'; import 'mind-ar/dist/mindar-image-aframe.prod.js'; const TARGET_CONFIG = [ { targetIndex: 0, videoId: 'video0', videoSrc: '/res/tour/riddle_7/videos/Oswin der Stallknecht.mp4' }, { targetIndex: 1, videoId: 'video1', videoSrc: '/res/tour/riddle_7/videos/Mara die Kräuterfrau.mp4' }, { targetIndex: 2, videoId: 'video2', videoSrc: '/res/tour/riddle_7/videos/Edmund der Schreiber.mp4' }, { targetIndex: 3, videoId: 'video3', videoSrc: '/res/tour/riddle_7/videos/Sir Garrick die Torwache.mp4' }, ]; export default function Riddle_7_AR_View() { const sceneRef = useRef(null); const targetsRef = useRef([]); const videosRef = useRef({}); const lostTimesRef = useRef({}); const TARGET_HEIGHT = 1; const [videoDimensions, setVideoDimensions] = useState({}); const [hasInteracted, setHasInteracted] = useState(false); useEffect(() => { const sceneEl = sceneRef.current; const arSystem = sceneEl.systems['mindar-image-system']; sceneEl.addEventListener('renderstart', () => { arSystem.start(); }); targetsRef.current.forEach((targetEl, index) => { if (!targetEl) return; const videoEl = videosRef.current[index]; const handleTargetFound = () => { if (videoEl) { const lostTime = lostTimesRef.current[index]; if (lostTime && Date.now() - lostTime > 10000) { videoEl.currentTime = 0; } lostTimesRef.current[index] = null; videoEl.play().catch((err) => { console.log(`Autoplay blocked on target ${index}:`, err); }); } }; const handleTargetLost = () => { if (videoEl) { videoEl.pause(); lostTimesRef.current[index] = Date.now(); } }; targetEl.addEventListener('targetFound', handleTargetFound); targetEl.addEventListener('targetLost', handleTargetLost); }); return () => { if (arSystem) { try { arSystem.stop(); } catch (error) { console.warn('MindAR stopped after DOM teardown:', error); } } }}, []); const handleLoadedMetadata = (index, videoEl) => { if (videoEl) { const aspect = videoEl.videoWidth / videoEl.videoHeight; setVideoDimensions((prev) => ({ ...prev, [index]: { width: TARGET_HEIGHT * aspect, height: TARGET_HEIGHT, }, })); } }; return (