7
7
import { useContext , useEffect , useState } from "react" ;
8
8
import { getGitpodService , gitpodHostUrl } from "../service/service" ;
9
9
import { iconForAuthProvider , openAuthorizeWindow , simplifyProviderName } from "../provider-utils" ;
10
- import { AuthProviderInfo , ProviderRepository , Team , TeamMemberInfo , User } from "@gitpod/gitpod-protocol" ;
10
+ import { AuthProviderInfo , Project , ProviderRepository , Team , TeamMemberInfo , User } from "@gitpod/gitpod-protocol" ;
11
11
import { TeamsContext } from "../teams/teams-context" ;
12
- import { useHistory , useLocation } from "react-router" ;
12
+ import { useLocation } from "react-router" ;
13
13
import ContextMenu , { ContextMenuEntry } from "../components/ContextMenu" ;
14
14
import CaretDown from "../icons/CaretDown.svg" ;
15
15
import Plus from "../icons/Plus.svg" ;
@@ -23,7 +23,6 @@ import exclamation from "../images/exclamation.svg";
23
23
24
24
export default function NewProject ( ) {
25
25
const location = useLocation ( ) ;
26
- const history = useHistory ( ) ;
27
26
const { teams } = useContext ( TeamsContext ) ;
28
27
const { user, setUser } = useContext ( UserContext ) ;
29
28
@@ -33,12 +32,16 @@ export default function NewProject() {
33
32
const [ selectedAccount , setSelectedAccount ] = useState < string | undefined > ( undefined ) ;
34
33
const [ noOrgs , setNoOrgs ] = useState < boolean > ( false ) ;
35
34
const [ showGitProviders , setShowGitProviders ] = useState < boolean > ( false ) ;
36
- const [ selectedRepo , setSelectedRepo ] = useState < string | undefined > ( undefined ) ;
35
+ const [ selectedRepo , setSelectedRepo ] = useState < ProviderRepository | undefined > ( undefined ) ;
37
36
const [ selectedTeamOrUser , setSelectedTeamOrUser ] = useState < Team | User | undefined > ( undefined ) ;
38
37
39
38
const [ showNewTeam , setShowNewTeam ] = useState < boolean > ( false ) ;
40
39
const [ loaded , setLoaded ] = useState < boolean > ( false ) ;
41
40
41
+ const [ project , setProject ] = useState < Project | undefined > ( ) ;
42
+ const [ guessedConfigString , setGuessedConfigString ] = useState < string | undefined > ( ) ;
43
+ const [ sourceOfConfig , setSourceOfConfig ] = useState < "repo" | "db" | undefined > ( ) ;
44
+
42
45
useEffect ( ( ) => {
43
46
if ( user && provider === undefined ) {
44
47
if ( user . identities . find ( i => i . authProviderId === "Public-GitLab" ) ) {
@@ -83,6 +86,32 @@ export default function NewProject() {
83
86
} ) ( ) ;
84
87
} , [ teams ] ) ;
85
88
89
+ useEffect ( ( ) => {
90
+ if ( selectedRepo ) {
91
+ ( async ( ) => {
92
+
93
+ try {
94
+ const guessedConfigStringPromise = getGitpodService ( ) . server . guessRepositoryConfiguration ( selectedRepo . cloneUrl ) ;
95
+ const repoConfigString = await getGitpodService ( ) . server . fetchRepositoryConfiguration ( selectedRepo . cloneUrl ) ;
96
+ if ( repoConfigString ) {
97
+ setSourceOfConfig ( "repo" ) ;
98
+ } else {
99
+ setSourceOfConfig ( "db" ) ;
100
+ setGuessedConfigString ( await guessedConfigStringPromise || `tasks:
101
+ - init: |
102
+ echo 'TODO: build project'
103
+ command: |
104
+ echo 'TODO: start app'` ) ;
105
+ }
106
+ } catch ( error ) {
107
+ console . error ( 'Getting project configuration failed' , error ) ;
108
+ setSourceOfConfig ( undefined ) ;
109
+ }
110
+
111
+ } ) ( ) ;
112
+ }
113
+ } , [ selectedRepo ] ) ;
114
+
86
115
useEffect ( ( ) => {
87
116
if ( selectedTeamOrUser && selectedRepo ) {
88
117
createProject ( selectedTeamOrUser , selectedRepo ) ;
@@ -118,6 +147,17 @@ export default function NewProject() {
118
147
} ) ( ) ;
119
148
} , [ provider ] ) ;
120
149
150
+ useEffect ( ( ) => {
151
+ if ( project && sourceOfConfig ) {
152
+ ( async ( ) => {
153
+ if ( guessedConfigString && sourceOfConfig === "db" ) {
154
+ await getGitpodService ( ) . server . setProjectConfiguration ( project . id , guessedConfigString ) ;
155
+ }
156
+ await getGitpodService ( ) . server . triggerPrebuild ( project . id , null ) ;
157
+ } ) ( ) ;
158
+ }
159
+ } , [ project , sourceOfConfig ] ) ;
160
+
121
161
const isGitHub = ( ) => provider === "github.com" ;
122
162
const isBitbucket = ( ) => provider === "bitbucket.org" ;
123
163
@@ -180,16 +220,10 @@ export default function NewProject() {
180
220
}
181
221
}
182
222
183
- const createProject = async ( teamOrUser : Team | User , selectedRepo : string ) => {
223
+ const createProject = async ( teamOrUser : Team | User , repo : ProviderRepository ) => {
184
224
if ( ! provider || isBitbucket ( ) ) {
185
225
return ;
186
226
}
187
- const repo = reposInAccounts . find ( r => r . account === selectedAccount && ( r . path ? r . path === selectedRepo : r . name === selectedRepo ) ) ;
188
- if ( ! repo ) {
189
- console . error ( "No repo selected!" )
190
- return ;
191
- }
192
-
193
227
const repoSlug = repo . path || repo . name ;
194
228
195
229
try {
@@ -203,7 +237,7 @@ export default function NewProject() {
203
237
appInstallationId : String ( repo . installationId ) ,
204
238
} ) ;
205
239
206
- history . push ( `/ ${ User . is ( teamOrUser ) ? 'projects' : 't/' + teamOrUser . slug } / ${ project . slug } /configure` ) ;
240
+ setProject ( project ) ;
207
241
} catch ( error ) {
208
242
const message = ( error && error ?. message ) || "Failed to create new project."
209
243
window . alert ( message ) ;
@@ -294,7 +328,7 @@ export default function NewProject() {
294
328
< div className = "flex justify-end" >
295
329
< div className = "h-full my-auto flex self-center opacity-0 group-hover:opacity-100" >
296
330
{ ! r . inUse ? (
297
- < button className = "primary" onClick = { ( ) => setSelectedRepo ( r . path || r . name ) } > Select</ button >
331
+ < button className = "primary" onClick = { ( ) => setSelectedRepo ( r ) } > Select</ button >
298
332
) : (
299
333
< p className = "my-auto" > already taken</ p >
300
334
) }
@@ -428,18 +462,54 @@ export default function NewProject() {
428
462
</ div > ) ;
429
463
}
430
464
431
- return ( < div className = "flex flex-col w-96 mt-24 mx-auto items-center" >
432
- < h1 > New Project</ h1 >
465
+ const onNewWorkspace = async ( ) => {
466
+ const redirectToNewWorkspace = ( ) => {
467
+ // instead of `history.push` we want forcibly to redirect here in order to avoid a following redirect from `/` -> `/projects` (cf. App.tsx)
468
+ const url = new URL ( window . location . toString ( ) ) ;
469
+ url . pathname = "/" ;
470
+ url . hash = project ?. cloneUrl ! ;
471
+ window . location . href = url . toString ( ) ;
472
+ }
473
+ redirectToNewWorkspace ( ) ;
474
+ }
475
+
476
+ if ( ! project ) {
477
+ return ( < div className = "flex flex-col w-96 mt-24 mx-auto items-center" >
433
478
434
- { ! selectedRepo && renderSelectRepository ( ) }
479
+ < >
480
+ < h1 > New Project</ h1 >
435
481
436
- { selectedRepo && ! selectedTeamOrUser && renderSelectTeam ( ) }
482
+ { ! selectedRepo && renderSelectRepository ( ) }
437
483
438
- { selectedRepo && selectedTeamOrUser && ( < div > </ div > ) }
484
+ { selectedRepo && ! selectedTeamOrUser && renderSelectTeam ( ) }
485
+
486
+ { selectedRepo && selectedTeamOrUser && ( < div > </ div > ) }
487
+ </ >
488
+
489
+ { isBitbucket ( ) && renderBitbucketWarning ( ) }
490
+
491
+ </ div > ) ;
492
+ } else {
493
+ const projectLink = User . is ( selectedTeamOrUser ) ? `/projects/${ project . slug } ` : `/t/${ selectedTeamOrUser ?. slug } /${ project . slug } ` ;
494
+ const location = User . is ( selectedTeamOrUser ) ? "" : ( < > in < a className = "gp-link" href = { `/t/${ selectedTeamOrUser ?. slug } /projects` } > { selectedTeamOrUser ?. name } </ a > </ > ) ;
439
495
440
- { isBitbucket ( ) && renderBitbucketWarning ( ) }
496
+ return ( < div className = "flex flex-col w-96 mt-24 mx-auto items-center" >
441
497
442
- </ div > ) ;
498
+ < >
499
+ < h1 > Project created</ h1 >
500
+
501
+ < p className = "mt-2 text-gray-500 text-center text-base" > Created < a className = "gp-link" href = { projectLink } > { project . name } </ a >
502
+ < br /> { location }
503
+ </ p >
504
+
505
+ < div className = "mt-12" >
506
+ < button onClick = { onNewWorkspace } > New Workspace</ button >
507
+ </ div >
508
+
509
+ </ >
510
+
511
+ </ div > ) ;
512
+ }
443
513
444
514
}
445
515
0 commit comments