11import { ChatGptUsageSummary } from "./ChatGptUsageSummary" ;
22import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView" ;
3- import { EnvironmentId , USAGE_CONTRACT_VERSION } from "@t3tools/contracts" ;
3+ import { EnvironmentId , USAGE_CONTRACT_VERSION , type UsageProviderKind } from "@t3tools/contracts" ;
44import { type RouteProp , useIsFocused , useNavigation , useRoute } from "@react-navigation/native" ;
55import { cursorKeychainAccessEnvironments } from "@t3tools/client-runtime/state/usage" ;
6+ import {
7+ updatingProvidersLabel ,
8+ usageEnvironmentProgress ,
9+ usageProgress ,
10+ } from "@t3tools/client-runtime/state/usage-progress" ;
611import {
712 isCompatibleUsageContractVersion ,
813 isModelCostUnknown ,
@@ -21,9 +26,16 @@ import {
2126 formatUsd ,
2227 makeWindow ,
2328} from "@t3tools/shared/usageFormat" ;
24- import { useCallback , useLayoutEffect , useMemo , useRef , useState } from "react" ;
25- import { Platform , Pressable , RefreshControl , View } from "react-native" ;
26- import Animated , { FadeIn , ReduceMotion } from "react-native-reanimated" ;
29+ import { useCallback , useLayoutEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
30+ import { ActivityIndicator , Platform , Pressable , RefreshControl , View } from "react-native" ;
31+ import Animated , {
32+ FadeIn ,
33+ ReduceMotion ,
34+ useAnimatedStyle ,
35+ useSharedValue ,
36+ withDelay ,
37+ withTiming ,
38+ } from "react-native-reanimated" ;
2739import { useSafeAreaInsets } from "react-native-safe-area-context" ;
2840
2941import { SegmentedControl } from "../../components/SegmentedControl" ;
@@ -65,6 +77,7 @@ const METRIC_OPTIONS = [
6577] as const satisfies readonly { value : UsageChartMetric ; label : string } [ ] ;
6678
6779const CHART_HEIGHT = 180 ;
80+ const providerLabel = ( provider : UsageProviderKind ) => PROVIDER_LABEL [ provider ] ;
6881const CURSOR_KEYCHAIN_COPY = "Requires access to your Cursor login in macOS Keychain." ;
6982
7083/**
@@ -156,6 +169,10 @@ export function UsageRouteScreen() {
156169 const [ refreshingUsage , setRefreshingUsage ] = useState ( false ) ;
157170 const refreshingRef = useRef ( false ) ;
158171 const showingLimits = tab === "limits" ;
172+ const progress = usageProgress ( selectedEnvironments , {
173+ refreshing : refreshingUsage ,
174+ providerLabel,
175+ } ) ;
159176 const selectWindow = ( days : number ) => {
160177 setWindowSelection ( {
161178 days,
@@ -182,10 +199,6 @@ export function UsageRouteScreen() {
182199 } ;
183200
184201 const showEnvironmentFilter = environments . length > 0 || selectedEnvironmentIds !== null ;
185- const hasLoadingEnvironments = selectedEnvironments . some ( isUsageLoading ) ;
186- const filterAccessibilityLabel = hasLoadingEnvironments
187- ? "Filter usage environments, some environments are loading"
188- : "Filter usage environments" ;
189202 const filterIcon =
190203 selectedEnvironmentIds === null
191204 ? "line.3.horizontal.decrease"
@@ -201,14 +214,14 @@ export function UsageRouteScreen() {
201214 ...environments . map ( ( environment ) => ( {
202215 id : environment . environmentId ,
203216 title : environment . label ,
204- subtitle : usageEnvironmentStatus ( environment ) ,
217+ subtitle : usageEnvironmentStatus ( environment , refreshingUsage ) ,
205218 state :
206219 selectedEnvironmentIds === null || selectedEnvironmentIds . has ( environment . environmentId )
207220 ? ( "on" as const )
208221 : ( "off" as const ) ,
209222 } ) ) ,
210223 ] ,
211- [ environments , selectedEnvironmentIds ] ,
224+ [ environments , refreshingUsage , selectedEnvironmentIds ] ,
212225 ) ;
213226 const selectEnvironment = useCallback (
214227 ( value : string ) => {
@@ -227,37 +240,24 @@ export function UsageRouteScreen() {
227240 < ControlPillMenu
228241 accessible
229242 accessibilityRole = "button"
230- accessibilityLabel = { filterAccessibilityLabel }
243+ accessibilityLabel = "Filter usage environments"
231244 title = "Environments"
232245 actions = { environmentActions }
233246 onPressAction = { ( { nativeEvent } ) => selectEnvironment ( nativeEvent . event ) }
234247 >
235248 < Pressable
236249 accessibilityRole = "button"
237- accessibilityLabel = { filterAccessibilityLabel }
250+ accessibilityLabel = "Filter usage environments"
238251 className = { cn (
239252 "items-center justify-center rounded-full" ,
240253 Platform . OS === "ios" ? "size-[28px]" : "size-[44px]" ,
241254 ) }
242255 >
243256 < SymbolView name = { filterIcon } size = { 22 } tintColorClassName = "accent-icon" />
244- { hasLoadingEnvironments ? (
245- < View
246- pointerEvents = "none"
247- className = "absolute -right-[2px] -top-[2px] size-[9px] rounded-full bg-amber-500"
248- />
249- ) : null }
250257 </ Pressable >
251258 </ ControlPillMenu >
252259 ) : null ,
253- [
254- showEnvironmentFilter ,
255- environmentActions ,
256- selectEnvironment ,
257- filterAccessibilityLabel ,
258- filterIcon ,
259- hasLoadingEnvironments ,
260- ] ,
260+ [ showEnvironmentFilter , environmentActions , selectEnvironment , filterIcon ] ,
261261 ) ;
262262
263263 useLayoutEffect ( ( ) => {
@@ -361,26 +361,28 @@ export function UsageRouteScreen() {
361361 { message }
362362 </ Text >
363363 ) ) }
364- < ChartCard
365- merged = { merged }
366- days = { chartDays }
367- daily = { chartTotals }
368- metric = { metric }
369- sinceDay = { window . sinceDay }
370- untilDay = { window . untilDay }
371- isPast24Hours = { isPast24Hours }
372- timeZone = { window . timeZone }
373- />
374- < ProviderSection
375- merged = { merged }
376- metric = { metric }
377- cursorAccessEnvironments = { cursorAccessEnvironments }
378- showCursorEnvironment = { selectedEnvironments . length > 1 }
379- onCursorEnabled = { refreshAfterCursorEnable }
380- />
381- < TotalsSection merged = { merged } isPast24Hours = { isPast24Hours } />
382- < CostSection merged = { merged } />
383- < ModelsSection merged = { merged } metric = { metric } />
364+ < UsageUpdating dimmed = { progress . dimmed } label = { progress . label } >
365+ < ChartCard
366+ merged = { merged }
367+ days = { chartDays }
368+ daily = { chartTotals }
369+ metric = { metric }
370+ sinceDay = { window . sinceDay }
371+ untilDay = { window . untilDay }
372+ isPast24Hours = { isPast24Hours }
373+ timeZone = { window . timeZone }
374+ />
375+ < ProviderSection
376+ merged = { merged }
377+ metric = { metric }
378+ cursorAccessEnvironments = { cursorAccessEnvironments }
379+ showCursorEnvironment = { selectedEnvironments . length > 1 }
380+ onCursorEnabled = { refreshAfterCursorEnable }
381+ />
382+ < TotalsSection merged = { merged } isPast24Hours = { isPast24Hours } />
383+ < CostSection merged = { merged } />
384+ < ModelsSection merged = { merged } metric = { metric } />
385+ </ UsageUpdating >
384386 </ >
385387 ) }
386388 </ >
@@ -391,6 +393,53 @@ export function UsageRouteScreen() {
391393 ) ;
392394}
393395
396+ /**
397+ * Dims totals that are about to change and says what is still updating. The
398+ * status overlays the first child's top-right corner, the chart card's label
399+ * row, so appearing never moves anything.
400+ */
401+ function UsageUpdating ( {
402+ dimmed,
403+ label,
404+ children,
405+ } : {
406+ readonly dimmed : boolean ;
407+ readonly label : string | null ;
408+ readonly children : ReactNode ;
409+ } ) {
410+ const opacity = useSharedValue ( 1 ) ;
411+ useLayoutEffect ( ( ) => {
412+ // The delay keeps a quick cached answer from flashing the dim.
413+ opacity . set (
414+ dimmed
415+ ? withDelay ( 150 , withTiming ( 0.5 , { duration : 150 , reduceMotion : ReduceMotion . System } ) )
416+ : withTiming ( 1 , { duration : 150 , reduceMotion : ReduceMotion . System } ) ,
417+ ) ;
418+ } , [ opacity , dimmed ] ) ;
419+ const dimStyle = useAnimatedStyle ( ( ) => ( { opacity : opacity . get ( ) } ) ) ;
420+
421+ return (
422+ < View >
423+ < Animated . View style = { dimStyle } className = "gap-6" accessibilityState = { { busy : dimmed } } >
424+ { children }
425+ </ Animated . View >
426+ { label !== null ? (
427+ < Animated . View
428+ entering = { FadeIn . delay ( 150 ) . duration ( 150 ) . reduceMotion ( ReduceMotion . System ) }
429+ accessibilityLiveRegion = "polite"
430+ pointerEvents = "none"
431+ className = "absolute right-4 top-4 h-5 max-w-[55%] flex-row items-center gap-1.5"
432+ >
433+ < ActivityIndicator size = "small" colorClassName = "accent-adaptive-sky-600-400" />
434+ < Text className = "shrink text-sm text-adaptive-sky-600-400" numberOfLines = { 1 } >
435+ { label }
436+ </ Text >
437+ </ Animated . View >
438+ ) : null }
439+ </ View >
440+ ) ;
441+ }
442+
394443function CursorEnableAction ( {
395444 environmentId,
396445 label,
@@ -860,11 +909,7 @@ function ModelsSection(props: { readonly merged: MergedUsage; readonly metric: U
860909 * one that failed, or one whose transcripts another environment already
861910 * reported.
862911 */
863- function isUsageLoading ( environment : EnvironmentUsageStatus ) {
864- return environment . isPending || ( environment . summary === null && environment . error === null ) ;
865- }
866-
867- function usageEnvironmentStatus ( environment : EnvironmentUsageStatus ) : string {
912+ function usageEnvironmentStatus ( environment : EnvironmentUsageStatus , refreshing : boolean ) : string {
868913 if (
869914 environment . summary &&
870915 ! isCompatibleUsageContractVersion ( environment . summary . contractVersion , USAGE_CONTRACT_VERSION )
@@ -881,7 +926,10 @@ function usageEnvironmentStatus(environment: EnvironmentUsageStatus): string {
881926 return environment . summary ? `${ environment . error } Showing saved totals.` : environment . error ;
882927 if ( ! environment . isConnected )
883928 return environment . summary ? "Disconnected · showing saved usage" : "Waiting for connection…" ;
884- if ( isUsageLoading ( environment ) )
885- return environment . summary ? "Updating usage…" : "Loading usage…" ;
929+ const progress = usageEnvironmentProgress ( environment , refreshing ) ;
930+ if ( progress . phase === "loading" ) return "Loading usage…" ;
931+ if ( progress . phase === "stale" ) return "Updating usage…" ;
932+ if ( progress . phase === "partway" )
933+ return updatingProvidersLabel ( progress . providers , providerLabel ) ;
886934 return "Usage up to date" ;
887935}
0 commit comments