File size: 4,075 Bytes
80d8c84
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { useEffect, useRef } from 'react';
import { motion } from 'framer-motion';
import { Play, Pause, FastForward, RotateCcw } from 'lucide-react';
import { cn } from '@/lib/utils';

interface AutoPlayControlsProps {
  isPlaying: boolean;
  speed: number;
  round: number;
  maxRounds: number;
  done: boolean;
  onTogglePlay: () => void;
  onSpeedChange: (speed: number) => void;
  className?: string;
}

const SPEEDS = [
  { value: 0.5, label: '0.5x' },
  { value: 1, label: '1x' },
  { value: 2, label: '2x' },
  { value: 4, label: '4x' },
];

export default function AutoPlayControls({
  isPlaying,
  speed,
  round,
  maxRounds,
  done,
  onTogglePlay,
  onSpeedChange,
  className,
}: AutoPlayControlsProps) {
  return (
    <motion.div
      className={cn('rounded-lg border border-primary/30 bg-primary/5 p-3', className)}
      initial={{ opacity: 0, height: 0 }}
      animate={{ opacity: 1, height: 'auto' }}
      exit={{ opacity: 0, height: 0 }}
    >
      <div className="mb-2 flex items-center justify-between">
        <span className="text-xs font-semibold text-primary">Auto-Play</span>
        <div className="flex items-center gap-1">
          {SPEEDS.map((s) => (
            <button
              key={s.value}
              onClick={() => onSpeedChange(s.value)}
              className={cn(
                'rounded px-1.5 py-0.5 text-[10px] font-bold transition-colors',
                speed === s.value
                  ? 'bg-primary text-primary-foreground'
                  : 'text-muted-foreground hover:bg-muted',
              )}
            >
              {s.label}
            </button>
          ))}
        </div>
      </div>
      <div className="flex items-center gap-2">
        <button
          onClick={onTogglePlay}
          disabled={done}
          className={cn(
            'flex items-center gap-1.5 rounded-md px-3 py-1.5 text-xs font-medium transition-colors',
            isPlaying
              ? 'bg-judge/20 text-judge hover:bg-judge/30'
              : 'bg-primary text-primary-foreground hover:bg-primary/90',
            done && 'opacity-50 cursor-not-allowed',
          )}
        >
          {isPlaying ? (
            <>
              <Pause className="h-3.5 w-3.5" /> Pause
            </>
          ) : done ? (
            <>
              <RotateCcw className="h-3.5 w-3.5" /> Done
            </>
          ) : (
            <>
              <Play className="h-3.5 w-3.5" /> Watch Episode
            </>
          )}
        </button>
        <div className="flex-1">
          <div className="h-1.5 w-full overflow-hidden rounded-full bg-muted">
            <motion.div
              className="h-full rounded-full bg-primary"
              animate={{ width: `${maxRounds > 0 ? (round / maxRounds) * 100 : 0}%` }}
              transition={{ duration: 0.3 }}
            />
          </div>
          <div className="mt-0.5 flex justify-between text-[10px] text-muted-foreground">
            <span>Round {round}</span>
            <span>{maxRounds} max</span>
          </div>
        </div>
      </div>
      {isPlaying && (
        <motion.div
          className="mt-2 flex items-center gap-1.5 text-[10px] text-primary"
          animate={{ opacity: [0.5, 1, 0.5] }}
          transition={{ duration: 1.5, repeat: Infinity }}
        >
          <FastForward className="h-3 w-3" />
          Running at {speed}x speed...
        </motion.div>
      )}
    </motion.div>
  );
}

export function useAutoPlay(
  onStep: (() => void) | undefined,
  speed: number,
  isPlaying: boolean,
  done: boolean,
  loading: boolean,
) {
  const timerRef = useRef<ReturnType<typeof setTimeout> | null>(null);

  useEffect(() => {
    if (!isPlaying || done || loading || !onStep) {
      if (timerRef.current) clearTimeout(timerRef.current);
      return;
    }

    const delay = 3000 / speed;
    timerRef.current = setTimeout(() => {
      onStep();
    }, delay);

    return () => {
      if (timerRef.current) clearTimeout(timerRef.current);
    };
  }, [isPlaying, done, loading, speed, onStep]);
}