1- import type { Vendor , VendorUsageData , VendorUsageResult } from "../../global" ;
1+ import type { SubscriptionItem , Vendor , VendorUsageData , VendorUsageResult } from "../../global" ;
2+ import { getSubscriptionVendors , shouldShowVendorUsage } from "../helpers" ;
23import { t } from "../i18n" ;
34import { toast } from "../toast" ;
45import { store } from "./state.svelte" ;
@@ -7,6 +8,26 @@ export function vendorUsageSummary(vendorName: string): string {
78 return store . vendorUsage [ vendorName ] ?. summary || "" ;
89}
910
11+ export function vendorUsageSummaryForVendor ( vendor : Vendor , subscriptions : SubscriptionItem [ ] ) : string {
12+ if ( ! shouldShowVendorUsage ( vendor , subscriptions ) ) return "" ;
13+ return vendorUsageSummary ( vendor . name ) ;
14+ }
15+
16+ export function clearVendorUsage ( vendorName : string ) : void {
17+ const name = String ( vendorName || "" ) . trim ( ) ;
18+ if ( ! name ) return ;
19+ delete store . vendorUsage [ name ] ;
20+ delete store . vendorUsageLoading [ name ] ;
21+ }
22+
23+ export function pruneVendorUsageForSubscriptions ( subscriptions : SubscriptionItem [ ] ) : void {
24+ for ( const vendor of getSubscriptionVendors ( store . profiles ) ) {
25+ if ( ! shouldShowVendorUsage ( vendor , subscriptions ) ) {
26+ clearVendorUsage ( vendor . name ) ;
27+ }
28+ }
29+ }
30+
1031export function isVendorUsageLoading ( vendorName : string ) : boolean {
1132 return Boolean ( store . vendorUsageLoading [ vendorName ] ) ;
1233}
@@ -74,6 +95,10 @@ export async function queryVendorUsage(vendor: Vendor, options: { silent?: boole
7495 const name = String ( vendor ?. name || "" ) . trim ( ) ;
7596 if ( ! name ) return ;
7697 if ( vendor . kind === "local" ) return ;
98+ if ( vendor . kind === "subscription" && ! shouldShowVendorUsage ( vendor , store . subscriptions ) ) {
99+ clearVendorUsage ( name ) ;
100+ return ;
101+ }
77102 if ( vendor . kind === "api" && ! vendorHasUsageCredentials ( vendor ) ) return ;
78103 const bridge = window . clovapiCli ;
79104 if ( ! bridge ?. profilesUsage ) {
@@ -87,7 +112,12 @@ export async function queryVendorUsage(vendor: Vendor, options: { silent?: boole
87112 const result = await bridge . profilesUsage ( name ) ;
88113 if ( ! result ?. ok || ! result . usage ?. success ) {
89114 const message = result ?. error || result ?. usage ?. error || t ( "toast.vendorUsageFailed" ) ;
90- if ( options . silent ) return ;
115+ if ( vendor . kind === "subscription" ) {
116+ clearVendorUsage ( name ) ;
117+ if ( options . silent ) return ;
118+ } else if ( options . silent ) {
119+ return ;
120+ }
91121 store . vendorUsage [ name ] = { summary : message , rows : [ ] , error : message } ;
92122 if ( ! options . silent ) toast . error ( message ) ;
93123 return ;
0 commit comments