@asteby/metacore-runtime-react 53.2.0 → 53.4.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.
Files changed (49) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/dynamic-columns.d.ts.map +1 -1
  3. package/dist/dynamic-columns.js +26 -4
  4. package/dist/index.d.ts +1 -1
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +1 -1
  7. package/dist/license/index.d.ts +3 -1
  8. package/dist/license/index.d.ts.map +1 -1
  9. package/dist/license/index.js +3 -1
  10. package/dist/license/license-expiry-banner.d.ts +8 -1
  11. package/dist/license/license-expiry-banner.d.ts.map +1 -1
  12. package/dist/license/license-expiry-banner.js +96 -54
  13. package/dist/license/license-gate.d.ts +14 -4
  14. package/dist/license/license-gate.d.ts.map +1 -1
  15. package/dist/license/license-gate.js +75 -63
  16. package/dist/license/license-status-badge.d.ts.map +1 -1
  17. package/dist/license/license-status-badge.js +8 -9
  18. package/dist/license/messages.d.ts +29 -0
  19. package/dist/license/messages.d.ts.map +1 -0
  20. package/dist/license/messages.js +159 -0
  21. package/dist/license/types.d.ts +30 -7
  22. package/dist/license/types.d.ts.map +1 -1
  23. package/dist/license/types.js +20 -6
  24. package/dist/license/use-license-t.d.ts +11 -0
  25. package/dist/license/use-license-t.d.ts.map +1 -0
  26. package/dist/license/use-license-t.js +18 -0
  27. package/dist/license/view.d.ts +40 -0
  28. package/dist/license/view.d.ts.map +1 -0
  29. package/dist/license/view.js +62 -0
  30. package/dist/relative-date.d.ts +37 -0
  31. package/dist/relative-date.d.ts.map +1 -0
  32. package/dist/relative-date.js +110 -0
  33. package/dist/types.d.ts +1 -1
  34. package/dist/types.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/__tests__/license-states.test.tsx +381 -0
  37. package/src/__tests__/relative-date-brand-avatar.test.tsx +154 -0
  38. package/src/dynamic-columns.tsx +48 -6
  39. package/src/index.ts +13 -0
  40. package/src/license/index.ts +19 -0
  41. package/src/license/license-expiry-banner.tsx +155 -94
  42. package/src/license/license-gate.tsx +210 -147
  43. package/src/license/license-status-badge.tsx +8 -10
  44. package/src/license/messages.ts +185 -0
  45. package/src/license/types.ts +38 -6
  46. package/src/license/use-license-t.ts +23 -0
  47. package/src/license/view.ts +93 -0
  48. package/src/relative-date.tsx +142 -0
  49. package/src/types.ts +2 -0
