Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

ComposerUsageLimit.tsx

87 lines3.5 KB

1import { useEffect, useState } from "react";
2import { HourglassIcon, TimerResetIcon } from "lucide-react";
3​
4import { Button } from "../ui/button";
5import type { ComposerBannerStackItem } from "./ComposerBannerStack";
6import { UsageLimitCountdown } from "./UsageLimitCountdown";
7​
8/**
9 * The toggle for the usage-limit card. The unavailable state stays visible the
10 * whole window; the action toggles auto-resume on and back off. Auto-resume
11 * here mirrors the server's own sweep — both dispatch an ordinary turn when
12 * the window passes. The window tracking lives in this component (not in the
13 * `usageLimitBannerItem` factory) so hooks stay tied to a stable element and
14 * ChatView's hook order is unaffected by the banner appearing or disappearing.
15 */
16function UsageLimitAutoResumeAction(input: {
17 readonly resetsAt: string;
18 readonly autoResumeArmed: boolean;
19 readonly onArmAutoResume: () => void;
20 readonly onCancelAutoResume: () => void;
21}) {
22 const { resetsAt, autoResumeArmed, onArmAutoResume, onCancelAutoResume } = input;
23 // Track whether the window has passed so the arm action can't fire once the
24 // server would reject it; the countdown flips its label at the same moment,
25 // and a re-render every 30s is enough to catch the flip. Re-syncing whenever
26 // a new window arrives keeps a passed window from disabling the next one.
27 const [windowPassed, setWindowPassed] = useState(() => Date.now() >= Date.parse(resetsAt));
28 useEffect(() => {
29 setWindowPassed(Date.now() >= Date.parse(resetsAt));
30 const interval = window.setInterval(() => {
31 setWindowPassed(Date.now() >= Date.parse(resetsAt));
32 }, 30_000);
33 return () => window.clearInterval(interval);
34 }, [resetsAt]);
35 return (
36 <Button
37 size="xs"
38 variant="ghost"
39 // An expired window can't fire an arm the server would reject, but an
40 // armed thread must stay cancellable even then; only the unarmed
41 // expired case disables the button.
42 onClick={autoResumeArmed ? onCancelAutoResume : onArmAutoResume}
43 disabled={windowPassed && !autoResumeArmed}
44 >
45 {autoResumeArmed
46 ? "Auto-resume on — cancel"
47 : windowPassed
48 ? "Window passed — send a message to continue"
49 : "Continue when tokens return"}
50 </Button>
51 );
53​
54export function usageLimitBannerItem(input: {
55 readonly threadId: string;
56 readonly resetsAt: string;
57 readonly autoResumeArmed: boolean;
58 readonly onArmAutoResume: () => void;
59 readonly onCancelAutoResume: () => void;
60}): ComposerBannerStackItem {
61 const { threadId, resetsAt, autoResumeArmed, onArmAutoResume, onCancelAutoResume } = input;
62 return {
63 id: `thread-usage-limit:${threadId}`,
64 variant: "warning",
65 priority: "urgent",
66 icon: autoResumeArmed ? <TimerResetIcon /> : <HourglassIcon />,
67 title: (
68 <span className="flex min-w-0 items-center gap-1">
69 <span className="shrink-0">You've used all your plan usage</span>
70 <span className="text-muted-foreground">
71 · <UsageLimitCountdown resetsAt={resetsAt} />
72 </span>
73 </span>
74 ),
75 description: autoResumeArmed
76 ? "This thread restarts on its own when the window resets."
77 : "Tokens return when the window resets. You can also keep working on another plan.",
78 actions: (
79 <UsageLimitAutoResumeAction
80 resetsAt={resetsAt}
81 autoResumeArmed={autoResumeArmed}
82 onArmAutoResume={onArmAutoResume}
83 onCancelAutoResume={onCancelAutoResume}
84 />
85 ),
86 };