@carlonicora/nextjs-jsonapi 6.10.1 → 6.11.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/dist/{AbstractApiData-XLhBP5Tl.d.mts → AbstractApiData-CKy8tbde.d.mts} +6 -0
- package/dist/{AbstractApiData-DtitRWPz.d.ts → AbstractApiData-rgvs0cdY.d.ts} +6 -0
- package/dist/{BlockNoteEditor-GK3DRTOP.mjs → BlockNoteEditor-5WM5DX3J.mjs} +4 -4
- package/dist/{BlockNoteEditor-YOKJOVV3.js → BlockNoteEditor-GY5R25YI.js} +19 -19
- package/dist/{BlockNoteEditor-YOKJOVV3.js.map → BlockNoteEditor-GY5R25YI.js.map} +1 -1
- package/dist/{BlockNoteViewer-EKAWVLIR.js → BlockNoteViewer-FLUHEXE6.js} +3 -3
- package/dist/{BlockNoteViewer-EKAWVLIR.js.map → BlockNoteViewer-FLUHEXE6.js.map} +1 -1
- package/dist/{BlockNoteViewer-RWRI3QO2.mjs → BlockNoteViewer-UCA3LV3N.mjs} +2 -2
- package/dist/billing/index.js +491 -491
- package/dist/billing/index.mjs +3 -3
- package/dist/{chunk-THZAK7S3.mjs → chunk-3PRLRCMF.mjs} +1281 -603
- package/dist/chunk-3PRLRCMF.mjs.map +1 -0
- package/dist/{chunk-AWLTSKMM.mjs → chunk-6RLATJ2L.mjs} +2 -2
- package/dist/{chunk-6HFRXEF5.mjs → chunk-BGLOFZ3K.mjs} +296 -6
- package/dist/chunk-BGLOFZ3K.mjs.map +1 -0
- package/dist/{chunk-KRQRJA4J.js → chunk-FNPXCYHO.js} +8 -8
- package/dist/{chunk-KRQRJA4J.js.map → chunk-FNPXCYHO.js.map} +1 -1
- package/dist/{chunk-4UFMLTL2.js → chunk-MP3DV2I5.js} +1647 -969
- package/dist/chunk-MP3DV2I5.js.map +1 -0
- package/dist/{chunk-RNZ3IK5J.js → chunk-OHJSI6L2.js} +4 -4
- package/dist/{chunk-RNZ3IK5J.js.map → chunk-OHJSI6L2.js.map} +1 -1
- package/dist/{chunk-SQTXJPRE.js → chunk-YGEOX2XX.js} +297 -7
- package/dist/chunk-YGEOX2XX.js.map +1 -0
- package/dist/{chunk-DPAM7BCE.mjs → chunk-YP6W4AB5.mjs} +2 -2
- package/dist/client/index.js +4 -4
- package/dist/client/index.mjs +3 -3
- package/dist/components/index.d.mts +68 -30
- package/dist/components/index.d.ts +68 -30
- package/dist/components/index.js +10 -4
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +9 -3
- package/dist/contexts/index.js +4 -4
- package/dist/contexts/index.mjs +3 -3
- package/dist/core/index.d.mts +160 -4
- package/dist/core/index.d.ts +160 -4
- package/dist/core/index.js +14 -2
- package/dist/core/index.js.map +1 -1
- package/dist/core/index.mjs +13 -1
- package/dist/email-automations/index.js +93 -93
- package/dist/email-automations/index.mjs +3 -3
- package/dist/features/analytics/index.d.mts +1 -1
- package/dist/features/analytics/index.d.ts +1 -1
- package/dist/features/analytics/index.js +120 -122
- package/dist/features/analytics/index.js.map +1 -1
- package/dist/features/analytics/index.mjs +7 -9
- package/dist/features/analytics/index.mjs.map +1 -1
- package/dist/features/analytics/server.js +4 -4
- package/dist/features/analytics/server.mjs +2 -2
- package/dist/features/blog/index.js +150 -150
- package/dist/features/blog/index.mjs +3 -3
- package/dist/features/blog/server.js +16 -16
- package/dist/features/blog/server.mjs +1 -1
- package/dist/features/help/index.js +37 -37
- package/dist/features/help/index.mjs +3 -3
- package/dist/features/tokenusage/index.d.mts +1 -1
- package/dist/features/tokenusage/index.d.ts +1 -1
- package/dist/features/tokenusage/index.js +126 -128
- package/dist/features/tokenusage/index.js.map +1 -1
- package/dist/features/tokenusage/index.mjs +14 -16
- package/dist/features/tokenusage/index.mjs.map +1 -1
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +15 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -2
- package/dist/{HandbookSectionInterface-BPgqqwgw.d.mts → referral-admin.interface-C87ApGMR.d.mts} +42 -2
- package/dist/{HandbookSectionInterface-_51oA3om.d.ts → referral-admin.interface-CMwl5NiV.d.ts} +42 -2
- package/dist/server/index.js +3 -3
- package/dist/server/index.mjs +2 -2
- package/messages/en.json +78 -1
- package/messages/it.json +78 -1
- package/package.json +1 -1
- package/src/core/abstracts/AbstractApiData.ts +14 -3
- package/src/core/abstracts/__tests__/readIncludedWithMeta.spec.ts +103 -0
- package/src/core/index.ts +2 -0
- package/src/core/registry/ModuleRegistry.ts +5 -0
- package/src/features/referral/__tests__/referral-admin-models.spec.ts +122 -0
- package/src/features/referral/__tests__/referral-admin-service.spec.ts +70 -0
- package/src/features/referral/__tests__/referral-code-capture.spec.tsx +105 -0
- package/src/features/referral/__tests__/referral-invite-errors.spec.tsx +82 -0
- package/src/features/referral/__tests__/referral-visit-service.spec.ts +47 -0
- package/src/features/referral/__tests__/referrals-admin-container.spec.tsx +230 -0
- package/src/features/referral/components/ReferralCodeCapture.tsx +3 -0
- package/src/features/referral/components/ReferralDialog.tsx +9 -13
- package/src/features/referral/components/ReferralWidget.tsx +75 -45
- package/src/features/referral/components/ReferralsAdminFunnel.tsx +74 -0
- package/src/features/referral/components/ReferralsAdminTiles.tsx +128 -0
- package/src/features/referral/components/containers/ReferralsAdminContainer.tsx +205 -0
- package/src/features/referral/components/index.ts +3 -0
- package/src/features/referral/data/ReferralAdminReferral.ts +65 -0
- package/src/features/referral/data/ReferralAdminReferrer.ts +65 -0
- package/src/features/referral/data/ReferralAdminService.ts +51 -0
- package/src/features/referral/data/ReferralAdminSummary.ts +60 -0
- package/src/features/referral/data/ReferralService.ts +20 -0
- package/src/features/referral/data/ReferralVisit.ts +23 -0
- package/src/features/referral/data/index.ts +5 -0
- package/src/features/referral/hooks/index.ts +2 -0
- package/src/features/referral/hooks/useReferralAdminReferralTableStructure.tsx +127 -0
- package/src/features/referral/hooks/useReferralAdminReferrerTableStructure.tsx +229 -0
- package/src/features/referral/hooks/useReferralInvite.ts +2 -0
- package/src/features/referral/index.ts +1 -0
- package/src/features/referral/interfaces/index.ts +1 -0
- package/src/features/referral/interfaces/referral-admin.interface.ts +45 -0
- package/src/features/referral/referral-admin.modules.ts +32 -0
- package/dist/chunk-3TNGKMJU.mjs +0 -115
- package/dist/chunk-3TNGKMJU.mjs.map +0 -1
- package/dist/chunk-4UFMLTL2.js.map +0 -1
- package/dist/chunk-6HFRXEF5.mjs.map +0 -1
- package/dist/chunk-G3YKDIAL.js +0 -115
- package/dist/chunk-G3YKDIAL.js.map +0 -1
- package/dist/chunk-SQTXJPRE.js.map +0 -1
- package/dist/chunk-THZAK7S3.mjs.map +0 -1
- /package/dist/{BlockNoteEditor-GK3DRTOP.mjs.map → BlockNoteEditor-5WM5DX3J.mjs.map} +0 -0
- /package/dist/{BlockNoteViewer-RWRI3QO2.mjs.map → BlockNoteViewer-UCA3LV3N.mjs.map} +0 -0
- /package/dist/{chunk-AWLTSKMM.mjs.map → chunk-6RLATJ2L.mjs.map} +0 -0
- /package/dist/{chunk-DPAM7BCE.mjs.map → chunk-YP6W4AB5.mjs.map} +0 -0
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ColumnDef } from "@tanstack/react-table";
|
|
4
|
+
import { useTranslations } from "next-intl";
|
|
5
|
+
import { useMemo } from "react";
|
|
6
|
+
import { cellDate } from "../../../components/tables/cells/cell.date";
|
|
7
|
+
import { registerTableGenerator, TableContent, UseTableStructureHook } from "../../../hooks";
|
|
8
|
+
import { Badge } from "../../../shadcnui";
|
|
9
|
+
import { useUsageFormatters } from "../../tokenusage/lib/formatters";
|
|
10
|
+
import { ReferralAdminReferralInterface } from "../interfaces/referral-admin.interface";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Columns of "Every referral". The values double as the TanStack column ids:
|
|
14
|
+
* ContentListTable builds its `columnVisibility` from the `fields` array.
|
|
15
|
+
*/
|
|
16
|
+
export enum ReferralAdminReferralFields {
|
|
17
|
+
account = "account",
|
|
18
|
+
referrer = "referrer",
|
|
19
|
+
source = "source",
|
|
20
|
+
date = "date",
|
|
21
|
+
status = "status",
|
|
22
|
+
credits = "credits",
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
type Row = { row: { original: TableContent<ReferralAdminReferralInterface> } };
|
|
26
|
+
|
|
27
|
+
export const useReferralAdminReferralTableStructure: UseTableStructureHook<
|
|
28
|
+
ReferralAdminReferralInterface,
|
|
29
|
+
ReferralAdminReferralFields
|
|
30
|
+
> = (params) => {
|
|
31
|
+
const t = useTranslations();
|
|
32
|
+
const { decimal } = useUsageFormatters();
|
|
33
|
+
|
|
34
|
+
const tableData = useMemo(() => {
|
|
35
|
+
return params.data.map((entry: ReferralAdminReferralInterface) => {
|
|
36
|
+
const row: TableContent<ReferralAdminReferralInterface> = { jsonApiData: entry };
|
|
37
|
+
// cellDate reads the value from the row by the column name.
|
|
38
|
+
row[ReferralAdminReferralFields.date] = entry.referredAt;
|
|
39
|
+
return row;
|
|
40
|
+
});
|
|
41
|
+
}, [params.data]);
|
|
42
|
+
|
|
43
|
+
const fieldColumnMap: Record<ReferralAdminReferralFields, () => any> = {
|
|
44
|
+
[ReferralAdminReferralFields.account]: () => ({
|
|
45
|
+
id: ReferralAdminReferralFields.account,
|
|
46
|
+
header: t("referral.admin.referrals.columns.account"),
|
|
47
|
+
cell: ({ row }: Row) => {
|
|
48
|
+
const entry = row.original.jsonApiData;
|
|
49
|
+
return (
|
|
50
|
+
<div className="flex flex-col">
|
|
51
|
+
<span className="text-sm">{entry.name}</span>
|
|
52
|
+
<span className="text-muted-foreground text-xs">{entry.email}</span>
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
},
|
|
56
|
+
enableSorting: false,
|
|
57
|
+
enableHiding: false,
|
|
58
|
+
}),
|
|
59
|
+
[ReferralAdminReferralFields.referrer]: () => ({
|
|
60
|
+
id: ReferralAdminReferralFields.referrer,
|
|
61
|
+
header: t("referral.admin.referrals.columns.referrer"),
|
|
62
|
+
cell: ({ row }: Row) => {
|
|
63
|
+
const entry = row.original.jsonApiData;
|
|
64
|
+
return (
|
|
65
|
+
<div className="flex flex-col">
|
|
66
|
+
<span className="text-sm">{entry.referrerName}</span>
|
|
67
|
+
<span className="text-muted-foreground text-xs">{entry.referrerEmail}</span>
|
|
68
|
+
</div>
|
|
69
|
+
);
|
|
70
|
+
},
|
|
71
|
+
enableSorting: false,
|
|
72
|
+
enableHiding: false,
|
|
73
|
+
}),
|
|
74
|
+
[ReferralAdminReferralFields.source]: () => ({
|
|
75
|
+
id: ReferralAdminReferralFields.source,
|
|
76
|
+
header: t("referral.admin.referrals.columns.source"),
|
|
77
|
+
cell: ({ row }: Row) => {
|
|
78
|
+
const source = row.original.jsonApiData.source;
|
|
79
|
+
if (source === "link") return <Badge variant="softBlue">{t("referral.admin.source.link")}</Badge>;
|
|
80
|
+
if (source === "direct") return <Badge variant="softGray">{t("referral.admin.source.direct")}</Badge>;
|
|
81
|
+
// A referral written before the source was recorded: unknown, not guessed.
|
|
82
|
+
return <span className="text-muted-foreground text-xs">—</span>;
|
|
83
|
+
},
|
|
84
|
+
enableSorting: false,
|
|
85
|
+
enableHiding: false,
|
|
86
|
+
}),
|
|
87
|
+
[ReferralAdminReferralFields.date]: () =>
|
|
88
|
+
cellDate({ name: ReferralAdminReferralFields.date, title: t("referral.admin.referrals.columns.date") }),
|
|
89
|
+
[ReferralAdminReferralFields.status]: () => ({
|
|
90
|
+
id: ReferralAdminReferralFields.status,
|
|
91
|
+
header: t("referral.admin.referrals.columns.status"),
|
|
92
|
+
cell: ({ row }: Row) =>
|
|
93
|
+
row.original.jsonApiData.status === "completed" ? (
|
|
94
|
+
<Badge variant="softGreen">{t("referral.admin.status.completed")}</Badge>
|
|
95
|
+
) : (
|
|
96
|
+
<Badge variant="softYellow">{t("referral.admin.status.pending")}</Badge>
|
|
97
|
+
),
|
|
98
|
+
enableSorting: false,
|
|
99
|
+
enableHiding: false,
|
|
100
|
+
}),
|
|
101
|
+
[ReferralAdminReferralFields.credits]: () => ({
|
|
102
|
+
id: ReferralAdminReferralFields.credits,
|
|
103
|
+
header: t("referral.admin.referrals.columns.credits"),
|
|
104
|
+
cell: ({ row }: Row) => {
|
|
105
|
+
const entry = row.original.jsonApiData;
|
|
106
|
+
// Both sides receive the same award: shown as "200 + 200", as in the approved mock.
|
|
107
|
+
if (entry.status !== "completed" || !entry.tokensAwarded)
|
|
108
|
+
return <span className="block text-muted-foreground text-xs text-end">—</span>;
|
|
109
|
+
const award = decimal(entry.tokensAwarded, 0);
|
|
110
|
+
return <span className="block text-xs tabular-nums text-end">{`${award} + ${award}`}</span>;
|
|
111
|
+
},
|
|
112
|
+
meta: { className: "text-end" },
|
|
113
|
+
enableSorting: false,
|
|
114
|
+
enableHiding: false,
|
|
115
|
+
}),
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
const columns = useMemo(() => {
|
|
119
|
+
return params.fields.map((field) => fieldColumnMap[field]?.()).filter((col) => col !== undefined) as ColumnDef<
|
|
120
|
+
TableContent<ReferralAdminReferralInterface>
|
|
121
|
+
>[];
|
|
122
|
+
}, [params.fields, fieldColumnMap, t, decimal]);
|
|
123
|
+
|
|
124
|
+
return useMemo(() => ({ data: tableData, columns: columns }), [tableData, columns]);
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
registerTableGenerator("referrals/administration/referrals", useReferralAdminReferralTableStructure);
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import { ColumnDef } from "@tanstack/react-table";
|
|
4
|
+
import { CopyIcon } from "lucide-react";
|
|
5
|
+
import { useTranslations } from "next-intl";
|
|
6
|
+
import { useMemo } from "react";
|
|
7
|
+
import { cellDate } from "../../../components/tables/cells/cell.date";
|
|
8
|
+
import { registerTableGenerator, TableContent, UseTableStructureHook } from "../../../hooks";
|
|
9
|
+
import { Button } from "../../../shadcnui";
|
|
10
|
+
import { showError, showToast } from "../../../utils";
|
|
11
|
+
import { useUsageFormatters } from "../../tokenusage/lib/formatters";
|
|
12
|
+
import { ReferralAdminReferrerInterface, ReferralAdminSort } from "../interfaces/referral-admin.interface";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Columns of the referrers ranking. The values double as the TanStack column
|
|
16
|
+
* ids: ContentListTable builds its `columnVisibility` from the `fields` array,
|
|
17
|
+
* so a column whose id differs from its field is invisible.
|
|
18
|
+
*/
|
|
19
|
+
export enum ReferralAdminReferrerFields {
|
|
20
|
+
rank = "rank",
|
|
21
|
+
referrer = "referrer",
|
|
22
|
+
visits = "visits",
|
|
23
|
+
invites = "invites",
|
|
24
|
+
signups = "signups",
|
|
25
|
+
paying = "paying",
|
|
26
|
+
conversion = "conversion",
|
|
27
|
+
credits = "credits",
|
|
28
|
+
lastInvite = "lastInvite",
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** What the container passes through ContentListTable's `context`: the active sort and its setter. */
|
|
32
|
+
export type ReferralAdminReferrerTableContext = {
|
|
33
|
+
sort: ReferralAdminSort;
|
|
34
|
+
onSort: (sort: ReferralAdminSort) => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Copies text to the clipboard from inside a click handler, with the toast on
|
|
39
|
+
* success and the error toast on refusal (a denied permission or an insecure
|
|
40
|
+
* context). Shared by the per-row button and the container's "Copy all emails".
|
|
41
|
+
*/
|
|
42
|
+
export async function copyReferrerEmails(
|
|
43
|
+
text: string,
|
|
44
|
+
t: (key: string, values?: Record<string, string | number>) => string,
|
|
45
|
+
): Promise<void> {
|
|
46
|
+
try {
|
|
47
|
+
await navigator.clipboard.writeText(text);
|
|
48
|
+
showToast(t("referral.admin.referrers.copied"));
|
|
49
|
+
} catch {
|
|
50
|
+
showError(t("referral.admin.referrers.copyFailed"));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const NUMERIC = { className: "text-end" };
|
|
55
|
+
|
|
56
|
+
export const useReferralAdminReferrerTableStructure: UseTableStructureHook<
|
|
57
|
+
ReferralAdminReferrerInterface,
|
|
58
|
+
ReferralAdminReferrerFields
|
|
59
|
+
> = (params) => {
|
|
60
|
+
const t = useTranslations();
|
|
61
|
+
const { decimal } = useUsageFormatters();
|
|
62
|
+
const context = params.context as ReferralAdminReferrerTableContext | undefined;
|
|
63
|
+
|
|
64
|
+
const tableData = useMemo(() => {
|
|
65
|
+
return params.data.map((entry: ReferralAdminReferrerInterface, index: number) => {
|
|
66
|
+
const row: TableContent<ReferralAdminReferrerInterface> = { jsonApiData: entry };
|
|
67
|
+
row[ReferralAdminReferrerFields.rank] = index + 1;
|
|
68
|
+
// cellDate reads the value from the row by the column name.
|
|
69
|
+
row[ReferralAdminReferrerFields.lastInvite] = entry.lastInviteAt;
|
|
70
|
+
return row;
|
|
71
|
+
});
|
|
72
|
+
}, [params.data]);
|
|
73
|
+
|
|
74
|
+
/** Header of a sortable column: a button that sets the sort the API reads (D8). */
|
|
75
|
+
const sortableHeader = (sort: ReferralAdminSort, label: string) => () => {
|
|
76
|
+
const active = context?.sort === sort;
|
|
77
|
+
return (
|
|
78
|
+
<Button
|
|
79
|
+
type="button"
|
|
80
|
+
variant="ghost"
|
|
81
|
+
size="xs"
|
|
82
|
+
className={active ? "text-primary" : "text-muted-foreground"}
|
|
83
|
+
title={t("referral.admin.referrers.sortBy")}
|
|
84
|
+
aria-sort={active ? "descending" : undefined}
|
|
85
|
+
data-testid={active ? "referrals-referrers-sort" : undefined}
|
|
86
|
+
onClick={() => context?.onSort(sort)}
|
|
87
|
+
>
|
|
88
|
+
{label}
|
|
89
|
+
</Button>
|
|
90
|
+
);
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
// Typography role 15 (numeric): tabular-nums, end-aligned.
|
|
94
|
+
const numericCell = (read: (entry: ReferralAdminReferrerInterface) => number) => {
|
|
95
|
+
return ({ row }: { row: { original: TableContent<ReferralAdminReferrerInterface> } }) => (
|
|
96
|
+
<span className="block text-xs tabular-nums text-end">{decimal(read(row.original.jsonApiData), 0)}</span>
|
|
97
|
+
);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const fieldColumnMap: Record<ReferralAdminReferrerFields, () => any> = {
|
|
101
|
+
[ReferralAdminReferrerFields.rank]: () => ({
|
|
102
|
+
id: ReferralAdminReferrerFields.rank,
|
|
103
|
+
header: "#",
|
|
104
|
+
cell: ({ row }: { row: { original: TableContent<ReferralAdminReferrerInterface> } }) => (
|
|
105
|
+
<span className="text-muted-foreground text-xs tabular-nums">
|
|
106
|
+
{row.original[ReferralAdminReferrerFields.rank]}
|
|
107
|
+
</span>
|
|
108
|
+
),
|
|
109
|
+
enableSorting: false,
|
|
110
|
+
enableHiding: false,
|
|
111
|
+
}),
|
|
112
|
+
[ReferralAdminReferrerFields.referrer]: () => ({
|
|
113
|
+
id: ReferralAdminReferrerFields.referrer,
|
|
114
|
+
header: t("referral.admin.referrers.columns.referrer"),
|
|
115
|
+
cell: ({ row }: { row: { original: TableContent<ReferralAdminReferrerInterface> } }) => {
|
|
116
|
+
const entry = row.original.jsonApiData;
|
|
117
|
+
return (
|
|
118
|
+
<div className="flex items-center gap-2">
|
|
119
|
+
<div className="flex flex-col">
|
|
120
|
+
<span className="text-sm">{entry.name}</span>
|
|
121
|
+
<span className="text-muted-foreground text-xs">{entry.email}</span>
|
|
122
|
+
</div>
|
|
123
|
+
{entry.email && (
|
|
124
|
+
<Button
|
|
125
|
+
type="button"
|
|
126
|
+
variant="ghost"
|
|
127
|
+
size="icon-xs"
|
|
128
|
+
aria-label={t("referral.admin.referrers.copyEmail")}
|
|
129
|
+
title={t("referral.admin.referrers.copyEmail")}
|
|
130
|
+
data-testid={`referrals-copy-email-${entry.id}`}
|
|
131
|
+
onClick={() => copyReferrerEmails(entry.email, t)}
|
|
132
|
+
>
|
|
133
|
+
<CopyIcon />
|
|
134
|
+
</Button>
|
|
135
|
+
)}
|
|
136
|
+
</div>
|
|
137
|
+
);
|
|
138
|
+
},
|
|
139
|
+
enableSorting: false,
|
|
140
|
+
enableHiding: false,
|
|
141
|
+
}),
|
|
142
|
+
[ReferralAdminReferrerFields.visits]: () => ({
|
|
143
|
+
id: ReferralAdminReferrerFields.visits,
|
|
144
|
+
header: t("referral.admin.referrers.columns.visits"),
|
|
145
|
+
cell: numericCell((entry) => entry.visits),
|
|
146
|
+
meta: NUMERIC,
|
|
147
|
+
enableSorting: false,
|
|
148
|
+
enableHiding: false,
|
|
149
|
+
}),
|
|
150
|
+
[ReferralAdminReferrerFields.invites]: () => ({
|
|
151
|
+
id: ReferralAdminReferrerFields.invites,
|
|
152
|
+
header: sortableHeader("invites", t("referral.admin.referrers.columns.invites")),
|
|
153
|
+
cell: numericCell((entry) => entry.invitesSent),
|
|
154
|
+
meta: NUMERIC,
|
|
155
|
+
enableSorting: false,
|
|
156
|
+
enableHiding: false,
|
|
157
|
+
}),
|
|
158
|
+
[ReferralAdminReferrerFields.signups]: () => ({
|
|
159
|
+
id: ReferralAdminReferrerFields.signups,
|
|
160
|
+
header: sortableHeader("signups", t("referral.admin.referrers.columns.signups")),
|
|
161
|
+
cell: ({ row }: { row: { original: TableContent<ReferralAdminReferrerInterface> } }) => {
|
|
162
|
+
const entry = row.original.jsonApiData;
|
|
163
|
+
return (
|
|
164
|
+
<div className="flex flex-col items-end">
|
|
165
|
+
<span className="text-xs tabular-nums">{decimal(entry.signupsLink + entry.signupsDirect, 0)}</span>
|
|
166
|
+
<span className="text-muted-foreground text-xs">
|
|
167
|
+
{t("referral.admin.referrers.split", { link: entry.signupsLink, direct: entry.signupsDirect })}
|
|
168
|
+
</span>
|
|
169
|
+
</div>
|
|
170
|
+
);
|
|
171
|
+
},
|
|
172
|
+
meta: NUMERIC,
|
|
173
|
+
enableSorting: false,
|
|
174
|
+
enableHiding: false,
|
|
175
|
+
}),
|
|
176
|
+
[ReferralAdminReferrerFields.paying]: () => ({
|
|
177
|
+
id: ReferralAdminReferrerFields.paying,
|
|
178
|
+
header: sortableHeader("paying", t("referral.admin.referrers.columns.paying")),
|
|
179
|
+
cell: numericCell((entry) => entry.paying),
|
|
180
|
+
meta: NUMERIC,
|
|
181
|
+
enableSorting: false,
|
|
182
|
+
enableHiding: false,
|
|
183
|
+
}),
|
|
184
|
+
[ReferralAdminReferrerFields.conversion]: () => ({
|
|
185
|
+
id: ReferralAdminReferrerFields.conversion,
|
|
186
|
+
header: sortableHeader("conversion", t("referral.admin.referrers.columns.conversion")),
|
|
187
|
+
cell: ({ row }: { row: { original: TableContent<ReferralAdminReferrerInterface> } }) => {
|
|
188
|
+
const entry = row.original.jsonApiData;
|
|
189
|
+
// Conversion = paying / emails sent; a referrer who sent none has no rate.
|
|
190
|
+
if (!entry.invitesSent) return <span className="block text-muted-foreground text-xs text-end">—</span>;
|
|
191
|
+
const rate = Math.round((entry.paying / entry.invitesSent) * 100);
|
|
192
|
+
return (
|
|
193
|
+
<span className="flex items-center justify-end gap-1.5">
|
|
194
|
+
<span className="bg-primary/15 relative h-1.5 w-12 overflow-hidden rounded-full">
|
|
195
|
+
<span className="bg-primary absolute inset-y-0 start-0" style={{ width: `${Math.min(rate, 100)}%` }} />
|
|
196
|
+
</span>
|
|
197
|
+
<span className="text-xs tabular-nums">{`${decimal(rate, 0)}%`}</span>
|
|
198
|
+
</span>
|
|
199
|
+
);
|
|
200
|
+
},
|
|
201
|
+
meta: NUMERIC,
|
|
202
|
+
enableSorting: false,
|
|
203
|
+
enableHiding: false,
|
|
204
|
+
}),
|
|
205
|
+
[ReferralAdminReferrerFields.credits]: () => ({
|
|
206
|
+
id: ReferralAdminReferrerFields.credits,
|
|
207
|
+
header: sortableHeader("credits", t("referral.admin.referrers.columns.credits")),
|
|
208
|
+
cell: numericCell((entry) => entry.creditsEarned),
|
|
209
|
+
meta: NUMERIC,
|
|
210
|
+
enableSorting: false,
|
|
211
|
+
enableHiding: false,
|
|
212
|
+
}),
|
|
213
|
+
[ReferralAdminReferrerFields.lastInvite]: () =>
|
|
214
|
+
cellDate({
|
|
215
|
+
name: ReferralAdminReferrerFields.lastInvite,
|
|
216
|
+
title: t("referral.admin.referrers.columns.lastInvite"),
|
|
217
|
+
}),
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const columns = useMemo(() => {
|
|
221
|
+
return params.fields.map((field) => fieldColumnMap[field]?.()).filter((col) => col !== undefined) as ColumnDef<
|
|
222
|
+
TableContent<ReferralAdminReferrerInterface>
|
|
223
|
+
>[];
|
|
224
|
+
}, [params.fields, fieldColumnMap, t, decimal, context?.sort]);
|
|
225
|
+
|
|
226
|
+
return useMemo(() => ({ data: tableData, columns: columns }), [tableData, columns]);
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
registerTableGenerator("referrals/administration/referrers", useReferralAdminReferrerTableStructure);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { ApiDataInterface } from "../../../core";
|
|
2
|
+
|
|
3
|
+
/** Report window of the administrative referral page. */
|
|
4
|
+
export type ReferralAdminPeriod = "30d" | "90d" | "all";
|
|
5
|
+
|
|
6
|
+
/** The referrers ranking order the API allowlists. */
|
|
7
|
+
export type ReferralAdminSort = "paying" | "signups" | "invites" | "conversion" | "credits";
|
|
8
|
+
|
|
9
|
+
/** One window (`current` or `previous`) of the headline tiles. */
|
|
10
|
+
export interface ReferralAdminSummaryInterface extends ApiDataInterface {
|
|
11
|
+
get window(): string;
|
|
12
|
+
get visits(): number;
|
|
13
|
+
get invitesSent(): number;
|
|
14
|
+
get signups(): number;
|
|
15
|
+
get paying(): number;
|
|
16
|
+
get creditsGiven(): number;
|
|
17
|
+
get sharingCompanies(): number;
|
|
18
|
+
get eligibleCompanies(): number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** One referrer company in the ranking; the id is the company id. */
|
|
22
|
+
export interface ReferralAdminReferrerInterface extends ApiDataInterface {
|
|
23
|
+
get name(): string;
|
|
24
|
+
get email(): string;
|
|
25
|
+
get visits(): number;
|
|
26
|
+
get invitesSent(): number;
|
|
27
|
+
get signupsLink(): number;
|
|
28
|
+
get signupsDirect(): number;
|
|
29
|
+
get paying(): number;
|
|
30
|
+
get creditsEarned(): number;
|
|
31
|
+
get lastInviteAt(): Date | undefined;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** One referral (new account and who brought it); the id is the Referral id. */
|
|
35
|
+
export interface ReferralAdminReferralInterface extends ApiDataInterface {
|
|
36
|
+
get name(): string;
|
|
37
|
+
get email(): string;
|
|
38
|
+
get referrerName(): string;
|
|
39
|
+
get referrerEmail(): string;
|
|
40
|
+
get source(): string;
|
|
41
|
+
get status(): string;
|
|
42
|
+
get referredAt(): Date;
|
|
43
|
+
get completedAt(): Date | undefined;
|
|
44
|
+
get tokensAwarded(): number | undefined;
|
|
45
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { GiftIcon } from "lucide-react";
|
|
2
|
+
import { ModuleFactory, ModuleWithPermissions } from "../../permissions";
|
|
3
|
+
import { ReferralAdminReferral } from "./data/ReferralAdminReferral";
|
|
4
|
+
import { ReferralAdminReferrer } from "./data/ReferralAdminReferrer";
|
|
5
|
+
import { ReferralAdminSummary } from "./data/ReferralAdminSummary";
|
|
6
|
+
import { ReferralVisit } from "./data/ReferralVisit";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The referral visit and administrative report resources, as one object a
|
|
10
|
+
* consuming app spreads into its `allModules` (same rationale as
|
|
11
|
+
* `analyticsModules`). An app records link visits only once it spreads this.
|
|
12
|
+
* None has a pageUrl: these are not navigable resources.
|
|
13
|
+
*/
|
|
14
|
+
export const referralAdminModules = (factory: ModuleFactory) =>
|
|
15
|
+
({
|
|
16
|
+
ReferralVisit: factory({ name: "referrals/visits", model: ReferralVisit }),
|
|
17
|
+
ReferralAdminSummary: factory({
|
|
18
|
+
name: "referrals/administration/summary",
|
|
19
|
+
model: ReferralAdminSummary,
|
|
20
|
+
icon: GiftIcon,
|
|
21
|
+
}),
|
|
22
|
+
ReferralAdminReferrer: factory({
|
|
23
|
+
name: "referrals/administration/referrers",
|
|
24
|
+
model: ReferralAdminReferrer,
|
|
25
|
+
icon: GiftIcon,
|
|
26
|
+
}),
|
|
27
|
+
ReferralAdminReferral: factory({
|
|
28
|
+
name: "referrals/administration/referrals",
|
|
29
|
+
model: ReferralAdminReferral,
|
|
30
|
+
icon: GiftIcon,
|
|
31
|
+
}),
|
|
32
|
+
}) satisfies Record<string, ModuleWithPermissions>;
|
package/dist/chunk-3TNGKMJU.mjs
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
getTokenUsageCurrency
|
|
3
|
-
} from "./chunk-6HFRXEF5.mjs";
|
|
4
|
-
import {
|
|
5
|
-
__name
|
|
6
|
-
} from "./chunk-PAWJFY3S.mjs";
|
|
7
|
-
|
|
8
|
-
// src/features/tokenusage/lib/formatters.ts
|
|
9
|
-
import { useLocale } from "next-intl";
|
|
10
|
-
import { useMemo } from "react";
|
|
11
|
-
function createUsageFormatters(locale, currency) {
|
|
12
|
-
const symbol = new Intl.NumberFormat(locale, { style: "currency", currency, currencyDisplay: "narrowSymbol" }).formatToParts(0).find((part) => part.type === "currency")?.value ?? currency;
|
|
13
|
-
const decimal = /* @__PURE__ */ __name((value, decimals) => value.toLocaleString(locale, { minimumFractionDigits: decimals, maximumFractionDigits: decimals }), "decimal");
|
|
14
|
-
const dayFormat = new Intl.DateTimeFormat(locale, { day: "numeric", month: "short", timeZone: "UTC" });
|
|
15
|
-
const monthFormat = new Intl.DateTimeFormat(locale, { month: "short", year: "numeric", timeZone: "UTC" });
|
|
16
|
-
const compactFormat = new Intl.NumberFormat(locale, { notation: "compact", maximumSignificantDigits: 3 });
|
|
17
|
-
const collator = new Intl.Collator(locale);
|
|
18
|
-
return {
|
|
19
|
-
decimal,
|
|
20
|
-
metricValue(value, metric) {
|
|
21
|
-
if (metric === "cost") return `${symbol} ${decimal(value, 2)}`;
|
|
22
|
-
if (metric === "credits") return decimal(value, 1);
|
|
23
|
-
return decimal(value, 0);
|
|
24
|
-
},
|
|
25
|
-
currency(value, decimals) {
|
|
26
|
-
return `${symbol} ${decimal(value, decimals)}`;
|
|
27
|
-
},
|
|
28
|
-
percent(value, decimals = 1) {
|
|
29
|
-
return `${decimal(value, decimals)} %`;
|
|
30
|
-
},
|
|
31
|
-
compact(value) {
|
|
32
|
-
return compactFormat.format(value);
|
|
33
|
-
},
|
|
34
|
-
bucketDate(iso, granularity) {
|
|
35
|
-
const date = /* @__PURE__ */ new Date(`${iso}T00:00:00.000Z`);
|
|
36
|
-
return granularity === "month" ? monthFormat.format(date) : dayFormat.format(date);
|
|
37
|
-
},
|
|
38
|
-
compare(a, b) {
|
|
39
|
-
return collator.compare(a, b);
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
__name(createUsageFormatters, "createUsageFormatters");
|
|
44
|
-
function useUsageFormatters() {
|
|
45
|
-
const locale = useLocale();
|
|
46
|
-
const currency = getTokenUsageCurrency();
|
|
47
|
-
return useMemo(() => createUsageFormatters(locale, currency), [locale, currency]);
|
|
48
|
-
}
|
|
49
|
-
__name(useUsageFormatters, "useUsageFormatters");
|
|
50
|
-
|
|
51
|
-
// src/features/tokenusage/lib/palette.ts
|
|
52
|
-
var CATEGORICAL_LIGHT = [
|
|
53
|
-
"#2a78d6",
|
|
54
|
-
"#eb6834",
|
|
55
|
-
"#1baf7a",
|
|
56
|
-
"#eda100",
|
|
57
|
-
"#e87ba4",
|
|
58
|
-
"#008300",
|
|
59
|
-
"#4a3aa7"
|
|
60
|
-
];
|
|
61
|
-
var CATEGORICAL_DARK = [
|
|
62
|
-
"#3987e5",
|
|
63
|
-
"#d95926",
|
|
64
|
-
"#199e70",
|
|
65
|
-
"#c98500",
|
|
66
|
-
"#d55181",
|
|
67
|
-
"#008300",
|
|
68
|
-
"#9085e9"
|
|
69
|
-
];
|
|
70
|
-
var CATEGORICAL_CEILING = CATEGORICAL_LIGHT.length;
|
|
71
|
-
var OTHER_COLOR = "#898781";
|
|
72
|
-
var SEQUENTIAL_RAMP_LIGHT = ["#104281", "#1c5cab", "#2a78d6", "#5598e7", "#86b6ef"];
|
|
73
|
-
var SEQUENTIAL_RAMP = SEQUENTIAL_RAMP_LIGHT;
|
|
74
|
-
var categoricalSet = /* @__PURE__ */ __name((mode) => mode === "dark" ? CATEGORICAL_DARK : CATEGORICAL_LIGHT, "categoricalSet");
|
|
75
|
-
function seriesColor(index, mode = "light") {
|
|
76
|
-
if (index < 0 || index >= CATEGORICAL_CEILING) return OTHER_COLOR;
|
|
77
|
-
return categoricalSet(mode)[index];
|
|
78
|
-
}
|
|
79
|
-
__name(seriesColor, "seriesColor");
|
|
80
|
-
function seriesTheme(index) {
|
|
81
|
-
return { light: seriesColor(index, "light"), dark: seriesColor(index, "dark") };
|
|
82
|
-
}
|
|
83
|
-
__name(seriesTheme, "seriesTheme");
|
|
84
|
-
|
|
85
|
-
// src/features/tokenusage/lib/metrics.ts
|
|
86
|
-
function metricValue(row, metric) {
|
|
87
|
-
if (metric === "cost") return row.cost;
|
|
88
|
-
if (metric === "credits") return row.credits;
|
|
89
|
-
return row.tokensIn + row.tokensOut;
|
|
90
|
-
}
|
|
91
|
-
__name(metricValue, "metricValue");
|
|
92
|
-
function cacheHitPercentage(cached, tokensIn) {
|
|
93
|
-
if (!tokensIn) return 0;
|
|
94
|
-
return cached / tokensIn * 100;
|
|
95
|
-
}
|
|
96
|
-
__name(cacheHitPercentage, "cacheHitPercentage");
|
|
97
|
-
function percentageDelta(current, previous) {
|
|
98
|
-
if (!previous) return void 0;
|
|
99
|
-
return Math.round((current - previous) / previous * 100);
|
|
100
|
-
}
|
|
101
|
-
__name(percentageDelta, "percentageDelta");
|
|
102
|
-
|
|
103
|
-
export {
|
|
104
|
-
createUsageFormatters,
|
|
105
|
-
useUsageFormatters,
|
|
106
|
-
metricValue,
|
|
107
|
-
cacheHitPercentage,
|
|
108
|
-
percentageDelta,
|
|
109
|
-
CATEGORICAL_CEILING,
|
|
110
|
-
OTHER_COLOR,
|
|
111
|
-
SEQUENTIAL_RAMP,
|
|
112
|
-
seriesColor,
|
|
113
|
-
seriesTheme
|
|
114
|
-
};
|
|
115
|
-
//# sourceMappingURL=chunk-3TNGKMJU.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/features/tokenusage/lib/formatters.ts","../src/features/tokenusage/lib/palette.ts","../src/features/tokenusage/lib/metrics.ts"],"sourcesContent":["\"use client\";\n\nimport { useLocale } from \"next-intl\";\nimport { useMemo } from \"react\";\nimport type { Metric } from \"../data/tokenusage-admin.types\";\nimport { getTokenUsageCurrency } from \"./config\";\n\nexport type UsageFormatters = {\n /** Locale-aware fixed-decimal number, e.g. 9.46 → \"9,46\" in it-IT. */\n decimal(value: number, decimals: number): string;\n /** Currency for cost, 2 decimals for credits, whole numbers for tokens. */\n metricValue(value: number, metric: Metric): string;\n /** Currency with an explicit decimal count — the per-call tile needs 4. */\n currency(value: number, decimals: number): string;\n /** Percentage with one decimal by default, e.g. 60 → \"60,0 %\" in it-IT. */\n percent(value: number, decimals?: number): string;\n /** Compact notation for axis ticks, e.g. 1500000 → \"1,5 Mln\" in it-IT. */\n compact(value: number): string;\n /** A \"YYYY-MM-DD\" bucket key rendered as an axis or tooltip label, in UTC. */\n bucketDate(iso: string, granularity: \"day\" | \"week\" | \"month\"): string;\n /** Locale collation for client-side table sorting. */\n compare(a: string, b: string): number;\n};\n\n/**\n * Builds every formatter the token-usage surfaces need, for one locale and one\n * currency.\n *\n * A pure factory on purpose: it takes no React context, so the arithmetic and\n * the formatting are unit-testable without rendering, and a server component or\n * a test can construct a set for an arbitrary locale.\n *\n * The currency symbol is placed with a space rather than through\n * `style: \"currency\"`, which is what the previous hard-coded implementation\n * rendered (\"€ 9,46\"). Keeping that shape means a consuming app on EUR sees\n * byte-identical output after this refactor.\n *\n * `bucketDate` formats in UTC, always. The wire value is a `type: \"date\"` — a\n * calendar day — parsed to UTC midnight; reading it back with local getters\n * would shift the label a day early west of UTC.\n */\nexport function createUsageFormatters(locale: string, currency: string): UsageFormatters {\n const symbol =\n new Intl.NumberFormat(locale, { style: \"currency\", currency, currencyDisplay: \"narrowSymbol\" })\n .formatToParts(0)\n .find((part) => part.type === \"currency\")?.value ?? currency;\n\n const decimal = (value: number, decimals: number): string =>\n value.toLocaleString(locale, { minimumFractionDigits: decimals, maximumFractionDigits: decimals });\n\n const dayFormat = new Intl.DateTimeFormat(locale, { day: \"numeric\", month: \"short\", timeZone: \"UTC\" });\n const monthFormat = new Intl.DateTimeFormat(locale, { month: \"short\", year: \"numeric\", timeZone: \"UTC\" });\n // Axis ticks step by 1, 2, 2.5 or 5 times a power of ten, so a tick never needs\n // more than three significant digits. A fixed one-decimal cap rounded a 0.05\n // step into repeated labels (0.1, 0.1, 0.2, 0.2); significant digits keep every\n // tick distinct at any scale (0.05, 0.15, 1.25K).\n const compactFormat = new Intl.NumberFormat(locale, { notation: \"compact\", maximumSignificantDigits: 3 });\n const collator = new Intl.Collator(locale);\n\n return {\n decimal,\n\n metricValue(value, metric) {\n if (metric === \"cost\") return `${symbol} ${decimal(value, 2)}`;\n // Credits are stored to 4 decimals (round4(cost / creditCost)), but that\n // precision is noise to a reader comparing rows: one decimal is enough to\n // separate two values and keeps the columns narrow enough to scan.\n if (metric === \"credits\") return decimal(value, 1);\n return decimal(value, 0);\n },\n\n currency(value, decimals) {\n return `${symbol} ${decimal(value, decimals)}`;\n },\n\n percent(value, decimals = 1) {\n return `${decimal(value, decimals)} %`;\n },\n\n compact(value) {\n return compactFormat.format(value);\n },\n\n bucketDate(iso, granularity) {\n const date = new Date(`${iso}T00:00:00.000Z`);\n return granularity === \"month\" ? monthFormat.format(date) : dayFormat.format(date);\n },\n\n compare(a, b) {\n return collator.compare(a, b);\n },\n };\n}\n\n/**\n * The formatter set for the current request's locale and the app's configured\n * currency. Memoised on both, so the Intl objects are built once per locale\n * rather than on every render.\n */\nexport function useUsageFormatters(): UsageFormatters {\n const locale = useLocale();\n const currency = getTokenUsageCurrency();\n return useMemo(() => createUsageFormatters(locale, currency), [locale, currency]);\n}\n","/**\n * Chart palette for the administrative token-usage dashboard.\n *\n * Every value here is a documented step of the dataviz skill's reference ramps —\n * nothing was eyeballed, nothing was hand-mixed. The palette was run through the\n * skill's validator against THIS application's real chart surfaces in both modes.\n * The verbatim output is below.\n *\n * The surface is `--card`, NOT `--background`. Every chart that uses this palette\n * is painted inside a `<Card>`, so the card is the surface the marks actually sit\n * on — and in dark mode it is the *lighter* of the two (`oklch(0.205 0 0)` vs the\n * background's `oklch(0.145 0 0)`), which is the harder case for contrast. Taken\n * from `apps/web/src/app/globals.css` (narr8 declares two `:root`/`.dark` pairs;\n * the LATER pair wins, and it is the one read here) — light `oklch(1 0 0)`, dark\n * `oklch(0.205 0 0)`. a360ai declares the same two values.\n *\n * oklch→hex conversion: the algebraic inverse of the Björn Ottosson OKLab\n * matrices — the same matrices the validator applies in the forward direction —\n * giving light `#ffffff` and dark `#171717`. Cross-checked by feeding it the\n * previously recorded background value, which it reproduces exactly:\n * `oklch(0.145 0 0)` → `#0a0a0a`. ✓\n *\n * Light and dark are two SELECTED sets of steps, chosen for their own surface —\n * never one set with its lightness flipped at runtime. Consumers pick a set from\n * the resolved theme (`seriesColor(i, mode)` / `sequentialColor(i, mode)`).\n *\n * ---------------------------------------------------------------------------\n * VALIDATOR OUTPUT — dataviz `scripts/validate_palette.js`, run 2026-08-15\n * ---------------------------------------------------------------------------\n *\n * $ node scripts/validate_palette.js \"#2a78d6,#eb6834,#1baf7a,#eda100,#e87ba4,#008300,#4a3aa7\" --mode light --surface \"#ffffff\"\n *\n * Palette (light, surface #ffffff, categorical): 7 slots\n * [PASS] Lightness band all 7 inside L 0.43–0.77\n * [PASS] Chroma floor all 7 >= 0.1\n * [PASS] CVD separation worst adjacent #eda100↔#1baf7a ΔE 9.1 (protan) · tritan 5.8\n * [PASS] Normal-vision floor worst adjacent #e87ba4↔#eda100 ΔE 19.6 (normal)\n * [WARN] Contrast vs surface below 3:1 — relief required (visible labels or table view): [[\"#1baf7a\",2.82],[\"#eda100\",2.17],[\"#e87ba4\",2.69]]\n *\n * → ALL CHECKS PASS (CVD in the 6–8 floor band is legal ONLY with secondary encoding: direct labels, gaps, or texture)\n * scope: categorical palettes only. For a lone status/text color check WCAG text contrast; for a sequential ramp, lightness monotonicity.\n *\n * $ node scripts/validate_palette.js \"#3987e5,#d95926,#199e70,#c98500,#d55181,#008300,#9085e9\" --mode dark --surface \"#171717\"\n *\n * Palette (dark, surface #171717, categorical): 7 slots\n * [PASS] Lightness band all 7 inside L 0.48–0.67\n * [PASS] Chroma floor all 7 >= 0.1\n * [PASS] CVD separation worst adjacent #c98500↔#199e70 ΔE 8.4 (protan) · tritan 8.7\n * [PASS] Normal-vision floor worst adjacent #d55181↔#c98500 ΔE 19.3 (normal)\n * [PASS] Contrast vs surface all 7 >= 3:1\n *\n * → ALL CHECKS PASS (CVD in the 6–8 floor band is legal ONLY with secondary encoding: direct labels, gaps, or texture)\n * scope: categorical palettes only. For a lone status/text color check WCAG text contrast; for a sequential ramp, lightness monotonicity.\n *\n * $ node scripts/validate_palette.js \"#104281,#1c5cab,#2a78d6,#5598e7,#86b6ef\" --mode light --surface \"#ffffff\" --ordinal\n *\n * Palette (light, surface #ffffff, ordinal ramp): 5 slots\n * [PASS] Lightness monotone steps read light→dark\n * [PASS] Adjacent ΔL all gaps >= 0.06\n * [PASS] Light-end contrast #86b6ef at 2.11:1 vs surface\n * [PASS] Single hue hue spread 3°\n *\n * → ALL CHECKS PASS (ordinal: one hue, monotone L, visible step gaps, light end clears surface)\n *\n * $ node scripts/validate_palette.js \"#9ec5f4,#6da7ec,#3987e5,#256abf,#184f95\" --mode dark --surface \"#171717\" --ordinal\n *\n * Palette (dark, surface #171717, ordinal ramp): 5 slots\n * [PASS] Lightness monotone steps read light→dark\n * [PASS] Adjacent ΔL all gaps >= 0.06\n * [PASS] Light-end contrast #184f95 at 2.21:1 vs surface\n * [PASS] Single hue hue spread 3°\n *\n * → ALL CHECKS PASS (ordinal: one hue, monotone L, visible step gaps, light end clears surface)\n *\n * OTHER_COLOR is achromatic, so the categorical checks (which gate hue identity)\n * do not apply to it; it was gated on contrast alone with the validator's own\n * `contrast()` export: 3.59:1 on `#ffffff`, 4.99:1 on `#171717` — both clear 3:1.\n *\n * ---------------------------------------------------------------------------\n * WHAT THE RESULTS OBLIGE US TO DO\n * ---------------------------------------------------------------------------\n *\n * - NO COLOUR VALUE CHANGED in the move from `--background` to `--card`. Every\n * gate that passed on the background still passes on the card, so re-stepping\n * a hue would have been churn, not a fix. Only the recorded surface, the\n * recorded numbers and this prose changed.\n * - The dark set still clears 3:1 on the LIGHTER card surface: the worst slot is\n * green `#008300` at 3.63:1, and the ordinal ramp's surface-nearest step lands\n * at 2.21:1 (down from 2.44:1 on the background) — still above the 2:1 gate.\n * That headroom is thin, so a future card-surface lightening MUST re-run these\n * four commands rather than assume the set still holds.\n * - The light-mode contrast WARN is NOT dismissable. Three slots (aqua, yellow,\n * magenta) sit below 3:1 on white, so every surface that paints with this\n * palette MUST ship the relief channel: a visible legend, direct labels and a\n * tooltip carrying the value in text. The timeline chart and the breakdown\n * table both do. The light card is `#ffffff`, identical to the light\n * background, so this WARN is unchanged rather than newly incurred.\n * - CVD separation is measured on ADJACENT pairs, which is the correct pairlist\n * for stacked bars, grouped bars and lines — the only forms this palette paints.\n * A scatter / bubble / small-multiples chart would need `--pairs all`, which\n * caps the usable slot count at three; do not reuse this array there without\n * re-running the validator with that flag.\n */\n\n/** Which surface the colours are being painted on. */\nexport type ChartMode = \"light\" | \"dark\";\n\n/**\n * The chart surfaces the palette was validated against (globals.css `--card`).\n *\n * `--card`, never `--background`: the charts are painted inside a `<Card>`.\n */\nexport const CHART_SURFACE: Readonly<Record<ChartMode, string>> = {\n light: \"#ffffff\",\n dark: \"#171717\",\n};\n\n/**\n * Categorical slots, in FIXED order. Slot n is series n — the order is the\n * CVD-safety mechanism, not decoration, so it is never re-ordered and never\n * cycled. The two-series default (customer / platform) takes slots 0 and 1.\n *\n * Hues, in order: blue, orange, aqua, yellow, magenta, green, violet.\n */\nconst CATEGORICAL_LIGHT: readonly string[] = [\n \"#2a78d6\",\n \"#eb6834\",\n \"#1baf7a\",\n \"#eda100\",\n \"#e87ba4\",\n \"#008300\",\n \"#4a3aa7\",\n];\n\n/** The same seven hues, re-stepped for the dark surface. Not a lightness flip. */\nconst CATEGORICAL_DARK: readonly string[] = [\n \"#3987e5\",\n \"#d95926\",\n \"#199e70\",\n \"#c98500\",\n \"#d55181\",\n \"#008300\",\n \"#9085e9\",\n];\n\n/**\n * The default (light-surface) categorical set.\n *\n * Prefer `seriesColor(index, mode)` — this array exists for consumers that only\n * need the documented default order.\n */\nexport const CATEGORICAL: readonly string[] = CATEGORICAL_LIGHT;\n\n/**\n * How many series may carry their own identity hue. An 8th series is NEVER a\n * generated hue: it folds into `other`.\n */\nexport const CATEGORICAL_CEILING = CATEGORICAL_LIGHT.length;\n\n/**\n * The \"other\" rollup colour: deliberately achromatic so it reads as \"not an\n * identity\" beside the seven hues. Same step in both modes — it clears 3:1 on\n * both card surfaces (3.59:1 on `#ffffff`, 4.99:1 on `#171717`).\n */\nexport const OTHER_COLOR = \"#898781\";\n\n/**\n * Sequential (single-hue, blue) ramp for MAGNITUDE, indexed by rank position:\n * index 0 is rank 1 — the largest value — so the strongest step leads.\n *\n * On the light surface \"strongest\" is the darkest step; on the dark surface it\n * is the lightest. The anchor flips with the surface, which is why there are two\n * selected sets rather than one array reversed at runtime.\n *\n * Five steps, not eight: with the adjacent-ΔL ≥ 0.06 gate and the ≥ 2:1\n * surface-contrast gate on the step nearest the surface, the blue ramp fits\n * exactly five distinguishable steps. Consumers clamp past the end.\n */\nconst SEQUENTIAL_RAMP_LIGHT: readonly string[] = [\"#104281\", \"#1c5cab\", \"#2a78d6\", \"#5598e7\", \"#86b6ef\"];\n\nconst SEQUENTIAL_RAMP_DARK: readonly string[] = [\"#9ec5f4\", \"#6da7ec\", \"#3987e5\", \"#256abf\", \"#184f95\"];\n\n/**\n * The default (light-surface) sequential ramp.\n *\n * Prefer `sequentialColor(index, mode)` — this array exists for consumers that\n * only need the documented default.\n */\nexport const SEQUENTIAL_RAMP: readonly string[] = SEQUENTIAL_RAMP_LIGHT;\n\nconst categoricalSet = (mode: ChartMode): readonly string[] => (mode === \"dark\" ? CATEGORICAL_DARK : CATEGORICAL_LIGHT);\n\nconst sequentialSet = (mode: ChartMode): readonly string[] =>\n mode === \"dark\" ? SEQUENTIAL_RAMP_DARK : SEQUENTIAL_RAMP_LIGHT;\n\n/**\n * The identity colour for series `index`.\n *\n * Never generates a hue and never cycles: anything at or past the ceiling gets\n * `OTHER_COLOR`, because a series past the ceiling should already have been\n * folded into the \"other\" bucket by the caller.\n */\nexport function seriesColor(index: number, mode: ChartMode = \"light\"): string {\n if (index < 0 || index >= CATEGORICAL_CEILING) return OTHER_COLOR;\n return categoricalSet(mode)[index];\n}\n\n/**\n * Both mode steps for series `index`, shaped for a shadcn `ChartConfig` entry's\n * `theme` field.\n */\nexport function seriesTheme(index: number): { light: string; dark: string } {\n return { light: seriesColor(index, \"light\"), dark: seriesColor(index, \"dark\") };\n}\n\n/**\n * The magnitude colour for rank position `index`, clamped to the last step so a\n * long list keeps its weakest step rather than falling off the ramp.\n */\nexport function sequentialColor(index: number, mode: ChartMode = \"light\"): string {\n const ramp = sequentialSet(mode);\n const clamped = Math.min(Math.max(index, 0), ramp.length - 1);\n return ramp[clamped];\n}\n","import type { Metric } from \"../data/tokenusage-admin.types\";\n\n/**\n * Locale-free arithmetic for the token-usage surfaces.\n *\n * Everything that turns a number into a STRING lives in `./formatters` instead,\n * because it depends on the request's locale and the app's configured currency.\n * This file stays pure arithmetic so it needs neither.\n */\n\n/**\n * The metric field set every admin token-usage resource carries. Declared\n * structurally so it accepts the summary, timeline and breakdown interfaces\n * alike — all three expose exactly these getters.\n */\nexport type TokenUsageMetrics = {\n cost: number;\n credits: number;\n tokensIn: number;\n tokensOut: number;\n cached: number;\n calls: number;\n};\n\n/** Reads the single number a row contributes for the currently selected metric. */\nexport function metricValue(row: TokenUsageMetrics, metric: Metric): number {\n if (metric === \"cost\") return row.cost;\n if (metric === \"credits\") return row.credits;\n return row.tokensIn + row.tokensOut;\n}\n\n/**\n * Share of cached input tokens. Returns 0 rather than NaN when the window holds\n * no input tokens at all, so the tile renders \"0,0 %\" instead of \"NaN %\".\n */\nexport function cacheHitPercentage(cached: number, tokensIn: number): number {\n if (!tokensIn) return 0;\n return (cached / tokensIn) * 100;\n}\n\n/**\n * Percentage change of `current` against `previous`, rounded to a whole number.\n *\n * Returns `undefined` when the previous window is zero — there is no meaningful\n * percentage change from nothing, and rendering Infinity would be worse than\n * rendering nothing. Callers show an em dash in that slot.\n */\nexport function percentageDelta(current: number, previous: number): number | undefined {\n if (!previous) return undefined;\n return Math.round(((current - previous) / previous) * 100);\n}\n"],"mappings":"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","names":[]}
|