@@ -14,18 +14,24 @@ public sealed partial class UsagePage : Page
1414{
1515 private static App CurrentApp => ( App ) Microsoft . UI . Xaml . Application . Current ;
1616 private AppState ? _appState ;
17+ private IOperatorGatewayClient ? _trackedClient ;
1718 // Default matches the XAML-selected Period7DaysItem (IsSelected="True").
1819 private int _currentPeriodDays = 7 ;
1920 private readonly AsyncListLoadingState _providerLoading = new ( ) ;
2021 private readonly AsyncListLoadingState _dailyCostLoading = new ( ) ;
2122 private DateTime _lastAppliedUsageCostUpdatedAtUtc = DateTime . MinValue ;
2223
24+ private const string DailyEmptyMessage = "No daily usage for this period" ;
25+ private const string ProviderEmptyMessage = "No providers configured" ;
26+ private const string DisconnectedListMessage = "Couldn't load. Check your gateway connection." ;
27+
2328 public UsagePage ( )
2429 {
2530 InitializeComponent ( ) ;
2631 Unloaded += ( _ , _ ) =>
2732 {
2833 if ( _appState != null ) _appState . PropertyChanged -= OnAppStateChanged ;
34+ DetachClient ( ) ;
2935 } ;
3036 }
3137
@@ -34,40 +40,95 @@ public void Initialize()
3440 if ( _appState != null ) _appState . PropertyChanged -= OnAppStateChanged ;
3541 _appState = CurrentApp . AppState ;
3642 _appState . PropertyChanged += OnAppStateChanged ;
43+
3744 var client = CurrentApp . GatewayClient ;
38- if ( client != null )
39- {
40- ConnectionInfoBar . IsOpen = false ;
41- // Apply cached data immediately, then request fresh.
42- if ( _appState ? . Usage != null ) UpdateUsage ( _appState . Usage ) ;
43- // Only apply cached cost data when its period matches the current
44- // selection — otherwise the daily list briefly shows e.g. 30-day
45- // data while the selector reads "7 Days".
46- if ( _appState ? . UsageCost != null && _appState . UsageCost . Days == _currentPeriodDays )
47- {
48- UpdateUsageCost ( _appState . UsageCost ) ;
49- _dailyCostLoading . BeginRefresh ( ) ;
50- }
51- else
52- {
53- _dailyCostLoading . BeginInitialRefresh ( ) ;
54- }
55- if ( _appState ? . UsageStatus != null ) UpdateUsageStatus ( _appState . UsageStatus ) ;
56- else _providerLoading . BeginInitialRefresh ( ) ;
57- UpdateDailyCostLoadingVisuals ( ) ;
58- UpdateProviderLoadingVisuals ( ) ;
59- _ = client . RequestUsageAsync ( ) ;
60- _ = client . RequestUsageCostAsync ( _currentPeriodDays ) ;
61- _ = client . RequestUsageStatusAsync ( ) ;
62- }
63- else
45+ AttachClient ( client ) ;
46+
47+ // A non-null client may still be disconnected (e.g. WebSocket reconnecting).
48+ // The underlying RequestUsage*/RequestUsageCost*/RequestUsageStatus calls
49+ // silently no-op when !IsConnectedToGateway, so without this check the
50+ // page would spin its progress rings forever — see user reports of
51+ // "page not loading, no values".
52+ if ( client == null || ! client . IsConnectedToGateway )
6453 {
6554 _providerLoading . Fail ( ) ;
6655 _dailyCostLoading . Fail ( ) ;
6756 ShowDisconnected ( ) ;
6857 UpdateProviderLoadingVisuals ( ) ;
6958 UpdateDailyCostLoadingVisuals ( ) ;
59+ return ;
60+ }
61+
62+ ConnectionInfoBar . IsOpen = false ;
63+ // Apply cached data immediately, then request fresh.
64+ if ( _appState ? . Usage != null ) UpdateUsage ( _appState . Usage ) ;
65+ // Only apply cached cost data when its period matches the current
66+ // selection — otherwise the daily list briefly shows e.g. 30-day
67+ // data while the selector reads "7 Days".
68+ if ( _appState ? . UsageCost != null && _appState . UsageCost . Days == _currentPeriodDays )
69+ {
70+ UpdateUsageCost ( _appState . UsageCost ) ;
71+ _dailyCostLoading . BeginRefresh ( ) ;
7072 }
73+ else
74+ {
75+ _dailyCostLoading . BeginInitialRefresh ( ) ;
76+ }
77+ if ( _appState ? . UsageStatus != null ) UpdateUsageStatus ( _appState . UsageStatus ) ;
78+ else _providerLoading . BeginInitialRefresh ( ) ;
79+ UpdateDailyCostLoadingVisuals ( ) ;
80+ UpdateProviderLoadingVisuals ( ) ;
81+ RequestRefresh ( client ) ;
82+ }
83+
84+ private void AttachClient ( IOperatorGatewayClient ? client )
85+ {
86+ if ( ReferenceEquals ( _trackedClient , client ) ) return ;
87+ DetachClient ( ) ;
88+ _trackedClient = client ;
89+ if ( _trackedClient != null )
90+ {
91+ _trackedClient . StatusChanged += OnClientStatusChanged ;
92+ }
93+ }
94+
95+ private void DetachClient ( )
96+ {
97+ if ( _trackedClient != null )
98+ {
99+ _trackedClient . StatusChanged -= OnClientStatusChanged ;
100+ _trackedClient = null ;
101+ }
102+ }
103+
104+ private void OnClientStatusChanged ( object ? sender , ConnectionStatus status )
105+ {
106+ // Recover automatically when the gateway comes online while the page
107+ // is open (otherwise the user is stuck on the disconnected info bar
108+ // with stale cards until they navigate away and back).
109+ if ( sender is not IOperatorGatewayClient client ) return ;
110+ if ( ! client . IsConnectedToGateway ) return ;
111+
112+ var dispatcher = DispatcherQueue ;
113+ if ( dispatcher == null ) return ;
114+ dispatcher . TryEnqueue ( ( ) =>
115+ {
116+ if ( _trackedClient != client ) return ;
117+ ConnectionInfoBar . IsOpen = false ;
118+ if ( ! _dailyCostLoading . HasLoaded ) _dailyCostLoading . BeginInitialRefresh ( ) ;
119+ else _dailyCostLoading . BeginRefresh ( ) ;
120+ if ( ! _providerLoading . HasLoaded ) _providerLoading . BeginInitialRefresh ( ) ;
121+ UpdateDailyCostLoadingVisuals ( ) ;
122+ UpdateProviderLoadingVisuals ( ) ;
123+ RequestRefresh ( client ) ;
124+ } ) ;
125+ }
126+
127+ private void RequestRefresh ( IOperatorGatewayClient client )
128+ {
129+ _ = client . RequestUsageAsync ( ) ;
130+ _ = client . RequestUsageCostAsync ( _currentPeriodDays ) ;
131+ _ = client . RequestUsageStatusAsync ( ) ;
71132 }
72133
73134 private void OnOpenConnectionClick ( object sender , RoutedEventArgs e )
@@ -100,13 +161,24 @@ public void UpdateUsage(GatewayUsageInfo usage)
100161
101162 public void UpdateUsageCost ( GatewayCostUsageInfo cost )
102163 {
103- if ( cost . Days != _currentPeriodDays )
104- return ;
105-
106164 if ( cost . UpdatedAt < _lastAppliedUsageCostUpdatedAtUtc )
107165 return ;
108166
167+ // The Windows tray fires usage.cost twice per refresh: once via
168+ // RequestUsageAsync() (always days=30) and once directly via the
169+ // selector (currently 7 or 30). If the gateway ignores the `days`
170+ // request param or only replies to one of the two, the page used to
171+ // reject the response that didn't match _currentPeriodDays and the
172+ // spinner ran forever. Accept whatever days the server returns and,
173+ // when the selector is out of sync, silently snap it to that period
174+ // so the header isn't lying about what data the user sees.
175+ if ( cost . Days > 0 && cost . Days != _currentPeriodDays )
176+ {
177+ SyncSelectorToServerDays ( cost . Days ) ;
178+ }
179+
109180 _lastAppliedUsageCostUpdatedAtUtc = cost . UpdatedAt ;
181+ ConnectionInfoBar . IsOpen = false ;
110182 TotalCostText . Text = $ "${ cost . Totals . TotalCost : F2} ";
111183 TokenCountText . Text = FormatLargeNumber ( cost . Totals . TotalTokens ) ;
112184
@@ -119,8 +191,24 @@ public void UpdateUsageCost(GatewayCostUsageInfo cost)
119191 UpdateDailyCostLoadingVisuals ( ) ;
120192 }
121193
194+ private void SyncSelectorToServerDays ( int days )
195+ {
196+ // Only the two SelectorBar items are valid targets; ignore anything
197+ // else (e.g. server-defaulted 14 days) and just keep the user's pick.
198+ if ( days != 7 && days != 30 ) return ;
199+ _currentPeriodDays = days ;
200+ var target = days == 30 ? Period30DaysItem : Period7DaysItem ;
201+ if ( ! ReferenceEquals ( PeriodSelector . SelectedItem , target ) )
202+ {
203+ PeriodSelector . SelectionChanged -= OnPeriodSelectionChanged ;
204+ try { PeriodSelector . SelectedItem = target ; }
205+ finally { PeriodSelector . SelectionChanged += OnPeriodSelectionChanged ; }
206+ }
207+ }
208+
122209 public void UpdateUsageStatus ( GatewayUsageStatusInfo status )
123210 {
211+ ConnectionInfoBar . IsOpen = false ;
124212 ProviderCountText . Text = status . Providers . Count . ToString ( ) ;
125213 ProviderListView . ItemsSource = status . Providers . Select ( p => new ProviderRow
126214 {
@@ -130,7 +218,6 @@ public void UpdateUsageStatus(GatewayUsageStatusInfo status)
130218 Status = p . Error ?? "" ,
131219 } ) . ToList ( ) ;
132220
133- bool hasProviders = status . Providers . Count > 0 ;
134221 _providerLoading . Complete ( status . Providers . Count ) ;
135222 UpdateProviderLoadingVisuals ( ) ;
136223 }
@@ -152,9 +239,10 @@ private void SelectPeriod(int days)
152239 _dailyCostLoading . BeginInitialRefresh ( ) ;
153240 UpdateDailyCostLoadingVisuals ( ) ;
154241
155- if ( CurrentApp . GatewayClient != null )
242+ var client = CurrentApp . GatewayClient ;
243+ if ( client != null && client . IsConnectedToGateway )
156244 {
157- _ = CurrentApp . GatewayClient . RequestUsageCostAsync ( days ) ;
245+ _ = client . RequestUsageCostAsync ( days ) ;
158246 }
159247 else
160248 {
@@ -168,15 +256,22 @@ private void UpdateDailyCostLoadingVisuals()
168256 {
169257 DailyLoadingPanel . Visibility = _dailyCostLoading . ShouldShowLoading ? Visibility . Visible : Visibility . Collapsed ;
170258 DailyListView . Visibility = _dailyCostLoading . ShouldShowContent ? Visibility . Visible : Visibility . Collapsed ;
171- DailyEmptyText . Visibility = _dailyCostLoading . ShouldShowEmpty ? Visibility . Visible : Visibility . Collapsed ;
259+ // After Fail() the state is !IsRefreshing && !HasLoaded, which leaves the
260+ // card visually empty (no spinner, no rows, no message). Surface a
261+ // message in that case so the page never looks frozen.
262+ bool failed = ! _dailyCostLoading . IsRefreshing && ! _dailyCostLoading . HasLoaded ;
263+ DailyEmptyText . Text = failed ? DisconnectedListMessage : DailyEmptyMessage ;
264+ DailyEmptyText . Visibility = ( _dailyCostLoading . ShouldShowEmpty || failed ) ? Visibility . Visible : Visibility . Collapsed ;
172265 PeriodSelector . IsEnabled = _dailyCostLoading . CanEdit ;
173266 }
174267
175268 private void UpdateProviderLoadingVisuals ( )
176269 {
177270 ProviderLoadingPanel . Visibility = _providerLoading . ShouldShowLoading ? Visibility . Visible : Visibility . Collapsed ;
178271 ProviderListView . Visibility = _providerLoading . ShouldShowContent ? Visibility . Visible : Visibility . Collapsed ;
179- ProviderEmptyText . Visibility = _providerLoading . ShouldShowEmpty ? Visibility . Visible : Visibility . Collapsed ;
272+ bool failed = ! _providerLoading . IsRefreshing && ! _providerLoading . HasLoaded ;
273+ ProviderEmptyText . Text = failed ? DisconnectedListMessage : ProviderEmptyMessage ;
274+ ProviderEmptyText . Visibility = ( _providerLoading . ShouldShowEmpty || failed ) ? Visibility . Visible : Visibility . Collapsed ;
180275 }
181276
182277 private void ShowDisconnected ( )
0 commit comments