@asteby/metacore-runtime-react 18.10.2 → 18.12.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.
@@ -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
+ }