1- import { AuthPreviewOperateScope , type BrowserProfile } from "@t3tools/contracts" ;
2- import { useEffect } from "react" ;
1+ import {
2+ AuthPreviewOperateScope ,
3+ type BrowserProfile ,
4+ type EnvironmentId ,
5+ type ServerConfig ,
6+ } from "@t3tools/contracts" ;
7+ import { useEffect , useRef } from "react" ;
38
49import { useClientSettings , useClientSettingsHydrated } from "~/hooks/useSettings" ;
510import { useServerConfigs } from "~/state/entities" ;
6- import { useEnvironments } from "~/state/environments" ;
11+ import { useConnectedEnvironmentIds , useEnvironments } from "~/state/environments" ;
712import { previewEnvironment } from "~/state/preview" ;
813import { useEnvironmentsWithScope } from "~/state/session" ;
914import { useAtomCommand } from "~/state/use-atom-command" ;
@@ -17,7 +22,8 @@ const selectDefaultProfileId = (settings: { readonly browserDefaultProfileId: st
1722 * Tells each environment that hosts browser tabs which profiles this client
1823 * has, so agents can open tabs under them. Profiles live in client settings;
1924 * the server keeps only the latest report in memory. A reconnect brings a new
20- * server config, which sends the report again.
25+ * server config, which sends the report again. Disconnected environments and
26+ * unchanged reports are skipped: session and config churn re-runs the effect.
2127 */
2228export function BrowserProfileReporter ( ) {
2329 const hydrated = useClientSettingsHydrated ( ) ;
@@ -26,15 +32,45 @@ export function BrowserProfileReporter() {
2632 const { environments } = useEnvironments ( ) ;
2733 const serverConfigs = useServerConfigs ( ) ;
2834 const operable = useEnvironmentsWithScope ( environments , AuthPreviewOperateScope ) ;
35+ const connected = useConnectedEnvironmentIds ( ) ;
2936 const report = useAtomCommand ( previewEnvironment . reportProfiles , { reportFailure : false } ) ;
37+ const reported = useRef (
38+ new Map <
39+ EnvironmentId ,
40+ {
41+ readonly config : ServerConfig ;
42+ readonly profiles : ReadonlyArray < BrowserProfile > ;
43+ readonly defaultProfileId : string ;
44+ }
45+ > ( ) ,
46+ ) ;
3047
3148 useEffect ( ( ) => {
3249 if ( ! hydrated ) return ;
33- for ( const [ environmentId , config ] of serverConfigs ) {
34- if ( ! config . environment . capabilities . serverBrowser || ! operable . has ( environmentId ) ) continue ;
35- void report ( { environmentId, input : { profiles, defaultProfileId } } ) ;
50+ for ( const environmentId of reported . current . keys ( ) ) {
51+ if ( ! connected . includes ( environmentId ) ) reported . current . delete ( environmentId ) ;
3652 }
37- } , [ defaultProfileId , hydrated , operable , profiles , report , serverConfigs ] ) ;
53+ for ( const environmentId of connected ) {
54+ const config = serverConfigs . get ( environmentId ) ;
55+ if ( ! config ?. environment . capabilities . serverBrowser || ! operable . has ( environmentId ) ) continue ;
56+ const last = reported . current . get ( environmentId ) ;
57+ if (
58+ last ?. config === config &&
59+ last . profiles === profiles &&
60+ last . defaultProfileId === defaultProfileId
61+ ) {
62+ continue ;
63+ }
64+ const entry = { config, profiles, defaultProfileId } ;
65+ reported . current . set ( environmentId , entry ) ;
66+ void report ( { environmentId, input : { profiles, defaultProfileId } } ) . then ( ( result ) => {
67+ // Unsent: the next run of this effect tries again.
68+ if ( result . _tag === "Failure" && reported . current . get ( environmentId ) === entry ) {
69+ reported . current . delete ( environmentId ) ;
70+ }
71+ } ) ;
72+ }
73+ } , [ connected , defaultProfileId , hydrated , operable , profiles , report , serverConfigs ] ) ;
3874
3975 return null ;
4076}
0 commit comments