Open History to see the last commit
1import { Button } from "../../components/ui/Button";
2import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
3import { AsyncResult } from "effect/reactivity";
4import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
5import type { EnvironmentId } from "@t3tools/contracts";
6import * as DateTime from "effect/DateTime";
7import { useState } from "react";
8import { View } from "react-native";
9import { AppText as Text } from "../../components/ui/AppText";
10import { useDeadlinePassed } from "../../lib/clock";
11import { threadEnvironment } from "../../state/threads";
12import { useAtomCommand } from "../../state/use-atom-command";
13
14export function UsageLimitRecoveryCard({
15 thread,
16 environmentId,
17}: {
18 thread: EnvironmentThreadShell;
19 environmentId: EnvironmentId;
20}) {
21 const updateMetadata = useAtomCommand(threadEnvironment.updateMetadata);
22 const [pending, setPending] = useState(false);
23 const [error, setError] = useState<string | null>(null);
24 const resetAt = thread.runtime?.usageLimitResetAt ?? null;
25 const canSchedule =
26 resetAt !== null &&
27 Date.parse(resetAt) > Date.parse(thread.latestRun?.completedAt ?? thread.updatedAt);
28 const resetPassed = useDeadlinePassed(resetAt === null ? null : Date.parse(resetAt));
29 const runId = thread.latestRun?.runId;
30 const recovery = thread.limitRecovery;
31 const scheduled =
32 recovery?.runId === runId && recovery?.resetAt === resetAt && recovery?.autoResume;
33 if (
34 thread.runtime?.status !== "failed" ||
35 thread.runtime.lastErrorClass !== "usage_limit" ||
36 !runId
37 )
38 return null;
39 const snoozed =
40 recovery?.snooze === true &&
41 recovery.runId === runId &&
42 recovery.resetAt === resetAt &&
43 resetAt !== null &&
44 thread.snoozedUntil !== null &&
45 Date.parse(thread.snoozedUntil) === Date.parse(resetAt);
46 async function toggle(action: "resume" | "snooze") {
47 if (!resetAt || !runId || !canSchedule) return;
48 if (action === "snooze" && !snoozed && Date.parse(resetAt) <= Date.now()) {
49 setError("The reset time has passed. Retry the thread manually.");
50 return;
51 }
52 setPending(true);
53 setError(null);
54 const result = await updateMetadata({
55 environmentId,
56 input: {
57 threadId: thread.id,
58 limitRecovery: {
59 runId,
60 resetAt,
61 ...(action === "resume" ? { autoResume: !scheduled } : { snooze: !snoozed }),
62 },
63 },
64 });
65 setPending(false);
66 if (AsyncResult.isFailure(result)) {
67 const cause = squashAtomCommandFailure(result);
68 setError(cause instanceof Error ? cause.message : "Could not change limit recovery.");
69 }
70 }
71 return (
72 <View className="mx-3 mb-2 gap-2 rounded-xl border border-warning-foreground/25 bg-screen p-3">
73 <Text className="text-sm text-warning-foreground">
74 {resetAt
75 ? `Usage limit resets ${DateTime.toDateUtc(DateTime.makeUnsafe(resetAt)).toLocaleString()}.`
76 : "The provider did not report a reset time. Retry manually when your limit is available."}
77 </Text>
78 {canSchedule ? (
79 <View className="flex-row flex-wrap gap-2">
80 <Button
81 label={scheduled ? "Cancel auto-resume" : "Resume at reset"}
82 tone="secondary"
83 disabled={pending}
84 onPress={() => void toggle("resume")}
85 />
86 <Button
87 label={snoozed ? "Wake now" : "Snooze until reset"}
88 tone="secondary"
89 disabled={pending || (!snoozed && resetPassed)}
90 onPress={() => void toggle("snooze")}
91 />
92 </View>
93 ) : null}
94 {error ? (
95 <Text accessibilityRole="alert" className="text-sm text-destructive">
96 {error}
97 </Text>
98 ) : null}
99 </View>
100 );
101}