'use client' import React, { useCallback, useEffect } from 'react' import { Slider } from '@/components/ui/slider' import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Play, Pause, SkipBack, SkipForward, Clock, Calendar } from 'lucide-react' import { format, addDays, subDays, startOfDay } from 'date-fns' import { cn } from '@/lib/utils' import { useTimeline } from '@/hooks/use-timeline' interface TimelineProps { className?: string startDate?: Date endDate?: Date currentDate?: Date onDateChange?: (date: Date) => void onPlay?: () => void onPause?: () => void isPlaying?: boolean speed?: 'slow' | 'normal' | 'fast' onSpeedChange?: (speed: 'slow' | 'normal' | 'fast') => void } export function Timeline({ className, startDate = subDays(new Date(), 30), endDate = new Date(), currentDate = new Date(), onDateChange, onPlay, onPause, isPlaying = false, speed = 'normal', onSpeedChange }: TimelineProps) { // 计算时间轴进度 (0-100) const totalDays = Math.ceil((endDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)) const currentDays = Math.ceil((currentDate.getTime() - startDate.getTime()) / (1000 * 60 * 60 * 24)) const progress = Math.max(0, Math.min(100, (currentDays / totalDays) * 100)) const timeline = useTimeline( { onDateChange(date) { console.log(date) } } ) const forward = useCallback(() => { }, [timeline]) useEffect(() => { const timer = setInterval(() => { // setFrameIndex((prev) => (prev + 1) % 10); // 每隔3秒切换一帧 }, 60000); return () => clearInterval(timer); // 清除定时器 }, []); const handleSliderChange = useCallback((value: number[]) => { const newProgress = value[0] const newDays = Math.round((newProgress / 100) * totalDays) const newDate = addDays(startDate, newDays) onDateChange?.(newDate) }, [startDate, totalDays, onDateChange]) const handlePlayPause = useCallback(() => { if (isPlaying) { onPause?.() } else { onPlay?.() } }, [isPlaying, onPlay, onPause]) const handleSkipBack = useCallback(() => { const newDate = subDays(currentDate, 1) onDateChange?.(newDate) }, [currentDate, onDateChange]) const handleSkipForward = useCallback(() => { const newDate = addDays(currentDate, 1) onDateChange?.(newDate) }, [currentDate, onDateChange]) const speedOptions = [ { value: 'slow', label: '慢速', interval: 2000 }, { value: 'normal', label: '正常', interval: 1000 }, { value: 'fast', label: '快速', interval: 500 } ] return (
{/* 时间显示 */}
{format(startDate, 'yyyy-MM-dd')}
{format(currentDate, 'yyyy-MM-dd')}
{format(endDate, 'yyyy-MM-dd')}
{/* 时间轴滑块 */}
{/* 控制按钮 */}
{/* 速度控制 */}
{speedOptions.map((option) => ( ))}
) }