@carlonicora/nextjs-jsonapi 3.8.0 → 3.8.2
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/dist/{BlockNoteEditor-F6P6SW3F.mjs → BlockNoteEditor-34AZITDR.mjs} +2 -2
- package/dist/{BlockNoteEditor-T7DCBLNO.js → BlockNoteEditor-KRGDA5RX.js} +9 -9
- package/dist/{BlockNoteEditor-T7DCBLNO.js.map → BlockNoteEditor-KRGDA5RX.js.map} +1 -1
- package/dist/billing/index.js +333 -333
- package/dist/billing/index.mjs +1 -1
- package/dist/{chunk-CDO4ZUOA.js → chunk-DGAVM2LZ.js} +12 -4
- package/dist/chunk-DGAVM2LZ.js.map +1 -0
- package/dist/{chunk-XUDZOANZ.mjs → chunk-GXSXD4OR.mjs} +13 -5
- package/dist/chunk-GXSXD4OR.mjs.map +1 -0
- package/dist/client/index.js +2 -2
- package/dist/client/index.mjs +1 -1
- package/dist/components/index.js +2 -2
- package/dist/components/index.mjs +1 -1
- package/dist/contexts/index.js +2 -2
- package/dist/contexts/index.mjs +1 -1
- package/dist/features/help/index.js +31 -31
- package/dist/features/help/index.mjs +1 -1
- package/dist/features/tokenusage/index.js +75 -75
- package/dist/features/tokenusage/index.mjs +1 -1
- package/package.json +1 -1
- package/src/features/user/contexts/CurrentUserContext.tsx +24 -0
- package/src/features/user/contexts/__tests__/CurrentUserContext.spec.tsx +40 -0
- package/src/hooks/__tests__/usePushNotifications.spec.tsx +70 -0
- package/src/hooks/usePushNotifications.ts +6 -1
- package/dist/chunk-CDO4ZUOA.js.map +0 -1
- package/dist/chunk-XUDZOANZ.mjs.map +0 -1
- /package/dist/{BlockNoteEditor-F6P6SW3F.mjs.map → BlockNoteEditor-34AZITDR.mjs.map} +0 -0
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
|
|
27
27
|
|
|
28
28
|
|
|
29
|
-
var
|
|
29
|
+
var _chunkDGAVM2LZjs = require('../../chunk-DGAVM2LZ.js');
|
|
30
30
|
require('../../chunk-TULJCLZA.js');
|
|
31
31
|
|
|
32
32
|
|
|
@@ -70,7 +70,7 @@ function TokenUsageAdminFilterBar({ granularity, companyId, metric, companies, o
|
|
|
70
70
|
};
|
|
71
71
|
return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "flex flex-wrap items-center gap-2", children: [
|
|
72
72
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
73
|
-
|
|
73
|
+
_chunkDGAVM2LZjs.DateRangeSelector,
|
|
74
74
|
{
|
|
75
75
|
onDateChange: (range) => {
|
|
76
76
|
if (!_optionalChain([range, 'optionalAccess', _ => _.from]) || !_optionalChain([range, 'optionalAccess', _2 => _2.to])) return;
|
|
@@ -105,16 +105,16 @@ function TokenUsageAdminFilterBar({ granularity, companyId, metric, companies, o
|
|
|
105
105
|
}
|
|
106
106
|
),
|
|
107
107
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
108
|
-
|
|
108
|
+
_chunkDGAVM2LZjs.Select,
|
|
109
109
|
{
|
|
110
110
|
items: companyItems,
|
|
111
111
|
value: _nullishCoalesce(companyId, () => ( ALL_COMPANIES)),
|
|
112
112
|
onValueChange: (value) => onChange({ companyId: !value || value === ALL_COMPANIES ? void 0 : value }),
|
|
113
113
|
children: [
|
|
114
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
115
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
116
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
117
|
-
companies.map((company) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
114
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectTrigger, { className: "w-56", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectValue, { placeholder: t("token_usage.admin.all_companies") }) }),
|
|
115
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.SelectContent, { children: [
|
|
116
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectItem, { value: ALL_COMPANIES, children: t("token_usage.admin.all_companies") }),
|
|
117
|
+
companies.map((company) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectItem, { value: company.id, children: company.label }, company.id))
|
|
118
118
|
] })
|
|
119
119
|
]
|
|
120
120
|
}
|
|
@@ -137,7 +137,7 @@ function Segmented({
|
|
|
137
137
|
children: options.map((option) => {
|
|
138
138
|
const selected = option.value === value;
|
|
139
139
|
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
140
|
-
|
|
140
|
+
_chunkDGAVM2LZjs.Button,
|
|
141
141
|
{
|
|
142
142
|
type: "button",
|
|
143
143
|
size: "sm",
|
|
@@ -219,7 +219,7 @@ var TokenUsageAdminProvider = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void
|
|
|
219
219
|
pageUrl = TOKEN_USAGE_ADMIN_PAGE_URL
|
|
220
220
|
}) => {
|
|
221
221
|
const t = _nextintl.useTranslations.call(void 0, );
|
|
222
|
-
const generateUrl =
|
|
222
|
+
const generateUrl = _chunkDGAVM2LZjs.usePageUrlGenerator.call(void 0, );
|
|
223
223
|
const [filters, setFilterState] = _react.useState.call(void 0, () => {
|
|
224
224
|
const range = defaultRange();
|
|
225
225
|
return {
|
|
@@ -337,7 +337,7 @@ var TokenUsageAdminProvider = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void
|
|
|
337
337
|
error
|
|
338
338
|
]
|
|
339
339
|
);
|
|
340
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
340
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SharedProvider, { value: { breadcrumbs: breadcrumb(), title: title() }, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageAdminContext.Provider, { value: contextValue, children }) });
|
|
341
341
|
}, "TokenUsageAdminProvider");
|
|
342
342
|
var useTokenUsageAdmin = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void 0, () => {
|
|
343
343
|
const ctx = _react.useContext.call(void 0, TokenUsageAdminContext);
|
|
@@ -497,7 +497,7 @@ function LeadTile({
|
|
|
497
497
|
previousLabel,
|
|
498
498
|
decimal
|
|
499
499
|
}) {
|
|
500
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
500
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": testId, children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
501
501
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: label }),
|
|
502
502
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-primary text-xl font-semibold tabular-nums", children: value }),
|
|
503
503
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "span", { className: "flex items-center gap-1", children: [
|
|
@@ -514,7 +514,7 @@ function SupportingTile({
|
|
|
514
514
|
delta,
|
|
515
515
|
decimal
|
|
516
516
|
}) {
|
|
517
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
517
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": testId, size: "sm", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
518
518
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: label }),
|
|
519
519
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "span", { className: "flex items-center gap-2", children: [
|
|
520
520
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-sm font-medium tabular-nums", children: value }),
|
|
@@ -611,8 +611,8 @@ function TokenUsageBreakdownTable({ rows, metric }) {
|
|
|
611
611
|
// The package's <Table> wraps itself in an `overflow-x-clip` container, which
|
|
612
612
|
// would swallow the overflow before this scroller ever saw it — hence the
|
|
613
613
|
// child override. Wide metric tables must scroll, never widen the page.
|
|
614
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "overflow-x-auto [&_[data-slot=table-container]]:overflow-x-visible", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
615
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
614
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "overflow-x-auto [&_[data-slot=table-container]]:overflow-x-visible", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Table, { children: [
|
|
615
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableRow, { children: columns.map((column) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableHead, { className: _chunkUYLS3LJWjs.cn.call(void 0, column.numeric && "text-end"), children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
616
616
|
"button",
|
|
617
617
|
{
|
|
618
618
|
type: "button",
|
|
@@ -627,20 +627,20 @@ function TokenUsageBreakdownTable({ rows, metric }) {
|
|
|
627
627
|
]
|
|
628
628
|
}
|
|
629
629
|
) }, column.key)) }) }),
|
|
630
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
630
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableBody, { children: sortedRows.map((row) => {
|
|
631
631
|
const value = metricValue(row, metric);
|
|
632
632
|
const share = total > 0 ? value / total * 100 : 0;
|
|
633
|
-
return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
634
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
635
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
636
|
-
showActiveUsers && /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
637
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
638
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
639
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
640
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
641
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
642
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
643
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
633
|
+
return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.TableRow, { "data-testid": `breakdown-row-${row.id}`, children: [
|
|
634
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-xs", children: row.id === "other" ? t("token_usage.admin.other") : row.label }),
|
|
635
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-muted-foreground text-xs", children: _nullishCoalesce(row.sublabel, () => ( "")) }),
|
|
636
|
+
showActiveUsers && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: row.activeUsers === void 0 ? "" : decimal(row.activeUsers, 0) }),
|
|
637
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: decimal(row.calls, 0) }),
|
|
638
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: decimal(row.tokensIn, 0) }),
|
|
639
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: decimal(row.tokensOut, 0) }),
|
|
640
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: percent(cacheHitPercentage(row.cached, row.tokensIn)) }),
|
|
641
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: formatValue(row.cost, "cost") }),
|
|
642
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: formatValue(row.credits, "credits") }),
|
|
643
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.TableCell, { className: "text-end text-xs tabular-nums", children: percent(share) })
|
|
644
644
|
] }, row.id);
|
|
645
645
|
}) })
|
|
646
646
|
] }) })
|
|
@@ -838,7 +838,7 @@ function TokenUsageTimelineChart({ rows, metric, stackBy, className }) {
|
|
|
838
838
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className, dir: "ltr", children: [
|
|
839
839
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "sr-only", "data-testid": "timeline-data", children: JSON.stringify(chartData) }),
|
|
840
840
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "sr-only", "data-testid": "timeline-series", children: JSON.stringify(seriesKeys) }),
|
|
841
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
841
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.ChartContainer, { config: chartConfig, className: "aspect-auto h-72 w-full", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _recharts.BarChart, { accessibilityLayer: true, data: chartData, margin: { top: 8, right: 8, bottom: 0, left: 0 }, children: [
|
|
842
842
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _recharts.CartesianGrid, { vertical: false }),
|
|
843
843
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
844
844
|
_recharts.XAxis,
|
|
@@ -862,10 +862,10 @@ function TokenUsageTimelineChart({ rows, metric, stackBy, className }) {
|
|
|
862
862
|
}
|
|
863
863
|
),
|
|
864
864
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
865
|
-
|
|
865
|
+
_chunkDGAVM2LZjs.ChartTooltip,
|
|
866
866
|
{
|
|
867
867
|
content: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
868
|
-
|
|
868
|
+
_chunkDGAVM2LZjs.ChartTooltipContent,
|
|
869
869
|
{
|
|
870
870
|
labelFormatter: (value) => bucketDate(String(value), granularity),
|
|
871
871
|
valueFormatter: (value) => formatValue(Number(value), metric),
|
|
@@ -874,7 +874,7 @@ function TokenUsageTimelineChart({ rows, metric, stackBy, className }) {
|
|
|
874
874
|
)
|
|
875
875
|
}
|
|
876
876
|
),
|
|
877
|
-
seriesKeys.length >= 2 ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
877
|
+
seriesKeys.length >= 2 ? /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.ChartLegend, { content: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.ChartLegendContent, {}) }) : null,
|
|
878
878
|
seriesKeys.map((series, index) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
879
879
|
_recharts.Bar,
|
|
880
880
|
{
|
|
@@ -926,19 +926,19 @@ function TokenUsageAdminContainer() {
|
|
|
926
926
|
[t]
|
|
927
927
|
);
|
|
928
928
|
if (error) {
|
|
929
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
929
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "p-4", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "p", { className: "text-destructive text-xs/relaxed", children: error }) }) }) }) });
|
|
930
930
|
}
|
|
931
|
-
if (isLoading) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
931
|
+
if (isLoading) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true });
|
|
932
932
|
const emptyLabel = t("token_usage.admin.no_data");
|
|
933
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
933
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true, children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "flex w-full flex-col gap-4 p-4", children: [
|
|
934
934
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageAdminTiles, { summary, metric: filters.metric, singleCustomerMode: hidePlatform }),
|
|
935
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
936
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
937
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
938
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
935
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
936
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: [
|
|
937
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.usage_over_time") }),
|
|
938
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardAction, { children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "flex items-center gap-2", children: [
|
|
939
939
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: t("token_usage.admin.stack_by") }),
|
|
940
940
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
941
|
-
|
|
941
|
+
_chunkDGAVM2LZjs.Select,
|
|
942
942
|
{
|
|
943
943
|
items: stackByItems,
|
|
944
944
|
value: filters.stackBy,
|
|
@@ -946,29 +946,29 @@ function TokenUsageAdminContainer() {
|
|
|
946
946
|
if (value) setFilters({ stackBy: value });
|
|
947
947
|
},
|
|
948
948
|
children: [
|
|
949
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
950
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
949
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectTrigger, { size: "sm", className: "w-36", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectValue, {}) }),
|
|
950
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectContent, { children: STACK_BY_VALUES.map((value) => /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SelectItem, { value, children: stackByItems[value] }, value)) })
|
|
951
951
|
]
|
|
952
952
|
}
|
|
953
953
|
)
|
|
954
954
|
] }) })
|
|
955
955
|
] }),
|
|
956
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
956
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageTimelineChart, { rows: timeline, metric: filters.metric, stackBy: filters.stackBy }) })
|
|
957
957
|
] }),
|
|
958
958
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "grid gap-4 md:grid-cols-2", children: [
|
|
959
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
960
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
961
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
959
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
960
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.by_company") }) }),
|
|
961
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageRankedBar, { rows: byCompany, metric: filters.metric, emptyLabel }) })
|
|
962
962
|
] }),
|
|
963
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
964
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
965
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
963
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
964
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.by_user") }) }),
|
|
965
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageRankedBar, { rows: byUser, metric: filters.metric, emptyLabel }) })
|
|
966
966
|
] })
|
|
967
967
|
] }),
|
|
968
968
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: _chunkUYLS3LJWjs.cn.call(void 0, "grid gap-4", !hidePlatform && "md:grid-cols-2"), children: [
|
|
969
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
970
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
971
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
969
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
970
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.customer_by_operation") }) }),
|
|
971
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
972
972
|
TokenUsageRankedBar,
|
|
973
973
|
{
|
|
974
974
|
rows: byCustomerOperation,
|
|
@@ -978,9 +978,9 @@ function TokenUsageAdminContainer() {
|
|
|
978
978
|
}
|
|
979
979
|
) })
|
|
980
980
|
] }),
|
|
981
|
-
!hidePlatform && /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
982
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
983
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
981
|
+
!hidePlatform && /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
982
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.platform_by_operation") }) }),
|
|
983
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
984
984
|
TokenUsageRankedBar,
|
|
985
985
|
{
|
|
986
986
|
rows: byOperation,
|
|
@@ -991,9 +991,9 @@ function TokenUsageAdminContainer() {
|
|
|
991
991
|
) })
|
|
992
992
|
] })
|
|
993
993
|
] }),
|
|
994
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
995
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
996
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
994
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
995
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.admin.detail") }) }),
|
|
996
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageBreakdownTable, { rows: byCompany, metric: filters.metric }) })
|
|
997
997
|
] })
|
|
998
998
|
] }) });
|
|
999
999
|
}
|
|
@@ -1114,7 +1114,7 @@ var TOKEN_USAGE_REPORT_I18N_KEYS = [
|
|
|
1114
1114
|
|
|
1115
1115
|
function TokenUsageReportFilterBar({ onChange }) {
|
|
1116
1116
|
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "flex flex-wrap items-center gap-2", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1117
|
-
|
|
1117
|
+
_chunkDGAVM2LZjs.DateRangeSelector,
|
|
1118
1118
|
{
|
|
1119
1119
|
onDateChange: (range) => {
|
|
1120
1120
|
if (!_optionalChain([range, 'optionalAccess', _10 => _10.from]) || !_optionalChain([range, 'optionalAccess', _11 => _11.to])) return;
|
|
@@ -1147,7 +1147,7 @@ var TokenUsageReportProvider = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void
|
|
|
1147
1147
|
pageUrl = TOKEN_USAGE_REPORT_PAGE_URL
|
|
1148
1148
|
}) => {
|
|
1149
1149
|
const t = _nextintl.useTranslations.call(void 0, );
|
|
1150
|
-
const generateUrl =
|
|
1150
|
+
const generateUrl = _chunkDGAVM2LZjs.usePageUrlGenerator.call(void 0, );
|
|
1151
1151
|
const [filters, setFilterState] = _react.useState.call(void 0, () => {
|
|
1152
1152
|
const range = defaultRange2();
|
|
1153
1153
|
return { from: _nullishCoalesce(initialFrom, () => ( range.from)), to: _nullishCoalesce(initialTo, () => ( range.to)) };
|
|
@@ -1211,7 +1211,7 @@ var TokenUsageReportProvider = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void
|
|
|
1211
1211
|
() => ({ summary, timeline, byOperation, byTarget, targetPanelTitleKey, filters, setFilters, isLoading, error }),
|
|
1212
1212
|
[summary, timeline, byOperation, byTarget, targetPanelTitleKey, filters, setFilters, isLoading, error]
|
|
1213
1213
|
);
|
|
1214
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1214
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.SharedProvider, { value: { breadcrumbs: breadcrumb(), title: title() }, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageReportContext.Provider, { value: contextValue, children }) });
|
|
1215
1215
|
}, "TokenUsageReportProvider");
|
|
1216
1216
|
var useTokenUsageReport = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void 0, () => {
|
|
1217
1217
|
const ctx = _react.useContext.call(void 0, TokenUsageReportContext);
|
|
@@ -1239,7 +1239,7 @@ function TokenUsageReportTiles({ summary, metric, balances }) {
|
|
|
1239
1239
|
const monthlyColor = monthlyPercentage > 75 ? "text-success" : monthlyPercentage >= 5 ? "text-warning" : "text-destructive";
|
|
1240
1240
|
const whole = /* @__PURE__ */ _chunk7QVYU63Ejs.__name.call(void 0, (value) => decimal(Math.max(0, Math.floor(value)), 0), "whole");
|
|
1241
1241
|
return /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "grid gap-3", children: [
|
|
1242
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1242
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": "tile-used", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
1243
1243
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: t("token_usage.report.used_in_period") }),
|
|
1244
1244
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-primary text-xl font-semibold tabular-nums", children: formatValue(metricValue(current, metric), metric) }),
|
|
1245
1245
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "span", { className: "flex items-center gap-1", children: [
|
|
@@ -1255,7 +1255,7 @@ function TokenUsageReportTiles({ summary, metric, balances }) {
|
|
|
1255
1255
|
] })
|
|
1256
1256
|
] }) }),
|
|
1257
1257
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "grid gap-3 sm:grid-cols-3", children: [
|
|
1258
|
-
balances && /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1258
|
+
balances && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": "tile-monthly", size: "sm", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
1259
1259
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: t("token_usage.report.monthly_left") }),
|
|
1260
1260
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "span", { className: "flex items-baseline gap-1", children: [
|
|
1261
1261
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { "data-testid": "tile-monthly-value", className: _chunkUYLS3LJWjs.cn.call(void 0, "text-sm font-medium tabular-nums", monthlyColor), children: whole(balances.availableMonthlyCredits) }),
|
|
@@ -1265,11 +1265,11 @@ function TokenUsageReportTiles({ summary, metric, balances }) {
|
|
|
1265
1265
|
] })
|
|
1266
1266
|
] })
|
|
1267
1267
|
] }) }),
|
|
1268
|
-
balances && /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1268
|
+
balances && /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": "tile-extra", size: "sm", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
1269
1269
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: t("token_usage.report.extra_credits") }),
|
|
1270
1270
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-sm font-medium tabular-nums", children: whole(balances.availableExtraCredits) })
|
|
1271
1271
|
] }) }),
|
|
1272
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1272
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { "data-testid": "tile-calls", size: "sm", children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.CardContent, { className: "grid gap-1", children: [
|
|
1273
1273
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-muted-foreground text-xs", children: t("token_usage.report.calls") }),
|
|
1274
1274
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, "span", { className: "text-sm font-medium tabular-nums", children: decimal(current.calls, 0) })
|
|
1275
1275
|
] }) })
|
|
@@ -1311,21 +1311,21 @@ function TokenUsageReportContainer({ balances = null }) {
|
|
|
1311
1311
|
const t = _nextintl.useTranslations.call(void 0, );
|
|
1312
1312
|
const { summary, timeline, byOperation, byTarget, isLoading, error, targetPanelTitleKey } = useTokenUsageReport();
|
|
1313
1313
|
if (error) {
|
|
1314
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1314
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true, children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "div", { className: "p-4", children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.Card, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, "p", { className: "text-destructive text-xs/relaxed", children: error }) }) }) }) });
|
|
1315
1315
|
}
|
|
1316
|
-
if (isLoading) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1316
|
+
if (isLoading) return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true });
|
|
1317
1317
|
const emptyLabel = t("token_usage.report.no_data");
|
|
1318
1318
|
const targetTitle = targetPanelTitleKey && byTarget.length > 0 ? t(targetPanelTitleKey) : void 0;
|
|
1319
|
-
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1319
|
+
return /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.RoundPageContainer, { fullWidth: true, forceHeader: true, children: /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: "flex w-full flex-col gap-4 p-4", children: [
|
|
1320
1320
|
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageReportTiles, { summary, metric: "credits", balances }),
|
|
1321
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
1322
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1323
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1321
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
1322
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.report.usage_over_time") }) }),
|
|
1323
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageTimelineChart, { rows: timeline, metric: "credits", stackBy: "type" }) })
|
|
1324
1324
|
] }),
|
|
1325
1325
|
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, "div", { className: _chunkUYLS3LJWjs.cn.call(void 0, "grid gap-4", targetTitle && "md:grid-cols-2"), children: [
|
|
1326
|
-
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
1327
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1328
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1326
|
+
/* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
1327
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: t("token_usage.report.by_operation") }) }),
|
|
1328
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1329
1329
|
TokenUsageRankedBar,
|
|
1330
1330
|
{
|
|
1331
1331
|
rows: byOperation,
|
|
@@ -1335,9 +1335,9 @@ function TokenUsageReportContainer({ balances = null }) {
|
|
|
1335
1335
|
}
|
|
1336
1336
|
) })
|
|
1337
1337
|
] }),
|
|
1338
|
-
targetTitle && /* @__PURE__ */ _jsxruntime.jsxs.call(void 0,
|
|
1339
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1340
|
-
/* @__PURE__ */ _jsxruntime.jsx.call(void 0,
|
|
1338
|
+
targetTitle && /* @__PURE__ */ _jsxruntime.jsxs.call(void 0, _chunkDGAVM2LZjs.Card, { children: [
|
|
1339
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardHeader, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardTitle, { children: targetTitle }) }),
|
|
1340
|
+
/* @__PURE__ */ _jsxruntime.jsx.call(void 0, _chunkDGAVM2LZjs.CardContent, { children: /* @__PURE__ */ _jsxruntime.jsx.call(void 0, TokenUsageRankedBar, { rows: byTarget, metric: "credits", emptyLabel }) })
|
|
1341
1341
|
] })
|
|
1342
1342
|
] })
|
|
1343
1343
|
] }) });
|
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use client";
|
|
2
2
|
|
|
3
|
+
import { getCookie } from "cookies-next";
|
|
3
4
|
import { useAtom } from "jotai";
|
|
4
5
|
import { atomWithStorage } from "jotai/utils";
|
|
5
6
|
import { usePathname } from "next/navigation";
|
|
@@ -196,6 +197,29 @@ export const CurrentUserProvider = ({ children }: { children: React.ReactNode })
|
|
|
196
197
|
const refreshUserRef = useRef(refreshUser);
|
|
197
198
|
refreshUserRef.current = refreshUser;
|
|
198
199
|
|
|
200
|
+
// Reconcile the local cache with the session on mount.
|
|
201
|
+
//
|
|
202
|
+
// `userAtom` is a localStorage CACHE of the user; the session itself is the
|
|
203
|
+
// token cookie. The two can disagree — an iOS home-screen web app shares
|
|
204
|
+
// Safari's cookies but starts with an empty localStorage — and nothing used
|
|
205
|
+
// to reconcile them: the user was written only by the login flow or by
|
|
206
|
+
// RefreshUser, which fires only when the proxy refreshes a token. With a
|
|
207
|
+
// cookie and no cached user the server rendered a logged-in page while every
|
|
208
|
+
// client surface (avatar, sidebar, campaign role) saw nobody.
|
|
209
|
+
//
|
|
210
|
+
// localStorage is read directly rather than through `dehydratedUser`:
|
|
211
|
+
// atomWithStorage reports its initial value on the first render and only
|
|
212
|
+
// then hydrates from storage, so the atom alone would trigger a needless
|
|
213
|
+
// fetch on every mount. skipCookieUpdate because the cookies are what we are
|
|
214
|
+
// reading FROM; rewriting them through the Server Action would reload the page.
|
|
215
|
+
useEffect(() => {
|
|
216
|
+
if (typeof window === "undefined") return;
|
|
217
|
+
if (window.localStorage.getItem(CURRENT_USER_STORAGE_KEY)) return;
|
|
218
|
+
if (!getCookie("token")) return;
|
|
219
|
+
void refreshUserRef.current({ skipCookieUpdate: true });
|
|
220
|
+
// Mount-only: this is a one-shot reconciliation, not a subscription.
|
|
221
|
+
}, []);
|
|
222
|
+
|
|
199
223
|
// Track refresh in progress to prevent duplicate API calls
|
|
200
224
|
const isRefreshingRef = useRef(false);
|
|
201
225
|
|
|
@@ -118,6 +118,46 @@ describe("CurrentUserProvider", () => {
|
|
|
118
118
|
expect(localStorage.getItem("user")).toBe(JSON.stringify(storedUser));
|
|
119
119
|
});
|
|
120
120
|
|
|
121
|
+
it("loads the user from the session when a token cookie exists but localStorage is empty", async () => {
|
|
122
|
+
// An iOS home-screen web app shares Safari's cookies but starts with an
|
|
123
|
+
// empty localStorage: the server renders a logged-in page while the client
|
|
124
|
+
// has no user. The provider must reconcile from the cookie session.
|
|
125
|
+
vi.mocked(getCookie).mockImplementation((name: any) => (name === "token" ? "jwt" : undefined));
|
|
126
|
+
const dehydrated = { id: "user-from-session", roles: [], company: null, modules: [] };
|
|
127
|
+
vi.mocked(UserService.findFullUser).mockResolvedValue({ ...dehydrated, dehydrate: () => dehydrated } as any);
|
|
128
|
+
const updateToken = vi.fn().mockResolvedValue(undefined);
|
|
129
|
+
vi.mocked(getTokenHandler).mockReturnValue({ updateToken, removeToken: vi.fn() } as any);
|
|
130
|
+
|
|
131
|
+
const { getByTestId } = render(
|
|
132
|
+
<CurrentUserProvider>
|
|
133
|
+
<Consumer />
|
|
134
|
+
</CurrentUserProvider>,
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
138
|
+
|
|
139
|
+
expect(UserService.findFullUser).toHaveBeenCalledTimes(1);
|
|
140
|
+
expect(getByTestId("current-user-id").textContent).toBe("user-from-session");
|
|
141
|
+
// The cookies are what we read FROM; rewriting them would reload the page.
|
|
142
|
+
expect(updateToken).not.toHaveBeenCalled();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("does not hit the API when localStorage already holds the user", async () => {
|
|
146
|
+
vi.mocked(getCookie).mockImplementation((name: any) => (name === "token" ? "jwt" : undefined));
|
|
147
|
+
localStorage.setItem("user", JSON.stringify({ id: "cached", roles: [], company: null, modules: [] }));
|
|
148
|
+
|
|
149
|
+
const { getByTestId } = render(
|
|
150
|
+
<CurrentUserProvider>
|
|
151
|
+
<Consumer />
|
|
152
|
+
</CurrentUserProvider>,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
await new Promise((resolve) => setTimeout(resolve, 100));
|
|
156
|
+
|
|
157
|
+
expect(UserService.findFullUser).not.toHaveBeenCalled();
|
|
158
|
+
expect(getByTestId("current-user-id").textContent).toBe("cached");
|
|
159
|
+
});
|
|
160
|
+
|
|
121
161
|
it("awaits updateToken before writing the user atom in refreshUser", async () => {
|
|
122
162
|
const oldUser = { id: "old", roles: [], company: null, modules: [] };
|
|
123
163
|
localStorage.setItem("user", JSON.stringify(oldUser));
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { render, waitFor } from "@testing-library/react";
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
3
|
+
import usePushNotifications from "../usePushNotifications";
|
|
4
|
+
|
|
5
|
+
// Hoisted so the vi.mock factories below can read it (Vitest hoists factories
|
|
6
|
+
// above module-scope consts).
|
|
7
|
+
const state = vi.hoisted(() => ({ isProduction: false }));
|
|
8
|
+
|
|
9
|
+
vi.mock("../../core/env", () => ({
|
|
10
|
+
ENV: {
|
|
11
|
+
get IS_PRODUCTION() {
|
|
12
|
+
return state.isProduction;
|
|
13
|
+
},
|
|
14
|
+
VAPID_PUBLIC_KEY: "",
|
|
15
|
+
},
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
vi.mock("../../contexts", () => ({
|
|
19
|
+
useCurrentUserContext: () => ({
|
|
20
|
+
currentUser: { id: "user-1" },
|
|
21
|
+
hasRole: () => false,
|
|
22
|
+
}),
|
|
23
|
+
}));
|
|
24
|
+
|
|
25
|
+
vi.mock("../../features/push/data/push.service", () => ({
|
|
26
|
+
PushService: { register: vi.fn() },
|
|
27
|
+
}));
|
|
28
|
+
|
|
29
|
+
vi.mock("../../roles", () => ({
|
|
30
|
+
getRoleId: () => ({ Administrator: "administrator" }),
|
|
31
|
+
}));
|
|
32
|
+
|
|
33
|
+
vi.mock("../../client/config", () => ({
|
|
34
|
+
getAppUrl: () => "http://app.test",
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
function Probe() {
|
|
38
|
+
usePushNotifications();
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
describe("usePushNotifications worker URL", () => {
|
|
43
|
+
const register = vi.fn().mockResolvedValue({ pushManager: { getSubscription: vi.fn() } });
|
|
44
|
+
|
|
45
|
+
beforeEach(() => {
|
|
46
|
+
register.mockClear();
|
|
47
|
+
vi.stubGlobal("navigator", { serviceWorker: { register, ready: Promise.resolve() } });
|
|
48
|
+
vi.stubGlobal("PushManager", function PushManager() {});
|
|
49
|
+
// "denied" makes the hook return right after registering, so this spec
|
|
50
|
+
// proves only the URL choice and nothing about subscription handling.
|
|
51
|
+
vi.stubGlobal("Notification", { permission: "denied", requestPermission: vi.fn() });
|
|
52
|
+
sessionStorage.clear();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
afterEach(() => {
|
|
56
|
+
vi.unstubAllGlobals();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("registers the push-only dev worker outside production", async () => {
|
|
60
|
+
state.isProduction = false;
|
|
61
|
+
render(<Probe />);
|
|
62
|
+
await waitFor(() => expect(register).toHaveBeenCalledWith("http://app.test/sw-dev.js"));
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("registers the full worker in production", async () => {
|
|
66
|
+
state.isProduction = true;
|
|
67
|
+
render(<Probe />);
|
|
68
|
+
await waitFor(() => expect(register).toHaveBeenCalledWith("http://app.test/sw.js"));
|
|
69
|
+
});
|
|
70
|
+
});
|
|
@@ -30,7 +30,12 @@ export default function usePushNotifications(): void {
|
|
|
30
30
|
const sessionKey = `push_registered_${currentUser?.id}`;
|
|
31
31
|
const lastRegisteredSubscription = sessionStorage.getItem(sessionKey);
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
// Dev uses a push-ONLY worker (public/sw-dev.js) with no fetch handler,
|
|
34
|
+
// so it can't cache stale Turbopack chunks the way the prod worker
|
|
35
|
+
// (sw.ts) would. Prod uses the full worker. Both live at scope "/", so
|
|
36
|
+
// the push subscription is identical either way.
|
|
37
|
+
const swUrl = ENV.IS_PRODUCTION ? `${getAppUrl()}/sw.js` : `${getAppUrl()}/sw-dev.js`;
|
|
38
|
+
const registration = await navigator.serviceWorker.register(swUrl);
|
|
34
39
|
|
|
35
40
|
// Check current permission status first
|
|
36
41
|
let permission = Notification.permission;
|