"use client"; import { useEffect, useState, useCallback } from "react"; import { lmsApi, StudentNote } from "@/lib/api"; import { StickyNote, Save, Loader2, CheckCircle2, AlertCircle } from "lucide-react"; import debounce from "lodash/debounce"; interface StudentNotesProps { lessonId: string; } export default function StudentNotes({ lessonId }: StudentNotesProps) { const [note, setNote] = useState(""); const [loading, setLoading] = useState(true); const [saving, setSaving] = useState(false); const [lastSaved, setLastSaved] = useState(null); const [error, setError] = useState(null); useEffect(() => { const fetchNote = async () => { try { setLoading(true); const data = await lmsApi.getNote(lessonId); if (data) { setNote(data.content); } else { setNote(""); } } catch (err) { console.error("Failed to fetch note:", err); setError("No se pudo cargar tu nota."); } finally { setLoading(false); } }; fetchNote(); }, [lessonId]); const debouncedSave = useCallback( debounce(async (content: string) => { if (!content.trim() && note === "") return; try { setSaving(true); await lmsApi.saveNote(lessonId, content); setLastSaved(new Date()); setError(null); } catch (err) { console.error("Failed to save note:", err); setError("Error al auto-guardar."); } finally { setSaving(false); } }, 1000), [lessonId, note] ); const handleChange = (e: React.ChangeEvent) => { const newContent = e.target.value; setNote(newContent); debouncedSave(newContent); }; if (loading) { return (
Cargando tus notas...
); } return (

Notas Personales

{saving ? ( Guardando... ) : error ? ( {error} ) : lastSaved ? ( Guardado {lastSaved.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })} ) : null}