11import { open } from '@tauri-apps/plugin-dialog' ;
2- import type { WorkspaceMeta } from '@yaakapp-internal/models' ;
3- import { createGlobalModel , updateModel } from '@yaakapp-internal/models' ;
2+ import type { CloneResult } from '@yaakapp-internal/git' ;
43import { useState } from 'react' ;
4+ import { openWorkspaceFromSyncDir } from '../commands/openWorkspaceFromSyncDir' ;
55import { appInfo } from '../lib/appInfo' ;
6- import { router } from '../lib/router' ;
76import { invokeCmd } from '../lib/tauri' ;
87import { showErrorToast } from '../lib/toast' ;
98import { Banner } from './core/Banner' ;
109import { Button } from './core/Button' ;
1110import { IconButton } from './core/IconButton' ;
1211import { PlainInput } from './core/PlainInput' ;
1312import { VStack } from './core/Stacks' ;
13+ import { promptCredentials } from './git/credentials' ;
1414
1515interface Props {
1616 hide : ( ) => void ;
@@ -45,6 +45,24 @@ export function CloneGitRepositoryDialog({ hide }: Props) {
4545 }
4646 } ;
4747
48+ const doClone = async ( ) : Promise < CloneResult > => {
49+ const result = await invokeCmd < CloneResult > ( 'cmd_git_clone' , { url, dir : directory } ) ;
50+ if ( result . type !== 'needs_credentials' ) return result ;
51+
52+ // Prompt for credentials
53+ const creds = await promptCredentials ( { url : result . url , error : result . error } ) ;
54+ if ( creds == null ) throw new Error ( 'Cancelled' ) ;
55+
56+ // Store credentials and retry
57+ await invokeCmd ( 'cmd_git_add_credential' , {
58+ remoteUrl : result . url ,
59+ username : creds . username ,
60+ password : creds . password ,
61+ } ) ;
62+
63+ return invokeCmd < CloneResult > ( 'cmd_git_clone' , { url, dir : directory } ) ;
64+ } ;
65+
4866 const handleClone = async ( e : React . FormEvent ) => {
4967 e . preventDefault ( ) ;
5068 if ( ! url || ! directory ) return ;
@@ -53,29 +71,17 @@ export function CloneGitRepositoryDialog({ hide }: Props) {
5371 setError ( null ) ;
5472
5573 try {
56- // Clone the repository
57- await invokeCmd ( 'cmd_git_clone' , { url, dir : directory } ) ;
74+ const result = await doClone ( ) ;
5875
59- // Create a new workspace
60- const workspaceId = await createGlobalModel ( { model : 'workspace' , name : repoName } ) ;
61- if ( workspaceId == null ) {
62- throw new Error ( 'Failed to create workspace' ) ;
76+ if ( result . type === 'needs_credentials' ) {
77+ setError (
78+ result . error ?? 'Authentication failed. Please check your credentials and try again.' ,
79+ ) ;
80+ return ;
6381 }
6482
65- // Get and update workspace meta to set the sync directory
66- const workspaceMeta = await invokeCmd < WorkspaceMeta > ( 'cmd_get_workspace_meta' , {
67- workspaceId,
68- } ) ;
69- await updateModel ( {
70- ...workspaceMeta ,
71- settingSyncDir : directory ,
72- } ) ;
73-
74- // Navigate to the new workspace
75- await router . navigate ( {
76- to : '/workspaces/$workspaceId' ,
77- params : { workspaceId } ,
78- } ) ;
83+ // Open the workspace from the cloned directory
84+ await openWorkspaceFromSyncDir . mutateAsync ( directory ) ;
7985
8086 hide ( ) ;
8187 } catch ( err ) {
0 commit comments