diff --git a/src/data/api/plans.api.ts b/src/data/api/plans.api.ts index 2ac4fde..61cfa5d 100644 --- a/src/data/api/plans.api.ts +++ b/src/data/api/plans.api.ts @@ -176,18 +176,31 @@ export async function plan_asignaturas_list( return data ?? [] } -export async function plans_history(planId: UUID): Promise> { +export async function plans_history( + planId: UUID, + page: number = 0, + pageSize: number = 4, +): Promise<{ data: Array; count: number }> { + // Cambiamos el retorno const supabase = supabaseBrowser() - const { data, error } = await supabase + const from = page * pageSize + const to = from + pageSize - 1 + + const { data, error, count } = await supabase .from('cambios_plan') .select( 'id,plan_estudio_id,cambiado_por,cambiado_en,tipo,campo,valor_anterior,valor_nuevo', + { count: 'exact' }, // <--- Pedimos el conteo exacto ) .eq('plan_estudio_id', planId) .order('cambiado_en', { ascending: false }) + .range(from, to) throwIfError(error) - return data ?? [] + return { + data: data ?? [], + count: count ?? 0, + } } /** Wizard: crear plan manual (Edge Function) */ diff --git a/src/data/hooks/usePlans.ts b/src/data/hooks/usePlans.ts index dcaf198..4f8d913 100644 --- a/src/data/hooks/usePlans.ts +++ b/src/data/hooks/usePlans.ts @@ -80,11 +80,17 @@ export function usePlanAsignaturas(planId: UUID | null | undefined) { }) } -export function usePlanHistorial(planId: UUID | null | undefined) { +export function usePlanHistorial( + planId: UUID | null | undefined, + page: number, +) { return useQuery({ - queryKey: planId ? qk.planHistorial(planId) : ['planes', 'historial', null], - queryFn: () => plans_history(planId as UUID), + queryKey: planId + ? [...qk.planHistorial(planId), page] + : ['planes', 'historial', null, page], + queryFn: () => plans_history(planId as UUID, page), enabled: Boolean(planId), + placeholderData: (previousData) => previousData, }) } diff --git a/src/routes/planes/$planId/_detalle/historial.tsx b/src/routes/planes/$planId/_detalle/historial.tsx index 9d7e8f1..c62df55 100644 --- a/src/routes/planes/$planId/_detalle/historial.tsx +++ b/src/routes/planes/$planId/_detalle/historial.tsx @@ -12,10 +12,13 @@ import { Eye, History, Calendar, + ChevronLeft, + ChevronRight, } from 'lucide-react' import { useEffect, useMemo, useState } from 'react' import { Badge } from '@/components/ui/badge' +import { Button } from '@/components/ui/button' import { Card, CardContent } from '@/components/ui/card' import { Dialog, @@ -57,15 +60,14 @@ const getEventConfig = (tipo: string, campo: string) => { function RouteComponent() { const { planId } = Route.useParams() - const { data: rawData, isLoading } = usePlanHistorial(planId) + const [page, setPage] = useState(0) + const pageSize = 4 + const { data: response, isLoading } = usePlanHistorial(planId, page) + const rawData = response?.data ?? [] + const totalRecords = response?.count ?? 0 + const totalPages = Math.ceil(totalRecords / pageSize) const [structure, setStructure] = useState(null) const { data } = usePlan(planId) - console.log('analizando estructura') - - console.log(data?.estructuras_plan?.definicion?.properties) - // console.log(structure) - - // ESTADOS PARA EL MODAL const [selectedEvent, setSelectedEvent] = useState(null) const [isModalOpen, setIsModalOpen] = useState(false) @@ -229,6 +231,46 @@ function RouteComponent() { )) )} + {historyEvents.length > 0 && ( +
+

+ Mostrando {rawData.length} de {totalRecords} cambios +

+ +
+ + + + Página {page + 1} de {totalPages || 1} + + + +
+
+ )} {/* MODAL DE COMPARACIÓN CON SCROLL INTERNO */} diff --git a/src/routes/planes/$planId/_detalle/iaplan.tsx b/src/routes/planes/$planId/_detalle/iaplan.tsx index b9f2b0a..90da05b 100644 --- a/src/routes/planes/$planId/_detalle/iaplan.tsx +++ b/src/routes/planes/$planId/_detalle/iaplan.tsx @@ -163,10 +163,6 @@ function RouteComponent() { ) }, [data]) - useEffect(() => { - console.log(uploadedFiles) - }, [uploadedFiles]) - // 2. Manejar el estado inicial si viene de "Datos Generales" useEffect(() => { const state = routerState.location.state as any