'use client'; import { useCallback, useEffect, useRef, useState, } from 'react'; export interface StoryItem { id: string; userId: string; username: string; avatar: string; mediaUrl: string; mediaType: 'image' | 'video'; text?: string; duration: number; createdAt: string; } interface StoryViewerProps { stories: StoryItem[]; startIndex: number; onClose: () => void; } function timeAgo(dateStr: string): string { const diff = Date.now() - new Date(dateStr).getTime(); const seconds = Math.floor(diff / 1000); if (seconds < 60) return `${seconds}s`; const minutes = Math.floor(seconds / 60); if (minutes < 60) return `${minutes}m`; const hours = Math.floor(minutes / 60); if (hours < 24) return `${hours}h`; const days = Math.floor(hours / 24); return `${days}d`; } export default function StoryViewer({ stories, startIndex, onClose }: StoryViewerProps) { const [currentIndex, setCurrentIndex] = useState( Math.max(0, Math.min(startIndex, stories.length - 1)) ); const [progress, setProgress] = useState(0); const [paused, setPaused] = useState(false); const intervalRef = useRef | null>(null); const longPressTimerRef = useRef | null>(null); const touchStartXRef = useRef(null); const videoRef = useRef(null); const currentStory = stories[currentIndex]; const clearProgressInterval = useCallback(() => { if (intervalRef.current !== null) { clearInterval(intervalRef.current); intervalRef.current = null; } }, []); const goToNext = useCallback(() => { setCurrentIndex((prev) => { if (prev < stories.length - 1) return prev + 1; onClose(); return prev; }); setProgress(0); }, [stories.length, onClose]); const goToPrev = useCallback(() => { setCurrentIndex((prev) => Math.max(0, prev - 1)); setProgress(0); }, []); // Start/restart progress animation useEffect(() => { clearProgressInterval(); setProgress(0); if (paused) return; const tickMs = 50; const totalTicks = (currentStory.duration * 1000) / tickMs; intervalRef.current = setInterval(() => { setProgress((prev) => { const next = prev + 100 / totalTicks; if (next >= 100) { clearProgressInterval(); goToNext(); return 100; } return next; }); }, tickMs); return () => clearProgressInterval(); }, [currentIndex, paused, currentStory.duration, clearProgressInterval, goToNext]); // Pause/resume video when paused state changes useEffect(() => { if (!videoRef.current) return; if (paused) { videoRef.current.pause(); } else { videoRef.current.play().catch(() => undefined); } }, [paused]); // Keyboard navigation useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (e.key === 'Escape') onClose(); if (e.key === 'ArrowRight') goToNext(); if (e.key === 'ArrowLeft') goToPrev(); }; window.addEventListener('keydown', handleKeyDown); return () => window.removeEventListener('keydown', handleKeyDown); }, [onClose, goToNext, goToPrev]); // Tap navigation (left half = prev, right half = next) const handleTap = useCallback( (e: React.MouseEvent) => { const rect = e.currentTarget.getBoundingClientRect(); const x = e.clientX - rect.left; if (x < rect.width / 2) { goToPrev(); } else { goToNext(); } }, [goToPrev, goToNext] ); // Long press handlers const handlePointerDown = useCallback(() => { longPressTimerRef.current = setTimeout(() => { setPaused(true); }, 200); }, []); const handlePointerUp = useCallback(() => { if (longPressTimerRef.current !== null) { clearTimeout(longPressTimerRef.current); longPressTimerRef.current = null; } setPaused(false); }, []); // Touch swipe (left/right) const handleTouchStart = useCallback((e: React.TouchEvent) => { touchStartXRef.current = e.touches[0].clientX; longPressTimerRef.current = setTimeout(() => { setPaused(true); }, 200); }, []); const handleTouchEnd = useCallback( (e: React.TouchEvent) => { if (longPressTimerRef.current !== null) { clearTimeout(longPressTimerRef.current); longPressTimerRef.current = null; } setPaused(false); if (touchStartXRef.current === null) return; const deltaX = e.changedTouches[0].clientX - touchStartXRef.current; touchStartXRef.current = null; if (Math.abs(deltaX) > 50) { if (deltaX < 0) { goToNext(); } else { goToPrev(); } } }, [goToNext, goToPrev] ); if (!currentStory) return null; return (
{/* Progress bars */}
{stories.map((story, i) => (
))}
{/* Header */}
{/* eslint-disable-next-line @next/next/no-img-element */} {currentStory.username} { (e.currentTarget as HTMLImageElement).src = `https://ui-avatars.com/api/?name=${encodeURIComponent(currentStory.username)}&background=6366f1&color=fff`; }} />
@{currentStory.username} {timeAgo(currentStory.createdAt)}
{/* Media + tap/swipe layer */}
{currentStory.mediaType === 'image' ? ( // eslint-disable-next-line @next/next/no-img-element Story media ) : (
{/* Text overlay */} {currentStory.text ? (

{currentStory.text}

) : null} {/* Paused indicator */} {paused ? (
) : null}
); }