|
| 1 | +import { Billboard, Edges, Html, Line } from '@react-three/drei' |
| 2 | +import { useCallback, useMemo, useState } from 'react' |
| 3 | +import * as THREE from 'three' |
| 4 | +import { useControlStore } from '~/stores/useControlStore' |
| 5 | +import { useMindsetStore } from '~/stores/useMindsetStore' |
| 6 | +import { usePlayerStore } from '~/stores/usePlayerStore' |
| 7 | + |
| 8 | +export const Highlights = () => { |
| 9 | + const [selectedId, setSelectedId] = useState<string | null>(null) |
| 10 | + const cameraControlsRef = useControlStore((s) => s.cameraControlsRef) |
| 11 | + const { playerRef } = usePlayerStore((s) => s) |
| 12 | + const highlights = useMindsetStore((s) => s.highlights) |
| 13 | + |
| 14 | + const positions = useMemo( |
| 15 | + () => |
| 16 | + highlights.map((_, i) => { |
| 17 | + const angle = (i / highlights.length) * Math.PI * 2 |
| 18 | + const radius = 1000 |
| 19 | + |
| 20 | + return [Math.cos(angle) * radius, (i % 2 === 0 ? 1 : -1) * 150, Math.sin(angle) * radius] as [ |
| 21 | + number, |
| 22 | + number, |
| 23 | + number, |
| 24 | + ] |
| 25 | + }), |
| 26 | + [highlights], |
| 27 | + ) |
| 28 | + |
| 29 | + const handleProgressChange = useCallback( |
| 30 | + (value: number | number[]) => { |
| 31 | + const newValue = Array.isArray(value) ? value[0] : value |
| 32 | + |
| 33 | + if (playerRef) { |
| 34 | + playerRef.seekTo(newValue, 'seconds') |
| 35 | + } |
| 36 | + }, |
| 37 | + [playerRef], |
| 38 | + ) |
| 39 | + |
| 40 | + const handleClick = (index: number) => { |
| 41 | + const position = new THREE.Vector3(...positions[index]) |
| 42 | + const cameraOffset = new THREE.Vector3(1, 1, 1).normalize().multiplyScalar(300) |
| 43 | + |
| 44 | + cameraControlsRef?.setLookAt( |
| 45 | + position.x + cameraOffset.x, |
| 46 | + position.y + cameraOffset.y, |
| 47 | + position.z + cameraOffset.z, |
| 48 | + position.x, |
| 49 | + position.y, |
| 50 | + position.z, |
| 51 | + true, |
| 52 | + ) |
| 53 | + |
| 54 | + setSelectedId(highlights[index].title) |
| 55 | + handleProgressChange(highlights[index].startTime) |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <group> |
| 60 | + {/* Curved lines between sequential nodes */} |
| 61 | + {positions.map((pos, i) => { |
| 62 | + if (i === 0) { |
| 63 | + return null |
| 64 | + } |
| 65 | + |
| 66 | + const start = new THREE.Vector3(...positions[i - 1]) |
| 67 | + const end = new THREE.Vector3(...positions[i]) |
| 68 | + const mid = start.clone().lerp(end, 0.5).add(new THREE.Vector3(0, 200, 0)) // raise the curve |
| 69 | + const curve = new THREE.QuadraticBezierCurve3(start, mid, end) |
| 70 | + const points = curve.getPoints(20) |
| 71 | + |
| 72 | + // eslint-disable-next-line react/no-array-index-key |
| 73 | + return <Line key={`curve-${i}`} color="#00bfff" dashed={false} lineWidth={1.5} points={points} /> |
| 74 | + })} |
| 75 | + |
| 76 | + {/* Segment nodes */} |
| 77 | + {highlights.map((highlight, i) => ( |
| 78 | + <Billboard key={highlight.title} position={positions[i]}> |
| 79 | + <mesh> |
| 80 | + <circleGeometry args={[50, 64, 64]} /> |
| 81 | + <meshBasicMaterial color={selectedId === highlight.title ? '#2e93b3' : '#8c6a97'} transparent /> |
| 82 | + <Edges color="#ffffff" /> |
| 83 | + <Html center> |
| 84 | + <button |
| 85 | + onClick={() => handleClick(i)} |
| 86 | + onPointerDown={(e) => e.stopPropagation()} |
| 87 | + onPointerOut={(e) => e.stopPropagation()} |
| 88 | + onPointerOver={(e) => e.stopPropagation()} |
| 89 | + onPointerUp={(e) => e.stopPropagation()} |
| 90 | + style={{ |
| 91 | + cursor: 'pointer', |
| 92 | + background: 'rgba(0, 0, 0, 0.8)', |
| 93 | + borderRadius: '50%', |
| 94 | + padding: '6px', |
| 95 | + width: '150px', |
| 96 | + height: '150px', |
| 97 | + color: 'white', |
| 98 | + border: selectedId === highlight.title ? '2px solid #ffd700' : '1px solid #2e93b3', |
| 99 | + textAlign: 'center', |
| 100 | + fontWeight: 'bold', |
| 101 | + fontSize: '12px', |
| 102 | + }} |
| 103 | + type="button" |
| 104 | + > |
| 105 | + {highlight.title} |
| 106 | + </button> |
| 107 | + </Html> |
| 108 | + </mesh> |
| 109 | + </Billboard> |
| 110 | + ))} |
| 111 | + </group> |
| 112 | + ) |
| 113 | +} |
0 commit comments