@@ -13,9 +13,28 @@ export {
13
13
  export {
14
14
  isLicenseOperable,
15
15
  isLicenseBlocking,
16
+ isLicenseReadOnly,
16
17
  isPresetEntitled,
17
18
  isTrialExpired,
18
19
  type LicenseState,
19
20
  type LicenseStatus,
21
+ type LicenseSeller,
22
+ type LicenseOnExpiry,
20
23
  type LicenseBranding,
21
24
  } from './types'
25
+ export {
26
+ deriveLicenseView,
27
+ isLicenseCloned,
28
+ sellerContactLine,
29
+ CLONE_NOTICE,
30
+ type LicenseView,
31
+ type LicenseCondition,
32
+ type LicenseSeverity,
33
+ } from './view'
34
+ export {
35
+ LICENSE_MESSAGES,
36
+ registerLicenseMessages,
37
+ formatLicenseDate,
38
+ licenseLang,
39
+ type LicenseLang,
40
+ } from './messages'
@@ -1,21 +1,24 @@
1
1
  /**
2
- * Banner de vencimiento de licencia — la señal degradada que acompaña a las
3
- * posturas que NO bloquean la app pero exigen atención:
4
- * - valid && days_remaining <= threshold → aviso de renovación próxima
5
- * - stale → check-in con el hub pendiente
6
- * - grace → vencida, dentro de la gracia
7
- * - expired → vencida en duro (persistente)
2
+ * Banner de licencia — la señal que acompaña a los estados que NO bloquean la
3
+ * app pero piden atención (ver `deriveLicenseView`):
4
+ * - por vencer aviso de renovación próxima
5
+ * - en gracia vencida, todavía se puede operar
6
+ * - vencida (solo lectura) / desactivada con plazo: se consulta y se exporta
7
+ * - hub caído sin contacto con el hub pero opera con su lease (solo dueño)
8
+ * - clonada licencia vista en otro servidor (solo dueño)
8
9
  *
9
10
  * Silencioso sin enforcement o si el estado aún no cargó — nunca rompe el shell.
11
+ * Descartable con TTL en localStorage, MENOS los estados de solo lectura, que se
12
+ * quedan fijos porque el backend ya rechaza escrituras nuevas.
10
13
  *
11
- * Descartabilidad: todas las posturas son descartables con TTL en localStorage
12
- * (re-aparecen pasado el TTL) MENOS `expired`, que se queda fijo como recordatorio
13
- * porque el backend ya bloquea escrituras nuevas.
14
+ * Cuando la licencia trae el contacto del vendedor, se muestra en el propio aviso.
14
15
  */
15
16
  import { useState } from 'react'
16
- import { useTranslation } from 'react-i18next'
17
- import { AlertTriangle, Clock, CloudOff, X } from 'lucide-react'
17
+ import { AlertTriangle, Clock, CloudOff, Copy, X } from 'lucide-react'
18
18
  import { Button } from '@asteby/metacore-ui'
19
+ import { formatLicenseDate } from './messages'
20
+ import { deriveLicenseView, sellerContactLine } from './view'
21
+ import { useLicenseT } from './use-license-t'
19
22
  import type { LicenseState } from './types'
20
23
 
21
24
  const DISMISS_KEY = 'license_banner_dismissed_at'
@@ -26,6 +29,17 @@ function cx(...parts: Array<string | false | undefined>): string {
26
29
  return parts.filter(Boolean).join(' ')
27
30
  }
28
31
 
32
+ function readDismissed(key: string): boolean {
33
+ try {
34
+ if (typeof window === 'undefined') return false
35
+ const raw = window.localStorage.getItem(key)
36
+ if (!raw) return false
37
+ return Date.now() - Number(raw) < DISMISS_TTL_MS
38
+ } catch {
39
+ return false // sin storage (modo privado, SSR): el aviso se muestra
40
+ }
41
+ }
42
+
29
43
  export interface LicenseExpiryBannerProps {
30
44
  state: LicenseState | undefined
31
45
  /** Días de anticipación para el aviso de renovación. Default 15. */
@@ -37,6 +51,13 @@ export interface LicenseExpiryBannerProps {
37
51
  onManage?: () => void
38
52
  /** Etiqueta del botón de gestión (default localizado). */
39
53
  manageLabel?: string
54
+ /** Exportar los datos (solo lectura): el host abre su flujo de exportación.
55
+ * Sin él, no se muestra el botón. */
56
+ onExport?: () => void
57
+ /** El usuario actual es el dueño/superadmin de la instancia. Los avisos de
58
+ * hub caído y de clonado son solo para él. Default false: un host que no
59
+ * informa el rol nunca los muestra a usuarios que no son dueños. */
60
+ isOwner?: boolean
40
61
  className?: string
41
62
  }
42
63
 
@@ -46,27 +67,30 @@ export function LicenseExpiryBanner({
46
67
  dismissStorageKey = DISMISS_KEY,
47
68
  onManage,
48
69
  manageLabel,
70
+ onExport,
71
+ isOwner = false,
49
72
  className,
50
73
  }: LicenseExpiryBannerProps) {
51
- const { t } = useTranslation()
52
- const [dismissed, setDismissed] = useState(() => {
53
- if (typeof window === 'undefined') return false
54
- const raw = window.localStorage.getItem(dismissStorageKey)
55
- if (!raw) return false
56
- return Date.now() - Number(raw) < DISMISS_TTL_MS
57
- })
58
-
59
- if (!state || !state.enforced) return null
74
+ const { t, lang } = useLicenseT()
75
+ const [dismissed, setDismissed] = useState(() => readDismissed(dismissStorageKey))
60
76
 
61
- const isUpcoming =
62
- state.status === 'valid' && state.days_remaining <= upcomingThresholdDays
63
- const isStale = state.status === 'stale'
64
- const isGrace = state.status === 'grace'
65
- const isExpired = state.status === 'expired'
66
- if (!isUpcoming && !isStale && !isGrace && !isExpired) return null
77
+ const view = deriveLicenseView(state, { upcomingThresholdDays })
78
+ if (!state || !view) return null
67
79
 
68
- // Expired no es descartable — se queda como recordatorio constante.
69
- if (dismissed && !isExpired) return null
80
+ const showMain =
81
+ view.condition === 'expiring' ||
82
+ view.condition === 'grace' ||
83
+ view.condition === 'hub_unreachable' ||
84
+ (view.condition === 'expired' && view.readOnly) ||
85
+ (view.condition === 'revoked' && view.readOnly) ||
86
+ // expired en modo bloqueo se pinta aquí solo si el host no monta el gate.
87
+ (view.condition === 'expired' && !view.readOnly)
88
+ const showClone = view.cloned && isOwner
89
+ const sticky = view.readOnly || view.condition === 'expired'
90
+ // El descarte solo oculta la fila principal no fija; la fila de clonado
91
+ // nunca se descarta con ella (un aviso menor no puede apagar el de clonado).
92
+ const mainVisible = showMain && !(view.ownerOnly && !isOwner) && !(dismissed && !sticky)
93
+ if (!mainVisible && !showClone) return null
70
94
 
71
95
  const dismiss = () => {
72
96
  setDismissed(true)
@@ -77,75 +101,112 @@ export function LicenseExpiryBanner({
77
101
  }
78
102
  }
79
103
 
80
- const message = isExpired
81
- ? t('license.banner.expired', {
82
- defaultValue:
83
- 'Tu licencia venció. Algunas acciones pueden estar bloqueadas.',
84
- })
85
- : isStale
86
- ? t('license.banner.stale', {
87
- defaultValue:
88
- 'Esta instancia no ha podido verificar su licencia con el hub. Reconéctala para completar el check-in.',
89
- })
90
- : isGrace
91
- ? t('license.banner.grace', {
92
- defaultValue:
93
- 'Tu licencia venció y está en periodo de gracia hasta {{date}} ({{days}} días restantes).',
94
- date: state.grace_until
95
- ? new Date(state.grace_until).toLocaleDateString()
96
- : '—',
97
- days: state.days_remaining,
98
- })
99
- : t('license.banner.upcoming', {
100
- defaultValue: 'Tu licencia vence en {{days}} días.',
101
- days: state.days_remaining,
102
- })
104
+ const provider = t('license.banner.provider')
105
+ const who = state.seller?.name?.trim() || provider
106
+ const whoCap = state.seller?.name?.trim() || t('license.gate.provider')
107
+ const contact = sellerContactLine(state)
108
+ const expiredOn = formatLicenseDate(state.expires_at, lang)
109
+ const graceUntil = formatLicenseDate(state.grace_until, lang)
110
+ const lastSeen = formatLicenseDate(state.last_checked_at, lang)
103
111
 
104
- const Icon = isExpired ? AlertTriangle : isStale ? CloudOff : Clock
112
+ const mainMessage = ((): string => {
113
+ switch (view.condition) {
114
+ case 'expiring':
115
+ return state.days_remaining <= 0
116
+ ? t('license.banner.upcoming_today')
117
+ : state.days_remaining === 1
118
+ ? t('license.banner.upcoming_one')
119
+ : t('license.banner.upcoming', { days: state.days_remaining })
120
+ case 'grace':
121
+ return expiredOn
122
+ ? t('license.banner.grace', { date: expiredOn, days: state.days_remaining })
123
+ : t('license.banner.grace_nodate', { days: state.days_remaining })
124
+ case 'expired':
125
+ if (!view.readOnly) return t('license.banner.expired')
126
+ return expiredOn
127
+ ? t('license.banner.expired_read_only', { date: expiredOn })
128
+ : t('license.banner.expired_read_only_nodate')
129
+ case 'revoked':
130
+ return graceUntil
131
+ ? t('license.banner.revoked', { who: whoCap, date: graceUntil })
132
+ : t('license.banner.revoked_nodate', { who: whoCap })
133
+ default: // hub_unreachable
134
+ return lastSeen
135
+ ? t('license.banner.stale', { date: lastSeen })
136
+ : t('license.banner.stale_nodate')
137
+ }
138
+ })()
139
+
140
+ const critical = view.severity === 'critical'
141
+ const rows: Array<{ id: string; message: string; Icon: typeof Clock; critical: boolean }> = []
142
+ if (mainVisible) {
143
+ rows.push({
144
+ id: 'main',
145
+ message: mainMessage,
146
+ Icon: critical ? AlertTriangle : view.condition === 'hub_unreachable' ? CloudOff : Clock,
147
+ critical,
148
+ })
149
+ }
150
+ if (showClone) {
151
+ rows.push({
152
+ id: 'clone',
153
+ message: t('license.banner.cloned', { who }),
154
+ Icon: Copy,
155
+ critical: false,
156
+ })
157
+ }
105
158
 
106
159
  return (
107
- <div
108
- role="alert"
109
- data-license-status={state.status}
110
- className={cx(
111
- 'flex items-center justify-between gap-3 border-b px-4 py-2.5 text-sm',
112
- isExpired
113
- ? 'border-destructive/30 bg-destructive text-white'
114
- : 'border-amber-500/30 bg-amber-500/10 text-foreground',
115
- className,
116
- )}
117
- >
118
- <div className="flex min-w-0 items-center gap-2">
119
- <Icon className="h-4 w-4 shrink-0" aria-hidden />
120
- <span className="truncate">{message}</span>
121
- </div>
122
- <div className="flex shrink-0 items-center gap-2">
123
- {onManage && (
124
- <Button
125
- size="sm"
126
- variant={isExpired ? 'secondary' : 'outline'}
127
- onClick={onManage}
128
- >
129
- {manageLabel ??
130
- t('license.banner.manage', {
131
- defaultValue: 'Gestionar licencia',
132
- })}
133
- </Button>
134
- )}
135
- {!isExpired && (
136
- <Button
137
- size="icon"
138
- variant="ghost"
139
- className="h-7 w-7"
140
- onClick={dismiss}
141
- title={t('license.banner.dismiss', {
142
- defaultValue: 'Descartar',
143
- })}
144
- >
145
- <X className="h-4 w-4" />
146
- </Button>
147
- )}
148
- </div>
160
+ <div data-license-status={state.status} data-license-condition={view.condition} className={className}>
161
+ {rows.map((row, i) => (
162
+ <div
163
+ key={row.id}
164
+ role="alert"
165
+ data-license-row={row.id}
166
+ className={cx(
167
+ 'flex items-center justify-between gap-3 border-b px-4 py-2.5 text-sm',
168
+ row.critical
169
+ ? 'border-destructive/30 bg-destructive text-white'
170
+ : 'border-amber-500/30 bg-amber-500/10 text-foreground',
171
+ )}
172
+ >
173
+ <div className="flex min-w-0 items-center gap-2">
174
+ <row.Icon className="h-4 w-4 shrink-0" aria-hidden />
175
+ <span className="min-w-0">
176
+ {row.message}
177
+ {row.id === 'main' && contact && (view.condition === 'expired' || view.condition === 'revoked' || view.condition === 'grace') && (
178
+ <span className="ml-1 opacity-90">
179
+ {t('license.banner.contact', { contact: `${state.seller?.name ? `${state.seller.name} · ` : ''}${contact}` })}
180
+ </span>
181
+ )}
182
+ </span>
183
+ </div>
184
+ <div className="flex shrink-0 items-center gap-2">
185
+ {i === 0 && onExport && view.readOnly && (
186
+ <Button size="sm" variant="secondary" onClick={onExport}>
187
+ {t('license.banner.export')}
188
+ </Button>
189
+ )}
190
+ {i === 0 && onManage && (
191
+ <Button size="sm" variant={row.critical ? 'secondary' : 'outline'} onClick={onManage}>
192
+ {manageLabel ?? t('license.banner.manage')}
193
+ </Button>
194
+ )}
195
+ {row.id === 'main' && !sticky && (
196
+ <Button
197
+ size="icon"
198
+ variant="ghost"
199
+ className="h-7 w-7"
200
+ onClick={dismiss}
201
+ title={t('license.banner.dismiss')}
202
+ >
203
+ <X className="h-4 w-4" />
204
+ </Button>
205
+ )}
206
+ </div>
207
+ </div>
208
+ ))}
149
209
  </div>
150
210
  )
151
211
  }
212
+