@asteby/metacore-runtime-react 51.0.0 → 52.1.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 +20 -0
- package/dist/activity-diff.d.ts +5 -0
- package/dist/activity-diff.d.ts.map +1 -1
- package/dist/activity-timeline.d.ts.map +1 -1
- package/dist/activity-timeline.js +7 -2
- package/dist/audit-info.d.ts.map +1 -1
- package/dist/audit-info.js +14 -1
- package/dist/barcode-scanner.js +4 -4
- package/dist/dialogs/dynamic-record.d.ts +3 -1
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +11 -1
- package/dist/dynamic-columns.d.ts.map +1 -1
- package/dist/dynamic-columns.js +11 -0
- package/dist/dynamic-date-field.js +1 -1
- package/dist/dynamic-form.js +2 -2
- package/dist/index.d.ts +3 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/org-runtime-context.d.ts +27 -0
- package/dist/org-runtime-context.d.ts.map +1 -1
- package/dist/org-runtime-context.js +6 -0
- package/dist/org-runtime-provider.d.ts +13 -2
- package/dist/org-runtime-provider.d.ts.map +1 -1
- package/dist/org-runtime-provider.js +3 -3
- package/dist/provenance.d.ts +216 -0
- package/dist/provenance.d.ts.map +1 -0
- package/dist/provenance.js +386 -0
- package/dist/record-history.d.ts.map +1 -1
- package/dist/record-history.js +3 -1
- package/dist/sidebar-layout-manager.js +2 -2
- package/dist/widgets/charts.d.ts +9 -0
- package/dist/widgets/charts.d.ts.map +1 -0
- package/dist/widgets/charts.js +55 -0
- package/dist/widgets/lazy-charts.d.ts +10 -0
- package/dist/widgets/lazy-charts.d.ts.map +1 -0
- package/dist/widgets/lazy-charts.js +30 -0
- package/dist/widgets/renderers.d.ts +5 -5
- package/dist/widgets/renderers.d.ts.map +1 -1
- package/dist/widgets/renderers.js +3 -53
- package/dist/widgets/widget-renderer.d.ts.map +1 -1
- package/dist/widgets/widget-renderer.js +2 -1
- package/package.json +8 -4
- package/src/__tests__/index-static-graph.test.ts +65 -0
- package/src/__tests__/provenance.test.tsx +209 -0
- package/src/activity-diff.tsx +5 -0
- package/src/activity-timeline.tsx +13 -1
- package/src/audit-info.tsx +17 -1
- package/src/barcode-scanner.tsx +4 -4
- package/src/dialogs/dynamic-record.tsx +18 -0
- package/src/dynamic-columns.tsx +14 -0
- package/src/dynamic-date-field.tsx +1 -1
- package/src/dynamic-form.tsx +2 -2
- package/src/index.ts +10 -4
- package/src/org-runtime-context.ts +36 -0
- package/src/org-runtime-provider.tsx +22 -1
- package/src/provenance.tsx +585 -0
- package/src/record-history.tsx +14 -3
- package/src/sidebar-layout-manager.tsx +2 -2
- package/src/widgets/charts.tsx +225 -0
- package/src/widgets/lazy-charts.tsx +51 -0
- package/src/widgets/renderers.tsx +5 -210
- package/src/widgets/widget-renderer.tsx +6 -4
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* provenance.tsx — who made a record, and how.
|
|
3
|
+
*
|
|
4
|
+
* The kernel's `provenance` package stores, per record and per change, the
|
|
5
|
+
* actor kind (human | agent | system | integration), the person the agent
|
|
6
|
+
* acted for, who authorized an agent proposal and when, the channel (`via`),
|
|
7
|
+
* the origin and the mode (manual | assisted | automated). Hosts attach the
|
|
8
|
+
* creation provenance to list rows at `created_by.provenance` (or
|
|
9
|
+
* `row.provenance`); this module turns it into one of four presentations:
|
|
10
|
+
*
|
|
11
|
+
* human — a person on their own: their avatar and name.
|
|
12
|
+
* agent — the app's agent (OrgRuntimeProvider `agentActor`, e.g. Gobi)
|
|
13
|
+
* with the origin; "pending proposal" when it waits for a person.
|
|
14
|
+
* system — platform work or an integration: system icon + origin.
|
|
15
|
+
* assisted — "Ana with Gobi": the person's avatar with the agent's
|
|
16
|
+
* superimposed, and an accessible tooltip with who authorized,
|
|
17
|
+
* what the agent proposed and when.
|
|
18
|
+
*
|
|
19
|
+
* Rows without stored provenance keep the legacy rules: a null/system
|
|
20
|
+
* `created_by_id` is the system (origin from OrgRuntimeProvider
|
|
21
|
+
* `provenanceOrigin`), anything else is the person in `created_by`.
|
|
22
|
+
*/
|
|
23
|
+
import * as React from 'react'
|
|
24
|
+
import { useTranslation } from 'react-i18next'
|
|
25
|
+
import { Bot, Plug, Server } from 'lucide-react'
|
|
26
|
+
import { Avatar, AvatarFallback, AvatarImage, Tooltip, TooltipContent, TooltipTrigger } from '@asteby/metacore-ui'
|
|
27
|
+
import { cn, getInitials } from '@asteby/metacore-ui/lib'
|
|
28
|
+
import { useImageUrl } from './image-url-context'
|
|
29
|
+
import { isNilUuid } from './nil-uuid'
|
|
30
|
+
import { useAgentActor, useProvenanceOrigin, useSystemActor, useTimeZone } from './org-runtime-context'
|
|
31
|
+
|
|
32
|
+
/** The kernel's fixed actor for unattended work. */
|
|
33
|
+
const KERNEL_SYSTEM_ACTOR_ID = '00000000-0000-0000-0000-000000000001'
|
|
34
|
+
|
|
35
|
+
export type ProvenanceActorKind = 'human' | 'agent' | 'system' | 'integration'
|
|
36
|
+
export type ProvenanceMode = 'manual' | 'assisted' | 'automated'
|
|
37
|
+
|
|
38
|
+
/** Wire shape of a stored change's provenance (kernel `Entry.Presentation`). */
|
|
39
|
+
export interface ProvenanceData {
|
|
40
|
+
actor_kind: ProvenanceActorKind
|
|
41
|
+
mode?: ProvenanceMode
|
|
42
|
+
actor_id?: string
|
|
43
|
+
actor_name?: string
|
|
44
|
+
actor_person_name?: string
|
|
45
|
+
actor_person_avatar?: string
|
|
46
|
+
on_behalf_of?: string
|
|
47
|
+
on_behalf_of_name?: string
|
|
48
|
+
on_behalf_of_avatar?: string
|
|
49
|
+
authorized_by?: string
|
|
50
|
+
authorized_by_name?: string
|
|
51
|
+
authorized_by_avatar?: string
|
|
52
|
+
authorized_at?: string
|
|
53
|
+
via?: string
|
|
54
|
+
origin?: string
|
|
55
|
+
ref_kind?: string
|
|
56
|
+
ref_id?: string
|
|
57
|
+
session_id?: string
|
|
58
|
+
summary?: string
|
|
59
|
+
proposed?: boolean
|
|
60
|
+
/** When the change happened. */
|
|
61
|
+
at?: string
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** The four presentations. */
|
|
65
|
+
export type AuthorshipKind = 'human' | 'agent' | 'system' | 'assisted'
|
|
66
|
+
|
|
67
|
+
const KINDS = new Set(['human', 'agent', 'system', 'integration'])
|
|
68
|
+
|
|
69
|
+
function str(v: unknown): string | undefined {
|
|
70
|
+
return typeof v === 'string' && v.trim() !== '' ? v : undefined
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Defensive reader: an object or JSON string with a known `actor_kind`. */
|
|
74
|
+
export function parseProvenance(raw: unknown): ProvenanceData | undefined {
|
|
75
|
+
let v = raw
|
|
76
|
+
if (typeof v === 'string') {
|
|
77
|
+
try {
|
|
78
|
+
v = JSON.parse(v)
|
|
79
|
+
} catch {
|
|
80
|
+
return undefined
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!v || typeof v !== 'object' || Array.isArray(v)) return undefined
|
|
84
|
+
const o = v as Record<string, unknown>
|
|
85
|
+
if (typeof o.actor_kind !== 'string' || !KINDS.has(o.actor_kind)) return undefined
|
|
86
|
+
return o as unknown as ProvenanceData
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** Reads a row's creation provenance (`created_by.provenance` or `provenance`). */
|
|
90
|
+
export function readProvenance(row: Record<string, unknown> | null | undefined): ProvenanceData | undefined {
|
|
91
|
+
if (!row) return undefined
|
|
92
|
+
const cb = row.created_by
|
|
93
|
+
if (cb && typeof cb === 'object') {
|
|
94
|
+
const p = parseProvenance((cb as Record<string, unknown>).provenance)
|
|
95
|
+
if (p) return p
|
|
96
|
+
}
|
|
97
|
+
return parseProvenance(row.provenance)
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** Which of the four presentations a provenance gets. */
|
|
101
|
+
export function classifyProvenance(p: ProvenanceData): AuthorshipKind {
|
|
102
|
+
if (p.actor_kind === 'human') return 'human'
|
|
103
|
+
if (p.mode === 'assisted' || (p.authorized_by && !p.proposed)) return 'assisted'
|
|
104
|
+
if (p.actor_kind === 'agent') return 'agent'
|
|
105
|
+
return 'system'
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** True when the row's creator is the system (null / nil / kernel actor). */
|
|
109
|
+
export function isSystemCreated(row: Record<string, unknown> | null | undefined): boolean {
|
|
110
|
+
if (!row) return false
|
|
111
|
+
const cb = row.created_by
|
|
112
|
+
if (cb && typeof cb === 'object') {
|
|
113
|
+
const o = cb as { system?: unknown; name?: unknown; email?: unknown }
|
|
114
|
+
if (o.system === true) return true
|
|
115
|
+
if (str(o.name) || str(o.email)) return false
|
|
116
|
+
}
|
|
117
|
+
if ('created_by_id' in row) {
|
|
118
|
+
const id = row.created_by_id
|
|
119
|
+
if (id === null || id === undefined || id === '') return true
|
|
120
|
+
const s = String(id).trim().toLowerCase()
|
|
121
|
+
return s === '<nil>' || isNilUuid(s) || s === KERNEL_SYSTEM_ACTOR_ID
|
|
122
|
+
}
|
|
123
|
+
return false
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ---- copy -------------------------------------------------------------------
|
|
127
|
+
|
|
128
|
+
const COPY = {
|
|
129
|
+
es: {
|
|
130
|
+
system: 'Sistema',
|
|
131
|
+
agent: 'Asistente',
|
|
132
|
+
user: 'Usuario',
|
|
133
|
+
with: '{{person}} con {{agent}}',
|
|
134
|
+
proposedBy: 'Propuesto por {{agent}}',
|
|
135
|
+
pending: 'Propuesta pendiente',
|
|
136
|
+
authorizedBy: 'Autorizó: {{person}}',
|
|
137
|
+
onBehalfOf: 'En nombre de {{person}}',
|
|
138
|
+
proposal: '{{agent}} propuso: {{summary}}',
|
|
139
|
+
createdBy: 'Creado por {{who}}',
|
|
140
|
+
'via.chat': 'Chat',
|
|
141
|
+
'via.automation': 'Automatización',
|
|
142
|
+
'via.import': 'Importación',
|
|
143
|
+
'via.webhook': 'Webhook',
|
|
144
|
+
'via.seed': 'Datos iniciales',
|
|
145
|
+
'via.migration': 'Migración',
|
|
146
|
+
'via.job': 'Proceso programado',
|
|
147
|
+
'via.event': 'Evento',
|
|
148
|
+
'via.ui': 'Manual',
|
|
149
|
+
'via.api': 'API',
|
|
150
|
+
'mode.manual': 'Manual',
|
|
151
|
+
'mode.assisted': 'Con asistencia',
|
|
152
|
+
'mode.automated': 'Automático',
|
|
153
|
+
},
|
|
154
|
+
en: {
|
|
155
|
+
system: 'System',
|
|
156
|
+
agent: 'Assistant',
|
|
157
|
+
user: 'User',
|
|
158
|
+
with: '{{person}} with {{agent}}',
|
|
159
|
+
proposedBy: 'Proposed by {{agent}}',
|
|
160
|
+
pending: 'Pending proposal',
|
|
161
|
+
authorizedBy: 'Authorized by: {{person}}',
|
|
162
|
+
onBehalfOf: 'On behalf of {{person}}',
|
|
163
|
+
proposal: '{{agent}} proposed: {{summary}}',
|
|
164
|
+
createdBy: 'Created by {{who}}',
|
|
165
|
+
'via.chat': 'Chat',
|
|
166
|
+
'via.automation': 'Automation',
|
|
167
|
+
'via.import': 'Import',
|
|
168
|
+
'via.webhook': 'Webhook',
|
|
169
|
+
'via.seed': 'Initial data',
|
|
170
|
+
'via.migration': 'Migration',
|
|
171
|
+
'via.job': 'Scheduled job',
|
|
172
|
+
'via.event': 'Event',
|
|
173
|
+
'via.ui': 'Manual',
|
|
174
|
+
'via.api': 'API',
|
|
175
|
+
'mode.manual': 'Manual',
|
|
176
|
+
'mode.assisted': 'Assisted',
|
|
177
|
+
'mode.automated': 'Automated',
|
|
178
|
+
},
|
|
179
|
+
} as const
|
|
180
|
+
|
|
181
|
+
export type ProvenanceCopyKey = keyof (typeof COPY)['es']
|
|
182
|
+
|
|
183
|
+
/** Copy lookup: `provenance.<key>` from the host i18n, else the built-in es/en. */
|
|
184
|
+
export function useProvenanceCopy() {
|
|
185
|
+
const { t, i18n } = useTranslation()
|
|
186
|
+
const lang = (i18n?.language || 'es').toLowerCase().startsWith('en') ? 'en' : 'es'
|
|
187
|
+
const copy = COPY[lang]
|
|
188
|
+
const tt = React.useCallback(
|
|
189
|
+
(key: ProvenanceCopyKey, vars?: Record<string, string>) => {
|
|
190
|
+
const fallback = copy[key].replace(/\{\{(\w+)\}\}/g, (_, k: string) => vars?.[k] ?? '')
|
|
191
|
+
const out = t(`provenance.${key}`, { defaultValue: fallback, ...vars }) as string
|
|
192
|
+
// i18n mocks/backends that ignore defaultValue echo the key back.
|
|
193
|
+
return !out || out === `provenance.${key}` ? fallback : out
|
|
194
|
+
},
|
|
195
|
+
[t, copy],
|
|
196
|
+
)
|
|
197
|
+
return { tt, lang }
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Person shown for a provenance (assisted / human), from the row or the data. */
|
|
201
|
+
export interface ProvenancePerson {
|
|
202
|
+
name?: string
|
|
203
|
+
avatar?: string
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
export interface ResolvedAuthorship {
|
|
207
|
+
kind: AuthorshipKind
|
|
208
|
+
/** Main line ("Ana Pérez con Gobi", "Gobi", "Sistema", "gobiernos-intel"). */
|
|
209
|
+
label: string
|
|
210
|
+
/** Secondary line (origin / channel / pending proposal). */
|
|
211
|
+
subtitle?: string
|
|
212
|
+
/** Full sentence for tooltip and screen readers. */
|
|
213
|
+
description: string
|
|
214
|
+
person?: ProvenancePerson
|
|
215
|
+
agentName: string
|
|
216
|
+
agentAvatar?: string
|
|
217
|
+
systemAvatar?: string
|
|
218
|
+
integration: boolean
|
|
219
|
+
proposed: boolean
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
function formatWhen(iso: string | undefined, lang: string, timeZone?: string): string | undefined {
|
|
223
|
+
if (!iso) return undefined
|
|
224
|
+
const d = new Date(iso)
|
|
225
|
+
if (Number.isNaN(d.getTime())) return undefined
|
|
226
|
+
try {
|
|
227
|
+
return d.toLocaleString(lang === 'en' ? 'en-US' : 'es-MX', {
|
|
228
|
+
dateStyle: 'medium',
|
|
229
|
+
timeStyle: 'short',
|
|
230
|
+
...(timeZone ? { timeZone } : {}),
|
|
231
|
+
})
|
|
232
|
+
} catch {
|
|
233
|
+
return d.toISOString()
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** Pure-ish resolver (hooks for copy and presentation contexts). */
|
|
238
|
+
export function useResolvedAuthorship(input: {
|
|
239
|
+
provenance?: ProvenanceData
|
|
240
|
+
person?: ProvenancePerson
|
|
241
|
+
/** Origin for system rows without provenance (legacy). */
|
|
242
|
+
fallbackOrigin?: string
|
|
243
|
+
/** Force the system presentation (legacy null created_by). */
|
|
244
|
+
system?: boolean
|
|
245
|
+
}): ResolvedAuthorship | null {
|
|
246
|
+
const { tt, lang } = useProvenanceCopy()
|
|
247
|
+
const agent = useAgentActor()
|
|
248
|
+
const sys = useSystemActor()
|
|
249
|
+
const tz = useTimeZone()
|
|
250
|
+
const { provenance: p, person, fallbackOrigin, system } = input
|
|
251
|
+
const viaLabel = (via?: string) => {
|
|
252
|
+
if (!via) return undefined
|
|
253
|
+
const key = `via.${via}` as ProvenanceCopyKey
|
|
254
|
+
return key in COPY.es ? tt(key) : via
|
|
255
|
+
}
|
|
256
|
+
if (!p) {
|
|
257
|
+
if (!system) return null
|
|
258
|
+
const name = sys?.name || tt('system')
|
|
259
|
+
return {
|
|
260
|
+
kind: 'system', label: name, subtitle: fallbackOrigin,
|
|
261
|
+
description: [tt('createdBy', { who: name }), fallbackOrigin].filter(Boolean).join(' · '),
|
|
262
|
+
agentName: agent?.name || tt('agent'), systemAvatar: sys?.avatarUrl, integration: false, proposed: false,
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
const kind = classifyProvenance(p)
|
|
266
|
+
const agentName = agent?.name || str(p.actor_name) || tt('agent')
|
|
267
|
+
const when = formatWhen(p.authorized_at || p.at, lang, tz)
|
|
268
|
+
const origin = str(p.origin)
|
|
269
|
+
if (kind === 'human') {
|
|
270
|
+
const name = str(person?.name) || str(p.actor_person_name) || tt('user')
|
|
271
|
+
return {
|
|
272
|
+
kind, label: name, subtitle: origin,
|
|
273
|
+
description: [tt('createdBy', { who: name }), viaLabel(p.via), when].filter(Boolean).join(' · '),
|
|
274
|
+
person: { name, avatar: person?.avatar || p.actor_person_avatar },
|
|
275
|
+
agentName, integration: false, proposed: false,
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
if (kind === 'assisted') {
|
|
279
|
+
const name = str(p.authorized_by_name) || str(person?.name) || str(p.on_behalf_of_name) || tt('user')
|
|
280
|
+
const avatar = p.authorized_by_avatar || person?.avatar || p.on_behalf_of_avatar
|
|
281
|
+
const parts = [tt('authorizedBy', { person: name })]
|
|
282
|
+
parts.push(p.summary ? tt('proposal', { agent: agentName, summary: p.summary }) : tt('proposedBy', { agent: agentName }))
|
|
283
|
+
if (origin) parts.push(origin)
|
|
284
|
+
if (when) parts.push(when)
|
|
285
|
+
return {
|
|
286
|
+
kind, label: tt('with', { person: name, agent: agentName }),
|
|
287
|
+
subtitle: origin || tt('proposedBy', { agent: agentName }),
|
|
288
|
+
description: parts.join(' · '),
|
|
289
|
+
person: { name, avatar }, agentName, agentAvatar: agent?.avatarUrl, integration: false, proposed: false,
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
if (kind === 'agent') {
|
|
293
|
+
const proposed = !!p.proposed
|
|
294
|
+
const sub = proposed ? tt('pending') : origin || viaLabel(p.via)
|
|
295
|
+
const parts = [tt('createdBy', { who: agentName })]
|
|
296
|
+
if (proposed) parts.push(tt('pending'))
|
|
297
|
+
if (origin) parts.push(origin)
|
|
298
|
+
else if (p.via) parts.push(viaLabel(p.via) as string)
|
|
299
|
+
if (p.on_behalf_of_name) parts.push(tt('onBehalfOf', { person: p.on_behalf_of_name }))
|
|
300
|
+
if (p.summary) parts.push(p.summary)
|
|
301
|
+
if (when) parts.push(when)
|
|
302
|
+
return {
|
|
303
|
+
kind, label: agentName, subtitle: sub, description: parts.join(' · '),
|
|
304
|
+
agentName, agentAvatar: agent?.avatarUrl, integration: false, proposed,
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
const integration = p.actor_kind === 'integration'
|
|
308
|
+
const name = integration ? str(p.actor_name) || tt('system') : sys?.name || tt('system')
|
|
309
|
+
const sub = origin || viaLabel(p.via)
|
|
310
|
+
return {
|
|
311
|
+
kind: 'system', label: name, subtitle: sub,
|
|
312
|
+
description: [tt('createdBy', { who: name }), sub, when].filter(Boolean).join(' · '),
|
|
313
|
+
agentName, systemAvatar: integration ? undefined : sys?.avatarUrl, integration, proposed: false,
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// ---- visuals ----------------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
type Size = 'sm' | 'md'
|
|
320
|
+
const PX: Record<Size, number> = { sm: 20, md: 32 }
|
|
321
|
+
|
|
322
|
+
function PersonAvatar({ name, avatar, size }: { name: string; avatar?: string; size: Size }) {
|
|
323
|
+
const getImageUrl = useImageUrl()
|
|
324
|
+
const px = PX[size]
|
|
325
|
+
return (
|
|
326
|
+
<Avatar className="rounded-lg ring-1 ring-border/50" style={{ width: px, height: px }}>
|
|
327
|
+
<AvatarImage src={avatar ? getImageUrl(avatar) : ''} alt="" className="object-cover" />
|
|
328
|
+
<AvatarFallback className="rounded-lg bg-primary/5 text-[10px] font-bold text-primary">
|
|
329
|
+
{getInitials(name)}
|
|
330
|
+
</AvatarFallback>
|
|
331
|
+
</Avatar>
|
|
332
|
+
)
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
function AgentAvatar({ name, url, px, round }: { name: string; url?: string; px: number; round?: boolean }) {
|
|
336
|
+
const radius = round ? 'rounded-full' : 'rounded-lg'
|
|
337
|
+
if (url) {
|
|
338
|
+
return (
|
|
339
|
+
<img
|
|
340
|
+
src={url}
|
|
341
|
+
alt=""
|
|
342
|
+
aria-hidden="true"
|
|
343
|
+
draggable={false}
|
|
344
|
+
className={cn('shrink-0 select-none bg-primary/5 object-contain', radius)}
|
|
345
|
+
style={{ width: px, height: px }}
|
|
346
|
+
/>
|
|
347
|
+
)
|
|
348
|
+
}
|
|
349
|
+
return (
|
|
350
|
+
<span
|
|
351
|
+
aria-hidden="true"
|
|
352
|
+
data-agent-initials={getInitials(name)}
|
|
353
|
+
className={cn('flex shrink-0 items-center justify-center bg-primary/10 text-primary', radius)}
|
|
354
|
+
style={{ width: px, height: px }}
|
|
355
|
+
>
|
|
356
|
+
<Bot style={{ width: px * 0.6, height: px * 0.6 }} />
|
|
357
|
+
</span>
|
|
358
|
+
)
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
function SystemAvatar({ url, integration, px }: { url?: string; integration: boolean; px: number }) {
|
|
362
|
+
if (url) {
|
|
363
|
+
return <img src={url} alt="" aria-hidden="true" className="shrink-0 rounded-lg object-contain" style={{ width: px, height: px }} />
|
|
364
|
+
}
|
|
365
|
+
const Icon = integration ? Plug : Server
|
|
366
|
+
return (
|
|
367
|
+
<span
|
|
368
|
+
aria-hidden="true"
|
|
369
|
+
className="flex shrink-0 items-center justify-center rounded-lg bg-muted text-muted-foreground ring-1 ring-border/50"
|
|
370
|
+
style={{ width: px, height: px }}
|
|
371
|
+
>
|
|
372
|
+
<Icon style={{ width: px * 0.55, height: px * 0.55 }} />
|
|
373
|
+
</span>
|
|
374
|
+
)
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/** Avatar for a resolved authorship (person with the agent superimposed when assisted). */
|
|
378
|
+
export function ProvenanceAvatar({ authorship: a, size = 'md' }: { authorship: ResolvedAuthorship; size?: Size }) {
|
|
379
|
+
const px = PX[size]
|
|
380
|
+
if (a.kind === 'agent') return <AgentAvatar name={a.agentName} url={a.agentAvatar} px={px} />
|
|
381
|
+
if (a.kind === 'system') return <SystemAvatar url={a.systemAvatar} integration={a.integration} px={px} />
|
|
382
|
+
const person = <PersonAvatar name={a.person?.name || a.label} avatar={a.person?.avatar} size={size} />
|
|
383
|
+
if (a.kind === 'human') return person
|
|
384
|
+
const mini = Math.max(12, Math.round(px * 0.5))
|
|
385
|
+
return (
|
|
386
|
+
<span className="relative inline-flex shrink-0" style={{ width: px, height: px }} data-provenance-overlay="">
|
|
387
|
+
{person}
|
|
388
|
+
<span className="absolute -bottom-1 -right-1 rounded-full bg-background p-px ring-1 ring-border" style={{ lineHeight: 0 }}>
|
|
389
|
+
<AgentAvatar name={a.agentName} url={a.agentAvatar} px={mini} round />
|
|
390
|
+
</span>
|
|
391
|
+
</span>
|
|
392
|
+
)
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
export interface ProvenanceBadgeProps {
|
|
396
|
+
/** Stored provenance (kernel wire shape). */
|
|
397
|
+
provenance?: ProvenanceData
|
|
398
|
+
/** Person from the row (`created_by` sibling) when the data has no names. */
|
|
399
|
+
person?: ProvenancePerson
|
|
400
|
+
/** Legacy system row without provenance. */
|
|
401
|
+
system?: boolean
|
|
402
|
+
/** Origin for `system` rows without provenance. */
|
|
403
|
+
fallbackOrigin?: string
|
|
404
|
+
/** `cell`: two lines with a 32px avatar (tables). `inline`: one line (detail, history, notifications). */
|
|
405
|
+
variant?: 'cell' | 'inline'
|
|
406
|
+
className?: string
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Who made a record and how — the four presentations (see module doc) with a
|
|
411
|
+
* keyboard-focusable trigger whose tooltip and screen-reader description say
|
|
412
|
+
* who authorized, what the agent proposed and when.
|
|
413
|
+
*/
|
|
414
|
+
export function ProvenanceBadge({ provenance, person, system, fallbackOrigin, variant = 'cell', className }: ProvenanceBadgeProps) {
|
|
415
|
+
const a = useResolvedAuthorship({ provenance, person, system, fallbackOrigin })
|
|
416
|
+
const descId = React.useId()
|
|
417
|
+
if (!a) return null
|
|
418
|
+
const inline = variant === 'inline'
|
|
419
|
+
const body = inline ? (
|
|
420
|
+
<span className="inline-flex min-w-0 max-w-full items-center gap-2 overflow-hidden">
|
|
421
|
+
<ProvenanceAvatar authorship={a} size="sm" />
|
|
422
|
+
<span className="shrink-0 truncate font-medium">{a.label}</span>
|
|
423
|
+
{a.subtitle ? <span className="min-w-0 truncate text-muted-foreground">· {a.subtitle}</span> : null}
|
|
424
|
+
</span>
|
|
425
|
+
) : (
|
|
426
|
+
<span className="flex min-w-0 items-center gap-3">
|
|
427
|
+
<ProvenanceAvatar authorship={a} size="md" />
|
|
428
|
+
<span className="flex min-w-0 flex-col overflow-hidden">
|
|
429
|
+
<span className="mb-0.5 truncate text-sm font-medium leading-none text-foreground/90">{a.label}</span>
|
|
430
|
+
{a.subtitle ? (
|
|
431
|
+
<span className={cn('truncate text-[11px] leading-none', a.proposed ? 'text-amber-700 dark:text-amber-400' : 'text-muted-foreground')}>
|
|
432
|
+
{a.subtitle}
|
|
433
|
+
</span>
|
|
434
|
+
) : null}
|
|
435
|
+
</span>
|
|
436
|
+
</span>
|
|
437
|
+
)
|
|
438
|
+
return (
|
|
439
|
+
<Tooltip>
|
|
440
|
+
<TooltipTrigger asChild>
|
|
441
|
+
<span
|
|
442
|
+
tabIndex={0}
|
|
443
|
+
role="group"
|
|
444
|
+
aria-label={a.label}
|
|
445
|
+
aria-describedby={descId}
|
|
446
|
+
data-provenance={a.kind}
|
|
447
|
+
className={cn(
|
|
448
|
+
'min-w-0 rounded-md outline-none focus-visible:ring-2 focus-visible:ring-ring',
|
|
449
|
+
inline ? 'inline-flex max-w-full text-sm' : 'flex max-w-full',
|
|
450
|
+
className,
|
|
451
|
+
)}
|
|
452
|
+
>
|
|
453
|
+
{body}
|
|
454
|
+
<span id={descId} className="sr-only">
|
|
455
|
+
{a.description}
|
|
456
|
+
</span>
|
|
457
|
+
</span>
|
|
458
|
+
</TooltipTrigger>
|
|
459
|
+
<TooltipContent className="max-w-xs">{a.description}</TooltipContent>
|
|
460
|
+
</Tooltip>
|
|
461
|
+
)
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function personFromRow(row: Record<string, unknown> | null | undefined): ProvenancePerson | undefined {
|
|
465
|
+
const cb = row?.created_by
|
|
466
|
+
if (cb && typeof cb === 'object') {
|
|
467
|
+
const o = cb as Record<string, unknown>
|
|
468
|
+
const name = str(o.name) || str(o.label) || str(o.email)
|
|
469
|
+
const avatar = str(o.avatar) || str(o.image) || str(o.photo)
|
|
470
|
+
if (name || avatar) return { name, avatar }
|
|
471
|
+
}
|
|
472
|
+
if (typeof cb === 'string' && cb.trim()) return { name: cb }
|
|
473
|
+
return undefined
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/** True when the row has something AuthorshipCell renders differently from a plain person. */
|
|
477
|
+
export function hasSpecialAuthorship(row: Record<string, unknown> | null | undefined): boolean {
|
|
478
|
+
const p = readProvenance(row)
|
|
479
|
+
if (p) return classifyProvenance(p) !== 'human'
|
|
480
|
+
return isSystemCreated(row)
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
export interface AuthorshipCellProps {
|
|
484
|
+
/** The record row (reads `created_by`, `created_by_id`, provenance). */
|
|
485
|
+
row: Record<string, unknown> | null | undefined
|
|
486
|
+
/** Model key, passed to the host origin resolver for legacy system rows. */
|
|
487
|
+
model?: string
|
|
488
|
+
variant?: 'cell' | 'inline'
|
|
489
|
+
className?: string
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
/**
|
|
493
|
+
* "Created by" for any record: the stored provenance when the host attached
|
|
494
|
+
* it, else the legacy rule (system when `created_by_id` is empty or the kernel
|
|
495
|
+
* actor, the `created_by` person otherwise). Renders nothing for a row with no
|
|
496
|
+
* creator information at all.
|
|
497
|
+
*/
|
|
498
|
+
export function AuthorshipCell({ row, model, variant = 'cell', className }: AuthorshipCellProps) {
|
|
499
|
+
const originFor = useProvenanceOrigin()
|
|
500
|
+
const provenance = readProvenance(row)
|
|
501
|
+
const person = personFromRow(row)
|
|
502
|
+
const system = !provenance && isSystemCreated(row)
|
|
503
|
+
if (!provenance && !system && !person) return null
|
|
504
|
+
if (!provenance && !system) {
|
|
505
|
+
return (
|
|
506
|
+
<ProvenanceBadge
|
|
507
|
+
provenance={{ actor_kind: 'human', mode: 'manual' }}
|
|
508
|
+
person={person}
|
|
509
|
+
variant={variant}
|
|
510
|
+
className={className}
|
|
511
|
+
/>
|
|
512
|
+
)
|
|
513
|
+
}
|
|
514
|
+
return (
|
|
515
|
+
<ProvenanceBadge
|
|
516
|
+
provenance={provenance}
|
|
517
|
+
person={person}
|
|
518
|
+
system={system}
|
|
519
|
+
fallbackOrigin={system ? originFor?.(model, row) : undefined}
|
|
520
|
+
variant={variant}
|
|
521
|
+
className={className}
|
|
522
|
+
/>
|
|
523
|
+
)
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
/**
|
|
527
|
+
* Avatar + name for an activity/history event that carries provenance (no
|
|
528
|
+
* tooltip trigger: it lives inside the event's toggle button). The full
|
|
529
|
+
* sentence goes to `title` and to screen readers.
|
|
530
|
+
*/
|
|
531
|
+
export function ProvenanceActorChip({
|
|
532
|
+
provenance,
|
|
533
|
+
person,
|
|
534
|
+
className,
|
|
535
|
+
}: {
|
|
536
|
+
provenance: ProvenanceData
|
|
537
|
+
person?: ProvenancePerson
|
|
538
|
+
className?: string
|
|
539
|
+
}) {
|
|
540
|
+
const a = useResolvedAuthorship({ provenance, person })
|
|
541
|
+
if (!a) return null
|
|
542
|
+
return (
|
|
543
|
+
<span className={cn('inline-flex min-w-0 items-center gap-2', className)} title={a.description} data-provenance={a.kind}>
|
|
544
|
+
<ProvenanceAvatar authorship={a} size="sm" />
|
|
545
|
+
<span className="truncate text-sm font-semibold text-foreground">{a.label}</span>
|
|
546
|
+
<span className="sr-only">({a.description})</span>
|
|
547
|
+
</span>
|
|
548
|
+
)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/** Provenance carried by a notification (metadata.provenance), if any. */
|
|
552
|
+
export function notificationProvenance(item: { metadata?: unknown } | null | undefined): ProvenanceData | undefined {
|
|
553
|
+
let meta = item?.metadata
|
|
554
|
+
if (typeof meta === 'string') {
|
|
555
|
+
try {
|
|
556
|
+
meta = JSON.parse(meta)
|
|
557
|
+
} catch {
|
|
558
|
+
return undefined
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
if (!meta || typeof meta !== 'object') return undefined
|
|
562
|
+
return parseProvenance((meta as Record<string, unknown>).provenance)
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Avatar for a notification list (`renderAvatar` of the notifications
|
|
567
|
+
* dropdown): the provenance avatar when the notification carries one, else
|
|
568
|
+
* `fallback`.
|
|
569
|
+
*/
|
|
570
|
+
export function NotificationProvenanceAvatar({
|
|
571
|
+
item,
|
|
572
|
+
fallback = null,
|
|
573
|
+
}: {
|
|
574
|
+
item: { metadata?: unknown } | null | undefined
|
|
575
|
+
fallback?: React.ReactNode
|
|
576
|
+
}) {
|
|
577
|
+
const p = notificationProvenance(item)
|
|
578
|
+
const a = useResolvedAuthorship({ provenance: p })
|
|
579
|
+
if (!a) return <>{fallback}</>
|
|
580
|
+
return (
|
|
581
|
+
<span title={a.description} aria-label={a.description} role="img" className="inline-flex">
|
|
582
|
+
<ProvenanceAvatar authorship={a} size="md" />
|
|
583
|
+
</span>
|
|
584
|
+
)
|
|
585
|
+
}
|
package/src/record-history.tsx
CHANGED
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
|
|
13
13
|
import * as React from 'react'
|
|
14
14
|
import { useSystemActor } from './org-runtime-context'
|
|
15
|
+
import { parseProvenance, ProvenanceActorChip } from './provenance'
|
|
15
16
|
import { formatDistanceToNow } from 'date-fns'
|
|
16
17
|
import { es, enUS } from 'date-fns/locale'
|
|
17
18
|
import { ChevronDown, ChevronRight, Clock, ExternalLink } from 'lucide-react'
|
|
@@ -167,6 +168,7 @@ export const RecordHistory: React.FC<RecordHistoryProps> = ({
|
|
|
167
168
|
const isOpen = openIds.has(event.id)
|
|
168
169
|
const dotColor = actionDotColor(event.action)
|
|
169
170
|
const actor = event.actor_label || event.actor_id || systemName
|
|
171
|
+
const prov = parseProvenance(event.provenance)
|
|
170
172
|
const timeAgo = (() => {
|
|
171
173
|
try {
|
|
172
174
|
return formatDistanceToNow(new Date(event.occurred_at), { addSuffix: true, locale: dateLocale })
|
|
@@ -205,6 +207,7 @@ export const RecordHistory: React.FC<RecordHistoryProps> = ({
|
|
|
205
207
|
>
|
|
206
208
|
{/* Actor avatar — the photo when the event carries one,
|
|
207
209
|
initials otherwise */}
|
|
210
|
+
{prov ? null : (
|
|
208
211
|
<Avatar className="h-7 w-7 rounded-full shrink-0">
|
|
209
212
|
{event.actor_avatar ? (
|
|
210
213
|
<AvatarImage
|
|
@@ -216,12 +219,20 @@ export const RecordHistory: React.FC<RecordHistoryProps> = ({
|
|
|
216
219
|
{getInitials(actor)}
|
|
217
220
|
</AvatarFallback>
|
|
218
221
|
</Avatar>
|
|
222
|
+
)}
|
|
219
223
|
|
|
220
224
|
<div className="flex-1 min-w-0">
|
|
221
225
|
<div className="flex items-center gap-2 flex-wrap">
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
226
|
+
{prov ? (
|
|
227
|
+
<ProvenanceActorChip
|
|
228
|
+
provenance={prov}
|
|
229
|
+
person={{ name: event.actor_label ?? undefined, avatar: event.actor_avatar ?? undefined }}
|
|
230
|
+
/>
|
|
231
|
+
) : (
|
|
232
|
+
<span className="text-sm font-semibold text-foreground truncate">
|
|
233
|
+
{actor}
|
|
234
|
+
</span>
|
|
235
|
+
)}
|
|
225
236
|
<span className="text-sm text-muted-foreground">
|
|
226
237
|
{actionLabel(event.action)}
|
|
227
238
|
</span>
|
|
@@ -395,7 +395,7 @@ export function SidebarLayoutManager({
|
|
|
395
395
|
currentGroups,
|
|
396
396
|
resolveLabel = (s) => s,
|
|
397
397
|
title = 'Menú lateral',
|
|
398
|
-
description = '
|
|
398
|
+
description = 'Arrastra, renombra y agrupa como en Discord. Así lo verá toda la organización.',
|
|
399
399
|
}: SidebarLayoutManagerProps) {
|
|
400
400
|
const [tree, setTree] = React.useState<SidebarLayoutNode[]>([])
|
|
401
401
|
const [baseline, setBaseline] = React.useState('[]')
|
|
@@ -728,7 +728,7 @@ export function SidebarLayoutManager({
|
|
|
728
728
|
>
|
|
729
729
|
{tree.length === 0 ? (
|
|
730
730
|
<p className="text-muted-foreground px-3 py-8 text-center text-sm">
|
|
731
|
-
Menú vacío.
|
|
731
|
+
Menú vacío. Crea una sección o recarga el menú actual.
|
|
732
732
|
</p>
|
|
733
733
|
) : (
|
|
734
734
|
renderList(tree, [])
|