start-plan-bucket-reminder.fixture.tsx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import { StrictMode, useMemo, useRef, useState } from "react";
  2. import { createRoot } from "react-dom/client";
  3. import type { IUsageStatsService } from "@zcode/services";
  4. import { BUILTIN_MODEL_PROVIDER_IDS, type UsageEntitlementSnapshot } from "@zcode/shared";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import { useUsageEntitlementWithService } from "@/hooks/useUsageEntitlement.js";
  7. import { useV4SessionQuotaBanner } from "@/v4/useV4SessionQuotaBanner.js";
  8. import { ConversationQuotaBanner } from "@/v4/ConversationQuotaBanner.js";
  9. import "@/styles.css";
  10. const query = new URLSearchParams(location.search);
  11. const providerId = BUILTIN_MODEL_PROVIDER_IDS.bigmodelStartPlan;
  12. document.documentElement.className =
  13. query.get("theme") === "light" ? "light theme-zai-light" : "dark theme-zai-dark";
  14. const end = 4_102_444_800_000;
  15. function snapshot(
  16. event: number,
  17. daily: number,
  18. other: number,
  19. cycle: number,
  20. ): UsageEntitlementSnapshot {
  21. return {
  22. generatedAt: Date.now(),
  23. serverTime: Number(query.get("serverTime")) || Date.now(),
  24. authenticated: true,
  25. provider: { id: providerId, name: "Start" },
  26. remaining: null,
  27. subscription: null,
  28. quota: {
  29. level: "Start",
  30. limits: [
  31. {
  32. type: "event",
  33. bucketId: "fixture-event",
  34. period: "one_time",
  35. number: 300_000_000,
  36. remaining: event * 3_000_000,
  37. percentage: event / 100,
  38. periodStart: 0,
  39. periodEnd: end,
  40. nextResetTime: end,
  41. usageDetails: [{ modelCode: "glm-flash", displayName: "GLM Flash", usage: 0 }],
  42. },
  43. {
  44. type: "daily",
  45. bucketId: "fixture-daily",
  46. period: "daily",
  47. number: 5_000_000,
  48. remaining: daily * 50_000,
  49. percentage: daily / 100,
  50. periodStart: cycle,
  51. periodEnd: end + cycle,
  52. nextResetTime: end + cycle,
  53. usageDetails: [{ modelCode: "glm-flash", displayName: "GLM Flash", usage: 0 }],
  54. },
  55. {
  56. type: "other",
  57. bucketId: "fixture-other",
  58. period: "daily",
  59. number: 3_000_000,
  60. remaining: other * 30_000,
  61. percentage: other / 100,
  62. periodStart: 0,
  63. periodEnd: end,
  64. nextResetTime: end,
  65. usageDetails: [{ modelCode: "glm-other", usage: 0 }],
  66. },
  67. ],
  68. },
  69. };
  70. }
  71. function Pane({ service, session }: { service: IUsageStatsService; session: string }) {
  72. const banner = useV4SessionQuotaBanner({
  73. sessionId: session,
  74. error: null,
  75. errorKey: null,
  76. phase: "idle",
  77. providerId,
  78. modelId: "glm-flash",
  79. usageStatsService: service,
  80. });
  81. return (
  82. <section data-testid="pane">
  83. <output data-testid="kind">{banner.state.kind ?? "none"}</output>
  84. {banner.state.visible && !banner.dismissed ? (
  85. <ConversationQuotaBanner
  86. state={banner.state}
  87. onShown={banner.markShown}
  88. onDismiss={banner.dismiss}
  89. onUpgrade={() => {
  90. document.body.dataset.upgraded = "true";
  91. }}
  92. />
  93. ) : null}
  94. </section>
  95. );
  96. }
  97. function Fixture() {
  98. const values = useRef({ event: 100, daily: 100, other: 100, cycle: 0 });
  99. const [session, setSession] = useState("a");
  100. const [mounted, setMounted] = useState(true);
  101. const [revision, setRevision] = useState(0);
  102. const service = useMemo(
  103. () =>
  104. ({
  105. getEntitlementSnapshot: async () => {
  106. const value = values.current;
  107. return snapshot(value.event, value.daily, value.other, value.cycle);
  108. },
  109. }) as unknown as IUsageStatsService,
  110. [],
  111. );
  112. const entitlement = useUsageEntitlementWithService(service, {
  113. preferredProviderId: providerId,
  114. enabled: true,
  115. includeSubscription: false,
  116. allowDisabledPreferredProvider: true,
  117. requirePreferredProvider: true,
  118. allowEnvApiKey: false,
  119. refreshOnMount: true,
  120. });
  121. async function refresh() {
  122. await entitlement.refresh({ force: true });
  123. setRevision((v) => v + 1);
  124. }
  125. return (
  126. <ZCodeIntlProvider initialLocale={query.get("locale") === "zh-CN" ? "zh-CN" : "en-US"}>
  127. <main className="min-h-screen bg-background p-4 text-ui-base text-foreground">
  128. {(["event", "daily", "other"] as const).map((key) => (
  129. <label key={key}>
  130. {key}
  131. <select
  132. data-testid={key}
  133. defaultValue="100"
  134. onChange={(event) => {
  135. values.current[key] = Number(event.target.value);
  136. void refresh();
  137. }}
  138. >
  139. {[100, 50, 48, 47, 20, 10.1, 10, 9, 0].map((value) => (
  140. <option key={value}>{value}</option>
  141. ))}
  142. </select>
  143. </label>
  144. ))}
  145. <button
  146. onClick={() => {
  147. values.current.cycle += 1;
  148. void refresh();
  149. }}
  150. >
  151. New cycle
  152. </button>
  153. <button onClick={() => setSession((v) => (v === "a" ? "b" : "a"))}>Switch task</button>
  154. <button onClick={() => setMounted((v) => !v)}>Toggle pane</button>
  155. <output data-testid="revision">{revision}</output>
  156. <output data-testid="session">{session}</output>
  157. {mounted ? <Pane service={service} session={session} /> : null}
  158. </main>
  159. </ZCodeIntlProvider>
  160. );
  161. }
  162. createRoot(document.getElementById("root")!).render(
  163. <StrictMode>
  164. <Fixture />
  165. </StrictMode>,
  166. );