@@ -16,7 +16,7 @@ import {
1616 type UsageSummary ,
1717 type UsageSummaryInput ,
1818} from "@t3tools/contracts" ;
19- import { runAtomCommand } from "@t3tools/client-runtime/state/runtime " ;
19+ import { refreshUsage } from "@t3tools/client-runtime/state/usage " ;
2020import { mergeUsage , type EnvironmentUsage , type MergedUsage } from "@t3tools/shared/usageMerge" ;
2121import * as Option from "effect/Option" ;
2222import { AsyncResult , Atom } from "effect/unstable/reactivity" ;
@@ -72,7 +72,7 @@ export interface UsageView {
7272 * improve by waiting on them, so they must not read as "still reporting".
7373 */
7474 readonly isPartial : boolean ;
75- readonly refresh : ( ) => void ;
75+ readonly refresh : ( input ?: UsageSummaryInput ) => Promise < void > ;
7676}
7777
7878export function useUsage ( input : UsageSummaryInput ) : UsageView {
@@ -98,26 +98,17 @@ export function useUsage(input: UsageSummaryInput): UsageView {
9898 const atom = usageByWindowAtom ( windowKey ) ;
9999 const environments = useAtomValue ( atom ) ;
100100
101- // Refreshing only the derived atom would re-read the per-environment SWR
102- // queries within their stale window and change nothing. Refresh each
103- // environment's query so pull-to-refresh always rescans.
104- //
105- // Each environment refetches model pricing first, so a model released since
106- // its last daily fetch gets priced by the rescan. The rescan runs whether or
107- // not the refetch succeeds: an offline environment still recounts tokens.
108- const refresh = useCallback ( ( ) => {
109- const input = JSON . parse ( windowKey ) as UsageSummaryInput ;
110- for ( const environment of environments ) {
111- const { environmentId } = environment ;
112- const query = serverEnvironment . usageSummary ( { environmentId, input } ) ;
113- void runAtomCommand (
114- appAtomRegistry ,
115- serverEnvironment . refreshUsageRates ,
116- { environmentId, input : { } } ,
117- { reportFailure : false } ,
118- ) . finally ( ( ) => appAtomRegistry . refresh ( query ) ) ;
119- }
120- } , [ environments , windowKey ] ) ;
101+ const refresh = useCallback (
102+ ( nextInput ?: UsageSummaryInput ) =>
103+ refreshUsage ( {
104+ registry : appAtomRegistry ,
105+ server : serverEnvironment ,
106+ presentations : environmentPresentations ,
107+ environmentIds : environments . map ( ( { environmentId } ) => environmentId ) ,
108+ input : nextInput ?? ( JSON . parse ( windowKey ) as UsageSummaryInput ) ,
109+ } ) ,
110+ [ environments , windowKey ] ,
111+ ) ;
121112
122113 const merged = useMemo ( ( ) => {
123114 const answered : EnvironmentUsage [ ] = environments . flatMap ( ( environment ) =>
0 commit comments