@asteby/metacore-runtime-react 18.2.0 → 18.3.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 +6 -0
- package/dist/dialogs/dynamic-record.d.ts +2 -2
- package/dist/dialogs/dynamic-record.d.ts.map +1 -1
- package/dist/dialogs/dynamic-record.js +1 -2
- package/dist/dynamic-form.d.ts.map +1 -1
- package/dist/dynamic-form.js +29 -3
- package/dist/dynamic-relation-helpers.d.ts.map +1 -1
- package/dist/dynamic-relation-helpers.js +18 -0
- package/dist/dynamic-relation.d.ts.map +1 -1
- package/dist/dynamic-relation.js +14 -0
- package/dist/dynamic-select-field.d.ts +8 -1
- package/dist/dynamic-select-field.d.ts.map +1 -1
- package/dist/dynamic-select-field.js +2 -1
- package/dist/image-url-context.d.ts +13 -0
- package/dist/image-url-context.d.ts.map +1 -0
- package/dist/image-url-context.js +17 -0
- package/package.json +1 -1
- package/src/__tests__/dynamic-relation.test.ts +17 -0
- package/src/dialogs/dynamic-record.tsx +4 -4
- package/src/dynamic-form.tsx +33 -2
- package/src/dynamic-relation-helpers.ts +18 -0
- package/src/dynamic-relation.tsx +19 -0
- package/src/dynamic-select-field.tsx +9 -1
- package/src/image-url-context.tsx +23 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,11 @@
|
|
|
1
1
|
# @asteby/metacore-runtime-react
|
|
2
2
|
|
|
3
|
+
## 18.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- fc14b4f: Relation (one_to_many) cells now show the FK product image: when an FK column's backend-resolved sibling carries an `image`, the relation row renders a thumbnail + label instead of plain text. The nested line-item edit form drops server-managed/audit columns (`id`, `created_at`, `updated_at`, `deleted_at`, `created_by(_id)`, `updated_by(_id)`) so they no longer render as `[object Object]` inputs, and the nested `dynamic_select` is seeded with the existing value's label/image from the initial record so the trigger shows the name + thumbnail instead of a raw UUID. The image-url resolver context moved to its own `image-url-context` module to avoid a circular import.
|
|
8
|
+
|
|
3
9
|
## 18.2.0
|
|
4
10
|
|
|
5
11
|
### Minor Changes
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ModelSchema } from './types';
|
|
2
|
-
|
|
3
|
-
export type GetImageUrl
|
|
2
|
+
import { type GetImageUrl } from '../image-url-context';
|
|
3
|
+
export type { GetImageUrl };
|
|
4
4
|
export interface FieldOption {
|
|
5
5
|
value: string;
|
|
6
6
|
label: string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-record.d.ts","sourceRoot":"","sources":["../../src/dialogs/dynamic-record.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"dynamic-record.d.ts","sourceRoot":"","sources":["../../src/dialogs/dynamic-record.tsx"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AA6C1C,OAAO,EAAqC,KAAK,WAAW,EAAE,MAAM,sBAAsB,CAAA;AAI1F,YAAY,EAAE,WAAW,EAAE,CAAA;AAE3B,MAAM,WAAW,WAAW;IACxB,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;IACb;;;;;OAKG;IACH,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,KAAK,CAAC,EAAE,MAAM,CAAA;CACjB;AAED,MAAM,WAAW,QAAQ;IACrB,GAAG,EAAE,MAAM,CAAA;IACX,KAAK,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,GAAG,UAAU,GAAG,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,MAAM,GAAG,OAAO,GAAG,KAAK,GAAG,SAAS,GAAG,OAAO,GAAG,MAAM,CAAA;IACpH,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,OAAO,CAAC,EAAE,WA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,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC9B;;;OAGG;IACH,MAAM,CAAC,EAAE,WAAW,CAAA;IACpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,CAAA;IAC9B;;;OAGG;IACH,MAAM,CAAC,EAAE,MAAM,IAAI,CAAA;IACnB;;;;OAIG;IACH,cAAc,CAAC,EAAE,MAAM,IAAI,CAAA;IAC3B;;;;;OAKG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI,CAAA;IAC1C;;;;OAIG;IACH,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB;;;;OAIG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AA6HD,wBAAgB,mBAAmB,CAAC,EAChC,IAAI,EACJ,YAAY,EACZ,IAAI,EACJ,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,OAAO,EACP,QAAQ,EACR,QAAQ,EACR,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,MAAM,EACN,cAAc,EACd,aAAa,EACb,WAA8B,EAC9B,QAAQ,GACX,EAAE,wBAAwB,+BAqW1B;AAgGD,wBAAgB,SAAS,CAAC,EACtB,KAAK,EACL,KAAK,EAAE,QAAQ,EACf,MAAM,EACN,WAAW,EAAE,eAAe,EAC5B,QAAQ,EAAE,YAAY,GACzB,EAAE;IACC,KAAK,EAAE,QAAQ,CAAA;IACf,KAAK,EAAE,GAAG,CAAA;IACV,MAAM,EAAE,GAAG,CAAA;IACX,mFAAmF;IACnF,WAAW,CAAC,EAAE,WAAW,CAAA;IACzB,0EAA0E;IAC1E,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB,+BAqIA"}
|
|
@@ -27,7 +27,7 @@ import { getFieldRef } from '../dynamic-form-schema';
|
|
|
27
27
|
import { isNilUuid, normalizeNilUuid } from '../nil-uuid';
|
|
28
28
|
import { humanizeToken } from '../dynamic-columns-helpers';
|
|
29
29
|
import { formatDateCell } from '../dynamic-columns';
|
|
30
|
-
|
|
30
|
+
import { ImageUrlContext, identityImageUrl } from '../image-url-context';
|
|
31
31
|
// localizedModelName resolves the (possibly addon-i18n) model name: prefer the
|
|
32
32
|
// translated titleKey, fall back to the backend-provided raw title.
|
|
33
33
|
function localizedModelName(meta, t) {
|
|
@@ -157,7 +157,6 @@ const MODE_CONFIG = {
|
|
|
157
157
|
// Context threading host runtime values to nested field components (uploads,
|
|
158
158
|
// image leads, tz-aware dates) without prop-drilling through every renderer.
|
|
159
159
|
const ModelContext = createContext('');
|
|
160
|
-
const ImageUrlContext = createContext(identityImageUrl);
|
|
161
160
|
const TimeZoneContext = createContext(undefined);
|
|
162
161
|
export function DynamicRecordDialog({ open, onOpenChange, mode, model, recordId, endpoint, onSaved, onCreate, onUpdate, defaults, schema, onDelete, onEdit, onOpenFullPage, initialRecord, getImageUrl = identityImageUrl, timeZone, }) {
|
|
163
162
|
const api = useApi();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-form.d.ts","sourceRoot":"","sources":["../src/dynamic-form.tsx"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAC7C,OAAO,EACH,cAAc,EACd,aAAa,EAGhB,MAAM,uBAAuB,CAAA;AAO9B,OAAO,EAAE,cAAc,EAAE,aAAa,EAAE,CAAA;AACxC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAA;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAA;AAE5C,MAAM,WAAW,gBAAgB;IAC7B,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,aAAa,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IACn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MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;CACrB;AAED,wBAAgB,WAAW,CAAC,EACxB,MAAM,EACN,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,WAAuB,EACvB,WAAwB,EACxB,QAAgB,GACnB,EAAE,gBAAgB,+BAmGlB"}
|
package/dist/dynamic-form.js
CHANGED
|
@@ -77,10 +77,35 @@ export function DynamicForm({ fields, initialValues, onSubmit, onCancel, submitL
|
|
|
77
77
|
const fullWidth = isLineItemsField(field) ||
|
|
78
78
|
resolveWidget(field) === 'textarea' ||
|
|
79
79
|
resolveWidget(field) === 'richtext';
|
|
80
|
-
return (_jsxs("div", { className: 'grid gap-2 ' + (fullWidth ? 'sm:col-span-2' : ''), children: [_jsxs(Label, { htmlFor: field.key, children: [field.label, field.required && _jsx("span", { className: "text-red-500 ml-1", children: "*" })] }), _jsx(FieldRenderer, { field: field, value: values[field.key], onChange: (v) => update(field.key, v) }), errors[field.key] && (_jsx("span", { className: "text-red-500 text-sm", role: "alert", children: errors[field.key] }))] }, field.key));
|
|
80
|
+
return (_jsxs("div", { className: 'grid gap-2 ' + (fullWidth ? 'sm:col-span-2' : ''), children: [_jsxs(Label, { htmlFor: field.key, children: [field.label, field.required && _jsx("span", { className: "text-red-500 ml-1", children: "*" })] }), _jsx(FieldRenderer, { field: field, value: values[field.key], onChange: (v) => update(field.key, v), initialValues: initialValues }), errors[field.key] && (_jsx("span", { className: "text-red-500 text-sm", role: "alert", children: errors[field.key] }))] }, field.key));
|
|
81
81
|
}) }), _jsxs("div", { className: "flex justify-end gap-2 pt-2", children: [onCancel && (_jsx(Button, { type: "button", variant: "outline", onClick: onCancel, disabled: submitting || disabled, children: cancelLabel })), _jsx(Button, { type: "submit", disabled: submitting || disabled || balanceBlocked, children: submitLabel })] })] }));
|
|
82
82
|
}
|
|
83
|
-
|
|
83
|
+
// seedOptionFromSibling builds a pre-resolved option for an FK field from the
|
|
84
|
+
// resolved sibling the backend served on the initial record (e.g. a line item's
|
|
85
|
+
// `product = { value, label, image }` alongside `product_id`). Lets the picker
|
|
86
|
+
// show the name + thumbnail for an existing value without a lookup. Returns
|
|
87
|
+
// undefined when the sibling carries nothing renderable.
|
|
88
|
+
function seedOptionFromSibling(field, value, initialValues) {
|
|
89
|
+
if (!field.key.endsWith('_id'))
|
|
90
|
+
return undefined;
|
|
91
|
+
const sib = initialValues?.[field.key.replace(/_id$/, '')];
|
|
92
|
+
if (!sib || typeof sib !== 'object')
|
|
93
|
+
return undefined;
|
|
94
|
+
const label = sib.label ?? sib.name ?? '';
|
|
95
|
+
if (!label && !sib.image)
|
|
96
|
+
return undefined;
|
|
97
|
+
const id = String(sib.value ?? sib.id ?? value ?? '');
|
|
98
|
+
return {
|
|
99
|
+
id,
|
|
100
|
+
value: id,
|
|
101
|
+
label: String(label),
|
|
102
|
+
name: String(label),
|
|
103
|
+
image: sib.image,
|
|
104
|
+
color: sib.color,
|
|
105
|
+
icon: sib.icon,
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function FieldRenderer({ field, value, onChange, initialValues }) {
|
|
84
109
|
// Repeatable line-items group → render the row grid. Its value is an array
|
|
85
110
|
// of row objects rather than a scalar.
|
|
86
111
|
if (isLineItemsField(field)) {
|
|
@@ -91,7 +116,8 @@ function FieldRenderer({ field, value, onChange }) {
|
|
|
91
116
|
// Preferred for FK fields with large option sets — no UUID typing, no
|
|
92
117
|
// dumping every row into a plain <select>.
|
|
93
118
|
if (widget === 'dynamic_select') {
|
|
94
|
-
|
|
119
|
+
const seedOption = seedOptionFromSibling(field, value, initialValues);
|
|
120
|
+
return _jsx(DynamicSelectField, { field: field, value: value, onChange: onChange, seedOption: seedOption });
|
|
95
121
|
}
|
|
96
122
|
// File upload → themed picker that POSTs to the host upload endpoint and
|
|
97
123
|
// stores the returned file url/path as the field value.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-relation-helpers.d.ts","sourceRoot":"","sources":["../src/dynamic-relation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AAE,MAAM,EAAE,CAAA;CAAE,CAYzC;AAED;;;;GAIG;AACH,wBAAgB,eAAe,CAC3B,GAAG,EAAE,aAAa,GAAG,IAAI,GAAG,SAAS,EACrC,UAAU,EAAE,MAAM,GAAG,SAAS,EAC9B,OAAO,EAAE,aAAa,CAAC,gBAAgB,CAAC,GAAG,IAAI,GAAG,SAAS,GAC5D,MAAM,CAiBR"}
|
|
@@ -10,6 +10,19 @@ function isEnumLikeColumn(col) {
|
|
|
10
10
|
renderAs === 'badge' ||
|
|
11
11
|
!!col.options?.length);
|
|
12
12
|
}
|
|
13
|
+
// Server-managed / audit columns that must never become editable form inputs.
|
|
14
|
+
// They're set by the backend and several ship as resolved objects (e.g.
|
|
15
|
+
// `created_by = { name, avatar, email }`) that would render as `[object Object]`.
|
|
16
|
+
const MANAGED_RELATION_COLUMNS = new Set([
|
|
17
|
+
'id',
|
|
18
|
+
'created_at',
|
|
19
|
+
'updated_at',
|
|
20
|
+
'deleted_at',
|
|
21
|
+
'created_by',
|
|
22
|
+
'created_by_id',
|
|
23
|
+
'updated_by',
|
|
24
|
+
'updated_by_id',
|
|
25
|
+
]);
|
|
13
26
|
// Pulls a human label off a resolved relation/user object a backend serves:
|
|
14
27
|
// `{ value, label }` (FK sibling), `{ name, … }` (user object such as
|
|
15
28
|
// created_by) or `{ title }`. Returns undefined for plain/empty objects so the
|
|
@@ -122,6 +135,11 @@ export function deriveRelationFormFields(metadata, foreignKey) {
|
|
|
122
135
|
continue;
|
|
123
136
|
if (col.hidden)
|
|
124
137
|
continue;
|
|
138
|
+
// Managed/audit columns are server-owned and ship resolved objects
|
|
139
|
+
// (`created_by = { name, avatar, … }`); making them editable inputs
|
|
140
|
+
// renders `[object Object]`. Never surface them in the inline form.
|
|
141
|
+
if (MANAGED_RELATION_COLUMNS.has(col.key.toLowerCase()))
|
|
142
|
+
continue;
|
|
125
143
|
out.push({
|
|
126
144
|
key: col.key,
|
|
127
145
|
label: col.label,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-relation.d.ts","sourceRoot":"","sources":["../src/dynamic-relation.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"dynamic-relation.d.ts","sourceRoot":"","sources":["../src/dynamic-relation.tsx"],"names":[],"mappings":"AA6CA,YAAY,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AACrE,OAAO,EACH,kBAAkB,EAClB,uBAAuB,EACvB,kBAAkB,EAClB,yBAAyB,EACzB,wBAAwB,EACxB,aAAa,EACb,wBAAwB,EACxB,kBAAkB,EAClB,WAAW,EACX,eAAe,EACf,cAAc,GACjB,MAAM,4BAA4B,CAAA;AAEnC,MAAM,WAAW,sBAAsB;IACnC,KAAK,EAAE,MAAM,CAAA;IACb,UAAU,EAAE,MAAM,CAAA;IAClB,QAAQ,EAAE,MAAM,CAAA;IAChB,SAAS,EAAE,MAAM,CAAA;IACjB,WAAW,EAAE,MAAM,CAAA;IACnB,kBAAkB,EAAE,MAAM,CAAA;IAC1B,wBAAwB,EAAE,MAAM,CAAA;IAChC,WAAW,EAAE,MAAM,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;IACjB,iBAAiB,EAAE,MAAM,CAAA;IACzB,uBAAuB,EAAE,MAAM,CAAA;IAC/B,WAAW,EAAE,MAAM,CAAA;CACtB;AAiBD,UAAU,WAAW;IACjB,6BAA6B;IAC7B,QAAQ,EAAE,MAAM,GAAG,MAAM,CAAA;IACzB;;;;;;OAMG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAChC,+DAA+D;IAC/D,aAAa,CAAC,EAAE,MAAM,EAAE,CAAA;IACxB,uCAAuC;IACvC,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,2BAA2B;IAC3B,OAAO,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,CAAA;IACzC,yBAAyB;IACzB,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,+DAA+D;IAC/D,QAAQ,CAAC,EAAE,MAAM,IAAI,CAAA;CACxB;AAED,MAAM,WAAW,6BAA8B,SAAQ,WAAW;IAC9D,IAAI,EAAE,aAAa,CAAA;IACnB,yFAAyF;IACzF,KAAK,EAAE,MAAM,CAAA;IACb,kDAAkD;IAClD,UAAU,EAAE,MAAM,CAAA;IAClB,mDAAmD;IACnD,QAAQ,CAAC,EAAE,MAAM,CAAA;CACpB;AAED,MAAM,WAAW,8BAA+B,SAAQ,WAAW;IAC/D,IAAI,EAAE,cAAc,CAAA;IACpB,wEAAwE;IACxE,OAAO,EAAE,MAAM,CAAA;IACf,sEAAsE;IACtE,UAAU,EAAE,MAAM,CAAA;IAClB,6BAA6B;IAC7B,UAAU,EAAE,MAAM,CAAA;IAClB,oEAAoE;IACpE,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,mEAAmE;IACnE,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,uEAAuE;IACvE,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B;;;OAGG;IACH,UAAU,CAAC,EAAE,MAAM,CAAA;CACtB;AAED,MAAM,MAAM,oBAAoB,GAC1B,6BAA6B,GAC7B,8BAA8B,CAAA;AAEpC,wBAAgB,eAAe,CAAC,KAAK,EAAE,oBAAoB,+BAK1D"}
|
package/dist/dynamic-relation.js
CHANGED
|
@@ -10,6 +10,8 @@ import { Plus, Trash2, Pencil } from 'lucide-react';
|
|
|
10
10
|
import { useApi } from './api-context';
|
|
11
11
|
import { useMetadataCache } from './metadata-cache';
|
|
12
12
|
import { DynamicForm } from './dynamic-form';
|
|
13
|
+
import { useImageUrl } from './image-url-context';
|
|
14
|
+
import { OptionThumb } from './dynamic-select-field';
|
|
13
15
|
import { useOptionsResolver } from './use-options-resolver';
|
|
14
16
|
import { buildCreatePayload, buildPivotAttachPayload, buildPivotRowIndex, buildRelationFilterParams, deriveRelationFormFields, diffSelection, extractSelectedTargetIds, formatRelationCell, pickOptionLabel, relationRowKey, } from './dynamic-relation-helpers';
|
|
15
17
|
export { buildCreatePayload, buildPivotAttachPayload, buildPivotRowIndex, buildRelationFilterParams, deriveRelationFormFields, diffSelection, extractSelectedTargetIds, formatRelationCell, objectLabel, pickOptionLabel, relationRowKey, } from './dynamic-relation-helpers';
|
|
@@ -35,6 +37,7 @@ export function DynamicRelation(props) {
|
|
|
35
37
|
}
|
|
36
38
|
function OneToManyRelation({ kind, model, foreignKey, parentId, filters, endpoint, hiddenColumns = [], canCreate = true, canDelete = true, canEdit = true, strings, className, onChange, }) {
|
|
37
39
|
const api = useApi();
|
|
40
|
+
const getImageUrl = useImageUrl();
|
|
38
41
|
const { getMetadata, setMetadata: cacheMetadata } = useMetadataCache();
|
|
39
42
|
const cachedMeta = getMetadata(model);
|
|
40
43
|
const labels = { ...DEFAULT_STRINGS, ...(strings || {}) };
|
|
@@ -136,6 +139,17 @@ function OneToManyRelation({ kind, model, foreignKey, parentId, filters, endpoin
|
|
|
136
139
|
}, [api, dataEndpoint, fetchAll, onChange, rowToDelete]);
|
|
137
140
|
return (_jsxs("div", { className: className, "data-relation-kind": kind, "data-relation-model": model, children: [(labels.title || canCreate) && (_jsxs("div", { className: "flex items-center justify-between pb-3", children: [labels.title ? _jsx("h3", { className: "text-sm font-medium", children: labels.title }) : _jsx("span", {}), canCreate && (_jsxs(Button, { size: "sm", variant: "outline", onClick: () => { setEditingRow(null); setFormOpen(true); }, children: [_jsx(Plus, { className: "h-4 w-4 mr-1" }), labels.addLabel] }))] })), loading ? (_jsx("div", { className: "space-y-2", children: Array.from({ length: 3 }).map((_, i) => (_jsx(Skeleton, { className: "h-10 w-full" }, `rel-skeleton-${i}`))) })) : rows.length === 0 ? (_jsx("div", { className: "text-center text-sm text-muted-foreground py-8 border rounded-md bg-muted/30", children: labels.emptyState })) : (_jsx("div", { className: "border rounded-md divide-y bg-card", children: rows.map((row, idx) => (_jsxs("div", { className: "flex items-center justify-between gap-3 px-3 py-2", children: [_jsx("div", { className: "flex-1 grid grid-cols-[repeat(auto-fit,minmax(0,1fr))] gap-2 text-sm", children: visibleColumns.map(col => {
|
|
138
141
|
const cell = formatRelationCell(row, col);
|
|
142
|
+
// FK column whose backend-resolved sibling
|
|
143
|
+
// carries an image → render a thumbnail + label
|
|
144
|
+
// instead of plain text (e.g. a line item's
|
|
145
|
+
// product photo). The sibling is the column key
|
|
146
|
+
// with the trailing `_id` stripped.
|
|
147
|
+
const isFk = !!col.ref || col.key.endsWith('_id');
|
|
148
|
+
const sibling = isFk ? row[col.key.replace(/_id$/, '')] : undefined;
|
|
149
|
+
if (sibling && typeof sibling === 'object' && sibling.image) {
|
|
150
|
+
const label = sibling.label ?? sibling.name ?? cell;
|
|
151
|
+
return (_jsxs("span", { className: "flex min-w-0 items-center gap-2", title: String(label), children: [_jsx(OptionThumb, { image: getImageUrl(sibling.image), size: 20 }), _jsx("span", { className: "truncate", children: label })] }, col.key));
|
|
152
|
+
}
|
|
139
153
|
return (_jsx("span", { className: "truncate", title: cell, children: cell }, col.key));
|
|
140
154
|
}) }), _jsxs("div", { className: "flex items-center gap-1 shrink-0", children: [canEdit && (_jsx(Button, { size: "sm", variant: "ghost", onClick: () => { setEditingRow(row); setFormOpen(true); }, "aria-label": labels.editLabel, children: _jsx(Pencil, { className: "h-4 w-4" }) })), canDelete && (_jsx(Button, { size: "sm", variant: "ghost", onClick: () => setRowToDelete(row), "aria-label": labels.removeLabel, children: _jsx(Trash2, { className: "h-4 w-4" }) }))] })] }, relationRowKey(row, idx, foreignKey)))) })), _jsx(Dialog, { open: formOpen, onOpenChange: (open) => { setFormOpen(open); if (!open)
|
|
141
155
|
setEditingRow(null); }, children: _jsxs(DialogContent, { children: [_jsx(DialogHeader, { children: _jsx(DialogTitle, { children: editingRow ? labels.editLabel : labels.addLabel }) }), _jsx(DynamicForm, { fields: formFields, initialValues: editingRow || undefined, onSubmit: handleSubmit, onCancel: () => { setFormOpen(false); setEditingRow(null); }, submitLabel: labels.saveLabel, cancelLabel: labels.cancelLabel, disabled: submitting })] }) }), _jsx(AlertDialog, { open: !!rowToDelete, onOpenChange: (open) => !open && setRowToDelete(null), children: _jsxs(AlertDialogContent, { children: [_jsxs(AlertDialogHeader, { children: [_jsx(AlertDialogTitle, { children: labels.confirmRemoveTitle }), _jsx(AlertDialogDescription, { children: labels.confirmRemoveDescription })] }), _jsxs(AlertDialogFooter, { children: [_jsx(AlertDialogCancel, { disabled: submitting, children: labels.cancelLabel }), _jsx(AlertDialogAction, { onClick: (e) => { e.preventDefault(); handleDelete(); }, className: "bg-red-600 hover:bg-red-700", disabled: submitting, children: labels.removeLabel })] })] }) })] }));
|
|
@@ -24,7 +24,14 @@ export interface DynamicSelectFieldProps {
|
|
|
24
24
|
field: ActionFieldDef;
|
|
25
25
|
value: any;
|
|
26
26
|
onChange: (v: any) => void;
|
|
27
|
+
/**
|
|
28
|
+
* Pre-resolved option for the CURRENT value (label + image/color/icon) the
|
|
29
|
+
* caller already has — e.g. the relation sibling the table served. Lets the
|
|
30
|
+
* trigger show the name + thumbnail for an existing value without waiting for
|
|
31
|
+
* a lookup (which only loads once the popover opens). Matched by id == value.
|
|
32
|
+
*/
|
|
33
|
+
seedOption?: ResolvedOption | null;
|
|
27
34
|
}
|
|
28
|
-
export declare function DynamicSelectField({ field, value, onChange }: DynamicSelectFieldProps): import("react").JSX.Element;
|
|
35
|
+
export declare function DynamicSelectField({ field, value, onChange, seedOption }: DynamicSelectFieldProps): import("react").JSX.Element;
|
|
29
36
|
export default DynamicSelectField;
|
|
30
37
|
//# sourceMappingURL=dynamic-select-field.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"dynamic-select-field.d.ts","sourceRoot":"","sources":["../src/dynamic-select-field.tsx"],"names":[],"mappings":"AAuCA,OAAO,EAAsB,KAAK,cAAc,EAAE,MAAM,wBAAwB,CAAA;AAEhF,OAAO,KAAK,EAAE,cAAc,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AAE,cAAc,CAAA;IACrB,KAAK,EAAE,GAAG,CAAA;IACV,QAAQ,EAAE,CAAC,CAAC,EAAE,GAAG,KAAK,IAAI,CAAA;IAC1B;;;;;OAKG;IACH,UAAU,CAAC,EAAE,cAAc,GAAG,IAAI,CAAA;CACrC;AAED,wBAAgB,kBAAkB,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,EAAE,uBAAuB,+BA2KjG;AAED,eAAe,kBAAkB,CAAA"}
|
|
@@ -79,7 +79,7 @@ function useDebounced(value, ms) {
|
|
|
79
79
|
}, [value, ms]);
|
|
80
80
|
return debounced;
|
|
81
81
|
}
|
|
82
|
-
export function DynamicSelectField({ field, value, onChange }) {
|
|
82
|
+
export function DynamicSelectField({ field, value, onChange, seedOption }) {
|
|
83
83
|
const [open, setOpen] = useState(false);
|
|
84
84
|
const [search, setSearch] = useState('');
|
|
85
85
|
const debounced = useDebounced(search, 250);
|
|
@@ -107,6 +107,7 @@ export function DynamicSelectField({ field, value, onChange }) {
|
|
|
107
107
|
// label and its thumbnail.
|
|
108
108
|
const selectedOption = (picked && String(picked.id) === String(value) ? picked : null) ??
|
|
109
109
|
options.find((o) => String(o.id) === String(value)) ??
|
|
110
|
+
(seedOption && String(seedOption.id) === String(value) ? seedOption : null) ??
|
|
110
111
|
null;
|
|
111
112
|
const selectedLabel = selectedOption?.label ?? (value ? String(value) : '');
|
|
112
113
|
// Only switch the picker into "with thumbnails" mode when the data actually
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** Resolves a (possibly relative) storage path into a fetchable URL. */
|
|
2
|
+
export type GetImageUrl = (path: string | null | undefined) => string;
|
|
3
|
+
/** Default resolver: pass the path through unchanged (works same-origin). */
|
|
4
|
+
export declare const identityImageUrl: GetImageUrl;
|
|
5
|
+
/**
|
|
6
|
+
* Threads the host's image-url resolver to nested field/cell components without
|
|
7
|
+
* prop-drilling. Provided by `DynamicRecordDialog`; consumers outside a provider
|
|
8
|
+
* fall back to `identityImageUrl` (the relative path, which renders same-origin).
|
|
9
|
+
*/
|
|
10
|
+
export declare const ImageUrlContext: import("react").Context<GetImageUrl>;
|
|
11
|
+
/** Reads the nearest image-url resolver (identity outside a provider). */
|
|
12
|
+
export declare const useImageUrl: () => GetImageUrl;
|
|
13
|
+
//# sourceMappingURL=image-url-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"image-url-context.d.ts","sourceRoot":"","sources":["../src/image-url-context.tsx"],"names":[],"mappings":"AAQA,wEAAwE;AACxE,MAAM,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,KAAK,MAAM,CAAA;AAErE,6EAA6E;AAC7E,eAAO,MAAM,gBAAgB,EAAE,WAA4B,CAAA;AAE3D;;;;GAIG;AACH,eAAO,MAAM,eAAe,sCAA+C,CAAA;AAE3E,0EAA0E;AAC1E,eAAO,MAAM,WAAW,mBAAoC,CAAA"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// Image-url resolver context — its own module so any renderer (record dialog,
|
|
2
|
+
// relation cells, …) can consume the host's storage-path → URL resolver without
|
|
3
|
+
// importing from `dialogs/dynamic-record`. That dialog imports
|
|
4
|
+
// `dynamic-relations` (which renders `dynamic-relation`), so the relation cell
|
|
5
|
+
// cannot import the context back from the dialog without a circular import —
|
|
6
|
+
// hence this standalone module is the single source of truth.
|
|
7
|
+
import { createContext, useContext } from 'react';
|
|
8
|
+
/** Default resolver: pass the path through unchanged (works same-origin). */
|
|
9
|
+
export const identityImageUrl = (p) => p ?? '';
|
|
10
|
+
/**
|
|
11
|
+
* Threads the host's image-url resolver to nested field/cell components without
|
|
12
|
+
* prop-drilling. Provided by `DynamicRecordDialog`; consumers outside a provider
|
|
13
|
+
* fall back to `identityImageUrl` (the relative path, which renders same-origin).
|
|
14
|
+
*/
|
|
15
|
+
export const ImageUrlContext = createContext(identityImageUrl);
|
|
16
|
+
/** Reads the nearest image-url resolver (identity outside a provider). */
|
|
17
|
+
export const useImageUrl = () => useContext(ImageUrlContext);
|
package/package.json
CHANGED
|
@@ -114,6 +114,23 @@ describe('deriveRelationFormFields', () => {
|
|
|
114
114
|
expect(fields.find(f => f.key === 'id')).toBeUndefined()
|
|
115
115
|
})
|
|
116
116
|
|
|
117
|
+
// Server-managed / audit columns ship as resolved objects (created_by =
|
|
118
|
+
// { name, avatar }) and must never become editable inputs ([object Object]).
|
|
119
|
+
it('omite columnas managed/audit aunque no estén hidden', () => {
|
|
120
|
+
const meta: Pick<TableMetadata, 'columns'> = {
|
|
121
|
+
columns: [
|
|
122
|
+
{ key: 'sku', label: 'SKU', type: 'text', sortable: true, filterable: true },
|
|
123
|
+
{ key: 'created_by', label: 'Creado por', type: 'text', sortable: false, filterable: false },
|
|
124
|
+
{ key: 'created_by_id', label: 'Creado por id', type: 'text', sortable: false, filterable: false },
|
|
125
|
+
{ key: 'created_at', label: 'Creado', type: 'date', sortable: true, filterable: false },
|
|
126
|
+
{ key: 'updated_at', label: 'Actualizado', type: 'date', sortable: true, filterable: false },
|
|
127
|
+
{ key: 'deleted_at', label: 'Eliminado', type: 'date', sortable: false, filterable: false },
|
|
128
|
+
],
|
|
129
|
+
}
|
|
130
|
+
const fields = deriveRelationFormFields(meta, 'invoice_id')
|
|
131
|
+
expect(fields.map(f => f.key)).toEqual(['sku'])
|
|
132
|
+
})
|
|
133
|
+
|
|
117
134
|
it('mapea types de ColumnDefinition al ActionFieldDef.type', () => {
|
|
118
135
|
const fields = deriveRelationFormFields(baseMeta, 'invoice_id')
|
|
119
136
|
const byKey = Object.fromEntries(fields.map(f => [f.key, f]))
|
|
@@ -56,10 +56,11 @@ import { isNilUuid, normalizeNilUuid } from '../nil-uuid'
|
|
|
56
56
|
import { humanizeToken } from '../dynamic-columns-helpers'
|
|
57
57
|
import { formatDateCell } from '../dynamic-columns'
|
|
58
58
|
import type { ActionFieldDef, RelationMeta } from '../types'
|
|
59
|
+
import { ImageUrlContext, identityImageUrl, type GetImageUrl } from '../image-url-context'
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
export type
|
|
62
|
-
|
|
61
|
+
// Re-export the resolver type so `index.ts`'s
|
|
62
|
+
// `export type { … GetImageUrl } from './dialogs/dynamic-record'` keeps working.
|
|
63
|
+
export type { GetImageUrl }
|
|
63
64
|
|
|
64
65
|
export interface FieldOption {
|
|
65
66
|
value: string
|
|
@@ -330,7 +331,6 @@ const MODE_CONFIG = {
|
|
|
330
331
|
// Context threading host runtime values to nested field components (uploads,
|
|
331
332
|
// image leads, tz-aware dates) without prop-drilling through every renderer.
|
|
332
333
|
const ModelContext = createContext('')
|
|
333
|
-
const ImageUrlContext = createContext<GetImageUrl>(identityImageUrl)
|
|
334
334
|
const TimeZoneContext = createContext<string | undefined>(undefined)
|
|
335
335
|
|
|
336
336
|
export function DynamicRecordDialog({
|
package/src/dynamic-form.tsx
CHANGED
|
@@ -129,6 +129,7 @@ export function DynamicForm({
|
|
|
129
129
|
field={field}
|
|
130
130
|
value={values[field.key]}
|
|
131
131
|
onChange={(v: any) => update(field.key, v)}
|
|
132
|
+
initialValues={initialValues}
|
|
132
133
|
/>
|
|
133
134
|
{errors[field.key] && (
|
|
134
135
|
<span className="text-red-500 text-sm" role="alert">{errors[field.key]}</span>
|
|
@@ -155,9 +156,38 @@ interface FieldRendererProps {
|
|
|
155
156
|
field: ActionFieldDef
|
|
156
157
|
value: any
|
|
157
158
|
onChange: (v: any) => void
|
|
159
|
+
/** The form's initial record — used to seed an FK picker's existing label/image. */
|
|
160
|
+
initialValues?: Record<string, any>
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// seedOptionFromSibling builds a pre-resolved option for an FK field from the
|
|
164
|
+
// resolved sibling the backend served on the initial record (e.g. a line item's
|
|
165
|
+
// `product = { value, label, image }` alongside `product_id`). Lets the picker
|
|
166
|
+
// show the name + thumbnail for an existing value without a lookup. Returns
|
|
167
|
+
// undefined when the sibling carries nothing renderable.
|
|
168
|
+
function seedOptionFromSibling(
|
|
169
|
+
field: ActionFieldDef,
|
|
170
|
+
value: any,
|
|
171
|
+
initialValues?: Record<string, any>,
|
|
172
|
+
): ResolvedOption | undefined {
|
|
173
|
+
if (!field.key.endsWith('_id')) return undefined
|
|
174
|
+
const sib = initialValues?.[field.key.replace(/_id$/, '')]
|
|
175
|
+
if (!sib || typeof sib !== 'object') return undefined
|
|
176
|
+
const label = sib.label ?? sib.name ?? ''
|
|
177
|
+
if (!label && !sib.image) return undefined
|
|
178
|
+
const id = String(sib.value ?? sib.id ?? value ?? '')
|
|
179
|
+
return {
|
|
180
|
+
id,
|
|
181
|
+
value: id,
|
|
182
|
+
label: String(label),
|
|
183
|
+
name: String(label),
|
|
184
|
+
image: sib.image,
|
|
185
|
+
color: sib.color,
|
|
186
|
+
icon: sib.icon,
|
|
187
|
+
}
|
|
158
188
|
}
|
|
159
189
|
|
|
160
|
-
function FieldRenderer({ field, value, onChange }: FieldRendererProps) {
|
|
190
|
+
function FieldRenderer({ field, value, onChange, initialValues }: FieldRendererProps) {
|
|
161
191
|
// Repeatable line-items group → render the row grid. Its value is an array
|
|
162
192
|
// of row objects rather than a scalar.
|
|
163
193
|
if (isLineItemsField(field)) {
|
|
@@ -168,7 +198,8 @@ function FieldRenderer({ field, value, onChange }: FieldRendererProps) {
|
|
|
168
198
|
// Preferred for FK fields with large option sets — no UUID typing, no
|
|
169
199
|
// dumping every row into a plain <select>.
|
|
170
200
|
if (widget === 'dynamic_select') {
|
|
171
|
-
|
|
201
|
+
const seedOption = seedOptionFromSibling(field, value, initialValues)
|
|
202
|
+
return <DynamicSelectField field={field} value={value} onChange={onChange} seedOption={seedOption} />
|
|
172
203
|
}
|
|
173
204
|
// File upload → themed picker that POSTs to the host upload endpoint and
|
|
174
205
|
// stores the returned file url/path as the field value.
|
|
@@ -20,6 +20,20 @@ function isEnumLikeColumn(col: ColumnDefinition): boolean {
|
|
|
20
20
|
|
|
21
21
|
export type DynamicRelationKind = 'one_to_many' | 'many_to_many'
|
|
22
22
|
|
|
23
|
+
// Server-managed / audit columns that must never become editable form inputs.
|
|
24
|
+
// They're set by the backend and several ship as resolved objects (e.g.
|
|
25
|
+
// `created_by = { name, avatar, email }`) that would render as `[object Object]`.
|
|
26
|
+
const MANAGED_RELATION_COLUMNS = new Set([
|
|
27
|
+
'id',
|
|
28
|
+
'created_at',
|
|
29
|
+
'updated_at',
|
|
30
|
+
'deleted_at',
|
|
31
|
+
'created_by',
|
|
32
|
+
'created_by_id',
|
|
33
|
+
'updated_by',
|
|
34
|
+
'updated_by_id',
|
|
35
|
+
])
|
|
36
|
+
|
|
23
37
|
// Pulls a human label off a resolved relation/user object a backend serves:
|
|
24
38
|
// `{ value, label }` (FK sibling), `{ name, … }` (user object such as
|
|
25
39
|
// created_by) or `{ title }`. Returns undefined for plain/empty objects so the
|
|
@@ -146,6 +160,10 @@ export function deriveRelationFormFields(
|
|
|
146
160
|
for (const col of metadata.columns) {
|
|
147
161
|
if (col.key === foreignKey) continue
|
|
148
162
|
if (col.hidden) continue
|
|
163
|
+
// Managed/audit columns are server-owned and ship resolved objects
|
|
164
|
+
// (`created_by = { name, avatar, … }`); making them editable inputs
|
|
165
|
+
// renders `[object Object]`. Never surface them in the inline form.
|
|
166
|
+
if (MANAGED_RELATION_COLUMNS.has(col.key.toLowerCase())) continue
|
|
149
167
|
out.push({
|
|
150
168
|
key: col.key,
|
|
151
169
|
label: col.label,
|
package/src/dynamic-relation.tsx
CHANGED
|
@@ -25,6 +25,8 @@ import { Plus, Trash2, Pencil } from 'lucide-react'
|
|
|
25
25
|
import { useApi } from './api-context'
|
|
26
26
|
import { useMetadataCache } from './metadata-cache'
|
|
27
27
|
import { DynamicForm } from './dynamic-form'
|
|
28
|
+
import { useImageUrl } from './image-url-context'
|
|
29
|
+
import { OptionThumb } from './dynamic-select-field'
|
|
28
30
|
import { useOptionsResolver } from './use-options-resolver'
|
|
29
31
|
import type { ApiResponse, TableMetadata } from './types'
|
|
30
32
|
import {
|
|
@@ -169,6 +171,7 @@ function OneToManyRelation({
|
|
|
169
171
|
onChange,
|
|
170
172
|
}: DynamicRelationOneToManyProps) {
|
|
171
173
|
const api = useApi()
|
|
174
|
+
const getImageUrl = useImageUrl()
|
|
172
175
|
const { getMetadata, setMetadata: cacheMetadata } = useMetadataCache()
|
|
173
176
|
const cachedMeta = getMetadata(model)
|
|
174
177
|
const labels = { ...DEFAULT_STRINGS, ...(strings || {}) }
|
|
@@ -305,6 +308,22 @@ function OneToManyRelation({
|
|
|
305
308
|
<div className="flex-1 grid grid-cols-[repeat(auto-fit,minmax(0,1fr))] gap-2 text-sm">
|
|
306
309
|
{visibleColumns.map(col => {
|
|
307
310
|
const cell = formatRelationCell(row, col)
|
|
311
|
+
// FK column whose backend-resolved sibling
|
|
312
|
+
// carries an image → render a thumbnail + label
|
|
313
|
+
// instead of plain text (e.g. a line item's
|
|
314
|
+
// product photo). The sibling is the column key
|
|
315
|
+
// with the trailing `_id` stripped.
|
|
316
|
+
const isFk = !!col.ref || col.key.endsWith('_id')
|
|
317
|
+
const sibling = isFk ? (row as any)[col.key.replace(/_id$/, '')] : undefined
|
|
318
|
+
if (sibling && typeof sibling === 'object' && sibling.image) {
|
|
319
|
+
const label = sibling.label ?? sibling.name ?? cell
|
|
320
|
+
return (
|
|
321
|
+
<span key={col.key} className="flex min-w-0 items-center gap-2" title={String(label)}>
|
|
322
|
+
<OptionThumb image={getImageUrl(sibling.image)} size={20} />
|
|
323
|
+
<span className="truncate">{label}</span>
|
|
324
|
+
</span>
|
|
325
|
+
)
|
|
326
|
+
}
|
|
308
327
|
return (
|
|
309
328
|
<span key={col.key} className="truncate" title={cell}>
|
|
310
329
|
{cell}
|
|
@@ -138,9 +138,16 @@ export interface DynamicSelectFieldProps {
|
|
|
138
138
|
field: ActionFieldDef
|
|
139
139
|
value: any
|
|
140
140
|
onChange: (v: any) => void
|
|
141
|
+
/**
|
|
142
|
+
* Pre-resolved option for the CURRENT value (label + image/color/icon) the
|
|
143
|
+
* caller already has — e.g. the relation sibling the table served. Lets the
|
|
144
|
+
* trigger show the name + thumbnail for an existing value without waiting for
|
|
145
|
+
* a lookup (which only loads once the popover opens). Matched by id == value.
|
|
146
|
+
*/
|
|
147
|
+
seedOption?: ResolvedOption | null
|
|
141
148
|
}
|
|
142
149
|
|
|
143
|
-
export function DynamicSelectField({ field, value, onChange }: DynamicSelectFieldProps) {
|
|
150
|
+
export function DynamicSelectField({ field, value, onChange, seedOption }: DynamicSelectFieldProps) {
|
|
144
151
|
const [open, setOpen] = useState(false)
|
|
145
152
|
const [search, setSearch] = useState('')
|
|
146
153
|
const debounced = useDebounced(search, 250)
|
|
@@ -172,6 +179,7 @@ export function DynamicSelectField({ field, value, onChange }: DynamicSelectFiel
|
|
|
172
179
|
const selectedOption =
|
|
173
180
|
(picked && String(picked.id) === String(value) ? picked : null) ??
|
|
174
181
|
options.find((o) => String(o.id) === String(value)) ??
|
|
182
|
+
(seedOption && String(seedOption.id) === String(value) ? seedOption : null) ??
|
|
175
183
|
null
|
|
176
184
|
|
|
177
185
|
const selectedLabel = selectedOption?.label ?? (value ? String(value) : '')
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
// Image-url resolver context — its own module so any renderer (record dialog,
|
|
2
|
+
// relation cells, …) can consume the host's storage-path → URL resolver without
|
|
3
|
+
// importing from `dialogs/dynamic-record`. That dialog imports
|
|
4
|
+
// `dynamic-relations` (which renders `dynamic-relation`), so the relation cell
|
|
5
|
+
// cannot import the context back from the dialog without a circular import —
|
|
6
|
+
// hence this standalone module is the single source of truth.
|
|
7
|
+
import { createContext, useContext } from 'react'
|
|
8
|
+
|
|
9
|
+
/** Resolves a (possibly relative) storage path into a fetchable URL. */
|
|
10
|
+
export type GetImageUrl = (path: string | null | undefined) => string
|
|
11
|
+
|
|
12
|
+
/** Default resolver: pass the path through unchanged (works same-origin). */
|
|
13
|
+
export const identityImageUrl: GetImageUrl = (p) => p ?? ''
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Threads the host's image-url resolver to nested field/cell components without
|
|
17
|
+
* prop-drilling. Provided by `DynamicRecordDialog`; consumers outside a provider
|
|
18
|
+
* fall back to `identityImageUrl` (the relative path, which renders same-origin).
|
|
19
|
+
*/
|
|
20
|
+
export const ImageUrlContext = createContext<GetImageUrl>(identityImageUrl)
|
|
21
|
+
|
|
22
|
+
/** Reads the nearest image-url resolver (identity outside a provider). */
|
|
23
|
+
export const useImageUrl = () => useContext(ImageUrlContext)
|