Merge pull request 'Redirección gobernada por la estructura' (#52) from fix/Persistencia into main

Reviewed-on: #52
This commit was merged in pull request #52.
This commit is contained in:
2026-01-29 15:52:28 +00:00
2 changed files with 30 additions and 20 deletions

View File

@@ -87,27 +87,33 @@ function RouteComponent() {
// 1. Transformar datos de la API para el menú de selección
const availableFields = useMemo(() => {
if (!data?.datos) return []
return Object.entries(data.datos).map(([key, value]) => ({
if (!data?.estructuras_plan?.definicion?.properties) return []
return Object.entries(data.estructuras_plan.definicion.properties).map(
([key, value]) => ({
key,
label: formatLabel(key),
value: String(value || ''),
}))
label: value.title,
value: String(value.description || ''),
}),
)
}, [data])
// 2. Manejar el estado inicial si viene de "Datos Generales"
useEffect(() => {
const state = routerState.location.state as any
if (state?.prefill && availableFields.length > 0) {
// Intentamos encontrar qué campo es por su valor o si mandaste el fieldKey
if (!state?.campo_edit || availableFields.length === 0) return
const field = availableFields.find(
(f) => f.value === state.prefill || f.key === state.fieldKey,
(f) =>
f.value === state.campo_edit.label || f.key === state.campo_edit.clave,
)
if (field && !selectedFields.find((sf) => sf.key === field.key)) {
if (field && !selectedFields.some((sf) => sf.key === field.key)) {
setSelectedFields([field])
}
setInput(`Mejora este campo: ${field?.label} `)
}
setInput((prev) =>
injectFieldsIntoInput(prev || 'Mejora este campo:', field ? [field] : []),
)
}, [availableFields])
// 3. Lógica para el disparador ":"
@@ -127,7 +133,7 @@ function RouteComponent() {
) => {
const baseText = input.replace(/\[[^\]]+]/g, '').trim()
const tags = fields.map((f) => `[${f.label}]`).join(' ')
const tags = fields.map((f) => `${f.label}`).join(' ')
return `${baseText} ${tags}`.trim()
}

View File

@@ -81,6 +81,7 @@ function DatosGeneralesPage() {
const rawValue = valores[key]
return {
clave: key,
id: (index + 1).toString(),
label: schema?.title || formatLabel(key),
helperText: schema?.description || '',
@@ -131,14 +132,16 @@ function DatosGeneralesPage() {
// toast.success('Cambios guardados localmente')
}
const handleIARequest = (descripcion: string) => {
const handleIARequest = (clave: string) => {
console.log(clave)
navigate({
to: '/planes/$planId/iaplan',
params: {
planId: planId, // o dinámico
},
state: {
prefill: descripcion,
campo_edit: clave,
} as any,
})
}
@@ -154,8 +157,9 @@ function DatosGeneralesPage() {
</div>
<div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
{campos.map((campo) => {
{campos.map((campo, key) => {
const isEditing = editingId === campo.id
console.log(campo)
return (
<div
@@ -194,7 +198,7 @@ function DatosGeneralesPage() {
variant="ghost"
size="icon"
className="h-8 w-8 text-teal-600"
onClick={() => handleIARequest(campo.value)}
onClick={() => handleIARequest(campo)}
>
<Sparkles size={14} />
</Button>