"use client"; import { useState, useEffect, useCallback, useRef } from "react"; import { lmsApi, LessonAnnotation, CreateAnnotationPayload } from "@/lib/api"; import { StickyNote, Plus, Pencil, Trash2, Check, X, Loader2, ChevronDown, ChevronUp, Clock, } from "lucide-react"; type Props = { lessonId: string; /** Posición actual del reproductor de video (segundos), si aplica */ videoTimestamp?: number; }; function formatTimestamp(secs: number): string { const m = Math.floor(secs / 60); const s = Math.floor(secs % 60); return `${m}:${s.toString().padStart(2, "0")}`; } function timeAgo(iso: string): string { const diff = (Date.now() - new Date(iso).getTime()) / 1000; if (diff < 60) return "Hace un momento"; if (diff < 3600) return `Hace ${Math.floor(diff / 60)}m`; if (diff < 86400) return `Hace ${Math.floor(diff / 3600)}h`; return new Date(iso).toLocaleDateString("es-MX", { day: "numeric", month: "short" }); } export default function LessonAnnotations({ lessonId, videoTimestamp }: Props) { const [annotations, setAnnotations] = useState([]); const [loading, setLoading] = useState(true); const [open, setOpen] = useState(false); const [draft, setDraft] = useState(""); const [saving, setSaving] = useState(false); const [editingId, setEditingId] = useState(null); const [editContent, setEditContent] = useState(""); const textareaRef = useRef(null); const load = useCallback(async () => { try { const data = await lmsApi.getLessonAnnotations(lessonId); setAnnotations(data); } catch { // silencioso } finally { setLoading(false); } }, [lessonId]); useEffect(() => { load(); }, [load]); // Auto-foco al abrir useEffect(() => { if (open && textareaRef.current) textareaRef.current.focus(); }, [open]); const handleCreate = async () => { if (!draft.trim()) return; setSaving(true); try { const payload: CreateAnnotationPayload = { content: draft.trim() }; if (videoTimestamp !== undefined) { payload.position_data = { type: "timestamp", value: Math.round(videoTimestamp) }; } const created = await lmsApi.createLessonAnnotation(lessonId, payload); setAnnotations(prev => [...prev, created]); setDraft(""); } finally { setSaving(false); } }; const startEdit = (ann: LessonAnnotation) => { setEditingId(ann.id); setEditContent(ann.content); }; const handleUpdate = async (ann: LessonAnnotation) => { if (!editContent.trim()) return; try { const updated = await lmsApi.updateLessonAnnotation(lessonId, ann.id, { content: editContent.trim(), position_data: ann.position_data, }); setAnnotations(prev => prev.map(a => a.id === updated.id ? updated : a)); setEditingId(null); } catch { // silencioso } }; const handleDelete = async (id: string) => { try { await lmsApi.deleteLessonAnnotation(lessonId, id); setAnnotations(prev => prev.filter(a => a.id !== id)); } catch { // silencioso } }; return (
{/* Header colapsable */} {open && (
{/* Editor de nueva nota */}