11import { useAtomValue } from "@effect/atom-react" ;
2- import { useEffect , useSyncExternalStore } from "react" ;
32import type { EnvironmentId , PreviewRuntime , PreviewSessionSnapshot } from "@t3tools/contracts" ;
43
54import { isElectron } from "~/env" ;
@@ -62,46 +61,18 @@ function desktopHostsServerTab(
6261 ) ;
6362}
6463
65- // Fixed when a tab first appears, until someone views it with T3 on screen: moving a page between
66- // hosts replaces it, so a tab nobody looks at keeps its live page in the background window.
67- let backgroundHostedTabs = new Map < string , boolean > ( ) ;
68- const hostingListeners = new Set < ( ) => void > ( ) ;
69-
70- const hostingKey = ( environmentId : EnvironmentId , snapshot : RuntimeSnapshot ) =>
71- `${ environmentId } \u0000${ snapshot . threadId } \u0000${ snapshot . tabId } ` ;
72-
73- // A new map makes memoized callers look again.
74- const returnTabToPanel = ( key : string ) => {
75- if ( backgroundHostedTabs . get ( key ) !== true ) return ;
76- backgroundHostedTabs = new Map ( backgroundHostedTabs ) . set ( key , false ) ;
77- for ( const listener of hostingListeners ) listener ( ) ;
78- } ;
79-
80- const subscribeBackgroundHosting = ( listener : ( ) => void ) => {
81- hostingListeners . add ( listener ) ;
82- return ( ) => {
83- hostingListeners . delete ( listener ) ;
84- } ;
85- } ;
86-
87- export type BackgroundHosting = ReadonlyMap < string , boolean > ;
88-
89- export const readBackgroundHosting = ( ) : BackgroundHosting => backgroundHostedTabs ;
90-
91- export function useBackgroundHosting ( ) : BackgroundHosting {
92- return useSyncExternalStore ( subscribeBackgroundHosting , readBackgroundHosting ) ;
93- }
64+ // Fixed when a tab first appears: Electron cannot move a page between a window and a
65+ // `<webview>`, so changing hosts later would reload it and lose the agent's live page.
66+ const backgroundHostedTabs = new Map < string , boolean > ( ) ;
9467
9568/**
9669 * A panel `<webview>` created while the app window is hidden or minimized never paints, so an
9770 * agent tab that appears then runs in a never-shown desktop window and the panel streams it.
98- * `hosting` is the map from `useBackgroundHosting`, passed so memoized callers recompute.
9971 */
10072export function hostsServerTabInBackground (
10173 environmentId : EnvironmentId ,
10274 primaryEnvironmentId : EnvironmentId | null ,
10375 snapshot : RuntimeSnapshot | null | undefined ,
104- hosting : BackgroundHosting ,
10576) : boolean {
10677 if (
10778 ! snapshot ||
@@ -110,8 +81,8 @@ export function hostsServerTabInBackground(
11081 ) {
11182 return false ;
11283 }
113- const key = hostingKey ( environmentId , snapshot ) ;
114- const decided = backgroundHostedTabs . get ( key ) ?? hosting . get ( key ) ;
84+ const key = ` ${ environmentId } \u0000 ${ snapshot . threadId } \u0000 ${ snapshot . tabId } ` ;
85+ const decided = backgroundHostedTabs . get ( key ) ;
11586 if ( decided !== undefined ) return decided ;
11687 const background = window . desktopBridge ?. getWindowShown ?.( ) === false ;
11788 backgroundHostedTabs . set ( key , background ) ;
@@ -122,35 +93,16 @@ export function rendersServerTabNatively(
12293 environmentId : EnvironmentId ,
12394 primaryEnvironmentId : EnvironmentId | null ,
12495 snapshot : RuntimeSnapshot | null | undefined ,
125- hosting : BackgroundHosting ,
12696) : boolean {
12797 return (
12898 desktopHostsServerTab ( environmentId , primaryEnvironmentId , snapshot ) &&
129- ! hostsServerTabInBackground ( environmentId , primaryEnvironmentId , snapshot , hosting )
99+ ! hostsServerTabInBackground ( environmentId , primaryEnvironmentId , snapshot )
130100 ) ;
131101}
132102
133- /** For the tab the panel shows: a background tab moves into the panel once T3 has focus. */
134103export function useRendersServerTabNatively (
135104 environmentId : EnvironmentId ,
136105 snapshot : RuntimeSnapshot | null | undefined ,
137106) : boolean {
138- const primaryEnvironmentId = useAtomValue ( primaryEnvironmentIdAtom ) ;
139- const hosting = useBackgroundHosting ( ) ;
140- const background = hostsServerTabInBackground (
141- environmentId ,
142- primaryEnvironmentId ,
143- snapshot ,
144- hosting ,
145- ) ;
146- const backgroundKey = background && snapshot ? hostingKey ( environmentId , snapshot ) : null ;
147- // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- moves the viewed tab into the panel when the app window has focus, which React does not track
148- useEffect ( ( ) => {
149- if ( backgroundKey === null ) return ;
150- const moveToPanel = ( ) => returnTabToPanel ( backgroundKey ) ;
151- if ( document . hasFocus ( ) ) moveToPanel ( ) ;
152- window . addEventListener ( "focus" , moveToPanel ) ;
153- return ( ) => window . removeEventListener ( "focus" , moveToPanel ) ;
154- } , [ backgroundKey ] ) ;
155- return desktopHostsServerTab ( environmentId , primaryEnvironmentId , snapshot ) && ! background ;
107+ return rendersServerTabNatively ( environmentId , useAtomValue ( primaryEnvironmentIdAtom ) , snapshot ) ;
156108}
0 commit comments