closes #63: - Añadido AIGenerateSubjectInput y nueva implementación ai_generate_subject que envía FormData (soporta archivosAdjuntos) al Edge Function. - Creado hook useGenerateSubjectAI (mutation) y usado en WizardControls de asignaturas para generar la asignatura vía IA. - WizardControls (asignaturas) construye el payload IA, invoca la mutación y navega al detalle de la asignatura creada pasando state.showConfetti para lanzar confetti. - Ajustes en subjects.api.ts (nombres de endpoint, tipos y envío de datos) y sincronización de tipos en WizardControls (plan y campos básicos). - Ruta de detalle de asignatura ($asignaturaId) ahora lee location.state.showConfetti y dispara lateralConfetti al entrar. - Eliminado el prop onCreate del modal de nueva asignatura (la creación IA se gestiona internamente).
145 lines
4.4 KiB
TypeScript
145 lines
4.4 KiB
TypeScript
import { useNavigate } from '@tanstack/react-router'
|
|
|
|
import type { AIGeneratePlanInput } from '@/data'
|
|
import type { NivelPlanEstudio, TipoCiclo } from '@/data/types/domain'
|
|
import type { NewPlanWizardState } from '@/features/planes/nuevo/types'
|
|
// import type { Database } from '@/types/supabase'
|
|
|
|
import { Button } from '@/components/ui/button'
|
|
// import { supabaseBrowser } from '@/data'
|
|
import { useCreatePlanManual, useGeneratePlanAI } from '@/data/hooks/usePlans'
|
|
|
|
export function WizardControls({
|
|
errorMessage,
|
|
onPrev,
|
|
onNext,
|
|
disablePrev,
|
|
disableNext,
|
|
disableCreate,
|
|
isLastStep,
|
|
wizard,
|
|
setWizard,
|
|
}: {
|
|
errorMessage?: string | null
|
|
onPrev: () => void
|
|
onNext: () => void
|
|
disablePrev: boolean
|
|
disableNext: boolean
|
|
disableCreate: boolean
|
|
isLastStep: boolean
|
|
wizard: NewPlanWizardState
|
|
setWizard: React.Dispatch<React.SetStateAction<NewPlanWizardState>>
|
|
}) {
|
|
const navigate = useNavigate()
|
|
const generatePlanAI = useGeneratePlanAI()
|
|
const createPlanManual = useCreatePlanManual()
|
|
// const supabaseClient = supabaseBrowser()
|
|
// const persistPlanFromAI = usePersistPlanFromAI()
|
|
|
|
const handleCreate = async () => {
|
|
// Start loading
|
|
setWizard(
|
|
(w: NewPlanWizardState): NewPlanWizardState => ({
|
|
...w,
|
|
isLoading: true,
|
|
errorMessage: null,
|
|
}),
|
|
)
|
|
|
|
try {
|
|
if (wizard.tipoOrigen === 'IA') {
|
|
const tipoCicloSafe = (wizard.datosBasicos.tipoCiclo ||
|
|
'Semestre') as any
|
|
const numCiclosSafe =
|
|
typeof wizard.datosBasicos.numCiclos === 'number'
|
|
? wizard.datosBasicos.numCiclos
|
|
: 1
|
|
|
|
const aiInput: AIGeneratePlanInput = {
|
|
datosBasicos: {
|
|
nombrePlan: wizard.datosBasicos.nombrePlan,
|
|
carreraId: wizard.datosBasicos.carrera.id,
|
|
facultadId: wizard.datosBasicos.facultad.id,
|
|
nivel: wizard.datosBasicos.nivel as string,
|
|
tipoCiclo: tipoCicloSafe,
|
|
numCiclos: numCiclosSafe,
|
|
estructuraPlanId: wizard.datosBasicos.estructuraPlanId as string,
|
|
},
|
|
iaConfig: {
|
|
descripcionEnfoqueAcademico:
|
|
wizard.iaConfig?.descripcionEnfoqueAcademico || '',
|
|
instruccionesAdicionalesIA:
|
|
wizard.iaConfig?.instruccionesAdicionalesIA || '',
|
|
archivosReferencia: wizard.iaConfig?.archivosReferencia || [],
|
|
repositoriosIds: wizard.iaConfig?.repositoriosReferencia || [],
|
|
archivosAdjuntos: wizard.iaConfig?.archivosAdjuntos || [],
|
|
},
|
|
}
|
|
|
|
console.log(`${new Date().toISOString()} - Enviando a generar plan IA`)
|
|
|
|
const plan = await generatePlanAI.mutateAsync(aiInput as any)
|
|
console.log(`${new Date().toISOString()} - Plan IA generado`, plan)
|
|
|
|
navigate({
|
|
to: `/planes/${plan.id}`,
|
|
state: { showConfetti: true },
|
|
})
|
|
return
|
|
}
|
|
|
|
if (wizard.tipoOrigen === 'MANUAL') {
|
|
// Crear plan vacío manualmente usando el hook
|
|
const plan = await createPlanManual.mutateAsync({
|
|
carreraId: wizard.datosBasicos.carrera.id,
|
|
estructuraId: wizard.datosBasicos.estructuraPlanId as string,
|
|
nombre: wizard.datosBasicos.nombrePlan,
|
|
nivel: wizard.datosBasicos.nivel as NivelPlanEstudio,
|
|
tipoCiclo: wizard.datosBasicos.tipoCiclo as TipoCiclo,
|
|
numCiclos: (wizard.datosBasicos.numCiclos as number) || 1,
|
|
datos: {},
|
|
})
|
|
|
|
// Navegar al nuevo plan
|
|
navigate({
|
|
to: `/planes/${plan.id}`,
|
|
state: { showConfetti: true },
|
|
})
|
|
return
|
|
}
|
|
} catch (err: any) {
|
|
setWizard((w) => ({
|
|
...w,
|
|
isLoading: false,
|
|
errorMessage: err?.message ?? 'Error generando el plan',
|
|
}))
|
|
} finally {
|
|
setWizard((w) => ({ ...w, isLoading: false }))
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div className="flex grow items-center justify-between">
|
|
<Button variant="secondary" onClick={onPrev} disabled={disablePrev}>
|
|
Anterior
|
|
</Button>
|
|
<div className="mx-2 flex-1">
|
|
{errorMessage && (
|
|
<span className="text-destructive text-sm font-medium">
|
|
{errorMessage}
|
|
</span>
|
|
)}
|
|
</div>
|
|
{isLastStep ? (
|
|
<Button onClick={handleCreate} disabled={disableCreate}>
|
|
Crear plan
|
|
</Button>
|
|
) : (
|
|
<Button onClick={onNext} disabled={disableNext}>
|
|
Siguiente
|
|
</Button>
|
|
)}
|
|
</div>
|
|
)
|
|
}
|