@asteby/metacore-runtime-react 52.0.0 → 53.0.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 +26 -0
- package/dist/addon-loader.d.ts +22 -1
- package/dist/addon-loader.d.ts.map +1 -1
- package/dist/addon-loader.js +23 -5
- package/dist/addon-page-host.d.ts +80 -0
- package/dist/addon-page-host.d.ts.map +1 -0
- package/dist/addon-page-host.js +216 -0
- package/dist/addon-page-loader.d.ts +40 -0
- package/dist/addon-page-loader.d.ts.map +1 -0
- package/dist/addon-page-loader.js +87 -0
- package/dist/dynamic-view.d.ts +20 -1
- package/dist/dynamic-view.d.ts.map +1 -1
- package/dist/dynamic-view.js +26 -3
- package/dist/federated-error-boundary.d.ts +46 -0
- package/dist/federated-error-boundary.d.ts.map +1 -0
- package/dist/federated-error-boundary.js +55 -0
- package/dist/host-api.d.ts +22 -0
- package/dist/host-api.d.ts.map +1 -0
- package/dist/host-api.js +113 -0
- package/dist/index.d.ts +8 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +12 -2
- package/dist/testing.d.ts +42 -0
- package/dist/testing.d.ts.map +1 -0
- package/dist/testing.js +53 -0
- package/dist/types.d.ts +13 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/views/calendar-view.d.ts +23 -0
- package/dist/views/calendar-view.d.ts.map +1 -0
- package/dist/views/calendar-view.js +138 -0
- package/dist/views/chart-view.d.ts +14 -0
- package/dist/views/chart-view.d.ts.map +1 -0
- package/dist/views/chart-view.js +77 -0
- package/dist/views/data-source.d.ts +30 -0
- package/dist/views/data-source.d.ts.map +1 -0
- package/dist/views/data-source.js +103 -0
- package/dist/views/index.d.ts +4 -0
- package/dist/views/index.d.ts.map +1 -0
- package/dist/views/index.js +2 -0
- package/dist/views/map-view.d.ts +39 -0
- package/dist/views/map-view.d.ts.map +1 -0
- package/dist/views/map-view.js +143 -0
- package/dist/views/panels.d.ts +13 -0
- package/dist/views/panels.d.ts.map +1 -0
- package/dist/views/panels.js +119 -0
- package/dist/views/registry.d.ts +46 -0
- package/dist/views/registry.d.ts.map +1 -0
- package/dist/views/registry.js +139 -0
- package/dist/views/skeleton.d.ts +5 -0
- package/dist/views/skeleton.d.ts.map +1 -0
- package/dist/views/skeleton.js +6 -0
- package/dist/views/split-view.d.ts +6 -0
- package/dist/views/split-view.d.ts.map +1 -0
- package/dist/views/split-view.js +52 -0
- package/dist/views/timeline-view.d.ts +25 -0
- package/dist/views/timeline-view.d.ts.map +1 -0
- package/dist/views/timeline-view.js +130 -0
- package/dist/views/types.d.ts +87 -0
- package/dist/views/types.d.ts.map +1 -0
- package/dist/views/types.js +1 -0
- 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 +21 -5
- package/src/__tests__/addon-page-host.test.tsx +122 -0
- package/src/__tests__/addon-page-loader.test.tsx +201 -0
- package/src/__tests__/declarative-views.test.tsx +351 -0
- package/src/__tests__/host-api.test.ts +87 -0
- package/src/__tests__/index-static-graph.test.ts +83 -0
- package/src/addon-loader.tsx +51 -8
- package/src/addon-page-host.tsx +319 -0
- package/src/addon-page-loader.tsx +203 -0
- package/src/dynamic-view.tsx +61 -3
- package/src/federated-error-boundary.tsx +121 -0
- package/src/host-api.ts +143 -0
- package/src/index.ts +47 -4
- package/src/testing.tsx +108 -0
- package/src/types.ts +13 -0
- package/src/views/calendar-view.tsx +234 -0
- package/src/views/chart-view.tsx +122 -0
- package/src/views/data-source.ts +127 -0
- package/src/views/index.ts +37 -0
- package/src/views/map-view.tsx +217 -0
- package/src/views/panels.tsx +177 -0
- package/src/views/registry.tsx +202 -0
- package/src/views/skeleton.tsx +13 -0
- package/src/views/split-view.tsx +115 -0
- package/src/views/timeline-view.tsx +229 -0
- package/src/views/types.ts +105 -0
- 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,115 @@
|
|
|
1
|
+
// `view_type: split` — master-detail on one screen: the model's list (the
|
|
2
|
+
// same DynamicTable every model uses, restricted to `split.columns`) and the
|
|
3
|
+
// selected record's detail panels (`split.panels`, else the model's
|
|
4
|
+
// `detail.panels`). Stacks below 1280 px. Lazy chunk.
|
|
5
|
+
import * as React from 'react'
|
|
6
|
+
import { useTranslation } from 'react-i18next'
|
|
7
|
+
import { X } from 'lucide-react'
|
|
8
|
+
import { Button } from '@asteby/metacore-ui/primitives'
|
|
9
|
+
import { DynamicTable } from '../dynamic-table'
|
|
10
|
+
import type { ViewRendererProps } from './types'
|
|
11
|
+
import { DetailPanels } from './registry'
|
|
12
|
+
import { displayValue } from './data-source'
|
|
13
|
+
import { ViewSkeleton } from './skeleton'
|
|
14
|
+
|
|
15
|
+
/** Pure: which metadata columns to hide so the list shows only `columns`. */
|
|
16
|
+
export function hiddenColumnsFor(allColumns: string[], columns: string[] | undefined): string[] {
|
|
17
|
+
if (!columns || columns.length === 0) return []
|
|
18
|
+
const keep = new Set(columns)
|
|
19
|
+
return allColumns.filter((c) => !keep.has(c))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default function SplitViewRenderer(props: ViewRendererProps<'split'>) {
|
|
23
|
+
const { model, view, metadata, data, locale, currency, timeZone, className } = props
|
|
24
|
+
const { t } = useTranslation()
|
|
25
|
+
const [selected, setSelected] = React.useState<Record<string, unknown> | null>(null)
|
|
26
|
+
const [record, setRecord] = React.useState<Record<string, unknown> | null>(null)
|
|
27
|
+
const detailRef = React.useRef<HTMLElement>(null)
|
|
28
|
+
|
|
29
|
+
const selectedId = selected?.id !== undefined && selected?.id !== null ? String(selected.id) : null
|
|
30
|
+
React.useEffect(() => {
|
|
31
|
+
if (!selectedId) {
|
|
32
|
+
setRecord(null)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
let cancelled = false
|
|
36
|
+
setRecord(null)
|
|
37
|
+
data.record(model, selectedId).then(
|
|
38
|
+
(r) => !cancelled && setRecord(r ?? selected),
|
|
39
|
+
// The list row is still a usable record for the panels.
|
|
40
|
+
() => !cancelled && setRecord(selected),
|
|
41
|
+
)
|
|
42
|
+
return () => {
|
|
43
|
+
cancelled = true
|
|
44
|
+
}
|
|
45
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
46
|
+
}, [data, model, selectedId])
|
|
47
|
+
|
|
48
|
+
// Move focus to the detail for keyboard and screen-reader users.
|
|
49
|
+
React.useEffect(() => {
|
|
50
|
+
if (record) detailRef.current?.focus()
|
|
51
|
+
}, [record])
|
|
52
|
+
|
|
53
|
+
const hidden = React.useMemo(
|
|
54
|
+
() => hiddenColumnsFor((metadata?.columns ?? []).map((c) => c.key), view.columns),
|
|
55
|
+
[metadata, view.columns],
|
|
56
|
+
)
|
|
57
|
+
const panels = view.panels ?? metadata?.detail?.panels ?? []
|
|
58
|
+
const titleKey = view.columns?.[0]
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
<div className={['grid gap-4 xl:grid-cols-[minmax(0,1fr)_minmax(0,1.1fr)]', className].filter(Boolean).join(' ')} data-testid="split-view">
|
|
62
|
+
<div className="min-w-0">
|
|
63
|
+
<DynamicTable
|
|
64
|
+
model={model}
|
|
65
|
+
hiddenColumns={hidden}
|
|
66
|
+
onRowClick={(row: Record<string, unknown>) => setSelected(row)}
|
|
67
|
+
timeZone={timeZone}
|
|
68
|
+
currency={currency}
|
|
69
|
+
/>
|
|
70
|
+
</div>
|
|
71
|
+
<section
|
|
72
|
+
ref={detailRef}
|
|
73
|
+
tabIndex={-1}
|
|
74
|
+
aria-live="polite"
|
|
75
|
+
aria-label={t('metacore.views.detail', 'Detalle')}
|
|
76
|
+
className="min-w-0 rounded-lg border bg-background p-4 outline-none xl:sticky xl:top-4 xl:max-h-[calc(100dvh-8rem)] xl:overflow-auto"
|
|
77
|
+
>
|
|
78
|
+
{!selected ? (
|
|
79
|
+
<p className="py-16 text-center text-sm text-muted-foreground" data-testid="split-empty">
|
|
80
|
+
{t('metacore.views.select_record', 'Selecciona un registro para ver su detalle.')}
|
|
81
|
+
</p>
|
|
82
|
+
) : !record ? (
|
|
83
|
+
<ViewSkeleton height={240} />
|
|
84
|
+
) : (
|
|
85
|
+
<>
|
|
86
|
+
<header className="mb-4 flex items-start justify-between gap-2">
|
|
87
|
+
<h2 className="text-base font-semibold text-foreground">
|
|
88
|
+
{displayValue(titleKey ? record[titleKey] : record.id)}
|
|
89
|
+
</h2>
|
|
90
|
+
<Button
|
|
91
|
+
variant="ghost"
|
|
92
|
+
size="icon"
|
|
93
|
+
aria-label={t('metacore.views.close_detail', 'Cerrar detalle')}
|
|
94
|
+
onClick={() => setSelected(null)}
|
|
95
|
+
>
|
|
96
|
+
<X aria-hidden className="h-4 w-4" />
|
|
97
|
+
</Button>
|
|
98
|
+
</header>
|
|
99
|
+
<DetailPanels
|
|
100
|
+
model={model}
|
|
101
|
+
panels={panels}
|
|
102
|
+
record={record}
|
|
103
|
+
metadata={metadata}
|
|
104
|
+
data={data}
|
|
105
|
+
locale={locale}
|
|
106
|
+
currency={currency}
|
|
107
|
+
timeZone={timeZone}
|
|
108
|
+
translate={(k) => t(k)}
|
|
109
|
+
/>
|
|
110
|
+
</>
|
|
111
|
+
)}
|
|
112
|
+
</section>
|
|
113
|
+
</div>
|
|
114
|
+
)
|
|
115
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
// `view_type: timeline` (a model's rows over time) and `kind: timeline` detail
|
|
2
|
+
// panels (a record's child rows, or the items of a capability query such as
|
|
3
|
+
// `commerce.shipping.track`). Lazy chunk. Dates are formatted, never computed:
|
|
4
|
+
// ETAs and statuses are whatever the server returned.
|
|
5
|
+
import * as React from 'react'
|
|
6
|
+
import { useTranslation } from 'react-i18next'
|
|
7
|
+
import { Badge, Button } from '@asteby/metacore-ui/primitives'
|
|
8
|
+
import type { TimelineView } from '@asteby/metacore-sdk'
|
|
9
|
+
import { buildRelationFilterParams } from '../dynamic-relation-helpers'
|
|
10
|
+
import type { PanelRendererProps, ViewRendererProps } from './types'
|
|
11
|
+
import {
|
|
12
|
+
displayValue,
|
|
13
|
+
pickItems,
|
|
14
|
+
resolveCapabilityInput,
|
|
15
|
+
sortByDate,
|
|
16
|
+
toDate,
|
|
17
|
+
} from './data-source'
|
|
18
|
+
import { ViewSkeleton } from './skeleton'
|
|
19
|
+
|
|
20
|
+
const PAGE = 50
|
|
21
|
+
|
|
22
|
+
export interface TimelineEntry {
|
|
23
|
+
key: string
|
|
24
|
+
at: Date | null
|
|
25
|
+
title: string
|
|
26
|
+
status?: string
|
|
27
|
+
description?: string
|
|
28
|
+
group?: string
|
|
29
|
+
row: Record<string, unknown>
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Pure: rows → ordered entries (newest first), grouped when `group_by` is set. */
|
|
33
|
+
export function toTimelineEntries(rows: Record<string, unknown>[], view: TimelineView): TimelineEntry[] {
|
|
34
|
+
return sortByDate(rows, view.date_field).map((row, i) => ({
|
|
35
|
+
key: String(row.id ?? `${i}`),
|
|
36
|
+
at: toDate(row[view.date_field]),
|
|
37
|
+
title: displayValue(row[view.title_field]),
|
|
38
|
+
status: view.status_field ? displayValue(row[view.status_field]) || undefined : undefined,
|
|
39
|
+
description: view.description_field ? displayValue(row[view.description_field]) || undefined : undefined,
|
|
40
|
+
group: view.group_by ? displayValue(row[view.group_by]) || undefined : undefined,
|
|
41
|
+
row,
|
|
42
|
+
}))
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function useFormatter(locale?: string, timeZone?: string) {
|
|
46
|
+
return React.useMemo(() => {
|
|
47
|
+
try {
|
|
48
|
+
return new Intl.DateTimeFormat(locale, { dateStyle: 'medium', timeStyle: 'short', timeZone })
|
|
49
|
+
} catch {
|
|
50
|
+
return new Intl.DateTimeFormat(undefined, { dateStyle: 'medium', timeStyle: 'short' })
|
|
51
|
+
}
|
|
52
|
+
}, [locale, timeZone])
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function TimelineList({
|
|
56
|
+
entries,
|
|
57
|
+
locale,
|
|
58
|
+
timeZone,
|
|
59
|
+
onOpen,
|
|
60
|
+
}: {
|
|
61
|
+
entries: TimelineEntry[]
|
|
62
|
+
locale?: string
|
|
63
|
+
timeZone?: string
|
|
64
|
+
onOpen?: (row: Record<string, unknown>) => void
|
|
65
|
+
}) {
|
|
66
|
+
const { t } = useTranslation()
|
|
67
|
+
const fmt = useFormatter(locale, timeZone)
|
|
68
|
+
if (entries.length === 0) {
|
|
69
|
+
return (
|
|
70
|
+
<p className="py-8 text-center text-sm text-muted-foreground" data-testid="timeline-empty">
|
|
71
|
+
{t('metacore.views.empty', 'Sin datos todavía.')}
|
|
72
|
+
</p>
|
|
73
|
+
)
|
|
74
|
+
}
|
|
75
|
+
let lastGroup: string | undefined
|
|
76
|
+
return (
|
|
77
|
+
<ol className="relative space-y-4 border-l border-border pl-5" data-testid="timeline">
|
|
78
|
+
{entries.map((e) => {
|
|
79
|
+
const header = e.group !== undefined && e.group !== lastGroup ? e.group : undefined
|
|
80
|
+
lastGroup = e.group
|
|
81
|
+
const body = (
|
|
82
|
+
<>
|
|
83
|
+
<span
|
|
84
|
+
aria-hidden
|
|
85
|
+
className="absolute -left-[5px] mt-1.5 h-2.5 w-2.5 rounded-full border-2 border-background bg-primary"
|
|
86
|
+
/>
|
|
87
|
+
<div className="flex flex-wrap items-center gap-2">
|
|
88
|
+
<span className="text-sm font-medium text-foreground">{e.title}</span>
|
|
89
|
+
{e.status ? <Badge variant="outline">{e.status}</Badge> : null}
|
|
90
|
+
</div>
|
|
91
|
+
{e.at ? (
|
|
92
|
+
<time dateTime={e.at.toISOString()} className="block text-xs text-muted-foreground">
|
|
93
|
+
{fmt.format(e.at)}
|
|
94
|
+
</time>
|
|
95
|
+
) : null}
|
|
96
|
+
{e.description ? <p className="mt-1 text-sm text-muted-foreground">{e.description}</p> : null}
|
|
97
|
+
</>
|
|
98
|
+
)
|
|
99
|
+
return (
|
|
100
|
+
<React.Fragment key={e.key}>
|
|
101
|
+
{header ? (
|
|
102
|
+
<li className="-ml-5 pt-2 text-xs font-semibold uppercase tracking-wide text-muted-foreground">
|
|
103
|
+
{header}
|
|
104
|
+
</li>
|
|
105
|
+
) : null}
|
|
106
|
+
<li className="relative">
|
|
107
|
+
{onOpen ? (
|
|
108
|
+
<button
|
|
109
|
+
type="button"
|
|
110
|
+
className="w-full rounded-md text-left focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring"
|
|
111
|
+
onClick={() => onOpen(e.row)}
|
|
112
|
+
>
|
|
113
|
+
{body}
|
|
114
|
+
</button>
|
|
115
|
+
) : (
|
|
116
|
+
body
|
|
117
|
+
)}
|
|
118
|
+
</li>
|
|
119
|
+
</React.Fragment>
|
|
120
|
+
)
|
|
121
|
+
})}
|
|
122
|
+
</ol>
|
|
123
|
+
)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** `view_type: timeline` on a nav entry: the model's rows, newest first, paged. */
|
|
127
|
+
export default function TimelineViewRenderer(props: ViewRendererProps<'timeline'>) {
|
|
128
|
+
const { model, view, data, locale, timeZone, onOpenRecord, className } = props
|
|
129
|
+
const { t } = useTranslation()
|
|
130
|
+
const [rows, setRows] = React.useState<Record<string, unknown>[] | null>(null)
|
|
131
|
+
const [total, setTotal] = React.useState<number | undefined>()
|
|
132
|
+
const [page, setPage] = React.useState(1)
|
|
133
|
+
const [error, setError] = React.useState(false)
|
|
134
|
+
|
|
135
|
+
React.useEffect(() => {
|
|
136
|
+
let cancelled = false
|
|
137
|
+
setError(false)
|
|
138
|
+
data
|
|
139
|
+
.list(model, { page, per_page: PAGE, sortBy: view.date_field, order: 'desc' })
|
|
140
|
+
.then((res) => {
|
|
141
|
+
if (cancelled) return
|
|
142
|
+
setRows((prev) => (page === 1 || !prev ? res.rows : [...prev, ...res.rows]))
|
|
143
|
+
setTotal(res.total)
|
|
144
|
+
})
|
|
145
|
+
.catch(() => !cancelled && setError(true))
|
|
146
|
+
return () => {
|
|
147
|
+
cancelled = true
|
|
148
|
+
}
|
|
149
|
+
}, [data, model, view.date_field, page])
|
|
150
|
+
|
|
151
|
+
if (error && !rows) {
|
|
152
|
+
return (
|
|
153
|
+
<p role="alert" className="text-sm text-destructive">
|
|
154
|
+
{t('metacore.views.error', 'No se pudo cargar la información.')}
|
|
155
|
+
</p>
|
|
156
|
+
)
|
|
157
|
+
}
|
|
158
|
+
if (!rows) return <ViewSkeleton />
|
|
159
|
+
const entries = toTimelineEntries(rows, view)
|
|
160
|
+
const more = total !== undefined ? rows.length < total : false
|
|
161
|
+
return (
|
|
162
|
+
<div className={className}>
|
|
163
|
+
<TimelineList entries={entries} locale={locale} timeZone={timeZone} onOpen={onOpenRecord} />
|
|
164
|
+
{more ? (
|
|
165
|
+
<div className="mt-4 flex justify-center">
|
|
166
|
+
<Button variant="outline" size="sm" onClick={() => setPage((p) => p + 1)}>
|
|
167
|
+
{t('metacore.views.load_more', 'Cargar más')}
|
|
168
|
+
</Button>
|
|
169
|
+
</div>
|
|
170
|
+
) : null}
|
|
171
|
+
</div>
|
|
172
|
+
)
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** `kind: timeline` panel: child rows of a relation or a capability's items. */
|
|
176
|
+
export function TimelinePanel({ panel, record, metadata, data, locale, timeZone }: PanelRendererProps) {
|
|
177
|
+
const { t } = useTranslation()
|
|
178
|
+
const view = panel.timeline
|
|
179
|
+
const source = view?.source
|
|
180
|
+
const [rows, setRows] = React.useState<Record<string, unknown>[] | null>(null)
|
|
181
|
+
const [error, setError] = React.useState(false)
|
|
182
|
+
const recordId = record.id
|
|
183
|
+
const sig = JSON.stringify([source, recordId])
|
|
184
|
+
|
|
185
|
+
React.useEffect(() => {
|
|
186
|
+
if (!view || !source) return
|
|
187
|
+
let cancelled = false
|
|
188
|
+
setRows(null)
|
|
189
|
+
setError(false)
|
|
190
|
+
const run = async (): Promise<Record<string, unknown>[]> => {
|
|
191
|
+
if (source.capability) {
|
|
192
|
+
const out = await data.capability(source.capability, resolveCapabilityInput(source.input, record))
|
|
193
|
+
return pickItems(out, source.items)
|
|
194
|
+
}
|
|
195
|
+
if (source.relation) {
|
|
196
|
+
const rel = metadata?.relations?.find((r) => r.name === source.relation)
|
|
197
|
+
if (!rel || recordId === undefined || recordId === null) return []
|
|
198
|
+
const res = await data.list(rel.through, {
|
|
199
|
+
...buildRelationFilterParams(rel.foreign_key, String(recordId), rel.scope),
|
|
200
|
+
per_page: PAGE,
|
|
201
|
+
sortBy: view.date_field,
|
|
202
|
+
order: 'desc',
|
|
203
|
+
})
|
|
204
|
+
return res.rows
|
|
205
|
+
}
|
|
206
|
+
return []
|
|
207
|
+
}
|
|
208
|
+
run().then(
|
|
209
|
+
(r) => !cancelled && setRows(r),
|
|
210
|
+
() => !cancelled && setError(true),
|
|
211
|
+
)
|
|
212
|
+
return () => {
|
|
213
|
+
cancelled = true
|
|
214
|
+
}
|
|
215
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
216
|
+
}, [data, sig, metadata])
|
|
217
|
+
|
|
218
|
+
if (!view) return null
|
|
219
|
+
if (error) {
|
|
220
|
+
return (
|
|
221
|
+
<p role="alert" className="text-sm text-destructive">
|
|
222
|
+
{t('metacore.views.error', 'No se pudo cargar la información.')}
|
|
223
|
+
</p>
|
|
224
|
+
)
|
|
225
|
+
}
|
|
226
|
+
if (!rows) return <ViewSkeleton height={200} />
|
|
227
|
+
return <TimelineList entries={toTimelineEntries(rows, view)} locale={locale} timeZone={timeZone} />
|
|
228
|
+
}
|
|
229
|
+
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Declarative view contract (levels A/B of the UI spectrum): the kernel types
|
|
2
|
+
// (NavView, MapView, TimelineView, CalendarView, ChartView, SplitView,
|
|
3
|
+
// DetailPanel…) come generated from the kernel through @asteby/metacore-sdk;
|
|
4
|
+
// this file only adds the renderer-side props and the data source the host
|
|
5
|
+
// injects. Type-only: costs 0 bytes in the bundle.
|
|
6
|
+
import type { ComponentType } from 'react'
|
|
7
|
+
import type {
|
|
8
|
+
CalendarView,
|
|
9
|
+
ChartView,
|
|
10
|
+
DetailPanel,
|
|
11
|
+
MapView,
|
|
12
|
+
SplitView,
|
|
13
|
+
TimelineView,
|
|
14
|
+
WidgetQuery,
|
|
15
|
+
} from '@asteby/metacore-sdk'
|
|
16
|
+
import type { TableMetadata } from '../types'
|
|
17
|
+
import type { WidgetData } from '../dashboard-types'
|
|
18
|
+
|
|
19
|
+
/** `view_type` values rendered by a declarative renderer (table/kanban excluded). */
|
|
20
|
+
export type DeclarativeViewType = 'map' | 'timeline' | 'calendar' | 'chart' | 'split'
|
|
21
|
+
|
|
22
|
+
/** Block of `NavView` each type reads. */
|
|
23
|
+
export interface DeclarativeViewConfig {
|
|
24
|
+
map: MapView
|
|
25
|
+
timeline: TimelineView
|
|
26
|
+
calendar: CalendarView
|
|
27
|
+
chart: ChartView
|
|
28
|
+
split: SplitView
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** A page of rows. */
|
|
32
|
+
export interface ViewRows<Row = Record<string, unknown>> {
|
|
33
|
+
rows: Row[]
|
|
34
|
+
total?: number
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** List params in the kernel's list syntax (`f_<col>`, `sortBy`, `order`…). */
|
|
38
|
+
export type ViewListParams = Record<string, string | number | boolean | undefined>
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Where declarative renderers read data. The host provides it (default:
|
|
42
|
+
* {@link createApiViewDataSource} over the runtime-react ApiClient). Every
|
|
43
|
+
* call runs with the viewer's permissions server-side; renderers never
|
|
44
|
+
* compute business values (totals, ETAs) — they display what comes back.
|
|
45
|
+
*/
|
|
46
|
+
export interface ViewDataSource {
|
|
47
|
+
/** Rows of a model (`GET /data/<model>`). */
|
|
48
|
+
list(model: string, params?: ViewListParams): Promise<ViewRows>
|
|
49
|
+
/** One record (`GET /data/<model>/<id>`). */
|
|
50
|
+
record(model: string, id: string): Promise<Record<string, unknown> | null>
|
|
51
|
+
/**
|
|
52
|
+
* Aggregate for a chart view or panel. The host must only answer queries
|
|
53
|
+
* the installed manifests declare (same rule as dashboard widgets): the
|
|
54
|
+
* request names the model and the declared query, never free SQL.
|
|
55
|
+
*/
|
|
56
|
+
chart(req: ChartRequest): Promise<WidgetData>
|
|
57
|
+
/** Capability query (timeline panels with `source.capability`). */
|
|
58
|
+
capability(key: string, input: Record<string, unknown>): Promise<unknown>
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface ChartRequest {
|
|
62
|
+
/** Model whose nav entry / detail declares the chart. */
|
|
63
|
+
model: string
|
|
64
|
+
query: WidgetQuery
|
|
65
|
+
/** Detail panels: the relation the query is scoped to, and the record. */
|
|
66
|
+
relation?: string
|
|
67
|
+
recordId?: string
|
|
68
|
+
/** Detail panels: the panel key; nav views: undefined. */
|
|
69
|
+
panelKey?: string
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Props every declarative view renderer receives. */
|
|
73
|
+
export interface ViewRendererProps<T extends DeclarativeViewType = DeclarativeViewType> {
|
|
74
|
+
model: string
|
|
75
|
+
view: DeclarativeViewConfig[T]
|
|
76
|
+
/** Served table metadata (columns, relations, options…), when loaded. */
|
|
77
|
+
metadata?: TableMetadata | null
|
|
78
|
+
data: ViewDataSource
|
|
79
|
+
/** Open a record (host route to its detail page or drawer). */
|
|
80
|
+
onOpenRecord?: (row: Record<string, unknown>) => void
|
|
81
|
+
locale?: string
|
|
82
|
+
currency?: string
|
|
83
|
+
timeZone?: string
|
|
84
|
+
className?: string
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Props every detail panel renderer receives. */
|
|
88
|
+
export interface PanelRendererProps {
|
|
89
|
+
model: string
|
|
90
|
+
panel: DetailPanel
|
|
91
|
+
record: Record<string, unknown>
|
|
92
|
+
metadata?: TableMetadata | null
|
|
93
|
+
data: ViewDataSource
|
|
94
|
+
locale?: string
|
|
95
|
+
currency?: string
|
|
96
|
+
timeZone?: string
|
|
97
|
+
onOpenRecord?: (model: string, row: Record<string, unknown>) => void
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export type ViewRendererComponent = ComponentType<ViewRendererProps<any>>
|
|
101
|
+
export type PanelRendererComponent = ComponentType<PanelRendererProps>
|
|
102
|
+
|
|
103
|
+
/** Lazy module of a renderer (default export = the component). */
|
|
104
|
+
export type ViewRendererLoader = () => Promise<{ default: ViewRendererComponent }>
|
|
105
|
+
export type PanelRendererLoader = () => Promise<{ default: PanelRendererComponent }>
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
// recharts-backed dashboard widgets (bar, line, area, pie, donut). Kept out of
|
|
2
|
+
// the package index on purpose: recharts is ~100 KB gzip and must not ride in
|
|
3
|
+
// the host's first paint. `WidgetRenderer` loads this module with React.lazy;
|
|
4
|
+
// hosts that want the components directly import
|
|
5
|
+
// `@asteby/metacore-runtime-react/charts`. Colors come from theme CSS vars
|
|
6
|
+
// (dark-mode safe), curves are smooth, axes/legends compact, tooltips on.
|
|
7
|
+
|
|
8
|
+
import * as React from 'react'
|
|
9
|
+
import {
|
|
10
|
+
Area,
|
|
11
|
+
AreaChart,
|
|
12
|
+
Bar,
|
|
13
|
+
BarChart,
|
|
14
|
+
CartesianGrid,
|
|
15
|
+
Cell,
|
|
16
|
+
Line,
|
|
17
|
+
LineChart,
|
|
18
|
+
Pie,
|
|
19
|
+
PieChart,
|
|
20
|
+
ResponsiveContainer,
|
|
21
|
+
Tooltip,
|
|
22
|
+
XAxis,
|
|
23
|
+
YAxis,
|
|
24
|
+
} from 'recharts'
|
|
25
|
+
import { cn } from '@asteby/metacore-ui/lib'
|
|
26
|
+
import { WidgetCard, WidgetEmpty } from './widget-card'
|
|
27
|
+
import {
|
|
28
|
+
accentClasses,
|
|
29
|
+
paletteColor,
|
|
30
|
+
formatWidgetValue,
|
|
31
|
+
formatAxisTick,
|
|
32
|
+
CHART_GRID,
|
|
33
|
+
CHART_MUTED,
|
|
34
|
+
type WidgetFormatCtx,
|
|
35
|
+
} from './widget-format'
|
|
36
|
+
import { fmtCtx, hasSeries, type WidgetRenderProps } from './renderers'
|
|
37
|
+
|
|
38
|
+
export type { WidgetRenderProps }
|
|
39
|
+
|
|
40
|
+
// Fixed-height chart body. In the masonry (CSS columns) layout cards take their
|
|
41
|
+
// natural height, so charts need a definite height for ResponsiveContainer to
|
|
42
|
+
// resolve. This height + the card chrome gives every chart card a consistent,
|
|
43
|
+
// taller footprint that balances cleanly against compact stat cards.
|
|
44
|
+
const CHART_H = 'h-[190px]'
|
|
45
|
+
function ChartArea({ children }: { children: React.ReactElement }) {
|
|
46
|
+
return (
|
|
47
|
+
<div className={cn(CHART_H, 'w-full')}>
|
|
48
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
49
|
+
{children}
|
|
50
|
+
</ResponsiveContainer>
|
|
51
|
+
</div>
|
|
52
|
+
)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Compact recharts tooltip styled with theme tokens.
|
|
56
|
+
function ChartTooltip({ ctx }: { ctx: WidgetFormatCtx }) {
|
|
57
|
+
return (
|
|
58
|
+
<Tooltip
|
|
59
|
+
cursor={{ fill: 'var(--muted, rgba(148,163,184,0.12))', opacity: 0.4 }}
|
|
60
|
+
contentStyle={{
|
|
61
|
+
background: 'var(--popover, #fff)',
|
|
62
|
+
border: '1px solid var(--border, #e2e8f0)',
|
|
63
|
+
borderRadius: 8,
|
|
64
|
+
fontSize: 12,
|
|
65
|
+
color: 'var(--popover-foreground, #0f172a)',
|
|
66
|
+
boxShadow: '0 4px 16px rgba(0,0,0,0.08)',
|
|
67
|
+
}}
|
|
68
|
+
labelStyle={{ color: 'var(--muted-foreground, #64748b)', marginBottom: 2 }}
|
|
69
|
+
formatter={(v) => formatWidgetValue(Number(v), ctx)}
|
|
70
|
+
/>
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
// --- bar ------------------------------------------------------------------
|
|
74
|
+
export function BarWidget(p: WidgetRenderProps) {
|
|
75
|
+
const ctx = fmtCtx(p.spec, p.locale, p.currency)
|
|
76
|
+
const a = accentClasses(p.spec.accent)
|
|
77
|
+
return (
|
|
78
|
+
<WidgetCard
|
|
79
|
+
data-testid={`widget-${p.spec.key}`}
|
|
80
|
+
title={p.spec.title}
|
|
81
|
+
subtitle={p.spec.subtitle}
|
|
82
|
+
icon={p.spec.icon}
|
|
83
|
+
accent={p.spec.accent}
|
|
84
|
+
>
|
|
85
|
+
{hasSeries(p.data) ? (
|
|
86
|
+
<ChartArea>
|
|
87
|
+
<BarChart data={p.data.series} margin={{ top: 4, right: 4, left: -16, bottom: 0 }}>
|
|
88
|
+
<CartesianGrid vertical={false} stroke={CHART_GRID} strokeDasharray="3 3" />
|
|
89
|
+
<XAxis
|
|
90
|
+
dataKey="label"
|
|
91
|
+
tick={{ fontSize: 10, fill: CHART_MUTED }}
|
|
92
|
+
tickLine={false}
|
|
93
|
+
axisLine={false}
|
|
94
|
+
interval="preserveStartEnd"
|
|
95
|
+
/>
|
|
96
|
+
<YAxis
|
|
97
|
+
tick={{ fontSize: 10, fill: CHART_MUTED }}
|
|
98
|
+
tickLine={false}
|
|
99
|
+
axisLine={false}
|
|
100
|
+
width={44}
|
|
101
|
+
tickFormatter={(v: number) => formatAxisTick(v, ctx)}
|
|
102
|
+
/>
|
|
103
|
+
<ChartTooltip ctx={ctx} />
|
|
104
|
+
<Bar dataKey="value" fill={a.chartVar} radius={[4, 4, 0, 0]} maxBarSize={48} />
|
|
105
|
+
</BarChart>
|
|
106
|
+
</ChartArea>
|
|
107
|
+
) : (
|
|
108
|
+
<WidgetEmpty message={p.emptyText} />
|
|
109
|
+
)}
|
|
110
|
+
</WidgetCard>
|
|
111
|
+
)
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// --- line / area (shared) -------------------------------------------------
|
|
115
|
+
function TimeSeriesWidget(p: WidgetRenderProps & { variant: 'line' | 'area' }) {
|
|
116
|
+
const ctx = fmtCtx(p.spec, p.locale, p.currency)
|
|
117
|
+
const a = accentClasses(p.spec.accent)
|
|
118
|
+
const gradId = `wg-grad-${p.spec.key}`
|
|
119
|
+
return (
|
|
120
|
+
<WidgetCard
|
|
121
|
+
data-testid={`widget-${p.spec.key}`}
|
|
122
|
+
title={p.spec.title}
|
|
123
|
+
subtitle={p.spec.subtitle}
|
|
124
|
+
icon={p.spec.icon}
|
|
125
|
+
accent={p.spec.accent}
|
|
126
|
+
>
|
|
127
|
+
{hasSeries(p.data) ? (
|
|
128
|
+
<ChartArea>
|
|
129
|
+
{p.variant === 'area' ? (
|
|
130
|
+
<AreaChart data={p.data.series} margin={{ top: 4, right: 6, left: -16, bottom: 0 }}>
|
|
131
|
+
<defs>
|
|
132
|
+
<linearGradient id={gradId} x1="0" y1="0" x2="0" y2="1">
|
|
133
|
+
<stop offset="0%" stopColor={a.chartVar} stopOpacity={0.35} />
|
|
134
|
+
<stop offset="100%" stopColor={a.chartVar} stopOpacity={0.02} />
|
|
135
|
+
</linearGradient>
|
|
136
|
+
</defs>
|
|
137
|
+
<CartesianGrid vertical={false} stroke={CHART_GRID} strokeDasharray="3 3" />
|
|
138
|
+
<XAxis dataKey="label" tick={{ fontSize: 10, fill: CHART_MUTED }} tickLine={false} axisLine={false} interval="preserveStartEnd" />
|
|
139
|
+
<YAxis tick={{ fontSize: 10, fill: CHART_MUTED }} tickLine={false} axisLine={false} width={44} tickFormatter={(v: number) => formatAxisTick(v, ctx)} />
|
|
140
|
+
<ChartTooltip ctx={ctx} />
|
|
141
|
+
<Area type="monotone" dataKey="value" stroke={a.chartVar} strokeWidth={2} fill={`url(#${gradId})`} />
|
|
142
|
+
</AreaChart>
|
|
143
|
+
) : (
|
|
144
|
+
<LineChart data={p.data.series} margin={{ top: 4, right: 6, left: -16, bottom: 0 }}>
|
|
145
|
+
<CartesianGrid vertical={false} stroke={CHART_GRID} strokeDasharray="3 3" />
|
|
146
|
+
<XAxis dataKey="label" tick={{ fontSize: 10, fill: CHART_MUTED }} tickLine={false} axisLine={false} interval="preserveStartEnd" />
|
|
147
|
+
<YAxis tick={{ fontSize: 10, fill: CHART_MUTED }} tickLine={false} axisLine={false} width={44} tickFormatter={(v: number) => formatAxisTick(v, ctx)} />
|
|
148
|
+
<ChartTooltip ctx={ctx} />
|
|
149
|
+
<Line type="monotone" dataKey="value" stroke={a.chartVar} strokeWidth={2} dot={false} activeDot={{ r: 4 }} />
|
|
150
|
+
</LineChart>
|
|
151
|
+
)}
|
|
152
|
+
</ChartArea>
|
|
153
|
+
) : (
|
|
154
|
+
<WidgetEmpty message={p.emptyText} />
|
|
155
|
+
)}
|
|
156
|
+
</WidgetCard>
|
|
157
|
+
)
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function LineWidget(p: WidgetRenderProps) {
|
|
161
|
+
return <TimeSeriesWidget {...p} variant="line" />
|
|
162
|
+
}
|
|
163
|
+
export function AreaWidget(p: WidgetRenderProps) {
|
|
164
|
+
return <TimeSeriesWidget {...p} variant="area" />
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// --- pie / donut (shared) -------------------------------------------------
|
|
168
|
+
function CircularWidget(p: WidgetRenderProps & { variant: 'pie' | 'donut' }) {
|
|
169
|
+
const ctx = fmtCtx(p.spec, p.locale, p.currency)
|
|
170
|
+
return (
|
|
171
|
+
<WidgetCard
|
|
172
|
+
data-testid={`widget-${p.spec.key}`}
|
|
173
|
+
title={p.spec.title}
|
|
174
|
+
subtitle={p.spec.subtitle}
|
|
175
|
+
icon={p.spec.icon}
|
|
176
|
+
accent={p.spec.accent}
|
|
177
|
+
>
|
|
178
|
+
{hasSeries(p.data) ? (
|
|
179
|
+
<div className={cn(CHART_H, 'flex items-center gap-3')}>
|
|
180
|
+
<div className="h-full min-h-0 w-[46%] shrink-0">
|
|
181
|
+
<ResponsiveContainer width="100%" height="100%">
|
|
182
|
+
<PieChart>
|
|
183
|
+
<ChartTooltip ctx={ctx} />
|
|
184
|
+
<Pie
|
|
185
|
+
data={p.data.series}
|
|
186
|
+
dataKey="value"
|
|
187
|
+
nameKey="label"
|
|
188
|
+
innerRadius={p.variant === 'donut' ? '55%' : 0}
|
|
189
|
+
outerRadius="92%"
|
|
190
|
+
paddingAngle={p.variant === 'donut' ? 2 : 0}
|
|
191
|
+
stroke="var(--background, #fff)"
|
|
192
|
+
strokeWidth={2}
|
|
193
|
+
>
|
|
194
|
+
{p.data.series.map((_, i) => (
|
|
195
|
+
<Cell key={i} fill={paletteColor(i)} />
|
|
196
|
+
))}
|
|
197
|
+
</Pie>
|
|
198
|
+
</PieChart>
|
|
199
|
+
</ResponsiveContainer>
|
|
200
|
+
</div>
|
|
201
|
+
<ul className="flex min-w-0 flex-1 flex-col gap-1.5">
|
|
202
|
+
{p.data.series.slice(0, 6).map((pt, i) => (
|
|
203
|
+
<li key={pt.key} className="flex items-center gap-2 text-xs">
|
|
204
|
+
<span className="size-2.5 shrink-0 rounded-sm" style={{ background: paletteColor(i) }} />
|
|
205
|
+
<span className="truncate text-muted-foreground">{pt.label}</span>
|
|
206
|
+
<span className="ml-auto shrink-0 font-medium tabular-nums text-foreground">
|
|
207
|
+
{formatWidgetValue(pt.value, ctx)}
|
|
208
|
+
</span>
|
|
209
|
+
</li>
|
|
210
|
+
))}
|
|
211
|
+
</ul>
|
|
212
|
+
</div>
|
|
213
|
+
) : (
|
|
214
|
+
<WidgetEmpty message={p.emptyText} />
|
|
215
|
+
)}
|
|
216
|
+
</WidgetCard>
|
|
217
|
+
)
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
export function PieWidget(p: WidgetRenderProps) {
|
|
221
|
+
return <CircularWidget {...p} variant="pie" />
|
|
222
|
+
}
|
|
223
|
+
export function DonutWidget(p: WidgetRenderProps) {
|
|
224
|
+
return <CircularWidget {...p} variant="donut" />
|
|
225
|
+
}
|