All files / src/components TimerPanel.tsx

94.28% Statements 33/35
86.11% Branches 31/36
87.5% Functions 7/8
93.75% Lines 30/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159                                  1x             6x 1x 1x 1x   5x 5x 5x 4x                         21x                                                               21x 21x   21x 21x   21x 3x   3x 2x 2x 2x 1x   1x   2x     1x       21x 3x     21x       21x   21x                           1x                                                                  
import { Timer as TimerIcon, ChevronUp, ChevronDown } from 'lucide-react'
import { useState } from 'react'
import type { UseTimersReturn } from '../hooks/useTimers'
import { detectTimes } from '../hooks/useTimers'
import { api } from '../api/client'
import { cn } from '../lib/utils'
import QuickTimerButtons from './QuickTimerButtons'
import DetectedTimers from './DetectedTimers'
import TimerList from './TimerList'
 
interface TimerPanelProps {
  timers: UseTimersReturn
  instructionText?: string
  aiAvailable?: boolean
  isLandscape?: boolean
}
 
const QUICK_TIMERS = [
  { label: '5 min', duration: 5 * 60 },
  { label: '10 min', duration: 10 * 60 },
  { label: '15 min', duration: 15 * 60 },
]
 
function formatDetectedTime(seconds: number): string {
  if (seconds >= 3600) {
    const hrs = Math.floor(seconds / 3600)
    const mins = Math.floor((seconds % 3600) / 60)
    return mins > 0 ? `${hrs}h ${mins}m` : `${hrs}h`
  }
  const mins = Math.floor(seconds / 60)
  const secs = seconds % 60
  if (mins === 0) return `${secs}s`
  Eif (secs === 0) return `${mins} min`
  return `${mins}m ${secs}s`
}
 
interface TimerPanelHeaderProps {
  expanded: boolean
  timerCount: number
  activeCount: number
  isLandscape: boolean
  onToggle: () => void
}
 
function TimerPanelHeader({ expanded, timerCount, activeCount, isLandscape, onToggle }: TimerPanelHeaderProps) {
  return (
    <button
      onClick={isLandscape ? undefined : onToggle}
      className={cn(
        'flex w-full shrink-0 items-center justify-between px-4 py-3',
        !isLandscape && 'cursor-pointer',
      )}
    >
      <div className="flex items-center gap-2">
        <TimerIcon className="h-5 w-5 text-primary" />
        <span className="font-medium text-foreground">Timers</span>
        {timerCount > 0 && (
          <span className="rounded-full bg-muted px-2 py-0.5 text-xs text-muted-foreground">
            {timerCount}
            {activeCount > 0 && (
              <span className="text-primary"> ({activeCount} active)</span>
            )}
          </span>
        )}
      </div>
      {!isLandscape && (
        expanded ? (
          <ChevronDown className="h-5 w-5 text-muted-foreground" />
        ) : (
          <ChevronUp className="h-5 w-5 text-muted-foreground" />
        )
      )}
    </button>
  )
}
 
export default function TimerPanel({ timers, instructionText, aiAvailable = false, isLandscape = false }: TimerPanelProps) {
  const [expanded, setExpanded] = useState(true)
  const [loadingTimerId, setLoadingTimerId] = useState<string | null>(null)
 
  const detectedTimes = instructionText ? detectTimes(instructionText) : []
  const showContent = isLandscape || expanded
 
  const handleAddTimer = async (id: string, fallbackLabel: string, duration: number) => {
    const durationMinutes = Math.ceil(duration / 60)
 
    if (aiAvailable && instructionText) {
      setLoadingTimerId(id)
      try {
        const response = await api.ai.timerName(instructionText, durationMinutes)
        timers.addTimer(response.label, duration)
      } catch {
        timers.addTimer(fallbackLabel, duration)
      } finally {
        setLoadingTimerId(null)
      }
    } else {
      timers.addTimer(fallbackLabel, duration)
    }
  }
 
  const handleAddQuickTimer = (label: string, duration: number, index: number) => {
    handleAddTimer(`quick-${index}`, label, duration)
  }
 
  const handleAddDetectedTimer = (seconds: number, index: number) => {
    handleAddTimer(`detected-${index}`, formatDetectedTime(seconds), seconds)
  }
 
  const activeTimerCount = timers.timers.filter((t) => t.isRunning).length
 
  return (
    <div
      className={cn(
        'flex flex-col bg-card',
        isLandscape
          ? 'min-h-0 flex-[2] overflow-hidden border-l border-border'
          : 'max-h-[45vh] shrink-0 border-t border-border',
      )}
    >
      <TimerPanelHeader
        expanded={showContent}
        timerCount={timers.timers.length}
        activeCount={activeTimerCount}
        isLandscape={isLandscape}
        onToggle={() => setExpanded(!expanded)}
      />
      {showContent && (
        <div
          className={cn(
            'space-y-4 px-4 pb-4',
            isLandscape && 'flex-1 overflow-y-auto',
          )}
        >
          <QuickTimerButtons
            quickTimers={QUICK_TIMERS}
            loadingTimerId={loadingTimerId}
            aiAvailable={aiAvailable}
            instructionText={instructionText}
            onAdd={handleAddQuickTimer}
          />
          <DetectedTimers
            detectedTimes={detectedTimes}
            loadingTimerId={loadingTimerId}
            formatTime={formatDetectedTime}
            onAdd={handleAddDetectedTimer}
          />
          <TimerList
            timers={timers.timers}
            onToggle={timers.toggleTimer}
            onReset={timers.resetTimer}
            onDelete={timers.deleteTimer}
          />
        </div>
      )}
    </div>
  )
}
 
← Back to Dashboard