From d55a92623c11b0b956fd38f608d3c6d02f0387eb Mon Sep 17 00:00:00 2001 From: SuperDooper <37051355+superdooper86@users.noreply.github.com> Date: Fri, 8 May 2026 16:00:04 +0200 Subject: [PATCH] fix: add LoadFromCacheAsync, same JS fallbacks in WebView2 refresh path --- windows/UsageViewModel.cs | 51 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 5 deletions(-) diff --git a/windows/UsageViewModel.cs b/windows/UsageViewModel.cs index 7239b24..a3b9239 100644 --- a/windows/UsageViewModel.cs +++ b/windows/UsageViewModel.cs @@ -191,12 +191,21 @@ public class UsageViewModel : INotifyPropertyChanged try { const string script = @"(async()=>{try{ - const b=await(await fetch('/api/bootstrap',{headers:{accept:'application/json'}})).json(); - const id=b?.memberships?.[0]?.organization?.uuid||b?.organizations?.[0]?.uuid - ||b?.default_organization?.uuid||null; + const h={headers:{accept:'application/json'}}; + const b=await(await fetch('/api/bootstrap',h)).json(); + let id=b?.memberships?.[0]?.organization?.uuid||b?.organizations?.[0]?.uuid + ||b?.default_organization?.uuid||null; const em=b?.account?.email_address||b?.account?.email||b?.email||null; - if(!id)return{email:em,orgId:null,usage:null}; - const u=await(await fetch('/api/organizations/'+id+'/usage',{headers:{accept:'application/json'}})).json(); + if(!id){ + try{const ol=await(await fetch('/api/organizations',h)).json(); + if(Array.isArray(ol)&&ol.length>0)id=ol[0]?.uuid||null;}catch(e2){} + } + if(!id){ + try{const pu=await(await fetch('/api/usage',h)).json(); + if(pu&&!pu.error)return{email:em,orgId:null,usage:pu};}catch(e3){} + return{email:em,orgId:null,usage:null}; + } + const u=await(await fetch('/api/organizations/'+id+'/usage',h)).json(); return{email:em,orgId:id,usage:u}; }catch(ex){return null;}})()"; @@ -237,6 +246,38 @@ public class UsageViewModel : INotifyPropertyChanged catch { return ([], null, null); } } + public async Task LoadFromCacheAsync() + { + var email = AppSettings.Default.Email; + var orgId = AppSettings.Default.OrgId; + var cookie = AppSettings.Default.CookieStore; + + bool isAuth = !string.IsNullOrEmpty(email) || !string.IsNullOrEmpty(orgId) + || !string.IsNullOrEmpty(cookie); + if (!isAuth) return; + + List limits = []; + if (!string.IsNullOrEmpty(AppSettings.Default.UsageJson)) + { + try + { + var cached = JsonSerializer.Deserialize( + AppSettings.Default.UsageJson, JsonOpts); + limits = BuildLimits(cached); + } + catch { } + } + + await Application.Current.Dispatcher.InvokeAsync(() => + { + if (!string.IsNullOrEmpty(email)) UserEmail = email; + if (limits.Count > 0) Limits = limits; + IsSignedIn = true; + ErrorMessage = limits.Count == 0 ? "Signed in — refreshing data…" : null; + LastUpdated = limits.Count > 0 ? DateTime.Now : LastUpdated; + }); + } + public async Task SignOutAsync() { AppSettings.Default.CookieStore = "";