@@ -387,7 +387,26 @@ function useBrowsePathInput(
387387 [ browseNavigation ] ,
388388 ) ;
389389
390- return { isBrowseNavigating, pathInput, setPathInput, navigateToBrowsePath } ;
390+ // Registered roots are absolute and `~` expands on the host, so ask the host for its home.
391+ const resolveHomePath = async ( path : string ) : Promise < string > => {
392+ if (
393+ ! / ^ ~ (?: [ \\ / ] | $ ) / . test ( path ) ||
394+ ! environment ||
395+ ! readEnvironmentScope ( environment . environmentId , AuthFilesystemReadScope ) ||
396+ ! canPreloadBrowsePath ( environmentRuntime ?. connectionState )
397+ ) {
398+ return path ;
399+ }
400+ const home = await loadBrowsePath ( {
401+ environmentId : environment . environmentId ,
402+ input : { partialPath : "~" } ,
403+ } ) ;
404+ return AsyncResult . isSuccess ( home ) && home . value
405+ ? `${ home . value . parentPath . replace ( / [ \\ / ] + $ / , "" ) } ${ path . slice ( 1 ) } `
406+ : path ;
407+ } ;
408+
409+ return { isBrowseNavigating, pathInput, setPathInput, navigateToBrowsePath, resolveHomePath } ;
391410}
392411
393412function useEnvironmentOptions (
@@ -730,16 +749,76 @@ async function draftWithPrompt(
730749 return draftId ;
731750}
732751
733- function useCreateProject ( environment : EnvironmentOption | null ) {
752+ function useOpenExistingProject ( environment : EnvironmentOption | null ) {
734753 const openNewTaskDraft = useOpenNewTaskDraft ( ) ;
735- const navigation = useNavigation ( ) ;
736- const createProject = useAtomCommand ( projectEnvironment . create , { reportFailure : false } ) ;
737754 const updateProject = useAtomCommand ( projectEnvironment . update , {
738755 label : "project unarchive" ,
739756 } ) ;
740757 const projects = useProjects ( ) ;
741758
742- return async ( workspaceRoot : string , originRoute = getActiveRoute ( navigation ) ) => {
759+ return async (
760+ path : string ,
761+ originRoute : ReturnType < typeof getActiveRoute > ,
762+ clone ?: { readonly remoteUrl : string ; readonly prompt : string | null } ,
763+ ) : Promise < boolean > => {
764+ if ( ! environment ) return false ;
765+ const existing = findExistingAddProject ( {
766+ projects,
767+ environmentId : environment . environmentId ,
768+ path,
769+ } ) ;
770+ if ( ! existing ) return false ;
771+ // A folder holding another repository is not this clone; let the clone report it occupied.
772+ if (
773+ clone &&
774+ existing . repositoryIdentity ?. canonicalKey !==
775+ sourceControlClients . repositoryKeyFromRemote ( clone . remoteUrl )
776+ ) {
777+ return false ;
778+ }
779+ const prompt = clone ?. prompt ;
780+ // Adding an archived project's folder again brings the project back.
781+ const unarchived =
782+ existing . archivedAt == null ||
783+ (
784+ await updateProject ( {
785+ environmentId : existing . environmentId ,
786+ input : { projectId : existing . id , archived : false } ,
787+ } )
788+ ) . _tag === "Success" ;
789+ Dialog . alert (
790+ "Project already exists" ,
791+ unarchived ? existing . title : `${ existing . title } is still archived. Try again later.` ,
792+ ) ;
793+ openNewTaskDraft (
794+ {
795+ environmentId : existing . environmentId ,
796+ projectId : existing . id ,
797+ title : existing . title ,
798+ ...( prompt
799+ ? await draftWithPrompt ( existing . environmentId , existing . id , prompt ) . then (
800+ ( draftId ) => ( { draftId } ) ,
801+ ( ) => ( { } ) ,
802+ )
803+ : { } ) ,
804+ } ,
805+ originRoute ,
806+ ) ;
807+ return true ;
808+ } ;
809+ }
810+
811+ function useCreateProject ( environment : EnvironmentOption | null ) {
812+ const openNewTaskDraft = useOpenNewTaskDraft ( ) ;
813+ const navigation = useNavigation ( ) ;
814+ const createProject = useAtomCommand ( projectEnvironment . create , { reportFailure : false } ) ;
815+ const openExistingProject = useOpenExistingProject ( environment ) ;
816+
817+ return async (
818+ workspaceRoot : string ,
819+ originRoute = getActiveRoute ( navigation ) ,
820+ lookupPath = workspaceRoot ,
821+ ) => {
743822 if (
744823 ! environment ||
745824 ! canCreateProjectInEnvironment ( environment . connectionState ) ||
@@ -752,33 +831,7 @@ function useCreateProject(environment: EnvironmentOption | null) {
752831 return ;
753832 }
754833
755- const existing = findExistingAddProject ( {
756- projects,
757- environmentId : environment . environmentId ,
758- path : workspaceRoot ,
759- } ) ;
760- if ( existing ) {
761- // Adding an archived project's folder again brings the project back.
762- const unarchived =
763- existing . archivedAt == null ||
764- (
765- await updateProject ( {
766- environmentId : existing . environmentId ,
767- input : { projectId : existing . id , archived : false } ,
768- } )
769- ) . _tag === "Success" ;
770- Dialog . alert (
771- "Project already exists" ,
772- unarchived ? existing . title : `${ existing . title } is still archived. Try again later.` ,
773- ) ;
774- openNewTaskDraft (
775- {
776- environmentId : existing . environmentId ,
777- projectId : existing . id ,
778- title : existing . title ,
779- } ,
780- originRoute ,
781- ) ;
834+ if ( await openExistingProject ( lookupPath , originRoute ) ) {
782835 return ;
783836 }
784837
@@ -1313,8 +1366,9 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
13131366 environment ?. environmentId ?? null ,
13141367 AuthOrchestrationOperateScope ,
13151368 ) ;
1369+ const navigation = useNavigation ( ) ;
13161370 const createProject = useCreateProject ( environment ) ;
1317- const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } =
1371+ const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput, resolveHomePath } =
13181372 useBrowsePathInput ( environment ) ;
13191373 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
13201374 const [ error , setError ] = useState < string | null > ( null ) ;
@@ -1333,7 +1387,11 @@ export function AddProjectLocalFolderScreen(props: { readonly environmentId?: st
13331387 }
13341388
13351389 setIsSubmitting ( true ) ;
1336- const result = await createProject ( resolved . path ) ;
1390+ const originRoute = getActiveRoute ( navigation ) ;
1391+ const lookupPath = resolved . path . startsWith ( "~" )
1392+ ? await resolveHomePath ( resolved . path )
1393+ : resolved . path ;
1394+ const result = await createProject ( resolved . path , originRoute , lookupPath ) ;
13371395 if ( result && AsyncResult . isFailure ( result ) ) {
13381396 setError ( errorMessage ( Cause . squash ( result . cause ) ) ) ;
13391397 }
@@ -1404,11 +1462,9 @@ export function AddProjectDestinationScreen(props: {
14041462 ? getCloneDirectoryPath ( remoteUrl , true , repositoryName )
14051463 : null ;
14061464 const cloneDirectoryName = ghqDirectoryPath ?. split ( "/" ) . at ( - 1 ) ?? repositoryName ;
1407- const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput } = useBrowsePathInput (
1408- environment ,
1409- cloneDirectoryName ,
1410- ghqDirectoryPath ?? repositoryName ,
1411- ) ;
1465+ const { isBrowseNavigating, navigateToBrowsePath, pathInput, setPathInput, resolveHomePath } =
1466+ useBrowsePathInput ( environment , cloneDirectoryName , ghqDirectoryPath ?? repositoryName ) ;
1467+ const openExistingProject = useOpenExistingProject ( environment ) ;
14121468 const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
14131469 const [ error , setError ] = useState < string | null > ( null ) ;
14141470
@@ -1436,6 +1492,15 @@ export function AddProjectDestinationScreen(props: {
14361492
14371493 const originRoute = getActiveRoute ( navigation ) ;
14381494 setIsSubmitting ( true ) ;
1495+ if (
1496+ await openExistingProject ( await resolveHomePath ( resolved . path ) , originRoute , {
1497+ remoteUrl,
1498+ prompt : prompt ?? null ,
1499+ } )
1500+ ) {
1501+ setIsSubmitting ( false ) ;
1502+ return ;
1503+ }
14391504 const title = inferProjectTitleFromPath ( resolved . path ) ;
14401505 const startResult = await startProjectClone ( {
14411506 environmentId : environment . environmentId ,
0 commit comments