feat: add collaborative document functionality for lessons
- Create migration scripts for lesson_collaborative_docs table in both cms-service and lms-service. - Implement CollaborativeDocEditor component for real-time editing of collaborative documents. - Add LessonCollaborativeDocPage for instructors to view and manage collaborative documents. - Include conflict resolution handling in the editor. - Enhance UI with status indicators and formatting options for the document editor. Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
@@ -29,6 +29,7 @@ import AITutor from "@/components/AITutor";
|
||||
import LessonLockedView from "@/components/LessonLockedView";
|
||||
import StudentNotes from "@/components/StudentNotes";
|
||||
import CollaborativeWhiteboard from "@/components/CollaborativeWhiteboard";
|
||||
import CollaborativeDocEditor from "@/components/CollaborativeDocEditor";
|
||||
import { ListMusic, StickyNote } from "lucide-react";
|
||||
import ReactMarkdown from "react-markdown";
|
||||
export default function LessonPlayerPage({ params }: { params: { id: string, lessonId: string } }) {
|
||||
@@ -631,6 +632,14 @@ export default function LessonPlayerPage({ params }: { params: { id: string, les
|
||||
<div className="pt-12 border-t border-black/5 dark:border-white/5 animate-in fade-in slide-in-from-bottom-8 duration-1000">
|
||||
<CollaborativeWhiteboard lessonId={params.lessonId} />
|
||||
</div>
|
||||
|
||||
<div className="pt-8 border-t border-black/5 dark:border-white/5 animate-in fade-in slide-in-from-bottom-8 duration-1000">
|
||||
<div className="mb-3">
|
||||
<h3 className="text-xs font-black uppercase tracking-widest text-black/40 dark:text-white/40">Documento Colaborativo</h3>
|
||||
<p className="text-[11px] text-black/40 dark:text-white/40 mt-0.5">Edición compartida en tiempo real con tu grupo</p>
|
||||
</div>
|
||||
<CollaborativeDocEditor lessonId={params.lessonId} />
|
||||
</div>
|
||||
</article>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useParams, useRouter } from "next/navigation";
|
||||
import { lmsApi, StudyRoom } from "@/lib/api";
|
||||
import { Video, Users, Clock, ExternalLink, ArrowLeft, RefreshCw } from "lucide-react";
|
||||
import { lmsApi, StudyRoom, BbbRecording } from "@/lib/api";
|
||||
import { Video, Users, Clock, ExternalLink, ArrowLeft, RefreshCw, Film, ChevronDown, ChevronRight } from "lucide-react";
|
||||
|
||||
const STATUS_LABEL: Record<string, string> = {
|
||||
pending: "Programada",
|
||||
@@ -23,6 +23,9 @@ export default function StudyRoomsPage() {
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [joiningId, setJoiningId] = useState<string | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [recordings, setRecordings] = useState<Record<string, BbbRecording[]>>({});
|
||||
const [loadingRec, setLoadingRec] = useState<Record<string, boolean>>({});
|
||||
const [expandedRec, setExpandedRec] = useState<Record<string, boolean>>({});
|
||||
|
||||
const loadRooms = useCallback(async () => {
|
||||
setLoading(true);
|
||||
@@ -58,6 +61,22 @@ export default function StudyRoomsPage() {
|
||||
const activeRooms = rooms.filter((r) => r.status !== "ended");
|
||||
const endedRooms = rooms.filter((r) => r.status === "ended");
|
||||
|
||||
const toggleRecordings = async (room: StudyRoom) => {
|
||||
const isExpanded = expandedRec[room.id];
|
||||
setExpandedRec((prev) => ({ ...prev, [room.id]: !isExpanded }));
|
||||
if (!isExpanded && !recordings[room.id]) {
|
||||
setLoadingRec((prev) => ({ ...prev, [room.id]: true }));
|
||||
try {
|
||||
const recs = await lmsApi.getStudyRoomRecordings(id, room.id);
|
||||
setRecordings((prev) => ({ ...prev, [room.id]: recs }));
|
||||
} catch {
|
||||
setRecordings((prev) => ({ ...prev, [room.id]: [] }));
|
||||
} finally {
|
||||
setLoadingRec((prev) => ({ ...prev, [room.id]: false }));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<main className="min-h-screen bg-gray-50 dark:bg-zinc-950 px-4 py-8 max-w-3xl mx-auto">
|
||||
<div className="flex items-center gap-3 mb-6">
|
||||
@@ -156,21 +175,57 @@ export default function StudyRoomsPage() {
|
||||
Salas finalizadas
|
||||
</h2>
|
||||
{endedRooms.map((room) => (
|
||||
<div
|
||||
key={room.id}
|
||||
className="rounded-xl border border-black/5 dark:border-white/5 bg-white/50 dark:bg-white/5 px-4 py-3 flex items-center justify-between gap-3 opacity-60"
|
||||
>
|
||||
<div>
|
||||
<span className="text-sm font-medium">{room.title}</span>
|
||||
{room.ended_at && (
|
||||
<span className="ml-2 text-[11px] text-black/40 dark:text-white/40">
|
||||
Finalizada {new Date(room.ended_at).toLocaleDateString()}
|
||||
<div key={room.id} className="rounded-xl border border-black/5 dark:border-white/5 bg-white/50 dark:bg-white/5">
|
||||
<div className="px-4 py-3 flex items-center justify-between gap-3">
|
||||
<div>
|
||||
<span className="text-sm font-medium">{room.title}</span>
|
||||
{room.ended_at && (
|
||||
<span className="ml-2 text-[11px] text-black/40 dark:text-white/40">
|
||||
Finalizada {new Date(room.ended_at).toLocaleDateString()}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center gap-2">
|
||||
<button
|
||||
onClick={() => void toggleRecordings(room)}
|
||||
className="inline-flex items-center gap-1 px-3 py-1 rounded-lg border border-blue-200 dark:border-blue-700 bg-blue-50 dark:bg-blue-900/20 text-blue-700 dark:text-blue-300 text-xs font-semibold hover:bg-blue-100"
|
||||
>
|
||||
{expandedRec[room.id] ? <ChevronDown className="w-3 h-3" /> : <ChevronRight className="w-3 h-3" />}
|
||||
<Film className="w-3 h-3" /> Grabaciones
|
||||
</button>
|
||||
<span className={`rounded-full px-2 py-0.5 text-[11px] font-semibold ${STATUS_COLOR.ended}`}>
|
||||
{STATUS_LABEL.ended}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<span className={`rounded-full px-2 py-0.5 text-[11px] font-semibold ${STATUS_COLOR.ended}`}>
|
||||
{STATUS_LABEL.ended}
|
||||
</span>
|
||||
{expandedRec[room.id] && (
|
||||
<div className="px-4 pb-3">
|
||||
{loadingRec[room.id] ? (
|
||||
<p className="text-xs text-black/50 dark:text-white/50">Cargando grabaciones…</p>
|
||||
) : recordings[room.id]?.length ? (
|
||||
<div className="space-y-2">
|
||||
{recordings[room.id].map((rec) => (
|
||||
<div key={rec.record_id} className="flex items-center justify-between gap-3 text-xs bg-gray-50 dark:bg-white/5 rounded-lg px-3 py-2">
|
||||
<div>
|
||||
<span className="font-semibold">{rec.name}</span>
|
||||
<span className="ml-2 text-black/40 dark:text-white/40">{rec.duration_minutes} min</span>
|
||||
</div>
|
||||
<a
|
||||
href={rec.playback_url}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex items-center gap-1 text-blue-600 dark:text-blue-400 hover:underline shrink-0"
|
||||
>
|
||||
<ExternalLink className="w-3 h-3" /> Ver grabación
|
||||
</a>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
) : (
|
||||
<p className="text-xs text-black/40 dark:text-white/40">No hay grabaciones disponibles.</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</section>
|
||||
|
||||
@@ -0,0 +1,266 @@
|
||||
"use client";
|
||||
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { lmsApi, getLmsApiUrl, CollaborativeDoc, UpdateCollaborativeDocResponse } from "@/lib/api";
|
||||
import {
|
||||
AlertTriangle,
|
||||
CheckCircle,
|
||||
Loader2,
|
||||
Save,
|
||||
Users,
|
||||
} from "lucide-react";
|
||||
|
||||
type ConflictInfo = {
|
||||
localContent: string;
|
||||
serverContent: string;
|
||||
serverRevision: number;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
lessonId: string;
|
||||
};
|
||||
|
||||
export default function CollaborativeDocEditor({ lessonId }: Props) {
|
||||
const [content, setContent] = useState("");
|
||||
const [revision, setRevision] = useState(0);
|
||||
const [saving, setSaving] = useState(false);
|
||||
const [status, setStatus] = useState<"idle" | "saved" | "conflict" | "error">("idle");
|
||||
const [conflict, setConflict] = useState<ConflictInfo | null>(null);
|
||||
const [activeEditors, setActiveEditors] = useState(0);
|
||||
const debounceRef = useRef<ReturnType<typeof setTimeout> | null>(null);
|
||||
const sseRef = useRef<EventSource | null>(null);
|
||||
const localDirtyRef = useRef(false);
|
||||
const revisionRef = useRef(0);
|
||||
const contentRef = useRef("");
|
||||
|
||||
// Cargar documento inicial
|
||||
useEffect(() => {
|
||||
void (async () => {
|
||||
try {
|
||||
const doc: CollaborativeDoc = await lmsApi.getLessonCollaborativeDoc(lessonId);
|
||||
setContent(doc.content);
|
||||
setRevision(doc.revision);
|
||||
revisionRef.current = doc.revision;
|
||||
contentRef.current = doc.content;
|
||||
} catch {
|
||||
setStatus("error");
|
||||
}
|
||||
})();
|
||||
}, [lessonId]);
|
||||
|
||||
// SSE: escuchar cambios remotos
|
||||
useEffect(() => {
|
||||
const token = typeof window !== "undefined"
|
||||
? localStorage.getItem("lms_token") ?? sessionStorage.getItem("lms_token") ?? ""
|
||||
: "";
|
||||
const baseUrl = getLmsApiUrl();
|
||||
const url = `${baseUrl}/lessons/${lessonId}/collaborative-doc/stream?preview_token=${encodeURIComponent(token)}`;
|
||||
|
||||
const es = new EventSource(url);
|
||||
sseRef.current = es;
|
||||
|
||||
es.onmessage = (e) => {
|
||||
try {
|
||||
const data = JSON.parse(e.data as string) as {
|
||||
content: string;
|
||||
revision: number;
|
||||
};
|
||||
// Solo actualizar si no tenemos cambios locales pendientes
|
||||
if (!localDirtyRef.current && data.revision !== revisionRef.current) {
|
||||
setContent(data.content);
|
||||
setRevision(data.revision);
|
||||
revisionRef.current = data.revision;
|
||||
contentRef.current = data.content;
|
||||
}
|
||||
// Contar "editores activos" como señal de conexión SSE viva
|
||||
setActiveEditors((n) => Math.max(1, n));
|
||||
} catch { /* ignore */ }
|
||||
};
|
||||
|
||||
es.onerror = () => {
|
||||
setActiveEditors(0);
|
||||
};
|
||||
|
||||
return () => {
|
||||
es.close();
|
||||
sseRef.current = null;
|
||||
};
|
||||
}, [lessonId]);
|
||||
|
||||
// Guardar con debounce 1.5s
|
||||
const save = useCallback(async (text: string, baseRevision: number) => {
|
||||
setSaving(true);
|
||||
setStatus("idle");
|
||||
try {
|
||||
const res: UpdateCollaborativeDocResponse = await lmsApi.updateLessonCollaborativeDoc(lessonId, {
|
||||
content: text,
|
||||
base_revision: baseRevision,
|
||||
});
|
||||
|
||||
if (res.conflict) {
|
||||
setConflict({
|
||||
localContent: text,
|
||||
serverContent: res.server_content ?? "",
|
||||
serverRevision: res.server_revision ?? baseRevision,
|
||||
});
|
||||
setStatus("conflict");
|
||||
} else {
|
||||
setRevision(res.revision);
|
||||
revisionRef.current = res.revision;
|
||||
contentRef.current = text;
|
||||
localDirtyRef.current = false;
|
||||
setStatus("saved");
|
||||
setTimeout(() => setStatus("idle"), 2000);
|
||||
}
|
||||
} catch {
|
||||
setStatus("error");
|
||||
} finally {
|
||||
setSaving(false);
|
||||
}
|
||||
}, [lessonId]);
|
||||
|
||||
const handleChange = (value: string) => {
|
||||
setContent(value);
|
||||
contentRef.current = value;
|
||||
localDirtyRef.current = true;
|
||||
setStatus("idle");
|
||||
|
||||
if (debounceRef.current) clearTimeout(debounceRef.current);
|
||||
debounceRef.current = setTimeout(() => {
|
||||
void save(contentRef.current, revisionRef.current);
|
||||
}, 1500);
|
||||
};
|
||||
|
||||
// Resolución de conflictos
|
||||
const resolveKeepMine = () => {
|
||||
if (!conflict) return;
|
||||
setConflict(null);
|
||||
setStatus("idle");
|
||||
// Forzar guardado con la revisión del servidor (sobreescribir)
|
||||
void save(conflict.localContent, conflict.serverRevision);
|
||||
};
|
||||
|
||||
const resolveKeepServer = () => {
|
||||
if (!conflict) return;
|
||||
setContent(conflict.serverContent);
|
||||
setRevision(conflict.serverRevision);
|
||||
revisionRef.current = conflict.serverRevision;
|
||||
contentRef.current = conflict.serverContent;
|
||||
localDirtyRef.current = false;
|
||||
setConflict(null);
|
||||
setStatus("idle");
|
||||
};
|
||||
|
||||
// Toolbar: aplicar formato HTML básico via execCommand (compatible con contenteditable)
|
||||
// Para mantener simpleza y no depender de librerías externas usamos un <textarea>
|
||||
// con soporte de Markdown ligero renderizado al vuelo.
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-3 w-full">
|
||||
{/* Barra de estado */}
|
||||
<div className="flex items-center justify-between px-1">
|
||||
<div className="flex items-center gap-2 text-xs text-black/50 dark:text-white/40">
|
||||
<Users className="w-3 h-3" />
|
||||
<span>{activeEditors > 0 ? "Conexión en vivo" : "Sin conexión SSE"}</span>
|
||||
<span className="opacity-50">· Rev. {revision}</span>
|
||||
</div>
|
||||
<div className="flex items-center gap-1 text-xs">
|
||||
{saving && <Loader2 className="w-3 h-3 animate-spin text-blue-500" />}
|
||||
{status === "saved" && <CheckCircle className="w-3 h-3 text-green-500" />}
|
||||
{status === "conflict" && <AlertTriangle className="w-3 h-3 text-amber-500" />}
|
||||
{status === "error" && <AlertTriangle className="w-3 h-3 text-red-500" />}
|
||||
{saving && <span className="text-blue-500">Guardando…</span>}
|
||||
{status === "saved" && <span className="text-green-500">Guardado</span>}
|
||||
{status === "conflict" && <span className="text-amber-500">Conflicto detectado</span>}
|
||||
{status === "error" && <span className="text-red-500">Error al guardar</span>}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Toolbar de formato */}
|
||||
<div className="flex items-center gap-1 px-2 py-1 rounded-lg bg-black/5 dark:bg-white/5 border border-black/10 dark:border-white/10 text-xs font-mono">
|
||||
{[
|
||||
{ label: "B", title: "Negrita: **texto**", insert: "**texto**" },
|
||||
{ label: "I", title: "Cursiva: _texto_", insert: "_texto_" },
|
||||
{ label: "H1", title: "Título: # Título", insert: "\n# Título\n" },
|
||||
{ label: "H2", title: "Subtítulo: ## Subtítulo", insert: "\n## Subtítulo\n" },
|
||||
{ label: "• Lista", title: "Lista: - ítem", insert: "\n- ítem\n" },
|
||||
{ label: "1. Lista", title: "Lista ordenada: 1. ítem", insert: "\n1. ítem\n" },
|
||||
{ label: "---", title: "Separador", insert: "\n---\n" },
|
||||
].map((btn) => (
|
||||
<button
|
||||
key={btn.label}
|
||||
title={btn.title}
|
||||
className="px-2 py-0.5 rounded hover:bg-black/10 dark:hover:bg-white/10"
|
||||
onMouseDown={(e) => {
|
||||
e.preventDefault();
|
||||
const newContent = contentRef.current + btn.insert;
|
||||
setContent(newContent);
|
||||
handleChange(newContent);
|
||||
}}
|
||||
>
|
||||
{btn.label}
|
||||
</button>
|
||||
))}
|
||||
<div className="ml-auto">
|
||||
<button
|
||||
title="Guardar ahora"
|
||||
className="px-2 py-0.5 rounded hover:bg-black/10 dark:hover:bg-white/10 flex items-center gap-1"
|
||||
onClick={() => void save(contentRef.current, revisionRef.current)}
|
||||
>
|
||||
<Save className="w-3 h-3" /> Guardar
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Área de edición */}
|
||||
<textarea
|
||||
className="w-full min-h-[320px] rounded-xl border border-black/10 dark:border-white/10 bg-white dark:bg-zinc-900 px-4 py-3 text-sm font-mono leading-relaxed resize-y focus:outline-none focus:ring-2 focus:ring-blue-500/40"
|
||||
value={content}
|
||||
onChange={(e) => handleChange(e.target.value)}
|
||||
placeholder="Empieza a escribir… Los cambios se sincronizan automáticamente con el grupo."
|
||||
spellCheck
|
||||
/>
|
||||
|
||||
{/* Panel de conflicto */}
|
||||
{conflict && (
|
||||
<div className="rounded-xl border border-amber-300 dark:border-amber-600 bg-amber-50 dark:bg-amber-900/20 p-4 space-y-3">
|
||||
<div className="flex items-center gap-2 text-sm font-black text-amber-800 dark:text-amber-300">
|
||||
<AlertTriangle className="w-4 h-4" />
|
||||
Conflicto de edición
|
||||
</div>
|
||||
<p className="text-xs text-amber-700 dark:text-amber-400">
|
||||
Otro usuario guardó cambios mientras editabas. ¿Qué versión deseas conservar?
|
||||
</p>
|
||||
<div className="grid grid-cols-2 gap-3 text-xs">
|
||||
<div>
|
||||
<div className="font-semibold mb-1 text-amber-700 dark:text-amber-300">Tu versión</div>
|
||||
<pre className="rounded-lg bg-white dark:bg-black/30 p-2 overflow-auto max-h-32 whitespace-pre-wrap text-[11px]">
|
||||
{conflict.localContent.slice(0, 300)}{conflict.localContent.length > 300 ? "…" : ""}
|
||||
</pre>
|
||||
</div>
|
||||
<div>
|
||||
<div className="font-semibold mb-1 text-amber-700 dark:text-amber-300">Versión del servidor (Rev. {conflict.serverRevision})</div>
|
||||
<pre className="rounded-lg bg-white dark:bg-black/30 p-2 overflow-auto max-h-32 whitespace-pre-wrap text-[11px]">
|
||||
{conflict.serverContent.slice(0, 300)}{conflict.serverContent.length > 300 ? "…" : ""}
|
||||
</pre>
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={resolveKeepMine}
|
||||
className="px-3 py-1.5 rounded-lg bg-amber-600 text-white text-xs font-semibold hover:bg-amber-700"
|
||||
>
|
||||
Conservar la mía
|
||||
</button>
|
||||
<button
|
||||
onClick={resolveKeepServer}
|
||||
className="px-3 py-1.5 rounded-lg border border-black/10 dark:border-white/10 text-xs font-semibold hover:bg-black/5 dark:hover:bg-white/10"
|
||||
>
|
||||
Usar la del servidor
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1347,6 +1347,24 @@ export const lmsApi = {
|
||||
joinStudyRoom(courseId: string, roomId: string): Promise<{ room_id: string; join_url: string }> {
|
||||
return apiFetch(`/courses/${courseId}/study-rooms/${roomId}/join`, { method: 'POST' });
|
||||
},
|
||||
|
||||
getStudyRoomRecordings(courseId: string, roomId: string): Promise<BbbRecording[]> {
|
||||
return apiFetch(`/courses/${courseId}/study-rooms/${roomId}/recordings`);
|
||||
},
|
||||
|
||||
getLessonCollaborativeDoc(lessonId: string): Promise<CollaborativeDoc> {
|
||||
return apiFetch(`/lessons/${lessonId}/collaborative-doc`);
|
||||
},
|
||||
|
||||
updateLessonCollaborativeDoc(
|
||||
lessonId: string,
|
||||
payload: { content: string; base_revision: number }
|
||||
): Promise<UpdateCollaborativeDocResponse> {
|
||||
return apiFetch(`/lessons/${lessonId}/collaborative-doc`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
},
|
||||
};
|
||||
|
||||
export interface StudyRoom {
|
||||
@@ -1366,3 +1384,34 @@ export interface StudyRoom {
|
||||
created_at: string;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface BbbRecording {
|
||||
record_id: string;
|
||||
meeting_id: string;
|
||||
name: string;
|
||||
state: string;
|
||||
start_time: string;
|
||||
end_time: string;
|
||||
participants: number;
|
||||
playback_url: string;
|
||||
duration_minutes: number;
|
||||
}
|
||||
|
||||
// ─── Documentos Colaborativos (Fase 40) ──────────────────────────────────────
|
||||
|
||||
export interface CollaborativeDoc {
|
||||
lesson_id: string;
|
||||
organization_id: string;
|
||||
content: string;
|
||||
revision: number;
|
||||
last_modified_by: string | null;
|
||||
updated_at: string;
|
||||
}
|
||||
|
||||
export interface UpdateCollaborativeDocResponse {
|
||||
lesson_id: string;
|
||||
revision: number;
|
||||
conflict: boolean;
|
||||
server_content?: string;
|
||||
server_revision?: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user