@fantastic.dev/better-stripe 0.60.1 → 0.62.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/README.md +5 -1
- package/dist/client/usage/index.d.ts +2 -0
- package/dist/client/usage/index.d.ts.map +1 -1
- package/dist/react/index.d.ts +17 -0
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +10 -0
- package/dist/react/index.js.map +1 -1
- package/dist/react/workflows/Pagination.d.ts +1 -0
- package/dist/react/workflows/Pagination.d.ts.map +1 -1
- package/dist/react/workflows/Pagination.js +1 -0
- package/dist/react/workflows/Pagination.js.map +1 -1
- package/dist/react/workflows/Usage.fixtures.d.ts +17 -0
- package/dist/react/workflows/Usage.fixtures.d.ts.map +1 -0
- package/dist/react/workflows/Usage.fixtures.js +79 -0
- package/dist/react/workflows/Usage.fixtures.js.map +1 -0
- package/dist/react/workflows/Usage.types.d.ts +25 -0
- package/dist/react/workflows/Usage.types.d.ts.map +1 -0
- package/dist/react/workflows/Usage.types.js +11 -0
- package/dist/react/workflows/Usage.types.js.map +1 -0
- package/dist/react/workflows/UsageAllowance.d.ts +14 -0
- package/dist/react/workflows/UsageAllowance.d.ts.map +1 -0
- package/dist/react/workflows/UsageAllowance.js +21 -0
- package/dist/react/workflows/UsageAllowance.js.map +1 -0
- package/dist/react/workflows/UsageBilling.d.ts +20 -0
- package/dist/react/workflows/UsageBilling.d.ts.map +1 -0
- package/dist/react/workflows/UsageBilling.js +40 -0
- package/dist/react/workflows/UsageBilling.js.map +1 -0
- package/dist/react/workflows/UsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/UsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.js +19 -0
- package/dist/react/workflows/UsageConfiguration.js.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts +65 -0
- package/dist/react/workflows/UsageConfiguration.types.d.ts.map +1 -0
- package/dist/react/workflows/UsageConfiguration.types.js +2 -0
- package/dist/react/workflows/UsageConfiguration.types.js.map +1 -0
- package/dist/react/workflows/UsageCredits.d.ts +14 -0
- package/dist/react/workflows/UsageCredits.d.ts.map +1 -0
- package/dist/react/workflows/UsageCredits.js +25 -0
- package/dist/react/workflows/UsageCredits.js.map +1 -0
- package/dist/react/workflows/UsageHistory.d.ts +40 -0
- package/dist/react/workflows/UsageHistory.d.ts.map +1 -0
- package/dist/react/workflows/UsageHistory.js +74 -0
- package/dist/react/workflows/UsageHistory.js.map +1 -0
- package/dist/react/workflows/UsagePriceRow.d.ts +26 -0
- package/dist/react/workflows/UsagePriceRow.d.ts.map +1 -0
- package/dist/react/workflows/UsagePriceRow.js +40 -0
- package/dist/react/workflows/UsagePriceRow.js.map +1 -0
- package/dist/react/workflows/UsageSummary.d.ts +19 -0
- package/dist/react/workflows/UsageSummary.d.ts.map +1 -0
- package/dist/react/workflows/UsageSummary.js +54 -0
- package/dist/react/workflows/UsageSummary.js.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts +4 -0
- package/dist/react/workflows/usage-configuration-validation.d.ts.map +1 -0
- package/dist/react/workflows/usage-configuration-validation.js +116 -0
- package/dist/react/workflows/usage-configuration-validation.js.map +1 -0
- package/dist/react/workflows/usage-format.d.ts +11 -0
- package/dist/react/workflows/usage-format.d.ts.map +1 -0
- package/dist/react/workflows/usage-format.js +43 -0
- package/dist/react/workflows/usage-format.js.map +1 -0
- package/dist/react/workflows/usage-scope.d.ts +5 -0
- package/dist/react/workflows/usage-scope.d.ts.map +1 -0
- package/dist/react/workflows/usage-scope.js +26 -0
- package/dist/react/workflows/usage-scope.js.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts +4 -0
- package/dist/react/workflows/useUsageConfiguration.d.ts.map +1 -0
- package/dist/react/workflows/useUsageConfiguration.js +84 -0
- package/dist/react/workflows/useUsageConfiguration.js.map +1 -0
- package/docs/README.md +4 -0
- package/docs/react-api.md +6 -0
- package/docs/usage-configuration.md +150 -0
- package/docs/usage-react.md +182 -0
- package/package.json +4 -2
- package/src/client/usage/index.ts +3 -0
- package/src/react/index.ts +36 -0
- package/src/react/workflows/Pagination.tsx +2 -0
- package/src/react/workflows/Usage.fixtures.ts +88 -0
- package/src/react/workflows/Usage.test.tsx +373 -0
- package/src/react/workflows/Usage.types.ts +39 -0
- package/src/react/workflows/UsageAllowance.tsx +114 -0
- package/src/react/workflows/UsageBilling.tsx +90 -0
- package/src/react/workflows/UsageConfiguration.test.tsx +393 -0
- package/src/react/workflows/UsageConfiguration.tsx +135 -0
- package/src/react/workflows/UsageConfiguration.types.ts +67 -0
- package/src/react/workflows/UsageCredits.tsx +112 -0
- package/src/react/workflows/UsageHistory.test.tsx +206 -0
- package/src/react/workflows/UsageHistory.tsx +154 -0
- package/src/react/workflows/UsagePriceRow.test.tsx +140 -0
- package/src/react/workflows/UsagePriceRow.tsx +109 -0
- package/src/react/workflows/UsageSummary.tsx +204 -0
- package/src/react/workflows/usage-configuration-validation.test.ts +107 -0
- package/src/react/workflows/usage-configuration-validation.ts +152 -0
- package/src/react/workflows/usage-format.ts +48 -0
- package/src/react/workflows/usage-scope.ts +42 -0
- package/src/react/workflows/useUsageConfiguration.ts +99 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import {
|
|
3
|
+
act,
|
|
4
|
+
cleanup,
|
|
5
|
+
fireEvent,
|
|
6
|
+
render,
|
|
7
|
+
screen,
|
|
8
|
+
waitFor,
|
|
9
|
+
} from "@testing-library/react";
|
|
10
|
+
import userEvent from "@testing-library/user-event";
|
|
11
|
+
import { afterEach, expect, it, vi } from "vitest";
|
|
12
|
+
import * as ui from "../index.js";
|
|
13
|
+
import { usageScope } from "./Usage.fixtures.js";
|
|
14
|
+
afterEach(cleanup);
|
|
15
|
+
const page = (
|
|
16
|
+
name: string,
|
|
17
|
+
cursor: string | null = null,
|
|
18
|
+
isDone = cursor === null,
|
|
19
|
+
) => ({
|
|
20
|
+
status: "available" as const,
|
|
21
|
+
observedAt: 700,
|
|
22
|
+
value: { records: [{ id: name }], cursor, isDone },
|
|
23
|
+
});
|
|
24
|
+
it("loads bounded authorized pages and exposes the same pagination through render props", async () => {
|
|
25
|
+
expect(ui.UsageHistory).toBeTypeOf("function");
|
|
26
|
+
const load = vi
|
|
27
|
+
.fn()
|
|
28
|
+
.mockResolvedValueOnce(page("first", "next1"))
|
|
29
|
+
.mockResolvedValueOnce(page("second"));
|
|
30
|
+
render(
|
|
31
|
+
<ui.UsageHistory
|
|
32
|
+
scope={usageScope}
|
|
33
|
+
historyKey="events"
|
|
34
|
+
load={load}
|
|
35
|
+
limit={5}
|
|
36
|
+
getKey={(row: { id: string }) => row.id}
|
|
37
|
+
renderRecord={(row) => <span>{row.id}</span>}
|
|
38
|
+
/>,
|
|
39
|
+
);
|
|
40
|
+
await screen.findByText("first");
|
|
41
|
+
expect(load).toHaveBeenCalledWith(usageScope, { cursor: null, limit: 5 });
|
|
42
|
+
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
|
43
|
+
await screen.findByText("second");
|
|
44
|
+
expect(load).toHaveBeenLastCalledWith(usageScope, {
|
|
45
|
+
cursor: "next1",
|
|
46
|
+
limit: 5,
|
|
47
|
+
});
|
|
48
|
+
expect(
|
|
49
|
+
(screen.getByRole("button", { name: "Next" }) as HTMLButtonElement)
|
|
50
|
+
.disabled,
|
|
51
|
+
).toBe(true);
|
|
52
|
+
expect(
|
|
53
|
+
(screen.getByRole("button", { name: "Previous" }) as HTMLButtonElement)
|
|
54
|
+
.disabled,
|
|
55
|
+
).toBe(false);
|
|
56
|
+
});
|
|
57
|
+
it("does not call an empty incomplete page complete or display unavailable history as zero", async () => {
|
|
58
|
+
expect(ui.UsageHistory).toBeTypeOf("function");
|
|
59
|
+
const load = vi
|
|
60
|
+
.fn()
|
|
61
|
+
.mockResolvedValueOnce({
|
|
62
|
+
status: "available",
|
|
63
|
+
observedAt: 700,
|
|
64
|
+
value: { records: [], cursor: "more", isDone: false, complete: false },
|
|
65
|
+
})
|
|
66
|
+
.mockResolvedValueOnce({
|
|
67
|
+
status: "unavailable",
|
|
68
|
+
reason: "read_unavailable",
|
|
69
|
+
retryable: true,
|
|
70
|
+
});
|
|
71
|
+
render(
|
|
72
|
+
<ui.UsageHistory
|
|
73
|
+
scope={usageScope}
|
|
74
|
+
historyKey="credits"
|
|
75
|
+
load={load}
|
|
76
|
+
getKey={(row: { id: string }) => row.id}
|
|
77
|
+
renderRecord={(row) => row.id}
|
|
78
|
+
/>,
|
|
79
|
+
);
|
|
80
|
+
await screen.findByText(/History incomplete/);
|
|
81
|
+
expect(screen.queryByText(/No history/)).toBeNull();
|
|
82
|
+
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
|
83
|
+
await screen.findByText(/History unavailable/);
|
|
84
|
+
expect(screen.queryByText(/No history/)).toBeNull();
|
|
85
|
+
});
|
|
86
|
+
it("resets cursor and drops late history after tenant and period switches", async () => {
|
|
87
|
+
expect(ui.UsageHistory).toBeTypeOf("function");
|
|
88
|
+
let finish!: (value: ReturnType<typeof page>) => void;
|
|
89
|
+
const load = vi
|
|
90
|
+
.fn()
|
|
91
|
+
.mockResolvedValueOnce(page("original", "later"))
|
|
92
|
+
.mockImplementationOnce(
|
|
93
|
+
() =>
|
|
94
|
+
new Promise((resolve) => {
|
|
95
|
+
finish = resolve;
|
|
96
|
+
}),
|
|
97
|
+
)
|
|
98
|
+
.mockResolvedValueOnce(page("other tenant"));
|
|
99
|
+
const props = {
|
|
100
|
+
scope: usageScope,
|
|
101
|
+
historyKey: "events",
|
|
102
|
+
load,
|
|
103
|
+
getKey: (row: { id: string }) => row.id,
|
|
104
|
+
renderRecord: (row: { id: string }) => row.id,
|
|
105
|
+
};
|
|
106
|
+
const { rerender } = render(<ui.UsageHistory {...props} />);
|
|
107
|
+
await screen.findByText("original");
|
|
108
|
+
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
|
109
|
+
await waitFor(() => expect(load).toHaveBeenCalledTimes(2));
|
|
110
|
+
const scope = {
|
|
111
|
+
...usageScope,
|
|
112
|
+
accountKey: "other",
|
|
113
|
+
period: { start: 1200, end: 1800 },
|
|
114
|
+
};
|
|
115
|
+
rerender(<ui.UsageHistory {...props} scope={scope} />);
|
|
116
|
+
await screen.findByText("other tenant");
|
|
117
|
+
expect(load).toHaveBeenLastCalledWith(scope, { cursor: null, limit: 25 });
|
|
118
|
+
await act(async () => finish(page("late original")));
|
|
119
|
+
expect(screen.queryByText("late original")).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
for (const cursor of ["", "c1"])
|
|
123
|
+
it(`holds a nonterminal ${cursor ? "repeated" : "empty"} cursor and retains previous-page navigation`, async () => {
|
|
124
|
+
const load = vi
|
|
125
|
+
.fn()
|
|
126
|
+
.mockResolvedValueOnce(page("first", "c1"))
|
|
127
|
+
.mockResolvedValueOnce(page("second", cursor, false))
|
|
128
|
+
.mockResolvedValueOnce(page("first", "c1"));
|
|
129
|
+
render(
|
|
130
|
+
<ui.UsageHistory
|
|
131
|
+
scope={usageScope}
|
|
132
|
+
historyKey="events"
|
|
133
|
+
load={load}
|
|
134
|
+
getKey={(row: { id: string }) => row.id}
|
|
135
|
+
renderRecord={(row) => row.id}
|
|
136
|
+
/>,
|
|
137
|
+
);
|
|
138
|
+
await screen.findByText("first");
|
|
139
|
+
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
|
140
|
+
await screen.findByText("second");
|
|
141
|
+
expect(
|
|
142
|
+
(screen.getByRole("button", { name: "Next" }) as HTMLButtonElement)
|
|
143
|
+
.disabled,
|
|
144
|
+
).toBe(true);
|
|
145
|
+
expect(screen.getByText(/History incomplete/)).toBeTruthy();
|
|
146
|
+
fireEvent.click(screen.getByRole("button", { name: "Previous" }));
|
|
147
|
+
await screen.findByText("first");
|
|
148
|
+
expect(
|
|
149
|
+
(screen.getByRole("button", { name: "Next" }) as HTMLButtonElement)
|
|
150
|
+
.disabled,
|
|
151
|
+
).toBe(false);
|
|
152
|
+
});
|
|
153
|
+
it("holds a cursor that loops back to any earlier page in the current navigation path", async () => {
|
|
154
|
+
const load = vi
|
|
155
|
+
.fn()
|
|
156
|
+
.mockResolvedValueOnce(page("one", "c1"))
|
|
157
|
+
.mockResolvedValueOnce(page("two", "c2"))
|
|
158
|
+
.mockResolvedValueOnce(page("three", "c1"));
|
|
159
|
+
render(
|
|
160
|
+
<ui.UsageHistory
|
|
161
|
+
scope={usageScope}
|
|
162
|
+
historyKey="events"
|
|
163
|
+
load={load}
|
|
164
|
+
getKey={(row: { id: string }) => row.id}
|
|
165
|
+
renderRecord={(row) => row.id}
|
|
166
|
+
/>,
|
|
167
|
+
);
|
|
168
|
+
for (const name of ["one", "two"]) {
|
|
169
|
+
await screen.findByText(name);
|
|
170
|
+
fireEvent.click(screen.getByRole("button", { name: "Next" }));
|
|
171
|
+
}
|
|
172
|
+
await screen.findByText("three");
|
|
173
|
+
expect(
|
|
174
|
+
(screen.getByRole("button", { name: "Next" }) as HTMLButtonElement)
|
|
175
|
+
.disabled,
|
|
176
|
+
).toBe(true);
|
|
177
|
+
expect(screen.getByText(/History incomplete/)).toBeTruthy();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("supports keyboard-only history pagination through labelled native buttons", async () => {
|
|
181
|
+
const user = userEvent.setup();
|
|
182
|
+
const load = vi
|
|
183
|
+
.fn()
|
|
184
|
+
.mockResolvedValueOnce(page("first", "next"))
|
|
185
|
+
.mockResolvedValueOnce(page("second"));
|
|
186
|
+
render(
|
|
187
|
+
<ui.UsageHistory
|
|
188
|
+
scope={usageScope}
|
|
189
|
+
historyKey="events"
|
|
190
|
+
load={load}
|
|
191
|
+
getKey={(row: { id: string }) => row.id}
|
|
192
|
+
renderRecord={(row) => row.id}
|
|
193
|
+
/>,
|
|
194
|
+
);
|
|
195
|
+
await screen.findByText("first");
|
|
196
|
+
await user.tab();
|
|
197
|
+
expect(document.activeElement).toBe(
|
|
198
|
+
screen.getByRole("button", { name: "Refresh history" }),
|
|
199
|
+
);
|
|
200
|
+
await user.tab();
|
|
201
|
+
expect(document.activeElement).toBe(
|
|
202
|
+
screen.getByRole("button", { name: "Next" }),
|
|
203
|
+
);
|
|
204
|
+
await user.keyboard("{Enter}");
|
|
205
|
+
await screen.findByText("second");
|
|
206
|
+
});
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import type { UsageAvailability } from "../../client/usage/boundaries.js";
|
|
4
|
+
import { useWorkflowResource } from "./useWorkflowResource.js";
|
|
5
|
+
import { Pagination, useCursorPagination } from "./Pagination.js";
|
|
6
|
+
import { usageViewKey, type UsageViewScope } from "./Usage.types.js";
|
|
7
|
+
export type UsageHistoryPage<Row> = {
|
|
8
|
+
records: readonly Row[];
|
|
9
|
+
cursor: string | null;
|
|
10
|
+
isDone: boolean;
|
|
11
|
+
complete?: boolean;
|
|
12
|
+
};
|
|
13
|
+
export type UsageHistoryProps<Row> = {
|
|
14
|
+
scope: UsageViewScope;
|
|
15
|
+
/** Distinguish event, credit, allowance or delivery readers and their filters. */
|
|
16
|
+
historyKey: string;
|
|
17
|
+
load: (
|
|
18
|
+
scope: UsageViewScope,
|
|
19
|
+
page: { cursor: string | null; limit: number },
|
|
20
|
+
) => Promise<UsageAvailability<UsageHistoryPage<Row>>>;
|
|
21
|
+
getKey: (record: Row) => string;
|
|
22
|
+
renderRecord: (record: Row) => ReactNode;
|
|
23
|
+
limit?: number;
|
|
24
|
+
label?: string;
|
|
25
|
+
className?: string;
|
|
26
|
+
children?: (state: ReturnType<typeof useUsageHistory<Row>>) => ReactNode;
|
|
27
|
+
};
|
|
28
|
+
export function useUsageHistory<Row>(
|
|
29
|
+
props: Pick<
|
|
30
|
+
UsageHistoryProps<Row>,
|
|
31
|
+
"scope" | "historyKey" | "load" | "limit"
|
|
32
|
+
>,
|
|
33
|
+
) {
|
|
34
|
+
const limit = props.limit ?? 25;
|
|
35
|
+
if (!Number.isInteger(limit) || limit < 1 || limit > 100)
|
|
36
|
+
throw new RangeError("Usage history page size must be 1–100");
|
|
37
|
+
const key = JSON.stringify([
|
|
38
|
+
usageViewKey(props.scope),
|
|
39
|
+
props.historyKey,
|
|
40
|
+
limit,
|
|
41
|
+
]);
|
|
42
|
+
const navigation = useCursorPagination(key);
|
|
43
|
+
const resource = useWorkflowResource(
|
|
44
|
+
async () => {
|
|
45
|
+
try {
|
|
46
|
+
const result = await props.load(props.scope, {
|
|
47
|
+
cursor: navigation.cursor,
|
|
48
|
+
limit,
|
|
49
|
+
});
|
|
50
|
+
if (
|
|
51
|
+
result.status === "available" &&
|
|
52
|
+
(result.value.records.length > limit ||
|
|
53
|
+
(result.value.cursor !== null && result.value.cursor.length > 2048))
|
|
54
|
+
)
|
|
55
|
+
throw new Error("Invalid history page");
|
|
56
|
+
return result;
|
|
57
|
+
} catch {
|
|
58
|
+
throw new Error("History unavailable. Try again.");
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
JSON.stringify([key, navigation.cursor]),
|
|
62
|
+
);
|
|
63
|
+
const available =
|
|
64
|
+
resource.data?.status === "available" ? resource.data.value : null;
|
|
65
|
+
const advancing =
|
|
66
|
+
!!available &&
|
|
67
|
+
typeof available.cursor === "string" &&
|
|
68
|
+
available.cursor.length > 0 &&
|
|
69
|
+
!navigation.hasCursor(available.cursor);
|
|
70
|
+
const incomplete =
|
|
71
|
+
available?.complete === false ||
|
|
72
|
+
(!!available && !available.isDone && !advancing);
|
|
73
|
+
const canNext = !!available && !available.isDone && advancing;
|
|
74
|
+
return {
|
|
75
|
+
...resource,
|
|
76
|
+
page: navigation.page,
|
|
77
|
+
canPrevious: navigation.canPrevious,
|
|
78
|
+
canNext,
|
|
79
|
+
isIncomplete: incomplete,
|
|
80
|
+
isEmpty:
|
|
81
|
+
!!available &&
|
|
82
|
+
available.records.length === 0 &&
|
|
83
|
+
available.isDone &&
|
|
84
|
+
!incomplete,
|
|
85
|
+
previous: () => {
|
|
86
|
+
if (!resource.isLoading) navigation.previous();
|
|
87
|
+
},
|
|
88
|
+
next: () => {
|
|
89
|
+
if (!resource.isLoading && canNext) navigation.next(available!.cursor!);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export function UsageHistory<Row>(props: UsageHistoryProps<Row>) {
|
|
94
|
+
return (
|
|
95
|
+
<UsageHistoryBody
|
|
96
|
+
key={JSON.stringify([
|
|
97
|
+
usageViewKey(props.scope),
|
|
98
|
+
props.historyKey,
|
|
99
|
+
props.limit ?? 25,
|
|
100
|
+
])}
|
|
101
|
+
{...props}
|
|
102
|
+
/>
|
|
103
|
+
);
|
|
104
|
+
}
|
|
105
|
+
function UsageHistoryBody<Row>(props: UsageHistoryProps<Row>) {
|
|
106
|
+
const state = useUsageHistory(props);
|
|
107
|
+
if (props.children) return <>{props.children(state)}</>;
|
|
108
|
+
const available =
|
|
109
|
+
state.data?.status === "available" ? state.data.value : null;
|
|
110
|
+
return (
|
|
111
|
+
<section
|
|
112
|
+
data-better-stripe="usage-history"
|
|
113
|
+
className={props.className}
|
|
114
|
+
aria-label={props.label ?? "Usage history"}
|
|
115
|
+
aria-busy={state.isLoading}
|
|
116
|
+
>
|
|
117
|
+
<h2>{props.label ?? "Usage history"}</h2>
|
|
118
|
+
<button type="button" disabled={state.isLoading} onClick={state.refresh}>
|
|
119
|
+
Refresh history
|
|
120
|
+
</button>
|
|
121
|
+
{state.isLoading && <p role="status">Loading history…</p>}
|
|
122
|
+
{(state.isLoading || state.error) && available && (
|
|
123
|
+
<p role="status">Displayed history may be stale.</p>
|
|
124
|
+
)}
|
|
125
|
+
{state.error && <p role="alert">{state.error}</p>}
|
|
126
|
+
{state.data && state.data.status !== "available" && (
|
|
127
|
+
<p role="status">
|
|
128
|
+
History {state.data.status}. Original scoped history is required.
|
|
129
|
+
</p>
|
|
130
|
+
)}
|
|
131
|
+
{state.isIncomplete && (
|
|
132
|
+
<p role="status">
|
|
133
|
+
History incomplete. This page does not establish complete totals.
|
|
134
|
+
</p>
|
|
135
|
+
)}
|
|
136
|
+
{state.isEmpty && <p>No history for this view.</p>}
|
|
137
|
+
{available && (
|
|
138
|
+
<ol>
|
|
139
|
+
{available.records.map((record) => (
|
|
140
|
+
<li key={props.getKey(record)}>{props.renderRecord(record)}</li>
|
|
141
|
+
))}
|
|
142
|
+
</ol>
|
|
143
|
+
)}
|
|
144
|
+
<Pagination
|
|
145
|
+
page={state.page}
|
|
146
|
+
canPrevious={state.canPrevious}
|
|
147
|
+
canNext={state.canNext}
|
|
148
|
+
isLoading={state.isLoading}
|
|
149
|
+
onPrevious={state.previous}
|
|
150
|
+
onNext={state.next}
|
|
151
|
+
/>
|
|
152
|
+
</section>
|
|
153
|
+
);
|
|
154
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { cleanup, render, screen } from "@testing-library/react";
|
|
3
|
+
import { afterEach, expect, it } from "vitest";
|
|
4
|
+
import * as ui from "../index.js";
|
|
5
|
+
import type { UsagePriceBinding } from "../../client/usage/catalogRead.js";
|
|
6
|
+
afterEach(cleanup);
|
|
7
|
+
const price: UsagePriceBinding = {
|
|
8
|
+
pricingMode: "metered",
|
|
9
|
+
stripePriceId: "price_usage",
|
|
10
|
+
stripeProductId: "prod_usage",
|
|
11
|
+
meterId: "mtr_usage",
|
|
12
|
+
active: true,
|
|
13
|
+
currency: "usd",
|
|
14
|
+
billingScheme: "per_unit",
|
|
15
|
+
unitAmountDecimal: "0.00125",
|
|
16
|
+
tiers: [],
|
|
17
|
+
interval: "month",
|
|
18
|
+
plan: {
|
|
19
|
+
key: "calls",
|
|
20
|
+
version: "v1",
|
|
21
|
+
model: "pay_as_you_go",
|
|
22
|
+
unit: { name: "call", precision: 2 },
|
|
23
|
+
rate: { currency: "usd", unitAmountDecimal: "0.125" },
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
it("reuses ordinary PriceCard formatting for fixed prices", () => {
|
|
27
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
28
|
+
render(
|
|
29
|
+
<ui.UsagePriceRow
|
|
30
|
+
value={{
|
|
31
|
+
kind: "fixed",
|
|
32
|
+
price: {
|
|
33
|
+
stripePriceId: "price_fixed",
|
|
34
|
+
unitAmount: 900,
|
|
35
|
+
currency: "usd",
|
|
36
|
+
type: "recurring",
|
|
37
|
+
interval: "month",
|
|
38
|
+
active: true,
|
|
39
|
+
},
|
|
40
|
+
}}
|
|
41
|
+
/>,
|
|
42
|
+
);
|
|
43
|
+
expect(screen.getByText("Fixed price")).toBeTruthy();
|
|
44
|
+
expect(screen.getByText("$9.00/month")).toBeTruthy();
|
|
45
|
+
});
|
|
46
|
+
it("shows exact whole-unit metered rates and the original version instead of treating null amount as free", () => {
|
|
47
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
48
|
+
render(<ui.UsagePriceRow value={{ kind: "metered", price }} />);
|
|
49
|
+
expect(screen.getByText(/0.00125 USD per call/)).toBeTruthy();
|
|
50
|
+
expect(screen.getByText(/v1/)).toBeTruthy();
|
|
51
|
+
expect(screen.getByText(/sum aggregation/)).toBeTruthy();
|
|
52
|
+
});
|
|
53
|
+
it("labels supported managed graduated tiers separately from other tiers and transformations", () => {
|
|
54
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
55
|
+
const { rerender } = render(
|
|
56
|
+
<ui.UsagePriceRow
|
|
57
|
+
value={{
|
|
58
|
+
kind: "metered",
|
|
59
|
+
price: {
|
|
60
|
+
...price,
|
|
61
|
+
billingScheme: "tiered",
|
|
62
|
+
plan: {
|
|
63
|
+
...price.plan,
|
|
64
|
+
model: "included_overage",
|
|
65
|
+
includedQuantity: "100",
|
|
66
|
+
reset: "billing_period",
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}}
|
|
70
|
+
/>,
|
|
71
|
+
);
|
|
72
|
+
expect(screen.getByText(/100 included/)).toBeTruthy();
|
|
73
|
+
rerender(
|
|
74
|
+
<ui.UsagePriceRow value={{ kind: "unsupported", reason: "tiered" }} />,
|
|
75
|
+
);
|
|
76
|
+
expect(screen.getByText(/Unsupported tiered pricing/)).toBeTruthy();
|
|
77
|
+
expect(screen.queryByText(/0.00/)).toBeNull();
|
|
78
|
+
rerender(
|
|
79
|
+
<ui.UsagePriceRow value={{ kind: "unsupported", reason: "transformed" }} />,
|
|
80
|
+
);
|
|
81
|
+
expect(screen.getByText(/Unsupported transformed pricing/)).toBeTruthy();
|
|
82
|
+
});
|
|
83
|
+
it("does not let legacy fixed rows misrepresent metered, tiered, transformed or missing amounts", () => {
|
|
84
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
85
|
+
const fixed = {
|
|
86
|
+
stripePriceId: "price_bad",
|
|
87
|
+
currency: "usd",
|
|
88
|
+
type: "recurring",
|
|
89
|
+
interval: "month",
|
|
90
|
+
active: true,
|
|
91
|
+
unitAmount: 0,
|
|
92
|
+
};
|
|
93
|
+
const { rerender } = render(
|
|
94
|
+
<ui.UsagePriceRow
|
|
95
|
+
value={{ kind: "fixed", price: { ...fixed, usageType: "metered" } }}
|
|
96
|
+
/>,
|
|
97
|
+
);
|
|
98
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
99
|
+
rerender(
|
|
100
|
+
<ui.UsagePriceRow
|
|
101
|
+
value={{ kind: "fixed", price: { ...fixed, billingScheme: "tiered" } }}
|
|
102
|
+
/>,
|
|
103
|
+
);
|
|
104
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
105
|
+
rerender(
|
|
106
|
+
<ui.UsagePriceRow
|
|
107
|
+
value={{
|
|
108
|
+
kind: "fixed",
|
|
109
|
+
price: { ...fixed, transformQuantity: { divideBy: 100 } },
|
|
110
|
+
}}
|
|
111
|
+
/>,
|
|
112
|
+
);
|
|
113
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
114
|
+
rerender(
|
|
115
|
+
<ui.UsagePriceRow
|
|
116
|
+
value={{ kind: "fixed", price: { ...fixed, unitAmount: null } }}
|
|
117
|
+
/>,
|
|
118
|
+
);
|
|
119
|
+
expect(screen.queryByText("$0.00/month")).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
it("keeps unsupported fixed charge currency and increments unavailable without crashing", () => {
|
|
122
|
+
expect(ui.UsagePriceRow).toBeTypeOf("function");
|
|
123
|
+
const price = {
|
|
124
|
+
stripePriceId: "price_bad",
|
|
125
|
+
unitAmount: 1,
|
|
126
|
+
currency: "bhd",
|
|
127
|
+
type: "one_time",
|
|
128
|
+
active: true,
|
|
129
|
+
};
|
|
130
|
+
const { rerender } = render(
|
|
131
|
+
<ui.UsagePriceRow value={{ kind: "fixed", price }} />,
|
|
132
|
+
);
|
|
133
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
134
|
+
rerender(
|
|
135
|
+
<ui.UsagePriceRow
|
|
136
|
+
value={{ kind: "fixed", price: { ...price, currency: "isk" } }}
|
|
137
|
+
/>,
|
|
138
|
+
);
|
|
139
|
+
expect(screen.getByText(/Unsupported pricing terms/)).toBeTruthy();
|
|
140
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import type { ReactNode } from "react";
|
|
3
|
+
import type { UsagePriceBinding } from "../../client/usage/catalogRead.js";
|
|
4
|
+
import { PriceCard, type PriceCardPrice } from "../components/PriceCard.js";
|
|
5
|
+
import { chargeAmountToDecimal } from "../../client/charge-currency.js";
|
|
6
|
+
import { formatUsageEstimate } from "./usage-format.js";
|
|
7
|
+
export type UsageCatalogPrice =
|
|
8
|
+
| {
|
|
9
|
+
kind: "fixed";
|
|
10
|
+
price: PriceCardPrice & {
|
|
11
|
+
usageType?: string;
|
|
12
|
+
billingScheme?: string;
|
|
13
|
+
transformQuantity?: unknown;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
| { kind: "metered"; price: UsagePriceBinding }
|
|
17
|
+
| { kind: "unsupported"; reason: "tiered" | "transformed" | "other" };
|
|
18
|
+
export type UsagePriceRowProps = {
|
|
19
|
+
value: UsageCatalogPrice;
|
|
20
|
+
productName?: string;
|
|
21
|
+
className?: string;
|
|
22
|
+
children?: (value: UsageCatalogPrice) => ReactNode;
|
|
23
|
+
};
|
|
24
|
+
/** Read-only catalog row. Only managed metered bindings carry original exact terms. */
|
|
25
|
+
export function UsagePriceRow({
|
|
26
|
+
value,
|
|
27
|
+
className,
|
|
28
|
+
productName,
|
|
29
|
+
children,
|
|
30
|
+
}: UsagePriceRowProps) {
|
|
31
|
+
if (children) return <>{children(value)}</>;
|
|
32
|
+
let contents: ReactNode;
|
|
33
|
+
if (value.kind === "fixed") {
|
|
34
|
+
const price = value.price;
|
|
35
|
+
const supported =
|
|
36
|
+
price.unitAmount != null &&
|
|
37
|
+
Number.isSafeInteger(price.unitAmount) &&
|
|
38
|
+
price.unitAmount >= 0 &&
|
|
39
|
+
chargeAmountToDecimal(price.unitAmount, price.currency) !== null &&
|
|
40
|
+
price.usageType !== "metered" &&
|
|
41
|
+
(!price.billingScheme || price.billingScheme === "per_unit") &&
|
|
42
|
+
price.transformQuantity == null;
|
|
43
|
+
contents = supported ? (
|
|
44
|
+
<>
|
|
45
|
+
<p>Fixed price</p>
|
|
46
|
+
<PriceCard price={price}>
|
|
47
|
+
{({ formattedPrice }) => <p>{formattedPrice}</p>}
|
|
48
|
+
</PriceCard>
|
|
49
|
+
</>
|
|
50
|
+
) : (
|
|
51
|
+
<p>
|
|
52
|
+
Unsupported pricing terms. Read the original provider terms before
|
|
53
|
+
configuration.
|
|
54
|
+
</p>
|
|
55
|
+
);
|
|
56
|
+
} else if (value.kind === "unsupported") {
|
|
57
|
+
contents = (
|
|
58
|
+
<p>
|
|
59
|
+
{value.reason === "tiered"
|
|
60
|
+
? "Unsupported tiered pricing"
|
|
61
|
+
: value.reason === "transformed"
|
|
62
|
+
? "Unsupported transformed pricing"
|
|
63
|
+
: "Unsupported pricing terms"}
|
|
64
|
+
. No fixed or zero price is inferred.
|
|
65
|
+
</p>
|
|
66
|
+
);
|
|
67
|
+
} else {
|
|
68
|
+
const { plan } = value.price;
|
|
69
|
+
const amount = formatUsageEstimate(
|
|
70
|
+
plan.rate.unitAmountDecimal,
|
|
71
|
+
plan.rate.currency,
|
|
72
|
+
);
|
|
73
|
+
contents = (
|
|
74
|
+
<>
|
|
75
|
+
<p>
|
|
76
|
+
{plan.model === "included_overage"
|
|
77
|
+
? "Included monthly units plus overages"
|
|
78
|
+
: plan.model === "prepaid"
|
|
79
|
+
? "Prepaid paid credits"
|
|
80
|
+
: "Pay as you go"}
|
|
81
|
+
</p>
|
|
82
|
+
<p>Version {plan.version}. Monthly billing, sum aggregation.</p>
|
|
83
|
+
<p>
|
|
84
|
+
{amount ? `${amount} per ${plan.unit.name}` : "Rate unavailable"}.
|
|
85
|
+
Quantity precision {plan.unit.precision}.
|
|
86
|
+
</p>
|
|
87
|
+
{plan.model === "included_overage" && (
|
|
88
|
+
<p>
|
|
89
|
+
{plan.includedQuantity} included per billing period. Graduated free
|
|
90
|
+
tier, then overages.
|
|
91
|
+
</p>
|
|
92
|
+
)}
|
|
93
|
+
{plan.model === "prepaid" && (
|
|
94
|
+
<p>
|
|
95
|
+
Payment-backed issuer credits. Consumption is not a second cash
|
|
96
|
+
collection.
|
|
97
|
+
</p>
|
|
98
|
+
)}
|
|
99
|
+
<p>{value.price.active ? "Active" : "Inactive"} immutable price</p>
|
|
100
|
+
</>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
return (
|
|
104
|
+
<div className={className} data-better-stripe="usage-price-row">
|
|
105
|
+
{productName && <h3>{productName}</h3>}
|
|
106
|
+
{contents}
|
|
107
|
+
</div>
|
|
108
|
+
);
|
|
109
|
+
}
|