@@ -12,7 +12,7 @@ import { needsCursorKeychainAccess, refreshUsage } from "@t3tools/client-runtime
1212import { resolveUsageAccess } from "@t3tools/client-runtime/state/usage-access" ;
1313import * as Option from "effect/Option" ;
1414import { AsyncResult , Atom } from "effect/reactivity" ;
15- import { useCallback , useMemo , useState } from "react" ;
15+ import { useState } from "react" ;
1616
1717import { mergeUsage , type EnvironmentUsage , type MergedUsage } from "@t3tools/shared/usageMerge" ;
1818import { appAtomRegistry } from "../rpc/atomRegistry" ;
@@ -132,64 +132,69 @@ function withoutProviders(
132132 ) ;
133133}
134134
135+ function deriveSelection (
136+ environments : readonly EnvironmentUsageStatus [ ] ,
137+ selectedEnvironmentIds : ReadonlySet < EnvironmentId > | null ,
138+ hiddenProviders : ReadonlySet < UsageProviderKind > ,
139+ ) {
140+ const selectedEnvironments =
141+ selectedEnvironmentIds === null
142+ ? environments
143+ : environments . filter ( ( environment ) => selectedEnvironmentIds . has ( environment . environmentId ) ) ;
144+ return {
145+ environments,
146+ selectedEnvironmentIds,
147+ hiddenProviders,
148+ selectedEnvironments,
149+ merged : mergeAnsweredUsage ( withoutProviders ( selectedEnvironments , hiddenProviders ) ) ,
150+ } ;
151+ }
152+
135153export function useUsage (
136154 input : UsageSummaryInput ,
137155 selectedEnvironmentIds : ReadonlySet < EnvironmentId > | null = null ,
138156 hiddenProviders : ReadonlySet < UsageProviderKind > = NO_HIDDEN_PROVIDERS ,
139157) : UsageView {
140- const windowKey = useMemo (
141- ( ) =>
142- JSON . stringify ( {
143- sinceDay : input . sinceDay ,
144- untilDay : input . untilDay ,
145- timeZone : input . timeZone ,
146- resolution : input . resolution ,
147- sinceTime : input . sinceTime ,
148- untilTime : input . untilTime ,
149- } ) ,
150- [
151- input . sinceDay ,
152- input . untilDay ,
153- input . timeZone ,
154- input . resolution ,
155- input . sinceTime ,
156- input . untilTime ,
157- ] ,
158- ) ;
158+ const windowKey = JSON . stringify ( {
159+ sinceDay : input . sinceDay ,
160+ untilDay : input . untilDay ,
161+ timeZone : input . timeZone ,
162+ resolution : input . resolution ,
163+ sinceTime : input . sinceTime ,
164+ untilTime : input . untilTime ,
165+ } ) ;
159166 const atom = usageByWindowAtom ( windowKey ) ;
160167 const environments = useAtomValue ( atom ) ;
161- const selectedEnvironments = useMemo (
162- ( ) =>
163- selectedEnvironmentIds === null
164- ? environments
165- : environments . filter ( ( environment ) =>
166- selectedEnvironmentIds . has ( environment . environmentId ) ,
167- ) ,
168- [ environments , selectedEnvironmentIds ] ,
169- ) ;
170-
171- const refresh = useCallback (
172- ( nextInput ?: UsageSummaryInput ) =>
173- refreshUsage ( {
174- registry : appAtomRegistry ,
175- server : serverEnvironment ,
176- presentations : environmentPresentations ,
177- environmentIds : selectedEnvironments
178- . filter (
179- ( environment ) =>
180- environment . canReadDiagnostics &&
181- readEnvironmentScope ( environment . environmentId , AuthDiagnosticsReadScope ) ,
182- )
183- . map ( ( { environmentId } ) => environmentId ) ,
184- input : nextInput ?? ( JSON . parse ( windowKey ) as UsageSummaryInput ) ,
185- } ) ,
186- [ selectedEnvironments , windowKey ] ,
168+ // The render-time sync below compares merged by identity, so the derivation is
169+ // held in state keyed on its inputs rather than rebuilt every render.
170+ const [ derived , setDerived ] = useState ( ( ) =>
171+ deriveSelection ( environments , selectedEnvironmentIds , hiddenProviders ) ,
187172 ) ;
173+ let current = derived ;
174+ if (
175+ derived . environments !== environments ||
176+ derived . selectedEnvironmentIds !== selectedEnvironmentIds ||
177+ derived . hiddenProviders !== hiddenProviders
178+ ) {
179+ current = deriveSelection ( environments , selectedEnvironmentIds , hiddenProviders ) ;
180+ setDerived ( current ) ;
181+ }
182+ const { selectedEnvironments, merged } = current ;
188183
189- const merged = useMemo (
190- ( ) => mergeAnsweredUsage ( withoutProviders ( selectedEnvironments , hiddenProviders ) ) ,
191- [ selectedEnvironments , hiddenProviders ] ,
192- ) ;
184+ const refresh = ( nextInput ?: UsageSummaryInput ) =>
185+ refreshUsage ( {
186+ registry : appAtomRegistry ,
187+ server : serverEnvironment ,
188+ presentations : environmentPresentations ,
189+ environmentIds : selectedEnvironments
190+ . filter (
191+ ( environment ) =>
192+ environment . canReadDiagnostics &&
193+ readEnvironmentScope ( environment . environmentId , AuthDiagnosticsReadScope ) ,
194+ )
195+ . map ( ( { environmentId } ) => environmentId ) ,
196+ input : nextInput ?? ( JSON . parse ( windowKey ) as UsageSummaryInput ) ,
197+ } ) ;
193198
194199 const answeredCount = selectedEnvironments . filter (
195200 ( environment ) => environment . summary !== null ,
0 commit comments