@@ -4,12 +4,17 @@ import {
44 addProjectRemoteSourceProvider ,
55 buildAddProjectRemoteSourceReadiness ,
66 buildProjectCreateCommand ,
7+ canCreateProjectInEnvironment ,
78 findExistingAddProject ,
89 getAddProjectInitialQuery ,
910 resolveAddProjectPath ,
1011 sortAddProjectProviderSources ,
1112 type AddProjectRemoteSource ,
1213} from "@t3tools/client-runtime/operations/projects" ;
14+ import {
15+ connectionStatusText ,
16+ type EnvironmentConnectionPhase ,
17+ } from "@t3tools/client-runtime/connection" ;
1318import {
1419 canPreloadBrowsePath ,
1520 createBrowseNavigationCoordinator ,
@@ -24,7 +29,7 @@ import {
2429import { CommandId , type EnvironmentId , ProjectId } from "@t3tools/contracts" ;
2530import { StackActions , useNavigation } from "@react-navigation/native" ;
2631import { SymbolView } from "../../components/AppSymbol" ;
27- import { useCallback , useEffect , useMemo , useState , type ReactNode } from "react" ;
32+ import { useCallback , useEffect , useMemo , useRef , useState , type ReactNode } from "react" ;
2833import { ActivityIndicator , Alert , Pressable , ScrollView , View } from "react-native" ;
2934import { useSafeAreaInsets } from "react-native-safe-area-context" ;
3035import * as Arr from "effect/Array" ;
@@ -46,15 +51,20 @@ import { uuidv4 } from "../../lib/uuid";
4651import { useAtomCommand } from "../../state/use-atom-command" ;
4752import { useAtomQueryRunner } from "../../state/use-atom-query-runner" ;
4853import {
54+ useRemoteConnectionStatus ,
4955 useRemoteEnvironmentRuntime ,
5056 useSavedRemoteConnections ,
5157} from "../../state/use-remote-environment-registry" ;
58+ import { resolveAddProjectEnvironment } from "./AddProjectScreen.logic" ;
5259
5360interface EnvironmentOption {
5461 readonly environmentId : EnvironmentId ;
5562 readonly label : string ;
5663 readonly platform : string ;
5764 readonly baseDirectory : string | null ;
65+ readonly connectionState : EnvironmentConnectionPhase ;
66+ readonly connectionError : string | null ;
67+ readonly connectionErrorTraceId : string | null ;
5868}
5969
6070const environmentOptionOrder = Order . mapInput (
@@ -227,10 +237,13 @@ function ProjectPathInput(props: {
227237}
228238
229239function useBrowsePathInput ( environment : EnvironmentOption | null ) {
240+ const environmentId = environment ?. environmentId ?? null ;
241+ const environmentBaseDirectory = environment ?. baseDirectory ?? null ;
230242 const [ pathInput , commitPathInput ] = useState ( ( ) =>
231- getAddProjectInitialQuery ( environment ?. baseDirectory ) ,
243+ getAddProjectInitialQuery ( environmentBaseDirectory ) ,
232244 ) ;
233- const environmentRuntime = useRemoteEnvironmentRuntime ( environment ?. environmentId ?? null ) ;
245+ const previousEnvironmentIdRef = useRef ( environmentId ) ;
246+ const environmentRuntime = useRemoteEnvironmentRuntime ( environmentId ) ;
234247 const loadBrowsePath = useAtomQueryRunner ( filesystemEnvironment . browse , {
235248 reportFailure : false ,
236249 reportDefect : false ,
@@ -268,10 +281,11 @@ function useBrowsePathInput(environment: EnvironmentOption | null) {
268281 ) ;
269282
270283 useEffect ( ( ) => {
271- if ( environment ) {
272- setPathInput ( getAddProjectInitialQuery ( environment . baseDirectory ) ) ;
284+ if ( environmentId !== null && environmentId !== previousEnvironmentIdRef . current ) {
285+ previousEnvironmentIdRef . current = environmentId ;
286+ setPathInput ( getAddProjectInitialQuery ( environmentBaseDirectory ) ) ;
273287 }
274- } , [ environment , setPathInput ] ) ;
288+ } , [ environmentBaseDirectory , environmentId , setPathInput ] ) ;
275289
276290 useEffect (
277291 ( ) => ( ) => {
@@ -286,19 +300,27 @@ function useBrowsePathInput(environment: EnvironmentOption | null) {
286300function useEnvironmentOptions ( ) : ReadonlyArray < EnvironmentOption > {
287301 const serverConfigByEnvironmentId = useServerConfigs ( ) ;
288302 const { savedConnectionsById } = useSavedRemoteConnections ( ) ;
303+ const { connectedEnvironments } = useRemoteConnectionStatus ( ) ;
289304
290305 return useMemo < ReadonlyArray < EnvironmentOption > > ( ( ) => {
306+ const runtimeByEnvironmentId = new Map (
307+ connectedEnvironments . map ( ( environment ) => [ environment . environmentId , environment ] as const ) ,
308+ ) ;
291309 const options = Object . values ( savedConnectionsById ) . map ( ( connection ) => {
292310 const config = serverConfigByEnvironmentId . get ( connection . environmentId ) ;
311+ const runtime = runtimeByEnvironmentId . get ( connection . environmentId ) ;
293312 return {
294313 environmentId : connection . environmentId ,
295314 label : connection . environmentLabel ,
296315 platform : platformFromOs ( config ?. environment . platform . os ?? null ) ,
297316 baseDirectory : config ?. settings . addProjectBaseDirectory ?? null ,
317+ connectionState : runtime ?. connectionState ?? "available" ,
318+ connectionError : runtime ?. connectionError ?? null ,
319+ connectionErrorTraceId : runtime ?. connectionErrorTraceId ?? null ,
298320 } ;
299321 } ) ;
300322 return Arr . sort ( options , environmentOptionOrder ) ;
301- } , [ savedConnectionsById , serverConfigByEnvironmentId ] ) ;
323+ } , [ connectedEnvironments , savedConnectionsById , serverConfigByEnvironmentId ] ) ;
302324}
303325
304326function useSelectedEnvironment ( ) : {
@@ -309,8 +331,14 @@ function useSelectedEnvironment(): {
309331 const [ selectedEnvironmentId , setSelectedEnvironmentId ] = useState < EnvironmentId | null > ( null ) ;
310332 const environmentOptions = useEnvironmentOptions ( ) ;
311333 const selectedEnvironment =
312- environmentOptions . find ( ( environment ) => environment . environmentId === selectedEnvironmentId ) ??
313- environmentOptions [ 0 ] ??
334+ environmentOptions . find (
335+ ( environment ) =>
336+ environment . environmentId === selectedEnvironmentId &&
337+ canCreateProjectInEnvironment ( environment . connectionState ) ,
338+ ) ??
339+ environmentOptions . find ( ( environment ) =>
340+ canCreateProjectInEnvironment ( environment . connectionState ) ,
341+ ) ??
314342 null ;
315343
316344 return {
@@ -325,9 +353,9 @@ function EmptyEnvironmentState() {
325353
326354 return (
327355 < View className = "items-center gap-3 rounded-2xl bg-card px-5 py-8" >
328- < Text className = "text-center text-lg font-t3-bold" > No environments connected </ Text >
356+ < Text className = "text-center text-lg font-t3-bold" > Environment unavailable </ Text >
329357 < Text className = "text-center text-sm leading-normal text-foreground-muted" >
330- Add an environment before adding a project.
358+ Start or reconnect an environment before adding a project.
331359 </ Text >
332360 < Pressable
333361 onPress = { ( ) => navigation . dispatch ( StackActions . replace ( "ConnectionsNew" ) ) }
@@ -407,17 +435,25 @@ export function AddProjectSourceScreen() {
407435
408436 return (
409437 < AddProjectShell >
410- { environmentOptions . length === 0 ? < EmptyEnvironmentState /> : null }
438+ { selectedEnvironment === null ? < EmptyEnvironmentState /> : null }
411439
412440 { environmentOptions . length > 1 ? (
413441 < >
414- < SectionTitle > Connected environments </ SectionTitle >
442+ < SectionTitle > Environments </ SectionTitle >
415443 < ListSection >
416444 { environmentOptions . map ( ( environment , index ) => (
417445 < ListRow
418446 key = { environment . environmentId }
419447 title = { environment . label }
420- subtitle = { environment . environmentId }
448+ subtitle = {
449+ canCreateProjectInEnvironment ( environment . connectionState )
450+ ? environment . environmentId
451+ : connectionStatusText ( {
452+ phase : environment . connectionState ,
453+ error : environment . connectionError ,
454+ traceId : environment . connectionErrorTraceId ,
455+ } )
456+ }
421457 icon = {
422458 < SymbolView
423459 name = "server.rack"
@@ -427,6 +463,7 @@ export function AddProjectSourceScreen() {
427463 />
428464 }
429465 selected = { environment . environmentId === selectedEnvironment ?. environmentId }
466+ disabled = { ! canCreateProjectInEnvironment ( environment . connectionState ) }
430467 isFirst = { index === 0 }
431468 right = {
432469 environment . environmentId === selectedEnvironment ?. environmentId ? (
@@ -500,7 +537,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
500537
501538 return useCallback (
502539 async ( workspaceRoot : string ) => {
503- if ( ! environment ) return ;
540+ if ( ! environment || ! canCreateProjectInEnvironment ( environment . connectionState ) ) return ;
504541
505542 const existing = findExistingAddProject ( {
506543 projects,
@@ -551,11 +588,7 @@ function useEnvironmentFromParam(
551588) : EnvironmentOption | null {
552589 const environmentOptions = useEnvironmentOptions ( ) ;
553590 const environmentId = stringParam ( environmentIdParam ) as EnvironmentId | null ;
554- return (
555- environmentOptions . find ( ( environment ) => environment . environmentId === environmentId ) ??
556- environmentOptions [ 0 ] ??
557- null
558- ) ;
591+ return resolveAddProjectEnvironment ( environmentOptions , environmentId ) ;
559592}
560593
561594export function AddProjectRepositoryScreen ( props : {
@@ -619,26 +652,32 @@ export function AddProjectRepositoryScreen(props: {
619652 return (
620653 < AddProjectShell >
621654 { error ? < ErrorBanner message = { error } /> : null }
622- < TextInput
623- className = "h-12 min-h-12 rounded-[24px] px-4 py-0 text-base leading-snug"
624- value = { repositoryInput }
625- onChangeText = { setRepositoryInput }
626- autoCapitalize = "none"
627- autoCorrect = { false }
628- placeholder = {
629- source === "url"
630- ? "https://github.com/org/repo.git"
631- : addProjectRemoteSourcePathHint ( source )
632- }
633- returnKeyType = "next"
634- onSubmitEditing = { ( ) => void lookupRepository ( ) }
635- />
636- < PrimaryActionButton
637- label = { source === "url" ? "Continue" : "Lookup repository" }
638- disabled = { isSubmitting || repositoryInput . trim ( ) . length === 0 }
639- onPress = { ( ) => void lookupRepository ( ) }
640- loading = { isSubmitting }
641- />
655+ { environment ? (
656+ < >
657+ < TextInput
658+ className = "h-12 min-h-12 rounded-[24px] px-4 py-0 text-base leading-snug"
659+ value = { repositoryInput }
660+ onChangeText = { setRepositoryInput }
661+ autoCapitalize = "none"
662+ autoCorrect = { false }
663+ placeholder = {
664+ source === "url"
665+ ? "https://github.com/org/repo.git"
666+ : addProjectRemoteSourcePathHint ( source )
667+ }
668+ returnKeyType = "next"
669+ onSubmitEditing = { ( ) => void lookupRepository ( ) }
670+ />
671+ < PrimaryActionButton
672+ label = { source === "url" ? "Continue" : "Lookup repository" }
673+ disabled = { isSubmitting || repositoryInput . trim ( ) . length === 0 }
674+ onPress = { ( ) => void lookupRepository ( ) }
675+ loading = { isSubmitting }
676+ />
677+ </ >
678+ ) : (
679+ < EmptyEnvironmentState />
680+ ) }
642681 </ AddProjectShell >
643682 ) ;
644683}
0 commit comments