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,14 @@ import {
1117 formatUsd ,
1218 makeWindow ,
1319} from "@t3tools/shared/usageFormat" ;
14- import { useMemo , useState } from "react" ;
20+ import { useEffect , useMemo , useState } from "react" ;
1521import { Platform , Pressable , RefreshControl , ScrollView , View } from "react-native" ;
1622import { useSafeAreaInsets } from "react-native-safe-area-context" ;
1723
1824import { AndroidScreenHeader } from "../../components/AndroidScreenHeader" ;
1925import { AppText as Text } from "../../components/AppText" ;
2026import { NativeStackScreenOptions } from "../../native/StackHeader" ;
27+ import { environmentServerConfigsAtom } from "../../state/server" ;
2128import { useUsage , type EnvironmentUsageStatus } from "../../state/usage" ;
2229import { SettingsSection } from "../settings/components/SettingsSection" ;
2330import { UsageDailyChart } from "./UsageDailyChart" ;
@@ -44,6 +51,36 @@ export function UsageRouteScreen() {
4451 const { days : windowDays , window } = windowSelection ;
4552 const isPast24Hours = windowDays === 1 ;
4653 const { merged, environments, isPending, isPartial, refresh } = useUsage ( window ) ;
54+ const serverConfigs = useAtomValue ( environmentServerConfigsAtom ) ;
55+ const rateLimitEnvironments = useMemo (
56+ ( ) =>
57+ environments . map ( ( environment ) => ( {
58+ environmentId : environment . environmentId ,
59+ label : environment . label ,
60+ providers : serverConfigs . get ( environment . environmentId ) ?. providers ?? [ ] ,
61+ } ) ) ,
62+ [ environments , serverConfigs ] ,
63+ ) ;
64+ const hasRateLimitSnapshots = rateLimitEnvironments . some ( ( environment ) =>
65+ environment . providers . some ( ( provider ) => ( provider . rateLimits ?. windows . length ?? 0 ) > 0 ) ,
66+ ) ;
67+ const [ rateLimitsNowMs , setRateLimitsNowMs ] = useState ( ( ) => Date . now ( ) ) ;
68+ useEffect ( ( ) => {
69+ if ( ! hasRateLimitSnapshots ) {
70+ return ;
71+ }
72+ setRateLimitsNowMs ( Date . now ( ) ) ;
73+ const intervalId = setInterval ( ( ) => setRateLimitsNowMs ( Date . now ( ) ) , 60_000 ) ;
74+ return ( ) => clearInterval ( intervalId ) ;
75+ } , [ hasRateLimitSnapshots ] ) ;
76+ const planLimits = useMemo (
77+ ( ) =>
78+ buildProviderRateLimitsView ( {
79+ environments : rateLimitEnvironments ,
80+ nowMs : rateLimitsNowMs ,
81+ } ) ,
82+ [ rateLimitEnvironments , rateLimitsNowMs ] ,
83+ ) ;
4784
4885 const days = useMemo (
4986 ( ) => enumerateDays ( window . sinceDay , window . untilDay ) ,
@@ -117,6 +154,8 @@ export function UsageRouteScreen() {
117154
118155 < UsageCoverageNotice environments = { environments } merged = { merged } isPartial = { isPartial } />
119156
157+ < PlanLimitsSection view = { planLimits } />
158+
120159 { isPending ? (
121160 < Text className = "py-16 text-center text-base text-foreground-muted" >
122161 Scanning provider transcripts…
@@ -148,6 +187,102 @@ export function UsageRouteScreen() {
148187 ) ;
149188}
150189
190+ function PlanLimitsSection ( props : { readonly view : ProviderRateLimitsView } ) {
191+ if ( props . view . providers . length === 0 ) {
192+ return null ;
193+ }
194+
195+ return (
196+ < SettingsSection title = "Plan limits" card >
197+ { props . view . providers . map ( ( provider , index ) => (
198+ < View
199+ key = { provider . key }
200+ className = { index === 0 ? "gap-3 p-4" : "gap-3 border-t border-border-subtle p-4" }
201+ >
202+ < View className = "flex-row items-baseline justify-between gap-3" >
203+ < View className = "min-w-0 flex-1" >
204+ < Text className = "text-base font-t3-medium text-foreground" numberOfLines = { 1 } >
205+ { provider . displayName }
206+ { provider . planLabel ? ` · ${ provider . planLabel } ` : "" }
207+ </ Text >
208+ { provider . environmentLabel ? (
209+ < Text className = "text-xs text-foreground-tertiary" numberOfLines = { 1 } >
210+ { provider . environmentLabel }
211+ </ Text >
212+ ) : null }
213+ </ View >
214+ </ View >
215+
216+ { provider . windows . map ( ( window ) => (
217+ < PlanLimitWindowRow
218+ key = { window . id }
219+ accentColor = { provider . accentColor }
220+ window = { window }
221+ />
222+ ) ) }
223+
224+ { provider . notice ? (
225+ < Text className = "text-sm text-danger-foreground" > { provider . notice } </ Text >
226+ ) : null }
227+ </ View >
228+ ) ) }
229+ </ SettingsSection >
230+ ) ;
231+ }
232+
233+ function PlanLimitWindowRow ( props : {
234+ readonly window : RateLimitWindowView ;
235+ readonly accentColor : string | undefined ;
236+ } ) {
237+ const { window } = props ;
238+ const percentLabel = window . isReset ? "reset" : `${ Math . round ( window . usedPercent ) } %` ;
239+
240+ return (
241+ < View className = "gap-1.5" >
242+ < View className = "flex-row items-baseline justify-between gap-3" >
243+ < Text className = "text-sm text-foreground-muted" > { window . label } </ Text >
244+ < Text
245+ className = {
246+ window . status === "exhausted"
247+ ? "text-sm font-t3-medium tabular-nums text-danger-foreground"
248+ : "text-sm tabular-nums text-foreground-muted"
249+ }
250+ >
251+ { percentLabel }
252+ { ! window . isReset && window . resetsInLabel ? ` · ${ window . resetsInLabel } left` : "" }
253+ </ Text >
254+ </ View >
255+ < View
256+ accessibilityLabel = { `${ window . label } usage` }
257+ accessibilityRole = "progressbar"
258+ accessibilityValue = { {
259+ min : 0 ,
260+ max : 100 ,
261+ now : Math . round ( window . usedPercent ) ,
262+ } }
263+ className = "h-1.5 overflow-hidden rounded-full bg-subtle-strong"
264+ >
265+ < View
266+ className = {
267+ window . status === "exhausted"
268+ ? "h-full rounded-full bg-danger-foreground"
269+ : "h-full rounded-full bg-foreground-muted"
270+ }
271+ style = { {
272+ width : `${ window . usedPercent } %` ,
273+ ...( props . accentColor && window . status === "ok"
274+ ? { backgroundColor : props . accentColor }
275+ : { } ) ,
276+ } }
277+ />
278+ </ View >
279+ { window . detail ? (
280+ < Text className = "text-xs text-foreground-tertiary" > { window . detail } </ Text >
281+ ) : null }
282+ </ View >
283+ ) ;
284+ }
285+
151286function SegmentedControl < Value extends number | string > ( props : {
152287 readonly options : readonly { readonly value : Value ; readonly label : string } [ ] ;
153288 readonly selected : Value ;
0 commit comments