@telamonjs/ui 1.0.3 → 1.0.4
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 +15 -0
- package/dist/components/data/DataTableActions.js +85 -49
- package/dist/components/forms/admin-form/visibility.js +2 -0
- package/dist/exports/types-constants.d.ts +1 -1
- package/dist/exports/types-constants.js +1 -1
- package/dist/queue/components/QueueTabbedDashboard.d.ts +5 -0
- package/dist/queue/components/QueueTabbedDashboard.js +11 -2
- package/dist/queue/components/tabs/TempoRealTab.d.ts +5 -1
- package/dist/queue/components/tabs/TempoRealTab.js +11 -5
- package/dist/queue/hooks/use-queue-events-timeline.d.ts +19 -1
- package/dist/queue/hooks/use-queue-events-timeline.js +23 -3
- package/dist/server/pagination/factory.js +8 -1
- package/dist/server/pagination/types.d.ts +6 -0
- package/dist/server/pagination/utils.d.ts +13 -0
- package/dist/server/pagination/utils.js +27 -16
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,20 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 1.0.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 7177dfb: Server pagination, row actions, queue polling and form-path hardening:
|
|
8
|
+
|
|
9
|
+
- `createPaginatedService`: `baseWhere`, visibility rules and search/filters are now always combined with `AND`. Before, a `baseWhere` with its own `AND` replaced the search conditions, and a visibility rule with a top-level `AND`/`OR` dropped `baseWhere`'s other keys (e.g. `deletedAt: null`), which could show hidden rows.
|
|
10
|
+
- `createPaginatedService`: an invalid `sortOrder` falls back to `defaultOrderDirection` instead of reaching Prisma, and the new optional `sortableFields` option ignores any `sortBy` outside the list (the default sort applies).
|
|
11
|
+
- Row actions in the "…" menu now honour `confirmDialog` (including a standard delete moved to the menu by `display.mode: 'dropdown'`). They used to run straight away.
|
|
12
|
+
- `QueueTabbedDashboard`: the real-time timeline polls only as a 30s safety net while SSE is connected and stops polling while another tab is open. The new optional `pollInterval` prop (default 2000 ms) sets the interval used while SSE is down.
|
|
13
|
+
- Form visibility helpers ignore field paths that go through `__proto__`, `constructor` or `prototype`.
|
|
14
|
+
|
|
15
|
+
- Updated dependencies [a599b51]
|
|
16
|
+
- @telamonjs/logger@1.0.2
|
|
17
|
+
|
|
3
18
|
## 1.0.3
|
|
4
19
|
|
|
5
20
|
### Patch Changes
|
|
@@ -81,6 +81,49 @@ async function runAction({
|
|
|
81
81
|
onSettled?.();
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
|
+
function ConfirmActionDialog({
|
|
85
|
+
action,
|
|
86
|
+
row,
|
|
87
|
+
open,
|
|
88
|
+
onOpenChange,
|
|
89
|
+
onConfirm,
|
|
90
|
+
children
|
|
91
|
+
}) {
|
|
92
|
+
const t = useTelamonjsT();
|
|
93
|
+
const text = useText();
|
|
94
|
+
const confirmConfig = action.confirmDialog === true ? {
|
|
95
|
+
title: t("dialog.confirmActionTitle"),
|
|
96
|
+
description: t("dialog.confirmActionDescription"),
|
|
97
|
+
confirmText: t("common.confirm"),
|
|
98
|
+
cancelText: t("common.cancel"),
|
|
99
|
+
variant: "default"
|
|
100
|
+
} : action.confirmDialog || {};
|
|
101
|
+
return /* @__PURE__ */ jsxs(AlertDialog, { open, onOpenChange, children: [
|
|
102
|
+
children && /* @__PURE__ */ jsx(AlertDialogTrigger, { asChild: true, children }),
|
|
103
|
+
/* @__PURE__ */ jsxs(AlertDialogContent, { children: [
|
|
104
|
+
/* @__PURE__ */ jsxs(AlertDialogHeader, { children: [
|
|
105
|
+
/* @__PURE__ */ jsx(AlertDialogTitle, { children: text(
|
|
106
|
+
typeof confirmConfig.title === "function" ? confirmConfig.title(row) : confirmConfig.title
|
|
107
|
+
) }),
|
|
108
|
+
confirmConfig.description && /* @__PURE__ */ jsx(AlertDialogDescription, { children: text(
|
|
109
|
+
typeof confirmConfig.description === "function" ? confirmConfig.description(row) : confirmConfig.description
|
|
110
|
+
) })
|
|
111
|
+
] }),
|
|
112
|
+
/* @__PURE__ */ jsxs(AlertDialogFooter, { children: [
|
|
113
|
+
/* @__PURE__ */ jsx(AlertDialogCancel, { "data-testid": "modal-cancel", children: text(confirmConfig.cancelText) || t("common.cancel") }),
|
|
114
|
+
/* @__PURE__ */ jsx(
|
|
115
|
+
AlertDialogAction,
|
|
116
|
+
{
|
|
117
|
+
"data-testid": "modal-confirm",
|
|
118
|
+
className: confirmConfig.variant === "destructive" ? "bg-red-500 text-white hover:bg-red-600" : "",
|
|
119
|
+
onClick: onConfirm,
|
|
120
|
+
children: text(confirmConfig.confirmText) || t("common.confirm")
|
|
121
|
+
}
|
|
122
|
+
)
|
|
123
|
+
] })
|
|
124
|
+
] })
|
|
125
|
+
] });
|
|
126
|
+
}
|
|
84
127
|
function ActionButton({
|
|
85
128
|
action,
|
|
86
129
|
row,
|
|
@@ -159,20 +202,6 @@ function ActionButton({
|
|
|
159
202
|
}
|
|
160
203
|
}, [action.confirmDialog, executeAction]);
|
|
161
204
|
if (!isVisible) return null;
|
|
162
|
-
const getConfirmDialogConfig = () => {
|
|
163
|
-
if (!action.confirmDialog) return {};
|
|
164
|
-
if (action.confirmDialog === true) {
|
|
165
|
-
return {
|
|
166
|
-
title: t("dialog.confirmActionTitle"),
|
|
167
|
-
description: t("dialog.confirmActionDescription"),
|
|
168
|
-
confirmText: t("common.confirm"),
|
|
169
|
-
cancelText: t("common.cancel"),
|
|
170
|
-
variant: "default"
|
|
171
|
-
};
|
|
172
|
-
}
|
|
173
|
-
return action.confirmDialog;
|
|
174
|
-
};
|
|
175
|
-
const confirmConfig = getConfirmDialogConfig();
|
|
176
205
|
const buttonContent = /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
177
206
|
action.icon && !isActionLoading && /* @__PURE__ */ jsx(action.icon, {}),
|
|
178
207
|
resolvedLabel && /* @__PURE__ */ jsx("span", { className: "ml-1.5", children: resolvedLabel }),
|
|
@@ -203,31 +232,17 @@ function ActionButton({
|
|
|
203
232
|
const withDisabledHint = (node) => !isEnabled && tooltipContent ? /* @__PURE__ */ jsx("span", { className: "inline-flex", "data-tooltip-text": tooltipContent, children: node }) : node;
|
|
204
233
|
if (action.confirmDialog) {
|
|
205
234
|
return withDisabledHint(
|
|
206
|
-
/* @__PURE__ */
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
/* @__PURE__ */ jsxs(AlertDialogFooter, { children: [
|
|
218
|
-
/* @__PURE__ */ jsx(AlertDialogCancel, { "data-testid": "modal-cancel", children: text(confirmConfig.cancelText) || t("common.cancel") }),
|
|
219
|
-
/* @__PURE__ */ jsx(
|
|
220
|
-
AlertDialogAction,
|
|
221
|
-
{
|
|
222
|
-
"data-testid": "modal-confirm",
|
|
223
|
-
className: confirmConfig.variant === "destructive" ? "bg-red-500 text-white hover:bg-red-600" : "",
|
|
224
|
-
onClick: executeAction,
|
|
225
|
-
children: text(confirmConfig.confirmText) || t("common.confirm")
|
|
226
|
-
}
|
|
227
|
-
)
|
|
228
|
-
] })
|
|
229
|
-
] })
|
|
230
|
-
] })
|
|
235
|
+
/* @__PURE__ */ jsx(
|
|
236
|
+
ConfirmActionDialog,
|
|
237
|
+
{
|
|
238
|
+
action,
|
|
239
|
+
row,
|
|
240
|
+
open: confirmDialogOpen,
|
|
241
|
+
onOpenChange: setConfirmDialogOpen,
|
|
242
|
+
onConfirm: executeAction,
|
|
243
|
+
children: button
|
|
244
|
+
}
|
|
245
|
+
)
|
|
231
246
|
);
|
|
232
247
|
}
|
|
233
248
|
return withDisabledHint(button);
|
|
@@ -331,6 +346,19 @@ function DataTableActions({
|
|
|
331
346
|
)
|
|
332
347
|
};
|
|
333
348
|
}, [allActions, config.userPermissions]);
|
|
349
|
+
const [pendingConfirm, setPendingConfirm] = useState(null);
|
|
350
|
+
const runMenuAction = useCallback(
|
|
351
|
+
(actionId, action) => void runAction({
|
|
352
|
+
action,
|
|
353
|
+
row: row.original,
|
|
354
|
+
actionId,
|
|
355
|
+
router,
|
|
356
|
+
onActionComplete: handleActionComplete,
|
|
357
|
+
text,
|
|
358
|
+
fallbackError: t("toast.unknownError")
|
|
359
|
+
}),
|
|
360
|
+
[row.original, router, handleActionComplete, text, t]
|
|
361
|
+
);
|
|
334
362
|
const displayMode = config.display?.mode || "inline";
|
|
335
363
|
const maxInline = config.display?.maxInline || 999;
|
|
336
364
|
const spacing = config.display?.spacing || "normal";
|
|
@@ -395,15 +423,11 @@ function DataTableActions({
|
|
|
395
423
|
disabled: !isEnabled,
|
|
396
424
|
onClick: (event) => {
|
|
397
425
|
event.stopPropagation();
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
onActionComplete: handleActionComplete,
|
|
404
|
-
text,
|
|
405
|
-
fallbackError: t("toast.unknownError")
|
|
406
|
-
});
|
|
426
|
+
if (actionConfig.confirmDialog) {
|
|
427
|
+
setPendingConfirm({ id, config: actionConfig });
|
|
428
|
+
} else {
|
|
429
|
+
runMenuAction(id, actionConfig);
|
|
430
|
+
}
|
|
407
431
|
},
|
|
408
432
|
children: [
|
|
409
433
|
actionConfig.icon && /* @__PURE__ */ jsx("span", { className: "mr-2 h-4 w-4 shrink-0 [&_svg]:size-4", children: React.createElement(actionConfig.icon) }),
|
|
@@ -421,7 +445,19 @@ function DataTableActions({
|
|
|
421
445
|
)
|
|
422
446
|
] }, id);
|
|
423
447
|
}) })
|
|
424
|
-
] })
|
|
448
|
+
] }),
|
|
449
|
+
pendingConfirm && /* @__PURE__ */ jsx(
|
|
450
|
+
ConfirmActionDialog,
|
|
451
|
+
{
|
|
452
|
+
action: pendingConfirm.config,
|
|
453
|
+
row: row.original,
|
|
454
|
+
open: true,
|
|
455
|
+
onOpenChange: (open) => {
|
|
456
|
+
if (!open) setPendingConfirm(null);
|
|
457
|
+
},
|
|
458
|
+
onConfirm: () => runMenuAction(pendingConfirm.id, pendingConfirm.config)
|
|
459
|
+
}
|
|
460
|
+
)
|
|
425
461
|
] });
|
|
426
462
|
}
|
|
427
463
|
export {
|
|
@@ -19,8 +19,10 @@ function collectHiddenFieldNames(config, values) {
|
|
|
19
19
|
}
|
|
20
20
|
return hidden;
|
|
21
21
|
}
|
|
22
|
+
var UNSAFE_PATH_SEGMENTS = /* @__PURE__ */ new Set(["__proto__", "constructor", "prototype"]);
|
|
22
23
|
function deleteAtPath(obj, path) {
|
|
23
24
|
const parts = path.split(".");
|
|
25
|
+
if (parts.some((p) => UNSAFE_PATH_SEGMENTS.has(p))) return;
|
|
24
26
|
let target = obj;
|
|
25
27
|
for (let i = 0; i < parts.length - 1; i++) {
|
|
26
28
|
const next = target[parts[i]];
|
|
@@ -7,4 +7,4 @@ export type { ModuleMetadata, ListConfig, CrudColumnDef, FormConfig, QueryConfig
|
|
|
7
7
|
export type { ColorPreset, StatItem, HeaderBadgeConfig, HeaderStatsConfig, DialogHeaderConfig, DialogHeaderProps, StatsGridProps, } from '../types/dialog-header';
|
|
8
8
|
export type { MenuItem, SingleMenuItem, GroupMenuItem, SeparatorMenuItem, MenuItemType, MenuVariant, IconComponent, } from '../types/menu';
|
|
9
9
|
export type { Density, GapSize, DialogSize, DialogHeight, FieldLayout, CardFieldLayout, } from '../utils/spacing';
|
|
10
|
-
export declare const version = "1.0.
|
|
10
|
+
export declare const version = "1.0.4";
|
|
@@ -15,6 +15,11 @@ export interface QueueTabbedDashboardProps {
|
|
|
15
15
|
moduleIcons?: Record<string, string>;
|
|
16
16
|
/** Enable SSE real-time updates (default: true) */
|
|
17
17
|
enableSSE?: boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Real-time timeline poll interval in ms while SSE is down (default: 2000).
|
|
20
|
+
* With SSE connected it polls every 30s; off the Real-time tab, not at all.
|
|
21
|
+
*/
|
|
22
|
+
pollInterval?: number;
|
|
18
23
|
/** Callback when tab changes (for URL sync) */
|
|
19
24
|
onTabChange?: (tab: string) => void;
|
|
20
25
|
/** Sync active tab with URL searchParams (default: false) */
|
|
@@ -27,6 +27,7 @@ function QueueTabbedDashboardCore({
|
|
|
27
27
|
icon,
|
|
28
28
|
moduleIcons,
|
|
29
29
|
enableSSE = true,
|
|
30
|
+
pollInterval,
|
|
30
31
|
onTabChange
|
|
31
32
|
}) {
|
|
32
33
|
const t = useTelamonjsT();
|
|
@@ -49,7 +50,15 @@ function QueueTabbedDashboardCore({
|
|
|
49
50
|
label: t("queue.tabs.realtime"),
|
|
50
51
|
shortLabel: t("queue.tabs.realtimeShort"),
|
|
51
52
|
icon: Radio,
|
|
52
|
-
content: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(PageSkeleton.TabContent, {}), children: /* @__PURE__ */ jsx(
|
|
53
|
+
content: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(PageSkeleton.TabContent, {}), children: /* @__PURE__ */ jsx(
|
|
54
|
+
TempoRealTab,
|
|
55
|
+
{
|
|
56
|
+
enableSSE,
|
|
57
|
+
moduleIcons,
|
|
58
|
+
active: activeTab === "tempo-real",
|
|
59
|
+
pollInterval
|
|
60
|
+
}
|
|
61
|
+
) })
|
|
53
62
|
},
|
|
54
63
|
{
|
|
55
64
|
value: "configuracao",
|
|
@@ -73,7 +82,7 @@ function QueueTabbedDashboardCore({
|
|
|
73
82
|
content: /* @__PURE__ */ jsx(Suspense, { fallback: /* @__PURE__ */ jsx(PageSkeleton.TabContent, {}), children: /* @__PURE__ */ jsx(HistoricoTab, {}) })
|
|
74
83
|
}
|
|
75
84
|
],
|
|
76
|
-
[moduleIcons, enableSSE, t]
|
|
85
|
+
[moduleIcons, enableSSE, pollInterval, activeTab, t]
|
|
77
86
|
);
|
|
78
87
|
const filteredTabs = useMemo(() => {
|
|
79
88
|
if (!visibleTabs) return allTabs;
|
|
@@ -4,6 +4,10 @@ interface TempoRealTabProps {
|
|
|
4
4
|
enableSSE?: boolean;
|
|
5
5
|
/** Module icons for the queues table (module name → emoji) */
|
|
6
6
|
moduleIcons?: Record<string, string>;
|
|
7
|
+
/** This tab is the visible one — visited tabs stay mounted (default: true) */
|
|
8
|
+
active?: boolean;
|
|
9
|
+
/** Timeline poll interval in ms while SSE is down (default: 2000) */
|
|
10
|
+
pollInterval?: number;
|
|
7
11
|
}
|
|
8
|
-
export default function TempoRealTab({ enableSSE, moduleIcons, }: TempoRealTabProps): React.JSX.Element;
|
|
12
|
+
export default function TempoRealTab({ enableSSE, moduleIcons, active, pollInterval, }: TempoRealTabProps): React.JSX.Element;
|
|
9
13
|
export {};
|
|
@@ -391,7 +391,9 @@ var JobDetailPanel = React.memo(function JobDetailPanel2({
|
|
|
391
391
|
});
|
|
392
392
|
function TempoRealTab({
|
|
393
393
|
enableSSE = true,
|
|
394
|
-
moduleIcons
|
|
394
|
+
moduleIcons,
|
|
395
|
+
active = true,
|
|
396
|
+
pollInterval
|
|
395
397
|
}) {
|
|
396
398
|
"use no memo";
|
|
397
399
|
const t = useTelamonjsT();
|
|
@@ -408,7 +410,11 @@ function TempoRealTab({
|
|
|
408
410
|
isError,
|
|
409
411
|
isFetching,
|
|
410
412
|
refetch: refetchTimeline
|
|
411
|
-
} = useQueueEventsTimeline(
|
|
413
|
+
} = useQueueEventsTimeline({
|
|
414
|
+
sseConnected: sseStatus === "connected",
|
|
415
|
+
active,
|
|
416
|
+
pollInterval
|
|
417
|
+
});
|
|
412
418
|
const handleQueueAction = useCallback(
|
|
413
419
|
async (action) => {
|
|
414
420
|
try {
|
|
@@ -448,13 +454,13 @@ function TempoRealTab({
|
|
|
448
454
|
const search = deferredSearch.toLowerCase();
|
|
449
455
|
return job.name.toLowerCase().includes(search) || job.queueName.toLowerCase().includes(search);
|
|
450
456
|
}) : allJobs;
|
|
451
|
-
let
|
|
457
|
+
let active2 = 0, completed = 0, failed = 0;
|
|
452
458
|
for (const job of searchFiltered) {
|
|
453
|
-
if (job.status === "active")
|
|
459
|
+
if (job.status === "active") active2++;
|
|
454
460
|
else if (job.status === "completed") completed++;
|
|
455
461
|
else if (job.status === "failed") failed++;
|
|
456
462
|
}
|
|
457
|
-
return { total: searchFiltered.length, active, completed, failed };
|
|
463
|
+
return { total: searchFiltered.length, active: active2, completed, failed };
|
|
458
464
|
}, [allJobs, deferredSearch]);
|
|
459
465
|
const selectedJob = useMemo(() => {
|
|
460
466
|
if (!selectedJobId) return null;
|
|
@@ -8,6 +8,24 @@ import type { TimelineJob } from '../types';
|
|
|
8
8
|
export interface TimelineData {
|
|
9
9
|
jobs: TimelineJob[];
|
|
10
10
|
}
|
|
11
|
+
export interface QueueEventsTimelineOptions {
|
|
12
|
+
/** SSE stream is live (it already pushes events into this query's cache) */
|
|
13
|
+
sseConnected?: boolean;
|
|
14
|
+
/** The view showing the timeline is the active one (default: true) */
|
|
15
|
+
active?: boolean;
|
|
16
|
+
/** Poll interval in ms while SSE is down (default: 2000) */
|
|
17
|
+
pollInterval?: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Poll interval for the timeline: none while hidden or on another tab, a
|
|
21
|
+
* 30s safety net while SSE is live, otherwise `pollInterval` (2s default).
|
|
22
|
+
*/
|
|
23
|
+
export declare function timelinePollInterval({ isVisible, active, sseConnected, pollInterval, }: {
|
|
24
|
+
isVisible: boolean;
|
|
25
|
+
active: boolean;
|
|
26
|
+
sseConnected: boolean;
|
|
27
|
+
pollInterval?: number;
|
|
28
|
+
}): number | false;
|
|
11
29
|
/**
|
|
12
30
|
* Hook for real-time job timeline events.
|
|
13
31
|
*
|
|
@@ -20,4 +38,4 @@ export interface TimelineData {
|
|
|
20
38
|
* const { data: jobs } = useQueueEventsTimeline()
|
|
21
39
|
* ```
|
|
22
40
|
*/
|
|
23
|
-
export declare function useQueueEventsTimeline(): import("@tanstack/react-query").UseQueryResult<TimelineData, Error>;
|
|
41
|
+
export declare function useQueueEventsTimeline({ sseConnected, active, pollInterval, }?: QueueEventsTimelineOptions): import("@tanstack/react-query").UseQueryResult<TimelineData, Error>;
|
|
@@ -3,7 +3,22 @@ import { useQuery } from "@tanstack/react-query";
|
|
|
3
3
|
import { useQueueApi } from "../api/provider.js";
|
|
4
4
|
import { usePageVisibility } from "../../hooks/usePageVisibility.js";
|
|
5
5
|
import { LIB_QUERY_META } from "../../utils/query-meta.js";
|
|
6
|
-
|
|
6
|
+
var SSE_SAFETY_NET_INTERVAL = 3e4;
|
|
7
|
+
var DEFAULT_POLL_INTERVAL = 2e3;
|
|
8
|
+
function timelinePollInterval({
|
|
9
|
+
isVisible,
|
|
10
|
+
active,
|
|
11
|
+
sseConnected,
|
|
12
|
+
pollInterval = DEFAULT_POLL_INTERVAL
|
|
13
|
+
}) {
|
|
14
|
+
if (!isVisible || !active) return false;
|
|
15
|
+
return sseConnected ? SSE_SAFETY_NET_INTERVAL : pollInterval;
|
|
16
|
+
}
|
|
17
|
+
function useQueueEventsTimeline({
|
|
18
|
+
sseConnected = false,
|
|
19
|
+
active = true,
|
|
20
|
+
pollInterval
|
|
21
|
+
} = {}) {
|
|
7
22
|
const api = useQueueApi();
|
|
8
23
|
const isVisible = usePageVisibility();
|
|
9
24
|
return useQuery({
|
|
@@ -94,10 +109,15 @@ function useQueueEventsTimeline() {
|
|
|
94
109
|
};
|
|
95
110
|
},
|
|
96
111
|
staleTime: 1e3,
|
|
97
|
-
|
|
98
|
-
|
|
112
|
+
refetchInterval: timelinePollInterval({
|
|
113
|
+
isVisible,
|
|
114
|
+
active,
|
|
115
|
+
sseConnected,
|
|
116
|
+
pollInterval
|
|
117
|
+
})
|
|
99
118
|
});
|
|
100
119
|
}
|
|
101
120
|
export {
|
|
121
|
+
timelinePollInterval,
|
|
102
122
|
useQueueEventsTimeline
|
|
103
123
|
};
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
buildSearchConditions,
|
|
11
11
|
buildWhereClause,
|
|
12
12
|
getFilterOptionsForField,
|
|
13
|
+
resolveSort,
|
|
13
14
|
sanitizePaginationParams
|
|
14
15
|
} from "./utils.js";
|
|
15
16
|
import { logger } from "../../utils/logger.js";
|
|
@@ -25,6 +26,7 @@ function createPaginationContext(context) {
|
|
|
25
26
|
select,
|
|
26
27
|
defaultOrderBy = "id",
|
|
27
28
|
defaultOrderDirection = "asc",
|
|
29
|
+
sortableFields,
|
|
28
30
|
visibility,
|
|
29
31
|
counts,
|
|
30
32
|
baseWhere,
|
|
@@ -79,7 +81,12 @@ function createPaginationContext(context) {
|
|
|
79
81
|
};
|
|
80
82
|
where = beforeQuery(queryParams, where);
|
|
81
83
|
}
|
|
82
|
-
const
|
|
84
|
+
const sort = resolveSort(sortBy, sortOrder, {
|
|
85
|
+
defaultOrderBy,
|
|
86
|
+
defaultOrderDirection,
|
|
87
|
+
sortableFields
|
|
88
|
+
});
|
|
89
|
+
const orderBy = buildOrderByClause(sort.sortBy, sort.sortOrder);
|
|
83
90
|
const includeClause = buildIncludeClause(include, counts);
|
|
84
91
|
const queryOptions = {
|
|
85
92
|
where,
|
|
@@ -55,6 +55,12 @@ export interface PaginatedServiceConfig<TModel extends string = string> {
|
|
|
55
55
|
select?: Record<string, boolean | Record<string, unknown>>;
|
|
56
56
|
defaultOrderBy?: string | string[];
|
|
57
57
|
defaultOrderDirection?: 'asc' | 'desc';
|
|
58
|
+
/**
|
|
59
|
+
* Fields the client may sort by (as sent in `sortBy`, e.g. `'customer.name'`).
|
|
60
|
+
* When set, any other `sortBy` is ignored and the default sort applies.
|
|
61
|
+
* When absent, any field is accepted.
|
|
62
|
+
*/
|
|
63
|
+
sortableFields?: string[];
|
|
58
64
|
visibility?: (user: AuthUser) => PrismaWhereClause;
|
|
59
65
|
counts?: string[];
|
|
60
66
|
baseWhere?: PrismaWhereClause;
|
|
@@ -20,6 +20,19 @@ export declare function buildFilterConditions(filters: Record<string, FilterValu
|
|
|
20
20
|
* Build complete where clause combining all conditions
|
|
21
21
|
*/
|
|
22
22
|
export declare function buildWhereClause(searchConditions: SearchCondition | undefined, filterConditions: PrismaWhereClause[], baseWhere?: PrismaWhereClause, visibilityWhere?: PrismaWhereClause): PrismaWhereClause;
|
|
23
|
+
/**
|
|
24
|
+
* Sanitize a client-supplied sort: an invalid direction becomes the default
|
|
25
|
+
* one, and with `sortableFields` set, fields outside it are dropped (an empty
|
|
26
|
+
* result falls back to the default sort).
|
|
27
|
+
*/
|
|
28
|
+
export declare function resolveSort(sortBy: string | string[] | SortField[], sortOrder: unknown, defaults: {
|
|
29
|
+
defaultOrderBy: string | string[];
|
|
30
|
+
defaultOrderDirection: 'asc' | 'desc';
|
|
31
|
+
sortableFields?: string[];
|
|
32
|
+
}): {
|
|
33
|
+
sortBy: string | string[] | SortField[];
|
|
34
|
+
sortOrder: 'asc' | 'desc';
|
|
35
|
+
};
|
|
23
36
|
/**
|
|
24
37
|
* Build orderBy clause from sortBy and sortOrder
|
|
25
38
|
*/
|
|
@@ -94,23 +94,33 @@ function buildWhereClause(searchConditions, filterConditions, baseWhere, visibil
|
|
|
94
94
|
conditions.push(searchConditions);
|
|
95
95
|
}
|
|
96
96
|
conditions.push(...filterConditions);
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
97
|
+
const parts = [
|
|
98
|
+
conditions.length > 0 ? { AND: conditions } : void 0,
|
|
99
|
+
baseWhere,
|
|
100
|
+
visibilityWhere
|
|
101
|
+
].filter(
|
|
102
|
+
(part) => !!part && Object.keys(part).length > 0
|
|
103
|
+
);
|
|
104
|
+
if (parts.length === 0) return {};
|
|
105
|
+
return parts.length === 1 ? parts[0] : { AND: parts };
|
|
106
|
+
}
|
|
107
|
+
var isSortOrder = (value) => value === "asc" || value === "desc";
|
|
108
|
+
function resolveSort(sortBy, sortOrder, defaults) {
|
|
109
|
+
const { defaultOrderBy, defaultOrderDirection, sortableFields } = defaults;
|
|
110
|
+
const fallback = { sortBy: defaultOrderBy, sortOrder: defaultOrderDirection };
|
|
111
|
+
const allowed = (field) => !sortableFields || sortableFields.includes(field);
|
|
112
|
+
const order = isSortOrder(sortOrder) ? sortOrder : defaultOrderDirection;
|
|
113
|
+
if (sortBy === defaultOrderBy) return { sortBy, sortOrder: order };
|
|
114
|
+
if (typeof sortBy === "string") {
|
|
115
|
+
return allowed(sortBy) ? { sortBy, sortOrder: order } : fallback;
|
|
112
116
|
}
|
|
113
|
-
|
|
117
|
+
const fields = sortBy.filter(
|
|
118
|
+
(s) => !!s && typeof s === "object" && typeof s.field === "string" && allowed(s.field)
|
|
119
|
+
).map((s) => ({
|
|
120
|
+
field: s.field,
|
|
121
|
+
order: isSortOrder(s.order) ? s.order : defaultOrderDirection
|
|
122
|
+
}));
|
|
123
|
+
return fields.length > 0 ? { sortBy: fields, sortOrder: order } : fallback;
|
|
114
124
|
}
|
|
115
125
|
function buildOrderByClause(sortBy, sortOrder) {
|
|
116
126
|
if (!sortBy) {
|
|
@@ -193,5 +203,6 @@ export {
|
|
|
193
203
|
buildSearchConditions,
|
|
194
204
|
buildWhereClause,
|
|
195
205
|
getFilterOptionsForField,
|
|
206
|
+
resolveSort,
|
|
196
207
|
sanitizePaginationParams
|
|
197
208
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@telamonjs/ui",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Admin framework for Next.js + React: declarative CRUD (CrudConfig + TanStack Query), 36-type field system, rich dialogs, app shell, dashboards and charts",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -134,7 +134,7 @@
|
|
|
134
134
|
"tailwind-merge": "^3.3.1",
|
|
135
135
|
"tailwindcss-animate": "^1.0.7",
|
|
136
136
|
"zustand": "^5.0.8",
|
|
137
|
-
"@telamonjs/logger": "^1.0.
|
|
137
|
+
"@telamonjs/logger": "^1.0.2"
|
|
138
138
|
},
|
|
139
139
|
"peerDependencies": {
|
|
140
140
|
"@prisma/client": "^5.0.0 || ^6.0.0",
|