import { useState, useEffect, useRef } from "react"; import { Paper, Box } from "@mantine/core"; import { Avatar as MantineAvatar, AvatarProps as MantineAvatarProps, } from "@mantine/core"; interface GlitchAvatarProps extends Omit { name: string; src?: string; glitchSrc?: string; size?: number; radius?: string | number; withBorder?: boolean; contain?: boolean; children?: React.ReactNode; px?: string | number; } const GlitchAvatar = ({ name, src, glitchSrc, size = 35, radius = "100%", withBorder = true, contain = false, children, px, ...props }: GlitchAvatarProps) => { const [showGlitch, setShowGlitch] = useState(false); const [isPlaying, setIsPlaying] = useState(false); const videoRef = useRef(null); useEffect(() => { if (!glitchSrc) return; const scheduleNextGlitch = () => { const delay = Math.random() * 10000 + 5000; return setTimeout(() => { setShowGlitch(true); setIsPlaying(true); setTimeout(() => { setShowGlitch(false); setIsPlaying(false); scheduleNextGlitch(); }, 4000); }, delay); }; const timeoutId = scheduleNextGlitch(); return () => clearTimeout(timeoutId); }, [glitchSrc]); useEffect(() => { const video = videoRef.current; if (!video) return; const handleEnded = () => { setShowGlitch(false); setIsPlaying(false); }; video.addEventListener("ended", handleEnded); return () => video.removeEventListener("ended", handleEnded); }, []); useEffect(() => { const video = videoRef.current; if (!video) return; video.load(); }, [glitchSrc]); useEffect(() => { const video = videoRef.current; if (!video || !showGlitch || !isPlaying) return; video.currentTime = 0; video.play().catch((err) => { console.error("Failed to play glitch", err); }); }, [showGlitch, isPlaying]); return ( {children} {glitchSrc && ( )} ); }; export default GlitchAvatar;