1- import { Button } from "../../components/ui/Button" ;
21import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime" ;
32import { AsyncResult } from "effect/reactivity" ;
43import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell" ;
4+ import { limitRecoveryOwnsSnooze } from "@t3tools/client-runtime/state/thread-execution" ;
55import type { EnvironmentId } from "@t3tools/contracts" ;
66import {
77 CHATGPT_USAGE_LIMIT_CODE ,
88 CHATGPT_USAGE_URL ,
99 CHATGPT_WORKSPACE_ADMIN_REQUEST ,
1010 CHATGPT_WORKSPACE_USAGE_LIMIT_CODE ,
1111} from "@t3tools/shared/usageLimits" ;
12- import * as DateTime from "effect/DateTime" ;
1312import * as Clipboard from "expo-clipboard" ;
1413import { useState } from "react" ;
1514import { Linking , Pressable , View } from "react-native" ;
@@ -31,13 +30,22 @@ export function UsageLimitRecoveryCard({
3130 const [ error , setError ] = useState < string | null > ( null ) ;
3231 const resetAt = thread . runtime ?. usageLimitResetAt ?? null ;
3332 const canSchedule =
33+ thread . settledOverride !== "settled" &&
3434 resetAt !== null &&
3535 Date . parse ( resetAt ) > Date . parse ( thread . latestRun ?. completedAt ?? thread . updatedAt ) ;
3636 const resetPassed = useDeadlinePassed ( resetAt === null ? null : Date . parse ( resetAt ) ) ;
3737 const runId = thread . latestRun ?. runId ;
3838 const recovery = thread . limitRecovery ;
3939 const scheduled =
4040 recovery ?. runId === runId && recovery ?. resetAt === resetAt && recovery ?. autoResume ;
41+ const displayAt =
42+ canSchedule &&
43+ scheduled &&
44+ resetAt &&
45+ thread . snoozedUntil &&
46+ Date . parse ( thread . snoozedUntil ) > Date . parse ( resetAt )
47+ ? thread . snoozedUntil
48+ : resetAt ;
4149 const [ copied , setCopied ] = useState ( false ) ;
4250 const [ actionPending , setActionPending ] = useState ( false ) ;
4351 const code = thread . runtime ?. lastErrorCode ;
@@ -51,16 +59,19 @@ export function UsageLimitRecoveryCard({
5159 ! runId
5260 )
5361 return null ;
54- const resetText = resetAt
55- ? `Usage limit resets ${ DateTime . toDateUtc ( DateTime . makeUnsafe ( resetAt ) ) . toLocaleString ( ) } .`
62+ const resetText = displayAt
63+ ? `${ canSchedule && scheduled ? "Resumes" : "Resets" } ${ new Date ( displayAt ) . toLocaleString (
64+ undefined ,
65+ { month : "short" , day : "numeric" , hour : "numeric" , minute : "2-digit" } ,
66+ ) } .`
5667 : null ;
5768 const snoozed =
58- recovery ?. snooze === true &&
59- recovery . runId === runId &&
60- recovery . resetAt === resetAt &&
61- resetAt !== null &&
62- thread . snoozedUntil !== null &&
63- Date . parse ( thread . snoozedUntil ) === Date . parse ( resetAt ) ;
69+ limitRecoveryOwnsSnooze ( {
70+ runId,
71+ resetAt,
72+ recovery ,
73+ snoozedUntil : thread . snoozedUntil ,
74+ } ) && ! resetPassed ;
6475 async function toggle ( action : "resume" | "snooze" ) {
6576 if ( ! resetAt || ! runId || ! canSchedule ) return ;
6677 if ( action === "snooze" && ! snoozed && Date . parse ( resetAt ) <= Date . now ( ) ) {
@@ -123,50 +134,52 @@ export function UsageLimitRecoveryCard({
123134 < ProviderIcon provider = "codex" size = { 16 } />
124135 </ View >
125136 ) : null }
126- < View className = "min-w-0 flex-1 gap-2 " >
137+ < View className = "min-w-0 flex-1 gap-1 " >
127138 { chatGptLimit ? (
128- < View className = "gap-1" >
129- < Text className = "text-sm font-t3-medium text-foreground" >
130- ChatGPT usage limit reached
131- </ Text >
132- < Text className = "text-sm text-foreground-muted" >
133- { workspaceLimit
134- ? copied
135- ? "Request copied. Send it to your workspace admin."
136- : "You've reached your workspace's limit for using your ChatGPT plan in other apps. Copy a request to ask your workspace admin for increased usage limits."
137- : ( resetText ?? "Review your usage settings in ChatGPT." ) }
138- </ Text >
139- < Pressable
140- accessibilityRole = { action . role }
141- accessibilityState = { { disabled : actionPending } }
142- disabled = { actionPending }
143- hitSlop = { 14 }
144- className = "mt-1 self-start active:opacity-70"
145- onPress = { action . run }
146- >
147- < Text className = "text-sm font-t3-medium text-primary" > { action . label } </ Text >
148- </ Pressable >
149- </ View >
150- ) : (
151- < Text className = "text-sm text-warning-foreground" >
152- { resetText ??
153- "The provider did not report a reset time. Retry manually when your limit is available." }
139+ < Text className = "text-sm font-t3-medium text-foreground" >
140+ ChatGPT usage limit reached
154141 </ Text >
155- ) }
156- { canSchedule ? (
157- < View className = "flex-row flex-wrap gap-2" >
158- < Button
159- label = { scheduled ? "Cancel auto-resume" : "Resume at reset" }
160- tone = "secondary"
161- disabled = { pending }
162- onPress = { ( ) => void toggle ( "resume" ) }
163- />
164- < Button
165- label = { snoozed ? "Wake now" : "Snooze until reset" }
166- tone = "secondary"
167- disabled = { pending || ( ! snoozed && resetPassed ) }
168- onPress = { ( ) => void toggle ( "snooze" ) }
169- />
142+ ) : null }
143+ < Text
144+ className = {
145+ chatGptLimit ? "text-sm text-foreground-muted" : "text-sm text-warning-foreground"
146+ }
147+ >
148+ { workspaceLimit
149+ ? copied
150+ ? "Request copied. Send it to your workspace admin."
151+ : "You've reached your workspace's limit for using your ChatGPT plan in other apps. Copy a request to ask your workspace admin for increased usage limits."
152+ : chatGptLimit
153+ ? ( resetText ?? "Review your usage settings in ChatGPT." )
154+ : resetText
155+ ? `Usage limit reached. ${ resetText } `
156+ : "Usage limit reached. The provider did not report a reset time. Retry manually when your limit is available." }
157+ </ Text >
158+ { chatGptLimit || canSchedule ? (
159+ // The margins cancel the 44pt touch height so the links sit 8pt under the text.
160+ < View className = "-mb-3 -mt-2 flex-row flex-wrap gap-x-4" >
161+ { chatGptLimit ? (
162+ < RecoveryAction
163+ label = { action . label }
164+ role = { action . role }
165+ disabled = { actionPending }
166+ onPress = { action . run }
167+ />
168+ ) : null }
169+ { canSchedule ? (
170+ < >
171+ < RecoveryAction
172+ label = { scheduled ? "Cancel auto-resume" : "Resume at reset" }
173+ disabled = { pending }
174+ onPress = { ( ) => void toggle ( "resume" ) }
175+ />
176+ < RecoveryAction
177+ label = { snoozed ? "Wake now" : "Snooze until reset" }
178+ disabled = { pending || ( ! snoozed && resetPassed ) }
179+ onPress = { ( ) => void toggle ( "snooze" ) }
180+ />
181+ </ >
182+ ) : null }
170183 </ View >
171184 ) : null }
172185 { error ? (
@@ -178,3 +191,31 @@ export function UsageLimitRecoveryCard({
178191 </ View >
179192 ) ;
180193}
194+
195+ function RecoveryAction ( props : {
196+ readonly label : string ;
197+ readonly role ?: "button" | "link" ;
198+ readonly disabled : boolean ;
199+ readonly onPress : ( ) => void ;
200+ } ) {
201+ return (
202+ < Pressable
203+ accessibilityRole = { props . role ?? "button" }
204+ accessibilityState = { { disabled : props . disabled } }
205+ disabled = { props . disabled }
206+ hitSlop = { { left : 8 , right : 8 } }
207+ onPress = { props . onPress }
208+ className = "min-h-11 justify-center active:opacity-70"
209+ >
210+ < Text
211+ className = {
212+ props . disabled
213+ ? "text-sm font-t3-medium text-foreground-muted"
214+ : "text-sm font-t3-medium text-primary"
215+ }
216+ >
217+ { props . label }
218+ </ Text >
219+ </ Pressable >
220+ ) ;
221+ }
0 commit comments