@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 89 lines 2.98 KB
Raw
1 import { msg, useMessages } from "gt-react";
2 import { m } from "~/i18n/messages";
3 import type {
4 EnvironmentId,
5 ProviderInstanceId,
6 ServerProvider,
7 UsageLimitSourceSnapshots,
8 } from "@t3tools/contracts";
9 import { collectProviderUsageLimits, hasProviderUsageLimits } from "@t3tools/shared/usageLimits";
10 import { useState } from "react";
11 import { useStableCallback } from "../../hooks/useStableCallback";
12 import { toastManager } from "../ui/toast";
13 import { usageLimitsBannerItem } from "./ComposerUsageLimits";
14
15 /** Keep opened data live. Retain hidden panels during reconnect; clear when quota-spending work resumes. */
16 export function useUsageLimitsPanel(input: {
17 readonly environmentId: EnvironmentId;
18 readonly threadKey: string;
19 readonly panelKey: string | null;
20 readonly providerInstanceId: ProviderInstanceId | null;
21 readonly model: string | null;
22 readonly providerDriver: ServerProvider["driver"] | null;
23 readonly providers: readonly ServerProvider[];
24 readonly sources: UsageLimitSourceSnapshots;
25 }) {
26 const {
27 environmentId,
28 threadKey,
29 panelKey,
30 providerInstanceId,
31 model,
32 providerDriver,
33 providers,
34 sources,
35 } = input;
36 const messages = useMessages();
37 const [panel, setPanel] = useState<{
38 readonly key: string;
39 readonly threadKey: string;
40 readonly now: number;
41 } | null>(null);
42 if (panel !== null && panelKey !== null && panel.key !== panelKey) {
43 setPanel(null);
44 }
45 const report =
46 panel !== null && panelKey !== null && panel.key === panelKey && providerInstanceId !== null
47 ? collectProviderUsageLimits(providerInstanceId, providers, sources, panel.now, model)
48 : null;
49 const banner =
50 report !== null && panel !== null
51 ? // A fresh id per opening: the stack keeps the last dismissed id as "exiting".
52 usageLimitsBannerItem(
53 `usage-limits:${panel.key}:${panel.now}`,
54 report,
55 environmentId,
56 () => setPanel(null),
57 messages,
58 )
59 : null;
60 // Intercept /usage-limits only when Limits has selected-provider data.
61 const offered =
62 providerDriver !== null && hasProviderUsageLimits(providerDriver, providers, sources);
63
64 const open = useStableCallback(() => {
65 const now = Date.now();
66 const nextReport =
67 providerInstanceId !== null && panelKey !== null
68 ? collectProviderUsageLimits(providerInstanceId, providers, sources, now, model)
69 : null;
70 if (nextReport && panelKey !== null) {
71 setPanel({ key: panelKey, threadKey, now });
72 return true;
73 }
74 setPanel(null);
75 toastManager.add({
76 type: "info",
77 title: m(msg("Usage limits are unavailable for this provider")),
78 });
79 return false;
80 });
81 // Async responses may clear only their originating thread's panel.
82 const clearFor = useStableCallback((originThreadKey: string) =>
83 setPanel((current) =>
84 current !== null && current.threadKey === originThreadKey ? null : current,
85 ),
86 );
87
88 return { banner, offered, open, clearFor };
89 }