@@ -39,6 +39,7 @@ import {
3939} from "@t3tools/client-runtime/state/filesystem" ;
4040import {
4141 appendBrowsePathSegment ,
42+ ensureBrowseDirectoryPath ,
4243 inferProjectTitleFromPath ,
4344 isWindowsPlatform ,
4445} from "@t3tools/client-runtime/state/projects" ;
@@ -706,10 +707,59 @@ async function draftWithPrompt(
706707 return draftId ;
707708}
708709
710+ // Adding a path the environment already tracks should feel like picking that
711+ // project from the list, so callers open its draft instead of failing. Stored
712+ // workspace roots are absolute, so a `~` path is resolved by the environment
713+ // first. Resolves to whether the path matched an existing project.
714+ function useOpenExistingProject ( environment : EnvironmentOption | null ) {
715+ const navigation = useNavigation ( ) ;
716+ const projects = useProjects ( ) ;
717+ const loadBrowsePath = useAtomQueryRunner ( filesystemEnvironment . browse , {
718+ reportFailure : false ,
719+ reportDefect : false ,
720+ } ) ;
721+
722+ return useCallback (
723+ async ( path : string , prompt ?: string | null ) => {
724+ if ( ! environment ) return false ;
725+ let workspaceRoot = path ;
726+ if ( path . startsWith ( "~" ) ) {
727+ // Browsing a directory path reports it back with home expanded.
728+ const browseResult = await loadBrowsePath ( {
729+ environmentId : environment . environmentId ,
730+ input : { partialPath : ensureBrowseDirectoryPath ( path ) } ,
731+ } ) ;
732+ if ( ! AsyncResult . isSuccess ( browseResult ) ) return false ;
733+ workspaceRoot = browseResult . value . parentPath ;
734+ }
735+ const existing = findExistingAddProject ( {
736+ projects,
737+ environmentId : environment . environmentId ,
738+ path : workspaceRoot ,
739+ } ) ;
740+ if ( ! existing ) return false ;
741+ openNewTaskDraft ( navigation , {
742+ environmentId : existing . environmentId ,
743+ projectId : existing . id ,
744+ title : existing . title ,
745+ ...( prompt
746+ ? // Without its prompt the draft still opens on the project.
747+ await draftWithPrompt ( existing . environmentId , existing . id , prompt ) . then (
748+ ( draftId ) => ( { draftId } ) ,
749+ ( ) => ( { } ) ,
750+ )
751+ : { } ) ,
752+ } ) ;
753+ return true ;
754+ } ,
755+ [ environment , loadBrowsePath , navigation , projects ] ,
756+ ) ;
757+ }
758+
709759function useCreateProject ( environment : EnvironmentOption | null ) {
710760 const navigation = useNavigation ( ) ;
711761 const createProject = useAtomCommand ( projectEnvironment . create , { reportFailure : false } ) ;
712- const projects = useProjects ( ) ;
762+ const openExistingProject = useOpenExistingProject ( environment ) ;
713763
714764 return useCallback (
715765 async ( workspaceRoot : string ) => {
@@ -725,30 +775,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
725775 return ;
726776 }
727777
728- const existing = findExistingAddProject ( {
729- projects,
730- environmentId : environment . environmentId ,
731- path : workspaceRoot ,
732- } ) ;
733- if ( existing ) {
734- Alert . alert ( "Project already exists" , existing . title ) ;
735- navigation . dispatch (
736- CommonActions . reset ( {
737- index : 0 ,
738- routes : [
739- {
740- name : "NewTaskDraft" ,
741- params : {
742- environmentId : existing . environmentId ,
743- projectId : existing . id ,
744- title : existing . title ,
745- } ,
746- } ,
747- ] ,
748- } ) ,
749- ) ;
750- return ;
751- }
778+ if ( await openExistingProject ( workspaceRoot ) ) return ;
752779
753780 const projectId = ProjectId . make ( uuidv4 ( ) ) ;
754781 const command = buildProjectCreateCommand ( {
@@ -780,7 +807,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
780807 ) ;
781808 return result ;
782809 } ,
783- [ createProject , environment , projects , navigation ] ,
810+ [ createProject , environment , navigation , openExistingProject ] ,
784811 ) ;
785812}
786813
@@ -1345,6 +1372,7 @@ export function AddProjectDestinationScreen(props: {
13451372 ) ;
13461373 const canCloneProject = canWriteSourceControl && canCreateProject ;
13471374 const createProject = useCreateProject ( environment ) ;
1375+ const openExistingProject = useOpenExistingProject ( environment ) ;
13481376 const remoteUrl = stringParam ( props . remoteUrl ) ;
13491377 const repositoryTitle = stringParam ( props . repositoryTitle ) ;
13501378 const prompt = stringParam ( props . prompt ) ;
@@ -1383,6 +1411,12 @@ export function AddProjectDestinationScreen(props: {
13831411 }
13841412
13851413 setIsSubmitting ( true ) ;
1414+ // Cloning into a directory that is already a project fails, so open that
1415+ // project instead.
1416+ if ( await openExistingProject ( resolved . path , prompt ) ) {
1417+ setIsSubmitting ( false ) ;
1418+ return ;
1419+ }
13861420 if ( environment . supportsCloneTracking ) {
13871421 // The server creates the project and clones in the background; the
13881422 // draft screen shows progress and holds Start until the files land.
@@ -1455,6 +1489,7 @@ export function AddProjectDestinationScreen(props: {
14551489 isBrowseNavigating ,
14561490 isSubmitting ,
14571491 navigation ,
1492+ openExistingProject ,
14581493 pathInput ,
14591494 prompt ,
14601495 remoteUrl ,
0 commit comments