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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { motion } from 'motion/react';
|
| 2 |
+
import { Play, Pause, Square, Clock, Image as ImageIcon, Trophy, RotateCw } from 'lucide-react';
|
| 3 |
+
import { useState, useEffect, useRef } from 'react';
|
| 4 |
+
import { invoke } from '@tauri-apps/api/core';
|
| 5 |
+
import { cn, defaultTransition } from '../../lib/utils';
|
| 6 |
+
|
| 7 |
+
interface StudySession { id: string; title: string; duration_sec: number; actual_sec: number; reference_url?: string; notes: string; created_at: number; }
|
| 8 |
+
interface LibItem { id: string; data_url: string; title: string; }
|
| 9 |
+
|
| 10 |
+
type StudyState = 'idle' | 'running' | 'paused' | 'complete';
|
| 11 |
+
|
| 12 |
+
export default function StudyView() {
|
| 13 |
+
const [state, setState] = useState<StudyState>('idle');
|
| 14 |
+
const [duration, setDuration] = useState(300); // 5 min default
|
| 15 |
+
const [elapsed, setElapsed] = useState(0);
|
| 16 |
+
const [title, setTitle] = useState('Quick Study');
|
| 17 |
+
const [referenceUrl, setReferenceUrl] = useState('');
|
| 18 |
+
const [notes, setNotes] = useState('');
|
| 19 |
+
const [sessions, setSessions] = useState<StudySession[]>([]);
|
| 20 |
+
const [libItems, setLibItems] = useState<LibItem[]>([]);
|
| 21 |
+
const [selectedRef, setSelectedRef] = useState<LibItem | null>(null);
|
| 22 |
+
const [activeSessionId, setActiveSessionId] = useState<string | null>(null);
|
| 23 |
+
const intervalRef = useRef<ReturnType<typeof setInterval>>();
|
| 24 |
+
|
| 25 |
+
useEffect(() => { invoke<StudySession[]>('study_list').then(setSessions).catch(() => {}); invoke<LibItem[]>('library_items').then(setLibItems).catch(() => {}); }, []);
|
| 26 |
+
|
| 27 |
+
useEffect(() => {
|
| 28 |
+
if (state === 'running') {
|
| 29 |
+
intervalRef.current = setInterval(() => {
|
| 30 |
+
setElapsed(prev => {
|
| 31 |
+
if (prev + 1 >= duration) { clearInterval(intervalRef.current); setState('complete'); return duration; }
|
| 32 |
+
return prev + 1;
|
| 33 |
+
});
|
| 34 |
+
}, 1000);
|
| 35 |
+
} else { clearInterval(intervalRef.current); }
|
| 36 |
+
return () => clearInterval(intervalRef.current);
|
| 37 |
+
}, [state, duration]);
|
| 38 |
+
|
| 39 |
+
const startStudy = () => {
|
| 40 |
+
invoke<StudySession>('study_start', { title, durationSec: duration, referenceUrl: selectedRef?.data_url || referenceUrl || null })
|
| 41 |
+
.then(s => { setActiveSessionId(s.id); setState('running'); setElapsed(0); })
|
| 42 |
+
.catch(console.error);
|
| 43 |
+
};
|
| 44 |
+
const pauseStudy = () => setState(s => s === 'running' ? 'paused' : 'running');
|
| 45 |
+
const stopStudy = () => {
|
| 46 |
+
if (activeSessionId) {
|
| 47 |
+
invoke<StudySession>('study_complete', { id: activeSessionId, actualSec: elapsed, notes })
|
| 48 |
+
.then(() => { invoke<StudySession[]>('study_list').then(setSessions); })
|
| 49 |
+
.catch(console.error);
|
| 50 |
+
}
|
| 51 |
+
setState('idle'); setElapsed(0); setActiveSessionId(null); setNotes('');
|
| 52 |
+
};
|
| 53 |
+
const completeStudy = () => {
|
| 54 |
+
if (activeSessionId) {
|
| 55 |
+
invoke<StudySession>('study_complete', { id: activeSessionId, actualSec: elapsed, notes })
|
| 56 |
+
.then(() => { invoke<StudySession[]>('study_list').then(setSessions); })
|
| 57 |
+
.catch(console.error);
|
| 58 |
+
}
|
| 59 |
+
setState('idle'); setElapsed(0); setActiveSessionId(null); setNotes('');
|
| 60 |
+
};
|
| 61 |
+
|
| 62 |
+
const formatTime = (sec: number) => `${Math.floor(sec / 60).toString().padStart(2, '0')}:${(sec % 60).toString().padStart(2, '0')}`;
|
| 63 |
+
const progress = duration > 0 ? (elapsed / duration) * 100 : 0;
|
| 64 |
+
const presets = [60, 120, 300, 600, 900, 1800];
|
| 65 |
+
|
| 66 |
+
return (
|
| 67 |
+
<motion.div initial={{ opacity: 0, y: 10 }} animate={{ opacity: 1, y: 0 }} exit={{ opacity: 0, y: -10 }} transition={defaultTransition} className="w-full h-full bg-dusk-bg flex overflow-hidden">
|
| 68 |
+
{/* Main Study Area */}
|
| 69 |
+
<div className="flex-1 flex flex-col items-center justify-center p-8 relative">
|
| 70 |
+
{/* Reference Image */}
|
| 71 |
+
{selectedRef && state !== 'idle' && (
|
| 72 |
+
<div className="absolute inset-0 flex items-center justify-center p-16 opacity-20 pointer-events-none">
|
| 73 |
+
<img src={selectedRef.data_url} className="max-w-full max-h-full object-contain rounded-xl" />
|
| 74 |
+
</div>
|
| 75 |
+
)}
|
| 76 |
+
|
| 77 |
+
{state === 'idle' && (
|
| 78 |
+
<div className="flex flex-col items-center gap-8 max-w-md w-full">
|
| 79 |
+
<div className="text-center">
|
| 80 |
+
<h1 className="text-3xl font-medium text-dusk-text mb-2">Quick Study</h1>
|
| 81 |
+
<p className="text-sm text-dusk-text-muted">Timed drawing practice with reference focus. No distractions.</p>
|
| 82 |
+
</div>
|
| 83 |
+
|
| 84 |
+
{/* Title */}
|
| 85 |
+
<input value={title} onChange={e => setTitle(e.target.value)} placeholder="Study title..." className="w-full bg-dusk-surface border border-dusk-border rounded-xl px-4 py-3 text-sm text-dusk-text focus:outline-none focus:border-dusk-accent" />
|
| 86 |
+
|
| 87 |
+
{/* Duration Presets */}
|
| 88 |
+
<div className="w-full">
|
| 89 |
+
<label className="text-[11px] font-bold text-dusk-text-muted uppercase tracking-wider mb-2 block">Duration</label>
|
| 90 |
+
<div className="grid grid-cols-3 gap-2">
|
| 91 |
+
{presets.map(p => (
|
| 92 |
+
<button key={p} onClick={() => setDuration(p)} className={cn('py-2.5 rounded-xl text-sm font-semibold border transition-colors', duration === p ? 'bg-dusk-accent/20 text-dusk-accent border-dusk-accent/30' : 'bg-dusk-surface text-dusk-text-muted border-dusk-border hover:border-dusk-accent/30')}>
|
| 93 |
+
{p < 60 ? `${p}s` : `${p / 60}m`}
|
| 94 |
+
</button>
|
| 95 |
+
))}
|
| 96 |
+
</div>
|
| 97 |
+
</div>
|
| 98 |
+
|
| 99 |
+
{/* Reference Selection */}
|
| 100 |
+
<div className="w-full">
|
| 101 |
+
<label className="text-[11px] font-bold text-dusk-text-muted uppercase tracking-wider mb-2 block">Reference (optional)</label>
|
| 102 |
+
{libItems.length > 0 ? (
|
| 103 |
+
<div className="grid grid-cols-4 gap-2 max-h-32 overflow-auto">
|
| 104 |
+
{libItems.slice(0, 12).map(it => (
|
| 105 |
+
<button key={it.id} onClick={() => setSelectedRef(selectedRef?.id === it.id ? null : it)} className={cn('aspect-square rounded-lg overflow-hidden border-2 transition-colors', selectedRef?.id === it.id ? 'border-dusk-accent' : 'border-dusk-border/50 hover:border-dusk-border')}>
|
| 106 |
+
<img src={it.data_url} className="w-full h-full object-cover" />
|
| 107 |
+
</button>
|
| 108 |
+
))}
|
| 109 |
+
</div>
|
| 110 |
+
) : <p className="text-xs text-dusk-text-muted">Add images to library first</p>}
|
| 111 |
+
</div>
|
| 112 |
+
|
| 113 |
+
{/* Start Button */}
|
| 114 |
+
<button onClick={startStudy} className="w-full py-4 rounded-2xl bg-dusk-accent text-dusk-bg font-bold text-base flex items-center justify-center gap-2 hover:bg-dusk-accent/90 transition-colors shadow-lg">
|
| 115 |
+
<Play className="w-5 h-5" /> Start Study Session
|
| 116 |
+
</button>
|
| 117 |
+
</div>
|
| 118 |
+
)}
|
| 119 |
+
|
| 120 |
+
{(state === 'running' || state === 'paused') && (
|
| 121 |
+
<div className="flex flex-col items-center gap-8">
|
| 122 |
+
{/* Timer Ring */}
|
| 123 |
+
<div className="relative w-48 h-48">
|
| 124 |
+
<svg className="w-full h-full -rotate-90" viewBox="0 0 100 100">
|
| 125 |
+
<circle cx="50" cy="50" r="45" stroke="var(--color-dusk-border)" strokeWidth="4" fill="none" />
|
| 126 |
+
<circle cx="50" cy="50" r="45" stroke="var(--color-dusk-accent)" strokeWidth="4" fill="none" strokeLinecap="round" strokeDasharray={`${progress * 2.83} 283`} className="transition-all duration-1000" />
|
| 127 |
+
</svg>
|
| 128 |
+
<div className="absolute inset-0 flex flex-col items-center justify-center">
|
| 129 |
+
<span className="text-4xl font-bold text-dusk-text font-mono">{formatTime(duration - elapsed)}</span>
|
| 130 |
+
<span className="text-xs text-dusk-text-muted mt-1">{state === 'paused' ? 'PAUSED' : 'remaining'}</span>
|
| 131 |
+
</div>
|
| 132 |
+
</div>
|
| 133 |
+
|
| 134 |
+
<h2 className="text-xl font-semibold text-dusk-text">{title}</h2>
|
| 135 |
+
|
| 136 |
+
{/* Controls */}
|
| 137 |
+
<div className="flex items-center gap-4">
|
| 138 |
+
<button onClick={pauseStudy} className="w-14 h-14 rounded-full bg-dusk-surface border border-dusk-border flex items-center justify-center text-dusk-text hover:bg-dusk-surface-hover">
|
| 139 |
+
{state === 'running' ? <Pause className="w-6 h-6" /> : <Play className="w-6 h-6" />}
|
| 140 |
+
</button>
|
| 141 |
+
<button onClick={stopStudy} className="w-14 h-14 rounded-full bg-red-500/10 border border-red-500/20 flex items-center justify-center text-red-400 hover:bg-red-500/20">
|
| 142 |
+
<Square className="w-5 h-5" />
|
| 143 |
+
</button>
|
| 144 |
+
</div>
|
| 145 |
+
|
| 146 |
+
{/* Notes */}
|
| 147 |
+
<textarea value={notes} onChange={e => setNotes(e.target.value)} placeholder="Quick notes during study..." className="w-80 h-24 bg-dusk-surface border border-dusk-border rounded-xl px-4 py-3 text-sm text-dusk-text resize-none focus:outline-none focus:border-dusk-accent" />
|
| 148 |
+
</div>
|
| 149 |
+
)}
|
| 150 |
+
|
| 151 |
+
{state === 'complete' && (
|
| 152 |
+
<div className="flex flex-col items-center gap-6">
|
| 153 |
+
<Trophy className="w-16 h-16 text-dusk-accent" />
|
| 154 |
+
<h2 className="text-2xl font-bold text-dusk-text">Session Complete!</h2>
|
| 155 |
+
<p className="text-dusk-text-muted">{formatTime(elapsed)} of focused study on "{title}"</p>
|
| 156 |
+
<textarea value={notes} onChange={e => setNotes(e.target.value)} placeholder="Session notes / observations..." className="w-80 h-24 bg-dusk-surface border border-dusk-border rounded-xl px-4 py-3 text-sm text-dusk-text resize-none focus:outline-none focus:border-dusk-accent" />
|
| 157 |
+
<div className="flex gap-3">
|
| 158 |
+
<button onClick={completeStudy} className="px-6 py-3 rounded-xl bg-dusk-accent text-dusk-bg font-semibold">Save & Done</button>
|
| 159 |
+
<button onClick={() => { completeStudy(); startStudy(); }} className="px-6 py-3 rounded-xl bg-dusk-surface border border-dusk-border text-dusk-text font-semibold flex items-center gap-2"><RotateCw className="w-4 h-4" /> Again</button>
|
| 160 |
+
</div>
|
| 161 |
+
</div>
|
| 162 |
+
)}
|
| 163 |
+
</div>
|
| 164 |
+
|
| 165 |
+
{/* History Sidebar */}
|
| 166 |
+
<div className="w-72 border-l border-dusk-border bg-dusk-surface flex flex-col shrink-0">
|
| 167 |
+
<div className="h-12 border-b border-dusk-border flex items-center px-4"><span className="text-[13px] font-semibold text-dusk-text">Study History</span></div>
|
| 168 |
+
<div className="flex-1 overflow-auto p-3 flex flex-col gap-2">
|
| 169 |
+
{sessions.length === 0 && <p className="text-xs text-dusk-text-muted text-center py-8">No sessions yet</p>}
|
| 170 |
+
{sessions.slice().reverse().map(s => (
|
| 171 |
+
<div key={s.id} className="p-3 bg-dusk-bg rounded-xl border border-dusk-border/50">
|
| 172 |
+
<div className="flex items-center justify-between mb-1">
|
| 173 |
+
<span className="text-[13px] font-semibold text-dusk-text truncate">{s.title}</span>
|
| 174 |
+
<span className="text-[11px] text-dusk-accent font-bold">{formatTime(s.actual_sec)}</span>
|
| 175 |
+
</div>
|
| 176 |
+
<div className="flex items-center gap-2 text-[11px] text-dusk-text-muted">
|
| 177 |
+
<Clock className="w-3 h-3" />
|
| 178 |
+
<span>Target: {formatTime(s.duration_sec)}</span>
|
| 179 |
+
<span>•</span>
|
| 180 |
+
<span>{new Date(s.created_at * 1000).toLocaleDateString()}</span>
|
| 181 |
+
</div>
|
| 182 |
+
{s.notes && <p className="text-[11px] text-dusk-text-muted mt-2 line-clamp-2">{s.notes}</p>}
|
| 183 |
+
</div>
|
| 184 |
+
))}
|
| 185 |
+
</div>
|
| 186 |
+
</div>
|
| 187 |
+
</motion.div>
|
| 188 |
+
);
|
| 189 |
+
}
|