88import { relayOfflineReasonMessage } from "@t3tools/client-runtime/relay" ;
99import {
1010 isAtomCommandInterrupted ,
11+ settlePromise ,
1112 squashAtomCommandFailure ,
1213} from "@t3tools/client-runtime/state/runtime" ;
1314import {
@@ -20,7 +21,7 @@ import type {
2021 RelayEnvironmentStatusResponse ,
2122} from "@t3tools/contracts/relay" ;
2223import * as Option from "effect/Option" ;
23- import { type ReactNode , useCallback , useEffect , useEffectEvent , useState } from "react" ;
24+ import { type ReactNode , useEffect , useEffectEvent , useState } from "react" ;
2425
2526import { environmentCatalog } from "~/connection/catalog" ;
2627import { cn } from "~/lib/utils" ;
@@ -99,18 +100,15 @@ export function CloudEnvironmentConnectRows({
99100 if ( environmentsState . refreshing || environmentsState . offline ) return ;
100101 await refreshRelayEnvironments ( ) ;
101102 } ) ;
102- const connectRelayEnvironment = useCallback (
103- ( environment : RelayClientEnvironmentRecord ) =>
104- registerEnvironment (
105- new RelayConnectionRegistration ( {
106- target : new RelayConnectionTarget ( {
107- environmentId : environment . environmentId ,
108- label : environment . label ,
109- } ) ,
103+ const connectRelayEnvironment = ( environment : RelayClientEnvironmentRecord ) =>
104+ registerEnvironment (
105+ new RelayConnectionRegistration ( {
106+ target : new RelayConnectionTarget ( {
107+ environmentId : environment . environmentId ,
108+ label : environment . label ,
110109 } ) ,
111- ) ,
112- [ registerEnvironment ] ,
113- ) ;
110+ } ) ,
111+ ) ;
114112 const [ connectingEnvironmentIds , setConnectingEnvironmentIds ] = useState <
115113 ReadonlySet < EnvironmentId >
116114 > ( new Set ( ) ) ;
@@ -190,38 +188,43 @@ export function CloudEnvironmentConnectRows({
190188 // During onboarding selection a machine saved over another route already
191189 // has its own row elsewhere, and selecting it must not add a T3 Connect
192190 // route as a side effect, so it is left out here.
191+ const isVisibleEnvironment = ( environmentId : EnvironmentId ) =>
192+ environmentId !== primaryEnvironmentId &&
193+ ( showSavedEnvironments || ! savedById . has ( environmentId ) ) &&
194+ ! ( selection && savedWithoutRelay . has ( environmentId ) ) ;
193195 const visibleEnvironments = [ ...environmentsState . environments . values ( ) ] . filter (
194- ( { environment } ) =>
195- environment . environmentId !== primaryEnvironmentId &&
196- ( showSavedEnvironments || ! savedById . has ( environment . environmentId ) ) &&
197- ! ( selection && savedWithoutRelay . has ( environment . environmentId ) ) ,
196+ ( { environment } ) => isVisibleEnvironment ( environment . environmentId ) ,
198197 ) ;
199- const selectNewComputers = useEffectEvent ( ( ) => {
200- const seen = selection ?. autoSelectedComputers ;
201- if ( ! selection || ! seen ) return ;
202- for ( const { environment, status, availability } of visibleEnvironments ) {
203- const id = environment . environmentId ;
204- if ( availability === "checking" ) continue ;
205- if (
206- discoveredCompatibilityError ( status ) !== null ||
207- savedById . get ( id ) ?. connection . phase === "unsupported"
208- ) {
198+ const selectNewComputers = useEffectEvent (
199+ ( environments : typeof environmentsState . environments ) => {
200+ const seen = selection ?. autoSelectedComputers ;
201+ if ( ! selection || ! seen ) return ;
202+ for ( const { environment, status, availability } of environments . values ( ) ) {
203+ if ( ! isVisibleEnvironment ( environment . environmentId ) ) continue ;
204+ const id = environment . environmentId ;
205+ if ( availability === "checking" ) continue ;
206+ if (
207+ discoveredCompatibilityError ( status ) !== null ||
208+ savedById . get ( id ) ?. connection . phase === "unsupported"
209+ ) {
210+ seen . add ( id ) ;
211+ if ( selection . selectedIds . has ( id ) ) selection . onChange ( id , false ) ;
212+ continue ;
213+ }
214+ if ( seen . has ( id ) ) continue ;
209215 seen . add ( id ) ;
210- if ( selection . selectedIds . has ( id ) ) selection . onChange ( id , false ) ;
211- continue ;
216+ selection . onChange ( id , true ) ;
217+ if ( ! savedById . has ( id ) ) {
218+ void connectEnvironment ( environment ) . then ( ( connected ) => {
219+ if ( ! connected ) selection . onChange ( id , false ) ;
220+ } ) ;
221+ }
212222 }
213- if ( seen . has ( id ) ) continue ;
214- seen . add ( id ) ;
215- selection . onChange ( id , true ) ;
216- if ( ! savedById . has ( id ) ) {
217- void connectEnvironment ( environment ) . then ( ( connected ) => {
218- if ( ! connected ) selection . onChange ( id , false ) ;
219- } ) ;
220- }
221- }
222- } ) ;
223+ } ,
224+ ) ;
223225 useEffect ( ( ) => {
224- selectNewComputers ( ) ;
226+ // oxlint-disable-next-line react/set-state-in-effect -- Discovery results arrive from an external store and auto-selection starts relay connections, which mark themselves connecting.
227+ selectNewComputers ( environmentsState . environments ) ;
225228 } , [ environmentsState . environments ] ) ;
226229
227230 // Discovery clears its list on refresh, so poll only until a machine appears.
@@ -244,12 +247,9 @@ export function CloudEnvironmentConnectRows({
244247 if ( disposed || pending || ! visible ( ) ) return ;
245248 clearTimeout ( timer ) ;
246249 pending = true ;
247- try {
248- await refreshDiscoveryWhenIdle ( ) ;
249- } finally {
250- pending = false ;
251- schedule ( ) ;
252- }
250+ await settlePromise ( ( ) => refreshDiscoveryWhenIdle ( ) ) ;
251+ pending = false ;
252+ schedule ( ) ;
253253 } ;
254254 const onFocus = ( ) => void refresh ( ) ;
255255 const onVisibilityChange = ( ) => {
0 commit comments