@asteby/metacore-runtime-react 53.11.0 → 53.13.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/bulk-image-dialog.d.ts +31 -0
- package/dist/bulk-image-dialog.d.ts.map +1 -0
- package/dist/bulk-image-dialog.js +339 -0
- package/dist/bulk-image-import.d.ts +158 -0
- package/dist/bulk-image-import.d.ts.map +1 -0
- package/dist/bulk-image-import.js +310 -0
- package/dist/dynamic-crud-page.d.ts.map +1 -1
- package/dist/dynamic-crud-page.js +2 -1
- package/dist/dynamic-relation-helpers.d.ts +27 -0
- package/dist/dynamic-relation-helpers.d.ts.map +1 -1
- package/dist/dynamic-relation-helpers.js +59 -0
- package/dist/dynamic-relation.d.ts.map +1 -1
- package/dist/dynamic-relation.js +95 -23
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/relation-gallery.d.ts +9 -1
- package/dist/relation-gallery.d.ts.map +1 -1
- package/dist/relation-gallery.js +4 -4
- package/package.json +2 -1
- package/src/__tests__/bulk-image-dialog.test.tsx +127 -0
- package/src/__tests__/bulk-image-import.test.ts +129 -0
- package/src/__tests__/relation-gallery-helpers.test.ts +29 -0
- package/src/__tests__/relation-gallery.test.tsx +91 -7
- package/src/bulk-image-dialog.tsx +594 -0
- package/src/bulk-image-import.ts +456 -0
- package/src/dynamic-crud-page.tsx +2 -0
- package/src/dynamic-relation-helpers.ts +66 -0
- package/src/dynamic-relation.tsx +98 -21
- package/src/index.ts +12 -0
- package/src/relation-gallery.tsx +36 -7
package/src/dynamic-relation.tsx
CHANGED
|
@@ -67,6 +67,8 @@ import {
|
|
|
67
67
|
pickOptionLabel,
|
|
68
68
|
relationColumnUi,
|
|
69
69
|
relationDerivedValues,
|
|
70
|
+
galleryLeadImage,
|
|
71
|
+
contiguousPositionUpdates,
|
|
70
72
|
relationGalleryColumn,
|
|
71
73
|
relationInitialValues,
|
|
72
74
|
relationPositionKey,
|
|
@@ -455,6 +457,10 @@ function OneToManyRelation({
|
|
|
455
457
|
const positionKey = useMemo(() => relationPositionKey(metadata), [metadata])
|
|
456
458
|
const orderedRows = useMemo(() => sortRelationRows(rows, positionKey), [rows, positionKey])
|
|
457
459
|
const galleryColumn = useMemo(() => relationGalleryColumn(metadata), [metadata])
|
|
460
|
+
const galleryLead = useMemo(
|
|
461
|
+
() => galleryLeadImage(galleryColumn, parentRecord, rows),
|
|
462
|
+
[galleryColumn, parentRecord, rows],
|
|
463
|
+
)
|
|
458
464
|
const captionKey = useMemo(
|
|
459
465
|
() => metadata?.columns?.find((c) => relationColumnUi(c).defaultFromParent)?.key,
|
|
460
466
|
[metadata],
|
|
@@ -595,11 +601,73 @@ function OneToManyRelation({
|
|
|
595
601
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
596
602
|
}, [api, dataEndpoint, editingRow, fetchAll, foreignKey, filtersKey, onChange, parentId, metadata, rows])
|
|
597
603
|
|
|
604
|
+
// Highest position this panel created itself. `rows` can lag behind the
|
|
605
|
+
// rows just created (the list refreshes after the batch), so two uploads in
|
|
606
|
+
// a row must not both read the same stale maximum. Reset per parent.
|
|
607
|
+
const createdMaxPositionRef = useRef(-1)
|
|
608
|
+
useEffect(() => { createdMaxPositionRef.current = -1 }, [parentId])
|
|
609
|
+
|
|
610
|
+
// Position writes (reorder and renumber) run one after another, never
|
|
611
|
+
// interleaved: a renumber that read the server before a drag would
|
|
612
|
+
// otherwise overwrite the order the user just chose.
|
|
613
|
+
const positionQueueRef = useRef<Promise<unknown>>(Promise.resolve())
|
|
614
|
+
const enqueuePositions = useCallback(<T,>(fn: () => Promise<T>): Promise<T> => {
|
|
615
|
+
const run = positionQueueRef.current.then(fn, fn)
|
|
616
|
+
positionQueueRef.current = run.catch(() => undefined)
|
|
617
|
+
return run
|
|
618
|
+
}, [])
|
|
619
|
+
|
|
620
|
+
// Renumbers the gallery 0..n-1 from what the SERVER holds (read when the
|
|
621
|
+
// job runs, not from state): heals duplicated positions left by older
|
|
622
|
+
// clients and closes the gap a deleted photo leaves. Reads every page;
|
|
623
|
+
// writes only the rows that differ; a failed write does not stop the rest.
|
|
624
|
+
const normalizePositions = useCallback(
|
|
625
|
+
() =>
|
|
626
|
+
enqueuePositions(async () => {
|
|
627
|
+
if (!positionKey || !relationGalleryColumn(metadataRef.current)) return
|
|
628
|
+
try {
|
|
629
|
+
const fresh: any[] = []
|
|
630
|
+
for (let page = 1; page <= 20; page++) {
|
|
631
|
+
const res = (await api.get(dataEndpoint, {
|
|
632
|
+
params: {
|
|
633
|
+
...buildRelationFilterParams(foreignKey, parentId, filters),
|
|
634
|
+
page,
|
|
635
|
+
per_page: GALLERY_PAGE_SIZE,
|
|
636
|
+
sortBy: positionKey,
|
|
637
|
+
order: 'asc',
|
|
638
|
+
},
|
|
639
|
+
})) as { data?: ApiResponse<any[]> }
|
|
640
|
+
if (!res?.data?.success) return
|
|
641
|
+
const got = res.data.data ?? []
|
|
642
|
+
fresh.push(...got)
|
|
643
|
+
if (got.length < GALLERY_PAGE_SIZE) break
|
|
644
|
+
if (page === 20) return // too many to renumber safely
|
|
645
|
+
}
|
|
646
|
+
const { updates } = contiguousPositionUpdates(dedupeById([], fresh), positionKey)
|
|
647
|
+
let failed = 0
|
|
648
|
+
for (const u of updates) {
|
|
649
|
+
try {
|
|
650
|
+
const r = (await api.put(`${dataEndpoint}/${u.id}`, { [positionKey]: u.position })) as any
|
|
651
|
+
if (r?.data?.success === false) failed++
|
|
652
|
+
} catch {
|
|
653
|
+
failed++
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
createdMaxPositionRef.current = Math.max(createdMaxPositionRef.current, fresh.length - 1)
|
|
657
|
+
if (failed) toast.error(labels.reorderError ?? 'No se pudo guardar el nuevo orden.')
|
|
658
|
+
} catch (err) {
|
|
659
|
+
console.error('DynamicRelation normalize positions error', err)
|
|
660
|
+
}
|
|
661
|
+
}),
|
|
662
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
663
|
+
[api, dataEndpoint, positionKey, foreignKey, parentId, filtersKey, enqueuePositions],
|
|
664
|
+
)
|
|
665
|
+
|
|
598
666
|
// ── Gallery (opt-in: image column with display_config.gallery) ─────────
|
|
599
667
|
// Same contracts as the table: POST /upload (multipart) for the file, then
|
|
600
668
|
// POST /data/<model> for the row. Positions are assigned in pick order.
|
|
601
669
|
const galleryUpload = useCallback(
|
|
602
|
-
async (file: File,
|
|
670
|
+
async (file: File, _offset: number, onProgress: (f: number) => void, reuseUrl?: string) => {
|
|
603
671
|
if (!galleryColumn) return
|
|
604
672
|
const size = await readImageSize(file)
|
|
605
673
|
let url = reuseUrl
|
|
@@ -623,7 +691,10 @@ function OneToManyRelation({
|
|
|
623
691
|
if (!url) throw new Error('upload failed')
|
|
624
692
|
}
|
|
625
693
|
const derived = relationDerivedValues(metadata, { isCreate: true, rows, image: size })
|
|
626
|
-
if (positionKey && typeof derived[positionKey] === 'number')
|
|
694
|
+
if (positionKey && typeof derived[positionKey] === 'number') {
|
|
695
|
+
derived[positionKey] = Math.max(derived[positionKey], createdMaxPositionRef.current + 1)
|
|
696
|
+
createdMaxPositionRef.current = derived[positionKey]
|
|
697
|
+
}
|
|
627
698
|
const payload = {
|
|
628
699
|
...(filters || {}),
|
|
629
700
|
...buildCreatePayload(foreignKey, parentId, {
|
|
@@ -651,24 +722,26 @@ function OneToManyRelation({
|
|
|
651
722
|
if (!positionKey) return
|
|
652
723
|
// Optimistic: show the new order now, persist only what changed.
|
|
653
724
|
setRows(ordered.map((r, i) => ({ ...r, [positionKey]: i })))
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
725
|
+
await enqueuePositions(async () => {
|
|
726
|
+
try {
|
|
727
|
+
await Promise.all(
|
|
728
|
+
ordered.map((r, i) => {
|
|
729
|
+
const raw = r[positionKey]
|
|
730
|
+
const same = raw !== null && raw !== undefined && raw !== '' && Number(raw) === i
|
|
731
|
+
return same ? null : api.put(`${dataEndpoint}/${r.id}`, { [positionKey]: i })
|
|
732
|
+
}),
|
|
733
|
+
)
|
|
734
|
+
onChange?.()
|
|
735
|
+
} catch (err) {
|
|
736
|
+
console.error('DynamicRelation reorder error', err)
|
|
737
|
+
toast.error(labels.reorderError ?? 'No se pudo guardar el nuevo orden.')
|
|
738
|
+
} finally {
|
|
739
|
+
await refreshSilently()
|
|
740
|
+
}
|
|
741
|
+
})
|
|
669
742
|
},
|
|
670
743
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
671
|
-
[api, dataEndpoint, positionKey, refreshSilently, onChange],
|
|
744
|
+
[api, dataEndpoint, positionKey, refreshSilently, onChange, enqueuePositions],
|
|
672
745
|
)
|
|
673
746
|
|
|
674
747
|
// Confirmed delete → hidden at once, «Deshacer» for a few seconds, then the
|
|
@@ -683,6 +756,7 @@ function OneToManyRelation({
|
|
|
683
756
|
try {
|
|
684
757
|
const res = await api.delete(`${dataEndpoint}/${row.id}`)
|
|
685
758
|
if (!(res as any).data?.success) throw new Error('delete failed')
|
|
759
|
+
await normalizePositions()
|
|
686
760
|
onChange?.()
|
|
687
761
|
} catch (err) {
|
|
688
762
|
console.error('DynamicRelation delete error', err)
|
|
@@ -704,6 +778,7 @@ function OneToManyRelation({
|
|
|
704
778
|
onClick: () => {
|
|
705
779
|
undone = true
|
|
706
780
|
clearTimeout(timer)
|
|
781
|
+
void normalizePositions().then(() => refreshSilently())
|
|
707
782
|
setPendingDelete((s) => {
|
|
708
783
|
const n = new Set(s)
|
|
709
784
|
n.delete(key)
|
|
@@ -714,7 +789,7 @@ function OneToManyRelation({
|
|
|
714
789
|
})
|
|
715
790
|
},
|
|
716
791
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
717
|
-
[api, dataEndpoint, refreshSilently, onChange],
|
|
792
|
+
[api, dataEndpoint, refreshSilently, onChange, normalizePositions],
|
|
718
793
|
)
|
|
719
794
|
|
|
720
795
|
const handleDelete = useCallback(async () => {
|
|
@@ -725,13 +800,14 @@ function OneToManyRelation({
|
|
|
725
800
|
if (!(res as any).data?.success) throw new Error('delete failed')
|
|
726
801
|
setRowToDelete(null)
|
|
727
802
|
await fetchAll()
|
|
803
|
+
void normalizePositions().then(() => refreshSilently())
|
|
728
804
|
onChange?.()
|
|
729
805
|
} catch (err) {
|
|
730
806
|
console.error('DynamicRelation delete error', err)
|
|
731
807
|
} finally {
|
|
732
808
|
setSubmitting(false)
|
|
733
809
|
}
|
|
734
|
-
}, [api, dataEndpoint, fetchAll, onChange, rowToDelete])
|
|
810
|
+
}, [api, dataEndpoint, fetchAll, onChange, rowToDelete, normalizePositions, refreshSilently])
|
|
735
811
|
|
|
736
812
|
return (
|
|
737
813
|
<div className={className} data-relation-kind={kind} data-relation-model={model}>
|
|
@@ -788,8 +864,9 @@ function OneToManyRelation({
|
|
|
788
864
|
canReorder={canEdit && !!positionKey}
|
|
789
865
|
canDelete={canDelete}
|
|
790
866
|
hiddenKeys={pendingDelete}
|
|
867
|
+
lead={galleryLead ? { src: getImageUrl(galleryLead) } : undefined}
|
|
791
868
|
onUpload={galleryUpload}
|
|
792
|
-
onUploadsSettled={() => { void refreshSilently(); onChange?.() }}
|
|
869
|
+
onUploadsSettled={() => { void normalizePositions().then(() => refreshSilently()); onChange?.() }}
|
|
793
870
|
onReorder={galleryReorder}
|
|
794
871
|
onEdit={canEdit ? (row) => { setEditingRow(row); uploadedImageRef.current = null; setFormOpen(true) } : undefined}
|
|
795
872
|
onDelete={canDelete ? (row) => setRowToDelete(row) : undefined}
|
package/src/index.ts
CHANGED
|
@@ -890,3 +890,15 @@ export {
|
|
|
890
890
|
type SplitActions,
|
|
891
891
|
} from './record-actions'
|
|
892
892
|
export * from './capability-providers'
|
|
893
|
+
export {
|
|
894
|
+
BulkImageImportButton,
|
|
895
|
+
BulkImageDialog,
|
|
896
|
+
type BulkImageImportButtonProps,
|
|
897
|
+
} from './bulk-image-dialog'
|
|
898
|
+
export {
|
|
899
|
+
buildPreview as buildBulkImagePreview,
|
|
900
|
+
parseImageName as parseBulkImageName,
|
|
901
|
+
deriveBulkImageTarget,
|
|
902
|
+
type BulkImageTarget,
|
|
903
|
+
type BatchLog as BulkImageBatchLog,
|
|
904
|
+
} from './bulk-image-import'
|
package/src/relation-gallery.tsx
CHANGED
|
@@ -63,6 +63,12 @@ export interface RelationGalleryProps {
|
|
|
63
63
|
onDelete?: (row: Record<string, any>) => void
|
|
64
64
|
/** Keys of rows pending deletion (hidden while «Deshacer» is offered). */
|
|
65
65
|
hiddenKeys?: Set<string>
|
|
66
|
+
/**
|
|
67
|
+
* The parent's own image, shown as a read-only first tile marked «Principal»
|
|
68
|
+
* while no gallery row holds it. Gallery rows are then additional photos:
|
|
69
|
+
* none of them is the main one, so «Hacer principal» is not offered.
|
|
70
|
+
*/
|
|
71
|
+
lead?: { src: string }
|
|
66
72
|
}
|
|
67
73
|
|
|
68
74
|
interface UploadItem {
|
|
@@ -97,6 +103,7 @@ export function RelationGallery({
|
|
|
97
103
|
onEdit,
|
|
98
104
|
onDelete,
|
|
99
105
|
hiddenKeys,
|
|
106
|
+
lead,
|
|
100
107
|
}: RelationGalleryProps) {
|
|
101
108
|
const { t } = useTranslation()
|
|
102
109
|
const inputRef = useRef<HTMLInputElement>(null)
|
|
@@ -337,10 +344,28 @@ export function RelationGallery({
|
|
|
337
344
|
</div>
|
|
338
345
|
)}
|
|
339
346
|
|
|
340
|
-
{keyed.length > 0 && (
|
|
347
|
+
{(keyed.length > 0 || lead) && (
|
|
341
348
|
<DndContext sensors={sensors} collisionDetection={closestCenter} onDragEnd={handleDragEnd}>
|
|
342
349
|
<SortableContext items={ids} strategy={rectSortingStrategy}>
|
|
343
350
|
<ul className="grid grid-cols-2 gap-3 sm:grid-cols-3 md:grid-cols-4" data-testid="gallery-grid">
|
|
351
|
+
{lead && (
|
|
352
|
+
<li
|
|
353
|
+
className="relative min-w-0 overflow-hidden rounded-lg border bg-card ring-2 ring-primary"
|
|
354
|
+
data-testid="gallery-lead"
|
|
355
|
+
data-main="true"
|
|
356
|
+
>
|
|
357
|
+
<div className="aspect-square w-full bg-muted" aria-label={`${t('dynamic.gallery.original', { defaultValue: 'Imagen original del producto' })} · ${mainLabel}`}>
|
|
358
|
+
<img src={lead.src} alt="" className="h-full w-full min-h-24 object-cover" draggable={false} />
|
|
359
|
+
</div>
|
|
360
|
+
<span className="absolute start-2 top-2 inline-flex items-center gap-1 rounded-full bg-primary px-2 py-0.5 text-xs font-medium text-primary-foreground">
|
|
361
|
+
<Star className="h-3 w-3" aria-hidden />
|
|
362
|
+
{mainLabel}
|
|
363
|
+
</span>
|
|
364
|
+
<div className="border-t bg-card px-2 py-1 text-xs text-muted-foreground">
|
|
365
|
+
{t('dynamic.gallery.original', { defaultValue: 'Imagen original del producto' })}
|
|
366
|
+
</div>
|
|
367
|
+
</li>
|
|
368
|
+
)}
|
|
344
369
|
{keyed.map(({ row, key }, index) => (
|
|
345
370
|
<GalleryTile
|
|
346
371
|
key={key}
|
|
@@ -349,7 +374,8 @@ export function RelationGallery({
|
|
|
349
374
|
caption={captionKey ? String(row[captionKey] ?? '') : ''}
|
|
350
375
|
index={index}
|
|
351
376
|
total={keyed.length}
|
|
352
|
-
isMain={index === 0}
|
|
377
|
+
isMain={!lead && index === 0}
|
|
378
|
+
canMakeMain={!lead}
|
|
353
379
|
mainLabel={mainLabel}
|
|
354
380
|
canEdit={canEdit}
|
|
355
381
|
canReorder={reorderable}
|
|
@@ -374,6 +400,7 @@ function GalleryTile({
|
|
|
374
400
|
index,
|
|
375
401
|
total,
|
|
376
402
|
isMain,
|
|
403
|
+
canMakeMain,
|
|
377
404
|
mainLabel,
|
|
378
405
|
canEdit,
|
|
379
406
|
canReorder,
|
|
@@ -388,6 +415,7 @@ function GalleryTile({
|
|
|
388
415
|
index: number
|
|
389
416
|
total: number
|
|
390
417
|
isMain: boolean
|
|
418
|
+
canMakeMain: boolean
|
|
391
419
|
mainLabel: string
|
|
392
420
|
canEdit: boolean
|
|
393
421
|
canReorder: boolean
|
|
@@ -404,7 +432,7 @@ function GalleryTile({
|
|
|
404
432
|
ref={setNodeRef}
|
|
405
433
|
style={{ transform: CSS.Transform.toString(transform), transition }}
|
|
406
434
|
className={cn(
|
|
407
|
-
'group relative overflow-hidden rounded-lg border bg-card',
|
|
435
|
+
'group relative min-w-0 overflow-hidden rounded-lg border bg-card',
|
|
408
436
|
isMain && 'ring-2 ring-primary',
|
|
409
437
|
isDragging && 'z-10 opacity-80 shadow-lg',
|
|
410
438
|
)}
|
|
@@ -428,8 +456,9 @@ function GalleryTile({
|
|
|
428
456
|
{mainLabel}
|
|
429
457
|
</span>
|
|
430
458
|
)}
|
|
431
|
-
|
|
432
|
-
|
|
459
|
+
{/* Wraps instead of clipping: a 140 px tile (or a phone) holds up to five icons. */}
|
|
460
|
+
<div className="flex flex-wrap items-center justify-between gap-x-1 border-t bg-card px-1 py-0.5" data-testid="gallery-tile-actions">
|
|
461
|
+
<div className="flex flex-wrap items-center">
|
|
433
462
|
{canReorder && (
|
|
434
463
|
<>
|
|
435
464
|
<Button
|
|
@@ -454,7 +483,7 @@ function GalleryTile({
|
|
|
454
483
|
>
|
|
455
484
|
<ArrowRight className="h-4 w-4 rtl:rotate-180" />
|
|
456
485
|
</Button>
|
|
457
|
-
{!isMain && (
|
|
486
|
+
{!isMain && canMakeMain && (
|
|
458
487
|
<Button
|
|
459
488
|
type="button"
|
|
460
489
|
size="sm"
|
|
@@ -470,7 +499,7 @@ function GalleryTile({
|
|
|
470
499
|
</>
|
|
471
500
|
)}
|
|
472
501
|
</div>
|
|
473
|
-
<div className="flex items-center">
|
|
502
|
+
<div className="flex flex-wrap items-center">
|
|
474
503
|
{canEdit && onEdit && (
|
|
475
504
|
<Button type="button" size="sm" variant="ghost" className="h-8 w-8 p-0" onClick={onEdit} aria-label={t('dynamic.gallery.edit', { defaultValue: 'Editar foto' })}>
|
|
476
505
|
<Pencil className="h-4 w-4" />
|