11import { useNavigation } from "@react-navigation/native" ;
2+ import { useAtomValue } from "@effect/atom-react" ;
23import type { DailyTotals , MergedUsage } from "@t3tools/shared/usageMerge" ;
4+ import {
5+ buildProviderRateLimitsView ,
6+ type ProviderRateLimitsView ,
7+ type RateLimitWindowView ,
8+ } from "@t3tools/shared/providerRateLimits" ;
39import {
410 enumerateDays ,
511 enumerateHourStarts ,
@@ -11,13 +17,22 @@ import {
1117 formatUsd ,
1218 makeWindow ,
1319} from "@t3tools/shared/usageFormat" ;
14- import { useMemo , useState } from "react" ;
15- import { Platform , Pressable , RefreshControl , ScrollView , View } from "react-native" ;
20+ import { useCallback , useEffect , useMemo , useRef , useState } from "react" ;
21+ import {
22+ ActivityIndicator ,
23+ Platform ,
24+ Pressable ,
25+ RefreshControl ,
26+ ScrollView ,
27+ View ,
28+ } from "react-native" ;
1629import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1730
1831import { AndroidScreenHeader } from "../../components/AndroidScreenHeader" ;
1932import { AppText as Text } from "../../components/AppText" ;
2033import { NativeStackScreenOptions } from "../../native/StackHeader" ;
34+ import { environmentServerConfigsAtom , serverEnvironment } from "../../state/server" ;
35+ import { useAtomCommand } from "../../state/use-atom-command" ;
2136import { useUsage , type EnvironmentUsageStatus } from "../../state/usage" ;
2237import { SettingsSection } from "../settings/components/SettingsSection" ;
2338import { UsageDailyChart } from "./UsageDailyChart" ;
@@ -44,6 +59,62 @@ export function UsageRouteScreen() {
4459 const { days : windowDays , window } = windowSelection ;
4560 const isPast24Hours = windowDays === 1 ;
4661 const { merged, environments, isPending, isPartial, refresh } = useUsage ( window ) ;
62+ const serverConfigs = useAtomValue ( environmentServerConfigsAtom ) ;
63+ const refreshProviderRateLimits = useAtomCommand ( serverEnvironment . refreshProviderRateLimits , {
64+ reportFailure : false ,
65+ } ) ;
66+ const refreshingPlanLimitsRef = useRef ( false ) ;
67+ const [ isRefreshingPlanLimits , setIsRefreshingPlanLimits ] = useState ( false ) ;
68+ const [ planLimitsRefreshFailed , setPlanLimitsRefreshFailed ] = useState ( false ) ;
69+ const rateLimitEnvironments = useMemo (
70+ ( ) =>
71+ environments . map ( ( environment ) => ( {
72+ environmentId : environment . environmentId ,
73+ label : environment . label ,
74+ providers : serverConfigs . get ( environment . environmentId ) ?. providers ?? [ ] ,
75+ } ) ) ,
76+ [ environments , serverConfigs ] ,
77+ ) ;
78+ const hasRateLimitSnapshots = rateLimitEnvironments . some ( ( environment ) =>
79+ environment . providers . some ( ( provider ) => ( provider . rateLimits ?. windows . length ?? 0 ) > 0 ) ,
80+ ) ;
81+ const [ rateLimitsNowMs , setRateLimitsNowMs ] = useState ( ( ) => Date . now ( ) ) ;
82+ useEffect ( ( ) => {
83+ if ( ! hasRateLimitSnapshots ) {
84+ return ;
85+ }
86+ setRateLimitsNowMs ( Date . now ( ) ) ;
87+ const intervalId = setInterval ( ( ) => setRateLimitsNowMs ( Date . now ( ) ) , 60_000 ) ;
88+ return ( ) => clearInterval ( intervalId ) ;
89+ } , [ hasRateLimitSnapshots ] ) ;
90+ const planLimits = useMemo (
91+ ( ) =>
92+ buildProviderRateLimitsView ( {
93+ environments : rateLimitEnvironments ,
94+ nowMs : rateLimitsNowMs ,
95+ } ) ,
96+ [ rateLimitEnvironments , rateLimitsNowMs ] ,
97+ ) ;
98+ const refreshPlanLimits = useCallback ( ( ) => {
99+ if ( refreshingPlanLimitsRef . current || planLimits . refreshTargets . length === 0 ) {
100+ return ;
101+ }
102+ refreshingPlanLimitsRef . current = true ;
103+ setIsRefreshingPlanLimits ( true ) ;
104+ setPlanLimitsRefreshFailed ( false ) ;
105+ void Promise . all (
106+ planLimits . refreshTargets . map ( ( target ) =>
107+ refreshProviderRateLimits ( {
108+ environmentId : target . environmentId ,
109+ input : { instanceId : target . instanceId } ,
110+ } ) ,
111+ ) ,
112+ ) . then ( ( results ) => {
113+ refreshingPlanLimitsRef . current = false ;
114+ setIsRefreshingPlanLimits ( false ) ;
115+ setPlanLimitsRefreshFailed ( results . some ( ( result ) => result . _tag === "Failure" ) ) ;
116+ } ) ;
117+ } , [ planLimits . refreshTargets , refreshProviderRateLimits ] ) ;
47118
48119 const days = useMemo (
49120 ( ) => enumerateDays ( window . sinceDay , window . untilDay ) ,
@@ -117,6 +188,13 @@ export function UsageRouteScreen() {
117188
118189 < UsageCoverageNotice environments = { environments } merged = { merged } isPartial = { isPartial } />
119190
191+ < PlanLimitsSection
192+ isRefreshing = { isRefreshingPlanLimits }
193+ refreshFailed = { planLimitsRefreshFailed }
194+ view = { planLimits }
195+ onRefresh = { refreshPlanLimits }
196+ />
197+
120198 { isPending ? (
121199 < Text className = "py-16 text-center text-base text-foreground-muted" >
122200 Scanning provider transcripts…
@@ -148,6 +226,136 @@ export function UsageRouteScreen() {
148226 ) ;
149227}
150228
229+ function PlanLimitsSection ( props : {
230+ readonly view : ProviderRateLimitsView ;
231+ readonly isRefreshing : boolean ;
232+ readonly refreshFailed : boolean ;
233+ readonly onRefresh : ( ) => void ;
234+ } ) {
235+ if ( props . view . providers . length === 0 && props . view . refreshTargets . length === 0 ) {
236+ return null ;
237+ }
238+
239+ return (
240+ < SettingsSection
241+ title = "Plan limits"
242+ card
243+ headerAction = {
244+ props . view . refreshTargets . length > 0 ? (
245+ < Pressable
246+ accessibilityLabel = "Refresh plan limits"
247+ accessibilityRole = "button"
248+ className = "min-h-8 min-w-16 flex-row items-center justify-center gap-2 rounded-full px-2 active:bg-subtle"
249+ disabled = { props . isRefreshing }
250+ onPress = { props . onRefresh }
251+ >
252+ { props . isRefreshing ? < ActivityIndicator size = "small" /> : null }
253+ < Text className = "text-sm font-t3-medium text-foreground-muted" >
254+ { props . isRefreshing ? "Refreshing" : "Refresh" }
255+ </ Text >
256+ </ Pressable >
257+ ) : null
258+ }
259+ >
260+ { props . refreshFailed ? (
261+ < Text className = "border-b border-border-subtle px-4 py-3 text-sm text-danger-foreground" >
262+ Some accounts could not refresh. Start a provider session and try again.
263+ </ Text >
264+ ) : null }
265+ { props . view . providers . length === 0 ? (
266+ < Text className = "px-4 py-4 text-sm text-foreground-muted" >
267+ No plan limits reported yet. Start a provider session, then refresh.
268+ </ Text >
269+ ) : null }
270+ { props . view . providers . map ( ( provider , index ) => (
271+ < View
272+ key = { provider . key }
273+ className = { index === 0 ? "gap-3 p-4" : "gap-3 border-t border-border-subtle p-4" }
274+ >
275+ < View className = "flex-row items-baseline justify-between gap-3" >
276+ < View className = "min-w-0 flex-1" >
277+ < Text className = "text-base font-t3-medium text-foreground" numberOfLines = { 1 } >
278+ { provider . displayName }
279+ { provider . planLabel ? ` · ${ provider . planLabel } ` : "" }
280+ </ Text >
281+ { provider . environmentLabel ? (
282+ < Text className = "text-xs text-foreground-tertiary" numberOfLines = { 1 } >
283+ { provider . environmentLabel }
284+ </ Text >
285+ ) : null }
286+ </ View >
287+ </ View >
288+
289+ { provider . windows . map ( ( window ) => (
290+ < PlanLimitWindowRow
291+ key = { window . id }
292+ accentColor = { provider . accentColor }
293+ window = { window }
294+ />
295+ ) ) }
296+
297+ { provider . notice ? (
298+ < Text className = "text-sm text-danger-foreground" > { provider . notice } </ Text >
299+ ) : null }
300+ </ View >
301+ ) ) }
302+ </ SettingsSection >
303+ ) ;
304+ }
305+
306+ function PlanLimitWindowRow ( props : {
307+ readonly window : RateLimitWindowView ;
308+ readonly accentColor : string | undefined ;
309+ } ) {
310+ const { window } = props ;
311+ const percentLabel = window . isReset ? "reset" : `${ Math . round ( window . usedPercent ) } %` ;
312+
313+ return (
314+ < View className = "gap-1.5" >
315+ < View className = "flex-row items-baseline justify-between gap-3" >
316+ < Text className = "text-sm text-foreground-muted" > { window . label } </ Text >
317+ < Text
318+ className = {
319+ window . status === "exhausted"
320+ ? "text-sm font-t3-medium tabular-nums text-danger-foreground"
321+ : "text-sm tabular-nums text-foreground-muted"
322+ }
323+ >
324+ { percentLabel }
325+ { ! window . isReset && window . resetsInLabel ? ` · ${ window . resetsInLabel } left` : "" }
326+ </ Text >
327+ </ View >
328+ < View
329+ accessibilityLabel = { `${ window . label } usage` }
330+ accessibilityRole = "progressbar"
331+ accessibilityValue = { {
332+ min : 0 ,
333+ max : 100 ,
334+ now : Math . round ( window . usedPercent ) ,
335+ } }
336+ className = "h-1.5 overflow-hidden rounded-full bg-subtle-strong"
337+ >
338+ < View
339+ className = {
340+ window . status === "exhausted"
341+ ? "h-full rounded-full bg-danger-foreground"
342+ : "h-full rounded-full bg-foreground-muted"
343+ }
344+ style = { {
345+ width : `${ window . usedPercent } %` ,
346+ ...( props . accentColor && window . status === "ok"
347+ ? { backgroundColor : props . accentColor }
348+ : { } ) ,
349+ } }
350+ />
351+ </ View >
352+ { window . detail ? (
353+ < Text className = "text-xs text-foreground-tertiary" > { window . detail } </ Text >
354+ ) : null }
355+ </ View >
356+ ) ;
357+ }
358+
151359function SegmentedControl < Value extends number | string > ( props : {
152360 readonly options : readonly { readonly value : Value ; readonly label : string } [ ] ;
153361 readonly selected : Value ;
0 commit comments