@asteby/metacore-runtime-react 45.0.0 → 45.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/approval-events.d.ts +16 -0
- package/dist/approval-events.d.ts.map +1 -0
- package/dist/approval-events.js +27 -0
- package/dist/approval-gate.d.ts +39 -0
- package/dist/approval-gate.d.ts.map +1 -0
- package/dist/approval-gate.js +110 -0
- package/dist/approval-pin-dialog.d.ts +25 -0
- package/dist/approval-pin-dialog.d.ts.map +1 -0
- package/dist/approval-pin-dialog.js +66 -0
- package/dist/business/catalog.d.ts +16 -0
- package/dist/business/catalog.d.ts.map +1 -0
- package/dist/business/catalog.js +42 -0
- package/dist/business/customer-picker.d.ts +39 -0
- package/dist/business/customer-picker.d.ts.map +1 -0
- package/dist/business/customer-picker.js +51 -0
- package/dist/business/feedback.d.ts +72 -0
- package/dist/business/feedback.d.ts.map +1 -0
- package/dist/business/feedback.js +52 -0
- package/dist/business/field-errors.d.ts +26 -0
- package/dist/business/field-errors.d.ts.map +1 -0
- package/dist/business/field-errors.js +59 -0
- package/dist/business/format.d.ts +49 -0
- package/dist/business/format.d.ts.map +1 -0
- package/dist/business/format.js +128 -0
- package/dist/business/index.d.ts +14 -0
- package/dist/business/index.d.ts.map +1 -0
- package/dist/business/index.js +13 -0
- package/dist/business/line-items-editor.d.ts +25 -0
- package/dist/business/line-items-editor.d.ts.map +1 -0
- package/dist/business/line-items-editor.js +36 -0
- package/dist/business/line-items.d.ts +69 -0
- package/dist/business/line-items.d.ts.map +1 -0
- package/dist/business/line-items.js +133 -0
- package/dist/business/payment-capture.d.ts +21 -0
- package/dist/business/payment-capture.d.ts.map +1 -0
- package/dist/business/payment-capture.js +24 -0
- package/dist/business/payment.d.ts +61 -0
- package/dist/business/payment.d.ts.map +1 -0
- package/dist/business/payment.js +74 -0
- package/dist/business/product-picker.d.ts +24 -0
- package/dist/business/product-picker.d.ts.map +1 -0
- package/dist/business/product-picker.js +48 -0
- package/dist/business/product-search.d.ts +70 -0
- package/dist/business/product-search.d.ts.map +1 -0
- package/dist/business/product-search.js +51 -0
- package/dist/business/refund-destination.d.ts +19 -0
- package/dist/business/refund-destination.d.ts.map +1 -0
- package/dist/business/refund-destination.js +31 -0
- package/dist/business/refund.d.ts +29 -0
- package/dist/business/refund.d.ts.map +1 -0
- package/dist/business/refund.js +43 -0
- package/dist/business/use-async-search.d.ts +10 -0
- package/dist/business/use-async-search.d.ts.map +1 -0
- package/dist/business/use-async-search.js +38 -0
- package/dist/document/action-bar.d.ts +12 -0
- package/dist/document/action-bar.d.ts.map +1 -0
- package/dist/document/action-bar.js +39 -0
- package/dist/document/cancel-with-reason.d.ts +47 -0
- package/dist/document/cancel-with-reason.d.ts.map +1 -0
- package/dist/document/cancel-with-reason.js +52 -0
- package/dist/document/document-header.d.ts +41 -0
- package/dist/document/document-header.d.ts.map +1 -0
- package/dist/document/document-header.js +30 -0
- package/dist/document/document-page.d.ts +37 -0
- package/dist/document/document-page.d.ts.map +1 -0
- package/dist/document/document-page.js +137 -0
- package/dist/document/document-timeline.d.ts +20 -0
- package/dist/document/document-timeline.d.ts.map +1 -0
- package/dist/document/document-timeline.js +26 -0
- package/dist/document/evaluate.d.ts +31 -0
- package/dist/document/evaluate.d.ts.map +1 -0
- package/dist/document/evaluate.js +170 -0
- package/dist/document/format.d.ts +9 -0
- package/dist/document/format.d.ts.map +1 -0
- package/dist/document/format.js +52 -0
- package/dist/document/index.d.ts +13 -0
- package/dist/document/index.d.ts.map +1 -0
- package/dist/document/index.js +12 -0
- package/dist/document/registry.d.ts +20 -0
- package/dist/document/registry.d.ts.map +1 -0
- package/dist/document/registry.js +20 -0
- package/dist/document/smart-buttons.d.ts +16 -0
- package/dist/document/smart-buttons.d.ts.map +1 -0
- package/dist/document/smart-buttons.js +16 -0
- package/dist/document/status-badge.d.ts +16 -0
- package/dist/document/status-badge.d.ts.map +1 -0
- package/dist/document/status-badge.js +23 -0
- package/dist/document/types.d.ts +196 -0
- package/dist/document/types.d.ts.map +1 -0
- package/dist/document/types.js +1 -0
- package/dist/document/use-document-sources.d.ts +14 -0
- package/dist/document/use-document-sources.d.ts.map +1 -0
- package/dist/document/use-document-sources.js +72 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -0
- package/dist/server-error.d.ts.map +1 -1
- package/dist/server-error.js +19 -0
- package/package.json +1 -1
- package/src/__tests__/approval-gate.test.tsx +134 -0
- package/src/approval-events.ts +39 -0
- package/src/approval-gate.tsx +180 -0
- package/src/approval-pin-dialog.tsx +162 -0
- package/src/business/business.test.ts +164 -0
- package/src/business/catalog.ts +60 -0
- package/src/business/customer-picker.tsx +201 -0
- package/src/business/feedback.tsx +141 -0
- package/src/business/field-errors.ts +92 -0
- package/src/business/format.ts +151 -0
- package/src/business/index.ts +13 -0
- package/src/business/line-items-editor.tsx +259 -0
- package/src/business/line-items.ts +190 -0
- package/src/business/payment-capture.tsx +158 -0
- package/src/business/payment.ts +125 -0
- package/src/business/product-picker.tsx +171 -0
- package/src/business/product-search.ts +108 -0
- package/src/business/refund-destination.tsx +121 -0
- package/src/business/refund.ts +71 -0
- package/src/business/use-async-search.ts +43 -0
- package/src/document/__tests__/document-page.test.tsx +107 -0
- package/src/document/__tests__/evaluate.test.ts +140 -0
- package/src/document/action-bar.tsx +140 -0
- package/src/document/cancel-with-reason.tsx +214 -0
- package/src/document/document-header.tsx +140 -0
- package/src/document/document-page.tsx +268 -0
- package/src/document/document-timeline.tsx +66 -0
- package/src/document/evaluate.ts +180 -0
- package/src/document/format.ts +59 -0
- package/src/document/index.ts +28 -0
- package/src/document/registry.ts +39 -0
- package/src/document/smart-buttons.tsx +54 -0
- package/src/document/status-badge.tsx +44 -0
- package/src/document/types.ts +203 -0
- package/src/document/use-document-sources.ts +91 -0
- package/src/index.ts +23 -0
- package/src/server-error.ts +20 -0
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// SmartButtons — accesos con contador al estilo Odoo (Pagos (2) · REP (1) ·
|
|
2
|
+
// Notas de crédito (0)…). Un botón sin registros se ve atenuado, no oculto,
|
|
3
|
+
// salvo que la definición pida `hideWhenEmpty`.
|
|
4
|
+
import { useTranslation } from 'react-i18next'
|
|
5
|
+
import { cn } from '@asteby/metacore-ui/lib'
|
|
6
|
+
import { DynamicIcon } from '../dynamic-icon'
|
|
7
|
+
|
|
8
|
+
export interface SmartButtonItem {
|
|
9
|
+
key: string
|
|
10
|
+
label: string
|
|
11
|
+
icon?: string
|
|
12
|
+
/** undefined = botón sin contador (p. ej. «Pedido origen»). */
|
|
13
|
+
count?: number
|
|
14
|
+
/** Deshabilitado (sin destino / vacío). */
|
|
15
|
+
disabled?: boolean
|
|
16
|
+
onClick?: () => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SmartButtonsProps {
|
|
20
|
+
items: SmartButtonItem[]
|
|
21
|
+
className?: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function SmartButtons({ items, className }: SmartButtonsProps) {
|
|
25
|
+
const { t } = useTranslation()
|
|
26
|
+
if (items.length === 0) return null
|
|
27
|
+
return (
|
|
28
|
+
<nav aria-label={t('document.related', { defaultValue: 'Relacionados' })} className={cn('flex flex-wrap gap-2', className)} data-smart-buttons=''>
|
|
29
|
+
{items.map((it) => {
|
|
30
|
+
const empty = it.count === 0
|
|
31
|
+
return (
|
|
32
|
+
<button
|
|
33
|
+
key={it.key}
|
|
34
|
+
type='button'
|
|
35
|
+
disabled={it.disabled}
|
|
36
|
+
onClick={it.onClick}
|
|
37
|
+
data-smart-button={it.key}
|
|
38
|
+
className={cn(
|
|
39
|
+
'inline-flex items-center gap-2 rounded-md border bg-card px-3 py-1.5 text-sm transition-colors',
|
|
40
|
+
'hover:bg-accent disabled:cursor-default disabled:opacity-50 disabled:hover:bg-card',
|
|
41
|
+
empty && 'text-muted-foreground',
|
|
42
|
+
)}
|
|
43
|
+
>
|
|
44
|
+
{it.icon ? <DynamicIcon name={it.icon} className='h-4 w-4' /> : null}
|
|
45
|
+
<span>{t(it.label, { defaultValue: it.label })}</span>
|
|
46
|
+
{it.count !== undefined ? (
|
|
47
|
+
<span className='rounded-full bg-muted px-1.5 text-xs font-semibold tabular-nums'>{it.count}</span>
|
|
48
|
+
) : null}
|
|
49
|
+
</button>
|
|
50
|
+
)
|
|
51
|
+
})}
|
|
52
|
+
</nav>
|
|
53
|
+
)
|
|
54
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// StatusBadge — badge semántico con texto explícito e ícono por tono: el color
|
|
2
|
+
// nunca es la única señal (benchmark §6.3). El catálogo de estados de cada
|
|
3
|
+
// documento vive en su `StatusMachine`; aquí solo se pinta.
|
|
4
|
+
import { useTranslation } from 'react-i18next'
|
|
5
|
+
import { AlertOctagon, AlertTriangle, CheckCircle2, Circle, Clock, Info, type LucideIcon } from 'lucide-react'
|
|
6
|
+
import { Badge } from '@asteby/metacore-ui/primitives'
|
|
7
|
+
import { cn } from '@asteby/metacore-ui/lib'
|
|
8
|
+
import type { StatusDef, StatusTone } from './types'
|
|
9
|
+
|
|
10
|
+
type BadgeVariant = 'muted' | 'info' | 'success' | 'warning' | 'danger'
|
|
11
|
+
|
|
12
|
+
export const STATUS_TONE_STYLE: Record<StatusTone, { variant: BadgeVariant; icon: LucideIcon }> = {
|
|
13
|
+
neutral: { variant: 'muted', icon: Circle },
|
|
14
|
+
info: { variant: 'info', icon: Info },
|
|
15
|
+
success: { variant: 'success', icon: CheckCircle2 },
|
|
16
|
+
caution: { variant: 'warning', icon: Clock },
|
|
17
|
+
warning: { variant: 'warning', icon: AlertTriangle },
|
|
18
|
+
critical: { variant: 'danger', icon: AlertOctagon },
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface StatusBadgeProps {
|
|
22
|
+
status: Pick<StatusDef, 'label' | 'tone' | 'strike'>
|
|
23
|
+
/** Descripción para lectores de pantalla («Estado fiscal»). */
|
|
24
|
+
srLabel?: string
|
|
25
|
+
className?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export function StatusBadge({ status, srLabel, className }: StatusBadgeProps) {
|
|
29
|
+
const { t } = useTranslation()
|
|
30
|
+
const style = STATUS_TONE_STYLE[status.tone] ?? STATUS_TONE_STYLE.neutral
|
|
31
|
+
const Icon = style.icon
|
|
32
|
+
const text = t(status.label, { defaultValue: status.label })
|
|
33
|
+
return (
|
|
34
|
+
<Badge
|
|
35
|
+
variant={style.variant}
|
|
36
|
+
data-tone={status.tone}
|
|
37
|
+
className={cn(status.strike && 'line-through', className)}
|
|
38
|
+
>
|
|
39
|
+
<Icon aria-hidden />
|
|
40
|
+
{srLabel ? <span className='sr-only'>{srLabel}: </span> : null}
|
|
41
|
+
{text}
|
|
42
|
+
</Badge>
|
|
43
|
+
)
|
|
44
|
+
}
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// Patrón «página de documento» (benchmark §5.2 / §6 / §7): cabecera con badges,
|
|
2
|
+
// barra de acciones por estado, botones inteligentes con contador, pestañas e
|
|
3
|
+
// historial. Todo se describe con un `DocumentSpec` de datos puros (sin
|
|
4
|
+
// funciones) para que cualquier modelo lo declare y, más adelante, un manifest
|
|
5
|
+
// pueda proyectarlo sin cambiar los componentes.
|
|
6
|
+
import type { ReactNode } from 'react'
|
|
7
|
+
|
|
8
|
+
/** Tono semántico de un estado. El color nunca es la única señal: cada tono trae ícono y texto. */
|
|
9
|
+
export type StatusTone = 'neutral' | 'info' | 'success' | 'caution' | 'warning' | 'critical'
|
|
10
|
+
|
|
11
|
+
/** Un estado del catálogo de un documento. */
|
|
12
|
+
export interface StatusDef {
|
|
13
|
+
value: string
|
|
14
|
+
/** Texto explícito (o clave i18n; se traduce con `t`). */
|
|
15
|
+
label: string
|
|
16
|
+
tone: StatusTone
|
|
17
|
+
/** Texto tachado (Cancelada, Anulada): conserva el rastro sin alarmar. */
|
|
18
|
+
strike?: boolean
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Una máquina de estados con su propio badge (p. ej. fiscal y cobro, independientes). */
|
|
22
|
+
export interface StatusMachine {
|
|
23
|
+
id: string
|
|
24
|
+
/** Etiqueta accesible del badge («Estado fiscal»). */
|
|
25
|
+
label?: string
|
|
26
|
+
/** Ruta del valor en el contexto de evaluación (`record.x`, `$.x`). */
|
|
27
|
+
field: string
|
|
28
|
+
states: StatusDef[]
|
|
29
|
+
/** Estado a mostrar cuando el valor no está en el catálogo. */
|
|
30
|
+
fallback?: StatusDef
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type PredicateOp = 'eq' | 'neq' | 'in' | 'not_in' | 'truthy' | 'falsy' | 'gt' | 'gte' | 'lt' | 'lte'
|
|
34
|
+
|
|
35
|
+
/** Condición sobre una ruta del contexto. Un arreglo de predicados es un AND. */
|
|
36
|
+
export interface Predicate {
|
|
37
|
+
field: string
|
|
38
|
+
op: PredicateOp
|
|
39
|
+
value?: unknown
|
|
40
|
+
}
|
|
41
|
+
export type When = Predicate | Predicate[]
|
|
42
|
+
|
|
43
|
+
/** Regla de bloqueo: si `when` se cumple, la acción se ve deshabilitada y `reason` explica qué hacer antes. */
|
|
44
|
+
export interface BlockRule {
|
|
45
|
+
when: When
|
|
46
|
+
reason: string
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** Un botón del documento (acción). `key` enlaza con `ActionDefinition.key` del modelo si existe. */
|
|
50
|
+
export interface DocumentActionDef {
|
|
51
|
+
key: string
|
|
52
|
+
label: string
|
|
53
|
+
/** Nombre de ícono lucide. */
|
|
54
|
+
icon?: string
|
|
55
|
+
/** Acción del modelo (metadata) que se dispara; por defecto `key`. */
|
|
56
|
+
modelAction?: string
|
|
57
|
+
/**
|
|
58
|
+
* Acciones que resuelve la propia página sin acción del modelo (las plantillas
|
|
59
|
+
* interpolan `{{campo}}` / `{{$.derivado}}`):
|
|
60
|
+
* - `href`: navega dentro de la app (`onNavigate` del host).
|
|
61
|
+
* - `openUrl`: abre una URL en otra pestaña (descargar PDF/XML).
|
|
62
|
+
* - `tab`: selecciona una pestaña (ver detalle del error, ver pagos).
|
|
63
|
+
*/
|
|
64
|
+
href?: string
|
|
65
|
+
openUrl?: string
|
|
66
|
+
tab?: string
|
|
67
|
+
/** Estilo crítico: siempre va en la zona destructiva. */
|
|
68
|
+
destructive?: boolean
|
|
69
|
+
/** Se muestra deshabilitada con el motivo en lugar de desaparecer. */
|
|
70
|
+
blockedWhen?: BlockRule | BlockRule[]
|
|
71
|
+
/** Se oculta por completo cuando se cumple (p. ej. sin permiso o sin conector). */
|
|
72
|
+
hiddenWhen?: When
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Distribución de acciones para un estado (§5.4): 1 primaria, 2–3 secundarias, resto en «Más…». */
|
|
76
|
+
export interface ActionLayout {
|
|
77
|
+
primary?: string
|
|
78
|
+
secondary?: string[]
|
|
79
|
+
more?: string[]
|
|
80
|
+
destructive?: string[]
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Regla: si `when` se cumple, se usa `layout`. Gana la primera que coincide. */
|
|
84
|
+
export interface ActionLayoutRule {
|
|
85
|
+
when: When
|
|
86
|
+
layout: ActionLayout
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Botón inteligente con contador (estilo Odoo): navega a lo relacionado. */
|
|
90
|
+
export interface SmartButtonDef {
|
|
91
|
+
key: string
|
|
92
|
+
label: string
|
|
93
|
+
icon?: string
|
|
94
|
+
/** Fuente (`DocumentSourceDef.key`) cuyo total alimenta el contador; sin ella no hay contador. */
|
|
95
|
+
source?: string
|
|
96
|
+
/** Ruta de un valor del registro cuyo truthy activa el botón sin fuente (p. ej. «Pedido origen»). */
|
|
97
|
+
field?: string
|
|
98
|
+
/** Modelo destino para navegar a la lista filtrada. */
|
|
99
|
+
href?: string
|
|
100
|
+
/** No se muestra cuando el contador es 0 (por defecto se muestra deshabilitado). */
|
|
101
|
+
hideWhenEmpty?: boolean
|
|
102
|
+
/** Selecciona esta pestaña al hacer clic. */
|
|
103
|
+
tab?: string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Consulta a un modelo relacionado con el registro (`{{id}}` se interpola). */
|
|
107
|
+
export interface DocumentSourceDef {
|
|
108
|
+
key: string
|
|
109
|
+
model: string
|
|
110
|
+
/** Columna del modelo relacionado que apunta al registro. */
|
|
111
|
+
foreignKey: string
|
|
112
|
+
/** Filtros estáticos extra (`f_<col>=eq:<val>`). */
|
|
113
|
+
where?: Record<string, string>
|
|
114
|
+
limit?: number
|
|
115
|
+
/**
|
|
116
|
+
* Relación de dos saltos: en vez de filtrar por el id del registro, filtra
|
|
117
|
+
* `foreignKey` con los valores de `field` de las filas de otra fuente ya
|
|
118
|
+
* cargada (`via.source`). Ej.: los REP de una factura cuelgan del pago
|
|
119
|
+
* (`source_id = payment_id`), y los pagos salen de las aplicaciones.
|
|
120
|
+
*/
|
|
121
|
+
via?: { source: string; field: string }
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export interface DocumentFieldDef {
|
|
125
|
+
label: string
|
|
126
|
+
/** Ruta en el contexto. */
|
|
127
|
+
path: string
|
|
128
|
+
format?: 'text' | 'money' | 'date' | 'datetime' | 'mono' | 'percent' | 'boolean'
|
|
129
|
+
/** Ancho completo de la rejilla. */
|
|
130
|
+
wide?: boolean
|
|
131
|
+
/** Oculta el campo cuando el valor está vacío. */
|
|
132
|
+
hideEmpty?: boolean
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export interface DocumentFieldGroup {
|
|
136
|
+
title?: string
|
|
137
|
+
fields: DocumentFieldDef[]
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export interface DocumentColumnDef {
|
|
141
|
+
label: string
|
|
142
|
+
path: string
|
|
143
|
+
format?: DocumentFieldDef['format']
|
|
144
|
+
/** Catálogo de estados (id de máquina registrada) para pintar un badge. */
|
|
145
|
+
status?: string
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export type DocumentTabDef =
|
|
149
|
+
| { key: string; label: string; kind: 'fields'; groups: DocumentFieldGroup[] }
|
|
150
|
+
| { key: string; label: string; kind: 'records'; source: string; columns: DocumentColumnDef[]; empty?: string }
|
|
151
|
+
| { key: string; label: string; kind: 'slot'; slot: string; count?: string }
|
|
152
|
+
|
|
153
|
+
/** Métrica del encabezado (total, saldo, fechas…). */
|
|
154
|
+
export interface DocumentMetricDef extends DocumentFieldDef {
|
|
155
|
+
tone?: 'default' | 'critical'
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface DocumentSpec {
|
|
159
|
+
/** Clave del modelo (`invoices`). */
|
|
160
|
+
model: string
|
|
161
|
+
/** Rutas candidatas para el título, en orden; `{{a}}{{b}}` se interpola. */
|
|
162
|
+
title: string
|
|
163
|
+
/** Texto bajo el título (cliente…). */
|
|
164
|
+
subtitle?: DocumentFieldDef
|
|
165
|
+
/** Campo que se copia con botón (UUID). */
|
|
166
|
+
copyable?: DocumentFieldDef
|
|
167
|
+
/** Badges independientes (fiscal, cobro…). */
|
|
168
|
+
statuses: StatusMachine[]
|
|
169
|
+
/** Catálogos extra que solo pintan celdas de pestañas (`column.status`), sin badge en la cabecera. */
|
|
170
|
+
catalogs?: StatusMachine[]
|
|
171
|
+
metrics?: DocumentMetricDef[]
|
|
172
|
+
actions: DocumentActionDef[]
|
|
173
|
+
layouts: ActionLayoutRule[]
|
|
174
|
+
/** Layout si ninguna regla coincide. */
|
|
175
|
+
defaultLayout?: ActionLayout
|
|
176
|
+
smartButtons?: SmartButtonDef[]
|
|
177
|
+
sources?: DocumentSourceDef[]
|
|
178
|
+
tabs: DocumentTabDef[]
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** Contexto de evaluación: registro, fuentes cargadas y derivados. */
|
|
182
|
+
export interface DocumentContext {
|
|
183
|
+
record: Record<string, unknown>
|
|
184
|
+
/** Filas por fuente (`sources[key]`). */
|
|
185
|
+
sources: Record<string, Record<string, unknown>[]>
|
|
186
|
+
/** Derivados (`$`). */
|
|
187
|
+
derived: Record<string, unknown>
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
export interface ResolvedAction {
|
|
191
|
+
def: DocumentActionDef
|
|
192
|
+
/** Motivo cuando la acción está bloqueada. */
|
|
193
|
+
blockedReason?: string
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
export interface ResolvedActionLayout {
|
|
197
|
+
primary?: ResolvedAction
|
|
198
|
+
secondary: ResolvedAction[]
|
|
199
|
+
more: ResolvedAction[]
|
|
200
|
+
destructive: ResolvedAction[]
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export type DocumentSlotRenderer = (ctx: DocumentContext) => ReactNode
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useCallback, useEffect, useState } from 'react'
|
|
2
|
+
import { useApi } from '../api-context'
|
|
3
|
+
import { buildRelationFilterParams } from '../dynamic-relation-helpers'
|
|
4
|
+
import type { DocumentSourceDef } from './types'
|
|
5
|
+
|
|
6
|
+
const DEFAULT_LIMIT = 50
|
|
7
|
+
// El kernel ≥ 0.139.2 acepta listas `in:` largas; se parte para no exceder la URL.
|
|
8
|
+
const VIA_CHUNK = 40
|
|
9
|
+
|
|
10
|
+
type Rows = Record<string, unknown>[]
|
|
11
|
+
|
|
12
|
+
export interface DocumentSourcesState {
|
|
13
|
+
sources: Record<string, Record<string, unknown>[]>
|
|
14
|
+
loading: boolean
|
|
15
|
+
/** Vuelve a pedir todas las fuentes (tras ejecutar una acción). */
|
|
16
|
+
reload: () => void
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Carga las filas relacionadas con el documento (pagos, REP, notas de crédito…)
|
|
21
|
+
* que alimentan contadores, pestañas y derivados. Un fallo en una fuente deja
|
|
22
|
+
* esa fuente vacía: la página sigue siendo usable.
|
|
23
|
+
*/
|
|
24
|
+
export function useDocumentSources(
|
|
25
|
+
defs: DocumentSourceDef[] | undefined,
|
|
26
|
+
recordId: string | number | undefined,
|
|
27
|
+
): DocumentSourcesState {
|
|
28
|
+
const api = useApi()
|
|
29
|
+
const [sources, setSources] = useState<Record<string, Record<string, unknown>[]>>({})
|
|
30
|
+
const [loading, setLoading] = useState(false)
|
|
31
|
+
const [nonce, setNonce] = useState(0)
|
|
32
|
+
const reload = useCallback(() => setNonce((n) => n + 1), [])
|
|
33
|
+
|
|
34
|
+
useEffect(() => {
|
|
35
|
+
if (!defs || defs.length === 0 || recordId === undefined || recordId === null) {
|
|
36
|
+
setSources({})
|
|
37
|
+
return
|
|
38
|
+
}
|
|
39
|
+
let cancelled = false
|
|
40
|
+
setLoading(true)
|
|
41
|
+
const fetchRows = async (d: DocumentSourceDef, params: Record<string, string>): Promise<Rows> => {
|
|
42
|
+
try {
|
|
43
|
+
const res = await api.get(`/data/${d.model}`, {
|
|
44
|
+
params: { ...params, page: 1, per_page: d.limit ?? DEFAULT_LIMIT },
|
|
45
|
+
})
|
|
46
|
+
const body = res.data
|
|
47
|
+
const rows = (body?.data ?? body) as unknown
|
|
48
|
+
return Array.isArray(rows) ? (rows as Rows) : []
|
|
49
|
+
} catch {
|
|
50
|
+
return []
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
const direct = defs.filter((d) => !d.via)
|
|
54
|
+
const hopped = defs.filter((d) => d.via)
|
|
55
|
+
;(async () => {
|
|
56
|
+
const out: Record<string, Rows> = {}
|
|
57
|
+
await Promise.all(
|
|
58
|
+
direct.map(async (d) => {
|
|
59
|
+
out[d.key] = await fetchRows(d, buildRelationFilterParams(d.foreignKey, recordId, d.where))
|
|
60
|
+
}),
|
|
61
|
+
)
|
|
62
|
+
// Segundo salto: depende de las filas del primero.
|
|
63
|
+
for (const d of hopped) {
|
|
64
|
+
const ids = Array.from(
|
|
65
|
+
new Set((out[d.via!.source] ?? []).map((r) => r[d.via!.field]).filter((v) => v != null && v !== '').map(String)),
|
|
66
|
+
)
|
|
67
|
+
const chunks: string[][] = []
|
|
68
|
+
for (let i = 0; i < ids.length; i += VIA_CHUNK) chunks.push(ids.slice(i, i + VIA_CHUNK))
|
|
69
|
+
const parts = await Promise.all(
|
|
70
|
+
chunks.map((c) => {
|
|
71
|
+
const params: Record<string, string> = { [`f_${d.foreignKey}`]: `in:${c.join(',')}` }
|
|
72
|
+
for (const [k, v] of Object.entries(d.where ?? {})) params[`f_${k}`] = `eq:${v}`
|
|
73
|
+
return fetchRows(d, params)
|
|
74
|
+
}),
|
|
75
|
+
)
|
|
76
|
+
out[d.key] = parts.flat()
|
|
77
|
+
}
|
|
78
|
+
return out
|
|
79
|
+
})()
|
|
80
|
+
.then((out) => {
|
|
81
|
+
if (cancelled) return
|
|
82
|
+
setSources(out)
|
|
83
|
+
setLoading(false)
|
|
84
|
+
})
|
|
85
|
+
return () => {
|
|
86
|
+
cancelled = true
|
|
87
|
+
}
|
|
88
|
+
}, [api, defs, recordId, nonce])
|
|
89
|
+
|
|
90
|
+
return { sources, loading, reload }
|
|
91
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -218,6 +218,25 @@ export {
|
|
|
218
218
|
type CanFn,
|
|
219
219
|
type PermissionsProviderProps,
|
|
220
220
|
} from './permissions-context'
|
|
221
|
+
export {
|
|
222
|
+
ApprovalPinDialog,
|
|
223
|
+
approvalErrorMessage,
|
|
224
|
+
type ApprovalPinDialogProps,
|
|
225
|
+
type ApprovalPinSubmit,
|
|
226
|
+
} from './approval-pin-dialog'
|
|
227
|
+
export {
|
|
228
|
+
ApprovalGateProvider,
|
|
229
|
+
useApprovalGate,
|
|
230
|
+
approvalCapability,
|
|
231
|
+
approvalRequiredInfo,
|
|
232
|
+
approvalGateAvailable,
|
|
233
|
+
APPROVAL_PARKED_EVENT,
|
|
234
|
+
APPROVAL_DECIDED_EVENT,
|
|
235
|
+
type ApprovalGateValue,
|
|
236
|
+
type ApprovalGrant,
|
|
237
|
+
type ApprovalRequiredInfo,
|
|
238
|
+
type RequestApprovalOptions,
|
|
239
|
+
} from './approval-gate'
|
|
221
240
|
export {
|
|
222
241
|
useDynamicRowActions,
|
|
223
242
|
type UseDynamicRowActionsParams,
|
|
@@ -282,6 +301,7 @@ export {
|
|
|
282
301
|
type SidebarLayoutManagerProps,
|
|
283
302
|
} from './sidebar-layout-manager'
|
|
284
303
|
export * from './org-runtime-context'
|
|
304
|
+
export * from './business'
|
|
285
305
|
export * from './org-runtime-provider'
|
|
286
306
|
export * from './navigation-builder'
|
|
287
307
|
export * from './i18n-provider'
|
|
@@ -581,3 +601,6 @@ export {
|
|
|
581
601
|
type AttributeClass,
|
|
582
602
|
type AttributeClassSection,
|
|
583
603
|
} from './attribute-classes'
|
|
604
|
+
|
|
605
|
+
// Página de documento (benchmark §5/§6): cabecera, badges, acciones por estado, botones inteligentes.
|
|
606
|
+
export * from './document'
|
package/src/server-error.ts
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
// the toast description, in ONE place so every call site behaves identically.
|
|
12
12
|
import { toast } from 'sonner'
|
|
13
13
|
import { validationCatalog, validationMessageKey } from './validation-catalog'
|
|
14
|
+
import { APPROVAL_PARKED_EVENT, approvalGateAvailable, approvalRequiredInfo } from './approval-events'
|
|
14
15
|
|
|
15
16
|
/** Structured, display-ready view of an error: a headline + an optional cause. */
|
|
16
17
|
export interface ExtractedError {
|
|
@@ -234,6 +235,25 @@ export function toastServerError(
|
|
|
234
235
|
const t = opts?.t
|
|
235
236
|
const lang = opts?.language
|
|
236
237
|
const cat = validationCatalog(lang)
|
|
238
|
+
// A write parked for approval is not a failure: say so, and when an
|
|
239
|
+
// <ApprovalGateProvider> is mounted offer to approve it right now with a PIN.
|
|
240
|
+
const parked = approvalRequiredInfo(err)
|
|
241
|
+
if (parked) {
|
|
242
|
+
const title = parked.label || (t ? t('approvals.sent', { defaultValue: 'Enviado a aprobación' }) : 'Enviado a aprobación')
|
|
243
|
+
toast.info(title, {
|
|
244
|
+
description: parked.roles.length
|
|
245
|
+
? (t ? t('approvals.waiting_for', { defaultValue: 'Lo decide: {{roles}}', roles: parked.roles.join(', ') }) : parked.roles.join(', '))
|
|
246
|
+
: undefined,
|
|
247
|
+
action:
|
|
248
|
+
parked.requestId && approvalGateAvailable()
|
|
249
|
+
? {
|
|
250
|
+
label: t ? t('approvals.approve_with_pin', { defaultValue: 'Aprobar con PIN' }) : 'Aprobar con PIN',
|
|
251
|
+
onClick: () => window.dispatchEvent(new CustomEvent(APPROVAL_PARKED_EVENT, { detail: parked })),
|
|
252
|
+
}
|
|
253
|
+
: undefined,
|
|
254
|
+
})
|
|
255
|
+
return
|
|
256
|
+
}
|
|
237
257
|
const map = extractFieldErrors(err)
|
|
238
258
|
if (map) {
|
|
239
259
|
const localized = t
|