"use client" import type React from "react" import { motion } from "motion/react" import { useMemo, useState, useCallback, useEffect, useRef, createContext, useContext } from "react" import { Volume2, VolumeX } from "lucide-react" interface AudioContextType { isMuted: boolean toggleMute: () => void playClick: () => void } const SplitFlapAudioContext = createContext(null) export function useSplitFlapAudio() { return useContext(SplitFlapAudioContext) } export function SplitFlapAudioProvider({ children }: { children: React.ReactNode }) { const [isMuted, setIsMuted] = useState(true) const audioContextRef = useRef(null) const getAudioContext = useCallback(() => { if (typeof window === "undefined") return null if (!audioContextRef.current) { const AudioContextClass = window.AudioContext || (window as Window & { webkitAudioContext?: typeof AudioContext }).webkitAudioContext if (AudioContextClass) { audioContextRef.current = new AudioContextClass() } } return audioContextRef.current }, []) const triggerHaptic = useCallback(() => { if (isMuted) return if (typeof navigator !== "undefined" && "vibrate" in navigator) { navigator.vibrate(10) } }, [isMuted]) const playClick = useCallback(() => { if (isMuted) return triggerHaptic() try { const ctx = getAudioContext() if (!ctx) return if (ctx.state === "suspended") { ctx.resume() } const oscillator = ctx.createOscillator() const gainNode = ctx.createGain() const filter = ctx.createBiquadFilter() const lowpass = ctx.createBiquadFilter() oscillator.type = "square" oscillator.frequency.setValueAtTime(800 + Math.random() * 400, ctx.currentTime) oscillator.frequency.exponentialRampToValueAtTime(200, ctx.currentTime + 0.015) filter.type = "bandpass" filter.frequency.setValueAtTime(1200, ctx.currentTime) filter.Q.setValueAtTime(0.8, ctx.currentTime) lowpass.type = "lowpass" lowpass.frequency.value = 2500 lowpass.Q.value = 0.5 gainNode.gain.setValueAtTime(0.05, ctx.currentTime) gainNode.gain.exponentialRampToValueAtTime(0.001, ctx.currentTime + 0.02) oscillator.connect(filter) filter.connect(gainNode) gainNode.connect(lowpass) lowpass.connect(ctx.destination) oscillator.start(ctx.currentTime) oscillator.stop(ctx.currentTime + 0.02) } catch { // Audio not supported } }, [isMuted, getAudioContext, triggerHaptic]) const toggleMute = useCallback(() => { setIsMuted((prev) => !prev) if (isMuted) { try { const ctx = getAudioContext() if (ctx && ctx.state === "suspended") { ctx.resume() } } catch { // Audio not supported } } }, [isMuted, getAudioContext]) const value = useMemo(() => ({ isMuted, toggleMute, playClick }), [isMuted, toggleMute, playClick]) return {children} } export function SplitFlapMuteToggle({ className = "" }: { className?: string }) { const audio = useSplitFlapAudio() if (!audio) return null return ( ) } interface SplitFlapTextProps { text: string className?: string speed?: number fontSize?: string | number } const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("") function SplitFlapTextInner({ text, className = "", speed = 50, fontSize = "inherit" }: SplitFlapTextProps) { const chars = useMemo(() => text.split(""), [text]) const [animationKey, setAnimationKey] = useState(0) const [hasInitialized, setHasInitialized] = useState(false) const audio = useSplitFlapAudio() const handleMouseEnter = useCallback(() => { setAnimationKey((prev) => prev + 1) }, []) useEffect(() => { const timer = setTimeout(() => { setHasInitialized(true) }, 1000) return () => clearTimeout(timer) }, []) return (
{chars.map((char, index) => ( ))}
) } export function SplitFlapText(props: SplitFlapTextProps) { return } interface SplitFlapCharProps { char: string index: number animationKey: number skipEntrance: boolean speed: number fontSize: string | number playClick?: () => void } function SplitFlapChar({ char, index, animationKey, skipEntrance, speed, fontSize, playClick }: SplitFlapCharProps) { const displayChar = CHARSET.includes(char) ? char : " " const isSpace = char === " " const [currentChar, setCurrentChar] = useState(skipEntrance ? displayChar : " ") const [isSettled, setIsSettled] = useState(skipEntrance) const intervalRef = useRef | null>(null) const timeoutRef = useRef | null>(null) const tileDelay = 0.15 * index const bgColor = isSettled ? "hsl(0, 0%, 0%)" : "rgba(249, 115, 22, 0.2)" const textColor = isSettled ? "#ffffff" : "#f97316" useEffect(() => { if (isSpace) return if (intervalRef.current) clearInterval(intervalRef.current) if (timeoutRef.current) clearTimeout(timeoutRef.current) const baseFlips = 8 const startDelay = skipEntrance ? tileDelay * 400 : tileDelay * 800 let flipIndex = 0 let hasStartedSettling = false timeoutRef.current = setTimeout(() => { setIsSettled(false) setCurrentChar(CHARSET[Math.floor(Math.random() * CHARSET.length)]) intervalRef.current = setInterval(() => { const settleThreshold = baseFlips + index * 3 if (flipIndex >= settleThreshold && !hasStartedSettling) { hasStartedSettling = true if (intervalRef.current) clearInterval(intervalRef.current) setCurrentChar(displayChar) setIsSettled(true) if (playClick) playClick() return } setCurrentChar(CHARSET[Math.floor(Math.random() * CHARSET.length)]) if (flipIndex % 2 === 0 && playClick) playClick() flipIndex++ }, speed) }, startDelay) return () => { if (intervalRef.current) clearInterval(intervalRef.current) if (timeoutRef.current) clearTimeout(timeoutRef.current) } }, [displayChar, isSpace, tileDelay, animationKey, skipEntrance, index, speed, playClick]) if (isSpace) { return (
) } return (
{currentChar}
{currentChar}
{currentChar}
) }