@asteby/metacore-runtime-react 18.10.1 → 18.11.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 +30 -0
- package/dist/activity-diff.d.ts +70 -0
- package/dist/activity-diff.d.ts.map +1 -0
- package/dist/activity-diff.js +133 -0
- package/dist/activity-timeline.d.ts +56 -0
- package/dist/activity-timeline.d.ts.map +1 -0
- package/dist/activity-timeline.js +215 -0
- package/dist/activity-value-renderer.d.ts +33 -0
- package/dist/activity-value-renderer.d.ts.map +1 -0
- package/dist/activity-value-renderer.js +213 -0
- package/dist/dynamic-table.d.ts.map +1 -1
- package/dist/dynamic-table.js +5 -5
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/record-history.d.ts +41 -0
- package/dist/record-history.d.ts.map +1 -0
- package/dist/record-history.js +99 -0
- package/package.json +3 -3
- package/src/activity-diff.tsx +298 -0
- package/src/activity-timeline.tsx +574 -0
- package/src/activity-value-renderer.tsx +371 -0
- package/src/dynamic-table.tsx +13 -3
- package/src/index.ts +17 -0
- package/src/record-history.tsx +243 -0
|
@@ -0,0 +1,371 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* activity-value-renderer.tsx
|
|
3
|
+
*
|
|
4
|
+
* Pure, transport-agnostic value renderer for Activity / Time Machine diffs.
|
|
5
|
+
* Reuses the same display-type logic as dynamic-columns.tsx (currency, status,
|
|
6
|
+
* date, boolean, badge, relation, url, tags, color, number, percent) so the
|
|
7
|
+
* diff cells and the table cells are always consistent.
|
|
8
|
+
*
|
|
9
|
+
* Kept in its own module so it has no dependency on tanstack-table or the
|
|
10
|
+
* column-factory machinery — only React + metacore-ui primitives.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as React from 'react'
|
|
14
|
+
import * as icons from 'lucide-react'
|
|
15
|
+
import { es, enUS } from 'date-fns/locale'
|
|
16
|
+
import {
|
|
17
|
+
Badge,
|
|
18
|
+
Avatar,
|
|
19
|
+
AvatarImage,
|
|
20
|
+
AvatarFallback,
|
|
21
|
+
} from '@asteby/metacore-ui/primitives'
|
|
22
|
+
import { generateBadgeStyles, getInitials, optionColor, relationChipStyles } from '@asteby/metacore-ui/lib'
|
|
23
|
+
import type { ColumnDefinition } from './types'
|
|
24
|
+
import { formatDateCell } from './dynamic-columns'
|
|
25
|
+
import { humanizeToken } from './dynamic-columns-helpers'
|
|
26
|
+
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
// Internal helpers (mirror dynamic-columns.tsx private helpers)
|
|
29
|
+
// ---------------------------------------------------------------------------
|
|
30
|
+
|
|
31
|
+
const styleCfg = (col: ColumnDefinition, ...keys: string[]): any => {
|
|
32
|
+
const cfg = col.styleConfig
|
|
33
|
+
if (!cfg) return undefined
|
|
34
|
+
for (const k of keys) {
|
|
35
|
+
if (cfg[k] !== undefined && cfg[k] !== null) return cfg[k]
|
|
36
|
+
}
|
|
37
|
+
return undefined
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const formatNumber = (value: number, opts: Intl.NumberFormatOptions, locale?: string) =>
|
|
41
|
+
new Intl.NumberFormat(locale || undefined, opts).format(value)
|
|
42
|
+
|
|
43
|
+
const statusColorFor = (value: string): string => {
|
|
44
|
+
const v = value.toLowerCase()
|
|
45
|
+
if (['active', 'enabled', 'paid', 'completed', 'done', 'success', 'approved', 'open'].includes(v))
|
|
46
|
+
return '#22c55e'
|
|
47
|
+
if (['pending', 'draft', 'processing', 'in_progress', 'review', 'waiting'].includes(v))
|
|
48
|
+
return '#eab308'
|
|
49
|
+
if (['inactive', 'disabled', 'cancelled', 'canceled', 'failed', 'rejected', 'error', 'closed'].includes(v))
|
|
50
|
+
return '#ef4444'
|
|
51
|
+
return '#6b7280'
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const useIsDarkTheme = () => {
|
|
55
|
+
const [isDark, setIsDark] = React.useState(() =>
|
|
56
|
+
typeof document !== 'undefined' && document.documentElement.classList.contains('dark'),
|
|
57
|
+
)
|
|
58
|
+
React.useEffect(() => {
|
|
59
|
+
if (typeof document === 'undefined') return
|
|
60
|
+
const sync = () => setIsDark(document.documentElement.classList.contains('dark'))
|
|
61
|
+
const observer = new MutationObserver(sync)
|
|
62
|
+
observer.observe(document.documentElement, { attributes: true, attributeFilter: ['class'] })
|
|
63
|
+
return () => observer.disconnect()
|
|
64
|
+
}, [])
|
|
65
|
+
return isDark
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Public API
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
|
|
72
|
+
export interface ActivityValueRendererProps {
|
|
73
|
+
/** The raw value to display (from before/after/changes). */
|
|
74
|
+
value: unknown
|
|
75
|
+
/** Column metadata for display formatting. Optional — falls back to string. */
|
|
76
|
+
col?: ColumnDefinition
|
|
77
|
+
/** IANA timezone (org config) for datetime cells. */
|
|
78
|
+
timeZone?: string
|
|
79
|
+
/** ISO 4217 currency for money cells. */
|
|
80
|
+
currency?: string
|
|
81
|
+
/** BCP-47 locale tag (e.g. 'es', 'en'). Defaults to 'es'. */
|
|
82
|
+
locale?: string
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Renders a single field value from an activity event using the same display
|
|
87
|
+
* type logic as `defaultGetDynamicColumns`. Pass a `ColumnDefinition` to get
|
|
88
|
+
* rich formatting (currency, status badge, date, boolean, etc.); without it
|
|
89
|
+
* the component falls back to a plain string representation.
|
|
90
|
+
*/
|
|
91
|
+
export const ActivityValueRenderer: React.FC<ActivityValueRendererProps> = ({
|
|
92
|
+
value,
|
|
93
|
+
col,
|
|
94
|
+
timeZone,
|
|
95
|
+
currency,
|
|
96
|
+
locale = 'es',
|
|
97
|
+
}) => {
|
|
98
|
+
const isDark = useIsDarkTheme()
|
|
99
|
+
const dateLocale = locale === 'en' ? enUS : es
|
|
100
|
+
|
|
101
|
+
// null / undefined → dash
|
|
102
|
+
if (value === null || value === undefined || value === '') {
|
|
103
|
+
return <span className="text-muted-foreground">—</span>
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// No column metadata → plain string
|
|
107
|
+
if (!col) {
|
|
108
|
+
if (typeof value === 'object') {
|
|
109
|
+
return (
|
|
110
|
+
<span className="text-muted-foreground text-xs font-mono">
|
|
111
|
+
{JSON.stringify(value)}
|
|
112
|
+
</span>
|
|
113
|
+
)
|
|
114
|
+
}
|
|
115
|
+
return <span className="font-medium text-sm">{String(value)}</span>
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const renderAs = col.cellStyle ?? col.type
|
|
119
|
+
|
|
120
|
+
// -----------------------------------------------------------------------
|
|
121
|
+
// Badge / Status / Select / Option
|
|
122
|
+
// -----------------------------------------------------------------------
|
|
123
|
+
|
|
124
|
+
if (renderAs === 'badge' || renderAs === 'status' || renderAs === 'select' || renderAs === 'option') {
|
|
125
|
+
const sv = String(value)
|
|
126
|
+
const option = col.options?.find((o) => o.value === sv)
|
|
127
|
+
if (option) {
|
|
128
|
+
const colorSource = option.color || optionColor(option.value || option.label)
|
|
129
|
+
const styles = generateBadgeStyles(colorSource, { isDark })
|
|
130
|
+
return (
|
|
131
|
+
<Badge variant="outline" className="border-0" style={styles}>
|
|
132
|
+
{option.label}
|
|
133
|
+
</Badge>
|
|
134
|
+
)
|
|
135
|
+
}
|
|
136
|
+
if (renderAs === 'status') {
|
|
137
|
+
const styles = generateBadgeStyles(statusColorFor(sv), { isDark })
|
|
138
|
+
return (
|
|
139
|
+
<Badge variant="outline" className="border-0" style={styles}>
|
|
140
|
+
{humanizeToken(sv)}
|
|
141
|
+
</Badge>
|
|
142
|
+
)
|
|
143
|
+
}
|
|
144
|
+
return <Badge variant="outline">{humanizeToken(sv)}</Badge>
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// -----------------------------------------------------------------------
|
|
148
|
+
// Date / Datetime / Timestamp
|
|
149
|
+
// -----------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
if (['date', 'datetime', 'timestamp', 'timestamptz'].includes(renderAs ?? '')) {
|
|
152
|
+
const formatted = formatDateCell(value, renderAs, dateLocale, timeZone)
|
|
153
|
+
if (!formatted) return <span className="text-muted-foreground">—</span>
|
|
154
|
+
return (
|
|
155
|
+
<span
|
|
156
|
+
className="inline-flex items-center gap-1 text-sm text-muted-foreground"
|
|
157
|
+
title={formatted.title}
|
|
158
|
+
>
|
|
159
|
+
<icons.Calendar className="h-3 w-3 opacity-60" />
|
|
160
|
+
{formatted.display}
|
|
161
|
+
</span>
|
|
162
|
+
)
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// -----------------------------------------------------------------------
|
|
166
|
+
// Boolean
|
|
167
|
+
// -----------------------------------------------------------------------
|
|
168
|
+
|
|
169
|
+
if (renderAs === 'boolean') {
|
|
170
|
+
return (
|
|
171
|
+
<span className="inline-flex items-center gap-1">
|
|
172
|
+
{value ? (
|
|
173
|
+
<icons.Check className="h-3.5 w-3.5 text-green-500" />
|
|
174
|
+
) : (
|
|
175
|
+
<icons.Minus className="h-3.5 w-3.5 text-muted-foreground" />
|
|
176
|
+
)}
|
|
177
|
+
<span className="text-sm text-muted-foreground">{value ? 'Sí' : 'No'}</span>
|
|
178
|
+
</span>
|
|
179
|
+
)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// -----------------------------------------------------------------------
|
|
183
|
+
// Currency
|
|
184
|
+
// -----------------------------------------------------------------------
|
|
185
|
+
|
|
186
|
+
if (renderAs === 'currency') {
|
|
187
|
+
const num = typeof value === 'number' ? value : Number(value)
|
|
188
|
+
if (isNaN(num)) return <span className="text-muted-foreground">—</span>
|
|
189
|
+
const decimals = styleCfg(col, 'decimals') ?? 2
|
|
190
|
+
const curr = styleCfg(col, 'currency') || currency || 'USD'
|
|
191
|
+
return (
|
|
192
|
+
<span className="font-medium tabular-nums text-sm">
|
|
193
|
+
{formatNumber(num, { style: 'currency', currency: curr, minimumFractionDigits: decimals, maximumFractionDigits: decimals }, locale)}
|
|
194
|
+
</span>
|
|
195
|
+
)
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// -----------------------------------------------------------------------
|
|
199
|
+
// Number / Percent / Progress
|
|
200
|
+
// -----------------------------------------------------------------------
|
|
201
|
+
|
|
202
|
+
if (renderAs === 'number') {
|
|
203
|
+
const num = typeof value === 'number' ? value : Number(value)
|
|
204
|
+
if (isNaN(num)) return <span className="text-muted-foreground">—</span>
|
|
205
|
+
const decimals = styleCfg(col, 'decimals')
|
|
206
|
+
return (
|
|
207
|
+
<span className="font-medium tabular-nums text-sm">
|
|
208
|
+
{formatNumber(
|
|
209
|
+
num,
|
|
210
|
+
decimals !== undefined ? { minimumFractionDigits: decimals, maximumFractionDigits: decimals } : {},
|
|
211
|
+
locale,
|
|
212
|
+
)}
|
|
213
|
+
</span>
|
|
214
|
+
)
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
if (renderAs === 'percent' || renderAs === 'progress') {
|
|
218
|
+
const num = typeof value === 'number' ? value : Number(value)
|
|
219
|
+
if (isNaN(num)) return <span className="text-muted-foreground">—</span>
|
|
220
|
+
return (
|
|
221
|
+
<span className="font-medium tabular-nums text-sm text-muted-foreground">
|
|
222
|
+
{Math.round(Math.max(0, Math.min(100, num)))}%
|
|
223
|
+
</span>
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// -----------------------------------------------------------------------
|
|
228
|
+
// Tags
|
|
229
|
+
// -----------------------------------------------------------------------
|
|
230
|
+
|
|
231
|
+
if (renderAs === 'tags') {
|
|
232
|
+
const list: string[] = Array.isArray(value)
|
|
233
|
+
? value.map(String)
|
|
234
|
+
: String(value).split(',').map((s) => s.trim()).filter(Boolean)
|
|
235
|
+
if (list.length === 0) return <span className="text-muted-foreground">—</span>
|
|
236
|
+
return (
|
|
237
|
+
<span className="inline-flex flex-wrap gap-1">
|
|
238
|
+
{list.map((tag, i) => (
|
|
239
|
+
<Badge key={i} variant="secondary" className="px-1.5 py-0 text-[10px]">
|
|
240
|
+
{tag}
|
|
241
|
+
</Badge>
|
|
242
|
+
))}
|
|
243
|
+
</span>
|
|
244
|
+
)
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// -----------------------------------------------------------------------
|
|
248
|
+
// Color
|
|
249
|
+
// -----------------------------------------------------------------------
|
|
250
|
+
|
|
251
|
+
if (renderAs === 'color') {
|
|
252
|
+
const hex = String(value)
|
|
253
|
+
return (
|
|
254
|
+
<span className="inline-flex items-center gap-1.5">
|
|
255
|
+
<span className="h-3.5 w-3.5 rounded border border-border/60 shrink-0" style={{ background: hex }} />
|
|
256
|
+
<code className="font-mono text-xs text-muted-foreground">{hex}</code>
|
|
257
|
+
</span>
|
|
258
|
+
)
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// -----------------------------------------------------------------------
|
|
262
|
+
// URL / Link
|
|
263
|
+
// -----------------------------------------------------------------------
|
|
264
|
+
|
|
265
|
+
if (renderAs === 'url' || renderAs === 'link') {
|
|
266
|
+
const urlStr = String(value)
|
|
267
|
+
const href = /^https?:\/\//i.test(urlStr) ? urlStr : `https://${urlStr}`
|
|
268
|
+
let label: string
|
|
269
|
+
try { label = new URL(href).hostname } catch { label = urlStr }
|
|
270
|
+
return (
|
|
271
|
+
<a
|
|
272
|
+
href={href}
|
|
273
|
+
target="_blank"
|
|
274
|
+
rel="noopener noreferrer"
|
|
275
|
+
className="inline-flex items-center gap-1 text-sm text-primary hover:underline"
|
|
276
|
+
onClick={(e) => e.stopPropagation()}
|
|
277
|
+
>
|
|
278
|
+
<icons.ExternalLink className="h-3 w-3 shrink-0" />
|
|
279
|
+
<span className="truncate" style={{ maxWidth: 200 }}>{label}</span>
|
|
280
|
+
</a>
|
|
281
|
+
)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// -----------------------------------------------------------------------
|
|
285
|
+
// Email
|
|
286
|
+
// -----------------------------------------------------------------------
|
|
287
|
+
|
|
288
|
+
if (renderAs === 'email') {
|
|
289
|
+
const email = String(value)
|
|
290
|
+
return (
|
|
291
|
+
<a
|
|
292
|
+
href={`mailto:${email}`}
|
|
293
|
+
className="inline-flex items-center gap-1 text-sm text-primary hover:underline"
|
|
294
|
+
onClick={(e) => e.stopPropagation()}
|
|
295
|
+
>
|
|
296
|
+
<icons.Mail className="h-3 w-3 shrink-0" />
|
|
297
|
+
<span className="truncate" style={{ maxWidth: 200 }}>{email}</span>
|
|
298
|
+
</a>
|
|
299
|
+
)
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// -----------------------------------------------------------------------
|
|
303
|
+
// Relation chip (FK / reference)
|
|
304
|
+
// -----------------------------------------------------------------------
|
|
305
|
+
|
|
306
|
+
if (renderAs === 'relation' || renderAs === 'reference' || col.ref) {
|
|
307
|
+
const sv = String(value)
|
|
308
|
+
const chipStyles = relationChipStyles(sv, { isDark })
|
|
309
|
+
return (
|
|
310
|
+
<span
|
|
311
|
+
className="inline-flex items-center rounded-md px-2 py-0.5 text-sm font-medium"
|
|
312
|
+
style={{ ...chipStyles, maxWidth: 180 }}
|
|
313
|
+
title={sv}
|
|
314
|
+
>
|
|
315
|
+
<span className="truncate">{sv}</span>
|
|
316
|
+
</span>
|
|
317
|
+
)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
// -----------------------------------------------------------------------
|
|
321
|
+
// Creator / User / Avatar — these carry an object (resolved by the backend);
|
|
322
|
+
// in a diff snapshot the value is likely a string (name/email) or the object.
|
|
323
|
+
// -----------------------------------------------------------------------
|
|
324
|
+
|
|
325
|
+
if (renderAs === 'creator' || renderAs === 'user' || renderAs === 'avatar' || renderAs === 'search') {
|
|
326
|
+
const name =
|
|
327
|
+
typeof value === 'object' && value !== null
|
|
328
|
+
? String((value as any).name ?? (value as any).label ?? JSON.stringify(value))
|
|
329
|
+
: String(value)
|
|
330
|
+
return (
|
|
331
|
+
<span className="inline-flex items-center gap-1.5">
|
|
332
|
+
<Avatar className="h-5 w-5 rounded-full">
|
|
333
|
+
<AvatarImage src="" alt={name} />
|
|
334
|
+
<AvatarFallback className="text-[8px] font-bold bg-primary/10 text-primary">
|
|
335
|
+
{getInitials(name)}
|
|
336
|
+
</AvatarFallback>
|
|
337
|
+
</Avatar>
|
|
338
|
+
<span className="text-sm font-medium truncate" style={{ maxWidth: 180 }}>{name}</span>
|
|
339
|
+
</span>
|
|
340
|
+
)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// -----------------------------------------------------------------------
|
|
344
|
+
// Code / truncate-text / phone
|
|
345
|
+
// -----------------------------------------------------------------------
|
|
346
|
+
|
|
347
|
+
if (renderAs === 'code' || renderAs === 'truncate-text') {
|
|
348
|
+
const sv = String(value)
|
|
349
|
+
const maxLength = styleCfg(col, 'max_length', 'maxLength')
|
|
350
|
+
const display = maxLength && sv.length > maxLength ? `${sv.slice(0, maxLength)}…` : sv
|
|
351
|
+
return <code className="rounded bg-muted px-1.5 py-0.5 font-mono text-xs">{display}</code>
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// -----------------------------------------------------------------------
|
|
355
|
+
// Generic object fallback
|
|
356
|
+
// -----------------------------------------------------------------------
|
|
357
|
+
|
|
358
|
+
if (typeof value === 'object') {
|
|
359
|
+
return (
|
|
360
|
+
<span className="text-muted-foreground text-xs font-mono">
|
|
361
|
+
{JSON.stringify(value)}
|
|
362
|
+
</span>
|
|
363
|
+
)
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// -----------------------------------------------------------------------
|
|
367
|
+
// Plain text fallback
|
|
368
|
+
// -----------------------------------------------------------------------
|
|
369
|
+
|
|
370
|
+
return <span className="font-medium text-sm">{String(value)}</span>
|
|
371
|
+
}
|
package/src/dynamic-table.tsx
CHANGED
|
@@ -755,7 +755,7 @@ export function DynamicTable({
|
|
|
755
755
|
a 7-column table forces a wide horizontal scroll there, so we
|
|
756
756
|
render a card-per-row list instead (see MobileCards below). */}
|
|
757
757
|
<div className='hidden sm:block flex-1 min-h-0 overflow-auto border rounded-md bg-card'>
|
|
758
|
-
<Table noWrapper className=
|
|
758
|
+
<Table noWrapper className={cn('min-w-max w-full', aggregateColumns.length > 0 && Object.keys(footerTotals).length > 0 && 'h-full')}>
|
|
759
759
|
<TableHeader className='sticky top-0 z-10'>
|
|
760
760
|
{table.getHeaderGroups().map((headerGroup: HeaderGroup<any>) => (
|
|
761
761
|
<TableRow key={headerGroup.id} className='border-b-0 hover:bg-transparent'>
|
|
@@ -779,7 +779,8 @@ export function DynamicTable({
|
|
|
779
779
|
{loadingData && data.length === 0 ? (
|
|
780
780
|
<TableSkeleton />
|
|
781
781
|
) : table.getRowModel().rows?.length ? (
|
|
782
|
-
|
|
782
|
+
<>
|
|
783
|
+
{table.getRowModel().rows.map((row: Row<any>) => (
|
|
783
784
|
<TableRow key={row.id} data-state={row.getIsSelected() && 'selected'}>
|
|
784
785
|
{row.getVisibleCells().map((cell: Cell<any, unknown>) => {
|
|
785
786
|
const isActionsColumn = cell.column.id === 'actions'
|
|
@@ -794,7 +795,16 @@ export function DynamicTable({
|
|
|
794
795
|
)
|
|
795
796
|
})}
|
|
796
797
|
</TableRow>
|
|
797
|
-
|
|
798
|
+
))}
|
|
799
|
+
{/* Spacer row: absorbs the table's leftover height (table is
|
|
800
|
+
h-full when a footer shows) so the totals footer is pinned to
|
|
801
|
+
the bottom of the box even with only a few rows. */}
|
|
802
|
+
{aggregateColumns.length > 0 && Object.keys(footerTotals).length > 0 && (
|
|
803
|
+
<TableRow className='border-0 hover:bg-transparent'>
|
|
804
|
+
<TableCell colSpan={columns.length} className='h-full p-0' />
|
|
805
|
+
</TableRow>
|
|
806
|
+
)}
|
|
807
|
+
</>
|
|
798
808
|
) : (
|
|
799
809
|
<TableRow className='border-b-0 hover:bg-transparent'>
|
|
800
810
|
<TableCell colSpan={columns.length} className='h-full p-0'>
|
package/src/index.ts
CHANGED
|
@@ -132,3 +132,20 @@ export {
|
|
|
132
132
|
type OrgConfigBridge,
|
|
133
133
|
} from './use-org-config-bridge'
|
|
134
134
|
export { registerValidator } from './dynamic-form-schema'
|
|
135
|
+
export {
|
|
136
|
+
ActivityValueRenderer,
|
|
137
|
+
type ActivityValueRendererProps,
|
|
138
|
+
} from './activity-value-renderer'
|
|
139
|
+
export {
|
|
140
|
+
ActivityDiff,
|
|
141
|
+
type ActivityEvent,
|
|
142
|
+
type ActivityDiffProps,
|
|
143
|
+
} from './activity-diff'
|
|
144
|
+
export {
|
|
145
|
+
RecordHistory,
|
|
146
|
+
type RecordHistoryProps,
|
|
147
|
+
} from './record-history'
|
|
148
|
+
export {
|
|
149
|
+
ActivityTimeline,
|
|
150
|
+
type ActivityTimelineProps,
|
|
151
|
+
} from './activity-timeline'
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* record-history.tsx
|
|
3
|
+
*
|
|
4
|
+
* <RecordHistory> — chronological timeline of all ActivityEvents for a single
|
|
5
|
+
* record. Most recent event first. Each event is shown as a collapsible card
|
|
6
|
+
* with actor, relative date, and the <ActivityDiff> inline.
|
|
7
|
+
*
|
|
8
|
+
* Intended to be embedded in a record dialog tab ("Historial").
|
|
9
|
+
*
|
|
10
|
+
* Transport-agnostic: events and column metadata arrive via props. No fetching.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import * as React from 'react'
|
|
14
|
+
import { formatDistanceToNow } from 'date-fns'
|
|
15
|
+
import { es, enUS } from 'date-fns/locale'
|
|
16
|
+
import { ChevronDown, ChevronRight, User, Clock } from 'lucide-react'
|
|
17
|
+
import { cn } from '@asteby/metacore-ui/lib'
|
|
18
|
+
import {
|
|
19
|
+
Avatar,
|
|
20
|
+
AvatarFallback,
|
|
21
|
+
Badge,
|
|
22
|
+
Collapsible,
|
|
23
|
+
CollapsibleContent,
|
|
24
|
+
CollapsibleTrigger,
|
|
25
|
+
} from '@asteby/metacore-ui/primitives'
|
|
26
|
+
import { getInitials } from '@asteby/metacore-ui/lib'
|
|
27
|
+
import type { ColumnDefinition } from './types'
|
|
28
|
+
import type { ActivityEvent } from './activity-diff'
|
|
29
|
+
import { ActivityDiff } from './activity-diff'
|
|
30
|
+
|
|
31
|
+
// ---------------------------------------------------------------------------
|
|
32
|
+
// Types
|
|
33
|
+
// ---------------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
export interface RecordHistoryProps {
|
|
36
|
+
/**
|
|
37
|
+
* All activity events for the record, in any order. The component sorts
|
|
38
|
+
* them chronologically (most recent first).
|
|
39
|
+
*/
|
|
40
|
+
events: ActivityEvent[]
|
|
41
|
+
/**
|
|
42
|
+
* Column metadata for the record's model. Forwarded to <ActivityDiff> so
|
|
43
|
+
* field labels and display types are resolved correctly.
|
|
44
|
+
*/
|
|
45
|
+
columns?: ColumnDefinition[]
|
|
46
|
+
/** IANA timezone for datetime cells. */
|
|
47
|
+
timeZone?: string
|
|
48
|
+
/** ISO 4217 currency for money cells. */
|
|
49
|
+
currency?: string
|
|
50
|
+
/** BCP-47 locale. Defaults to 'es'. */
|
|
51
|
+
locale?: string
|
|
52
|
+
/** Class applied to the root element. */
|
|
53
|
+
className?: string
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// ---------------------------------------------------------------------------
|
|
57
|
+
// Helpers
|
|
58
|
+
// ---------------------------------------------------------------------------
|
|
59
|
+
|
|
60
|
+
const ACTION_LABELS: Record<string, string> = {
|
|
61
|
+
created: 'Creó el registro',
|
|
62
|
+
create: 'Creó el registro',
|
|
63
|
+
updated: 'Actualizó el registro',
|
|
64
|
+
update: 'Actualizó el registro',
|
|
65
|
+
deleted: 'Eliminó el registro',
|
|
66
|
+
delete: 'Eliminó el registro',
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function actionLabel(action: string): string {
|
|
70
|
+
return ACTION_LABELS[action.toLowerCase()] ?? action
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const ACTION_DOT_COLOR: Record<string, string> = {
|
|
74
|
+
created: '#22c55e',
|
|
75
|
+
create: '#22c55e',
|
|
76
|
+
updated: '#eab308',
|
|
77
|
+
update: '#eab308',
|
|
78
|
+
deleted: '#ef4444',
|
|
79
|
+
delete: '#ef4444',
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function actionDotColor(action: string): string {
|
|
83
|
+
return ACTION_DOT_COLOR[action.toLowerCase()] ?? '#6b7280'
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ---------------------------------------------------------------------------
|
|
87
|
+
// Component
|
|
88
|
+
// ---------------------------------------------------------------------------
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Shows the full activity history of a single record as a vertical timeline.
|
|
92
|
+
* Each event is collapsible — the header shows actor + time; expanding reveals
|
|
93
|
+
* the <ActivityDiff> with field-level changes.
|
|
94
|
+
*/
|
|
95
|
+
export const RecordHistory: React.FC<RecordHistoryProps> = ({
|
|
96
|
+
events,
|
|
97
|
+
columns,
|
|
98
|
+
timeZone,
|
|
99
|
+
currency,
|
|
100
|
+
locale = 'es',
|
|
101
|
+
className,
|
|
102
|
+
}) => {
|
|
103
|
+
const dateLocale = locale === 'en' ? enUS : es
|
|
104
|
+
|
|
105
|
+
// Sort: most recent first
|
|
106
|
+
const sorted = React.useMemo(
|
|
107
|
+
() => [...events].sort((a, b) => new Date(b.occurred_at).getTime() - new Date(a.occurred_at).getTime()),
|
|
108
|
+
[events],
|
|
109
|
+
)
|
|
110
|
+
|
|
111
|
+
// Expand the most-recent event by default
|
|
112
|
+
const [openIds, setOpenIds] = React.useState<Set<string>>(() =>
|
|
113
|
+
sorted.length > 0 ? new Set([sorted[0].id]) : new Set(),
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
const toggle = (id: string) => {
|
|
117
|
+
setOpenIds((prev) => {
|
|
118
|
+
const next = new Set(prev)
|
|
119
|
+
if (next.has(id)) next.delete(id)
|
|
120
|
+
else next.add(id)
|
|
121
|
+
return next
|
|
122
|
+
})
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (sorted.length === 0) {
|
|
126
|
+
return (
|
|
127
|
+
<div className={cn('flex flex-col items-center justify-center py-12 gap-2 text-muted-foreground', className)}>
|
|
128
|
+
<Clock className="h-8 w-8 opacity-40" />
|
|
129
|
+
<p className="text-sm">Sin historial de cambios.</p>
|
|
130
|
+
</div>
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return (
|
|
135
|
+
<div className={cn('relative pl-5', className)}>
|
|
136
|
+
{/* Vertical line */}
|
|
137
|
+
<span
|
|
138
|
+
className="absolute left-2 top-2 bottom-2 w-px bg-border"
|
|
139
|
+
aria-hidden="true"
|
|
140
|
+
/>
|
|
141
|
+
|
|
142
|
+
<div className="space-y-3">
|
|
143
|
+
{sorted.map((event) => {
|
|
144
|
+
const isOpen = openIds.has(event.id)
|
|
145
|
+
const dotColor = actionDotColor(event.action)
|
|
146
|
+
const actor = event.actor_label || event.actor_id || 'Sistema'
|
|
147
|
+
const timeAgo = (() => {
|
|
148
|
+
try {
|
|
149
|
+
return formatDistanceToNow(new Date(event.occurred_at), { addSuffix: true, locale: dateLocale })
|
|
150
|
+
} catch {
|
|
151
|
+
return event.occurred_at
|
|
152
|
+
}
|
|
153
|
+
})()
|
|
154
|
+
const fullDate = (() => {
|
|
155
|
+
try {
|
|
156
|
+
return new Date(event.occurred_at).toLocaleString(locale === 'en' ? 'en-US' : 'es-MX', {
|
|
157
|
+
...(timeZone ? { timeZone } : {}),
|
|
158
|
+
dateStyle: 'medium',
|
|
159
|
+
timeStyle: 'short',
|
|
160
|
+
})
|
|
161
|
+
} catch {
|
|
162
|
+
return event.occurred_at
|
|
163
|
+
}
|
|
164
|
+
})()
|
|
165
|
+
|
|
166
|
+
return (
|
|
167
|
+
<Collapsible key={event.id} open={isOpen} onOpenChange={() => toggle(event.id)}>
|
|
168
|
+
<div className="relative">
|
|
169
|
+
{/* Timeline dot */}
|
|
170
|
+
<span
|
|
171
|
+
className="absolute -left-5 top-3.5 h-2.5 w-2.5 rounded-full border-2 border-background -translate-x-[4px]"
|
|
172
|
+
style={{ background: dotColor }}
|
|
173
|
+
aria-hidden="true"
|
|
174
|
+
/>
|
|
175
|
+
|
|
176
|
+
<div className="rounded-lg border border-border/60 bg-card overflow-hidden">
|
|
177
|
+
{/* Event header — always visible */}
|
|
178
|
+
<CollapsibleTrigger asChild>
|
|
179
|
+
<button
|
|
180
|
+
type="button"
|
|
181
|
+
className="w-full flex items-center gap-3 px-4 py-3 text-left hover:bg-muted/30 transition-colors"
|
|
182
|
+
>
|
|
183
|
+
{/* Actor avatar */}
|
|
184
|
+
<Avatar className="h-7 w-7 rounded-full shrink-0">
|
|
185
|
+
<AvatarFallback className="text-[9px] font-bold bg-primary/10 text-primary">
|
|
186
|
+
{getInitials(actor)}
|
|
187
|
+
</AvatarFallback>
|
|
188
|
+
</Avatar>
|
|
189
|
+
|
|
190
|
+
<div className="flex-1 min-w-0">
|
|
191
|
+
<div className="flex items-center gap-2 flex-wrap">
|
|
192
|
+
<span className="text-sm font-semibold text-foreground truncate">
|
|
193
|
+
{actor}
|
|
194
|
+
</span>
|
|
195
|
+
<span className="text-sm text-muted-foreground">
|
|
196
|
+
{actionLabel(event.action)}
|
|
197
|
+
</span>
|
|
198
|
+
</div>
|
|
199
|
+
<div className="flex items-center gap-1.5 mt-0.5">
|
|
200
|
+
<Clock className="h-3 w-3 text-muted-foreground/60 shrink-0" />
|
|
201
|
+
<span className="text-xs text-muted-foreground" title={fullDate}>
|
|
202
|
+
{timeAgo}
|
|
203
|
+
</span>
|
|
204
|
+
{event.addon_key && (
|
|
205
|
+
<Badge variant="outline" className="text-[10px] px-1.5 py-0 h-4 ml-1">
|
|
206
|
+
{event.addon_key}
|
|
207
|
+
</Badge>
|
|
208
|
+
)}
|
|
209
|
+
</div>
|
|
210
|
+
</div>
|
|
211
|
+
|
|
212
|
+
{/* Expand/collapse chevron */}
|
|
213
|
+
<span className="shrink-0 text-muted-foreground">
|
|
214
|
+
{isOpen ? (
|
|
215
|
+
<ChevronDown className="h-4 w-4" />
|
|
216
|
+
) : (
|
|
217
|
+
<ChevronRight className="h-4 w-4" />
|
|
218
|
+
)}
|
|
219
|
+
</span>
|
|
220
|
+
</button>
|
|
221
|
+
</CollapsibleTrigger>
|
|
222
|
+
|
|
223
|
+
{/* Diff — revealed when expanded */}
|
|
224
|
+
<CollapsibleContent>
|
|
225
|
+
<div className="border-t border-border/40 px-4 py-3">
|
|
226
|
+
<ActivityDiff
|
|
227
|
+
event={event}
|
|
228
|
+
columns={columns}
|
|
229
|
+
timeZone={timeZone}
|
|
230
|
+
currency={currency}
|
|
231
|
+
locale={locale}
|
|
232
|
+
/>
|
|
233
|
+
</div>
|
|
234
|
+
</CollapsibleContent>
|
|
235
|
+
</div>
|
|
236
|
+
</div>
|
|
237
|
+
</Collapsible>
|
|
238
|
+
)
|
|
239
|
+
})}
|
|
240
|
+
</div>
|
|
241
|
+
</div>
|
|
242
|
+
)
|
|
243
|
+
}
|