7
7
import { Project } from "@gitpod/gitpod-protocol" ;
8
8
import React , { createContext , useContext , useEffect , useMemo , useState } from "react" ;
9
9
import { useHistory , useLocation , useRouteMatch } from "react-router" ;
10
- import { validate as uuidValidate } from "uuid" ;
11
10
import { useCurrentOrg , useOrganizations } from "../data/organizations/orgs-query" ;
12
11
import { listAllProjects } from "../service/public-api" ;
13
12
import { useCurrentUser } from "../user-context" ;
@@ -27,25 +26,41 @@ export const ProjectContextProvider: React.FC = ({ children }) => {
27
26
return < ProjectContext . Provider value = { ctx } > { children } </ ProjectContext . Provider > ;
28
27
} ;
29
28
30
- export function useProjectSlugs ( ) : { projectSlug ?: string ; prebuildId ?: string } {
31
- const projectsRouteMatch = useRouteMatch < { projectSlug ?: string ; prebuildId ?: string } > (
32
- "/projects/:projectSlug?/:prebuildId?" ,
33
- ) ;
29
+ interface ProjectInfo {
30
+ id : string ;
31
+ name ?: string ;
32
+ }
33
+
34
+ export function useProjectInfo ( ) : ProjectInfo | undefined {
35
+ const projectsRouteMatch = useRouteMatch < { projectSlug ?: string } > ( "/projects/:projectSlug" ) ;
34
36
35
37
return useMemo ( ( ) => {
36
38
const projectSlug = projectsRouteMatch ?. params . projectSlug ;
37
- const result : { projectSlug ?: string ; prebuildId ?: string } = { } ;
38
- const reservedProjectSlugs = [ "new" ] ;
39
- if ( ! projectSlug || reservedProjectSlugs . includes ( projectSlug ) ) {
40
- return result ;
39
+ if ( ! projectSlug ) {
40
+ return undefined ;
41
41
}
42
- result . projectSlug = projectSlug ;
43
- const prebuildId = projectsRouteMatch ?. params . prebuildId ;
44
- if ( prebuildId && uuidValidate ( prebuildId ) ) {
45
- result . prebuildId = projectsRouteMatch ?. params . prebuildId ;
42
+ const result = parseProjectSlug ( projectSlug ) ;
43
+ if ( ! result ) {
44
+ return undefined ;
46
45
}
47
46
return result ;
48
- } , [ projectsRouteMatch ?. params . projectSlug , projectsRouteMatch ?. params . prebuildId ] ) ;
47
+ } , [ projectsRouteMatch ?. params . projectSlug ] ) ;
48
+ }
49
+
50
+ const pattern : RegExp = / ^ ( ( .+ ) - ) ? ( [ a - f A - F 0 - 9 ] { 8 } - [ a - f A - F 0 - 9 ] { 4 } - [ a - f A - F 0 - 9 ] { 4 } - [ a - f A - F 0 - 9 ] { 4 } - [ a - f A - F 0 - 9 ] { 12 } ) $ / ;
51
+ function parseProjectSlug ( slug : string ) : ProjectInfo | undefined {
52
+ const match = slug . match ( pattern ) ;
53
+
54
+ if ( match ) {
55
+ const name = match [ 2 ] ;
56
+ const id = match [ 3 ] ;
57
+ return {
58
+ name,
59
+ id,
60
+ } ;
61
+ } else {
62
+ return undefined ;
63
+ }
49
64
}
50
65
51
66
export function useCurrentProject ( ) : { project : Project | undefined ; loading : boolean } {
@@ -54,7 +69,7 @@ export function useCurrentProject(): { project: Project | undefined; loading: bo
54
69
const user = useCurrentUser ( ) ;
55
70
const org = useCurrentOrg ( ) ;
56
71
const orgs = useOrganizations ( ) ;
57
- const slugs = useProjectSlugs ( ) ;
72
+ const projectInfo = useProjectInfo ( ) ;
58
73
const location = useLocation ( ) ;
59
74
const history = useHistory ( ) ;
60
75
@@ -65,7 +80,7 @@ export function useCurrentProject(): { project: Project | undefined; loading: bo
65
80
// without a user we are still consider this loading
66
81
return ;
67
82
}
68
- if ( ! slugs . projectSlug ) {
83
+ if ( ! projectInfo ) {
69
84
setProject ( undefined ) ;
70
85
setLoading ( false ) ;
71
86
return ;
@@ -77,15 +92,15 @@ export function useCurrentProject(): { project: Project | undefined; loading: bo
77
92
let projects = await listAllProjects ( { orgId : org . data . id } ) ;
78
93
79
94
// Find project matching with slug, otherwise with name
80
- const project = projects . find ( ( p ) => Project . slug ( p ) === slugs . projectSlug ) ;
95
+ const project = projects . find ( ( p ) => p . id === projectInfo . id ) ;
81
96
if ( ! project && orgs . data ) {
82
97
// check other orgs
83
98
for ( const t of orgs . data || [ ] ) {
84
99
if ( t . id === org . data ?. id ) {
85
100
continue ;
86
101
}
87
102
const projects = await listAllProjects ( { orgId : t . id } ) ;
88
- const project = projects . find ( ( p ) => Project . slug ( p ) === slugs . projectSlug ) ;
103
+ const project = projects . find ( ( p ) => p . id === projectInfo . id ) ;
89
104
if ( project ) {
90
105
// redirect to the other org
91
106
history . push ( location . pathname + "?org=" + t . id ) ;
@@ -95,7 +110,7 @@ export function useCurrentProject(): { project: Project | undefined; loading: bo
95
110
setProject ( project ) ;
96
111
setLoading ( false ) ;
97
112
} ) ( ) ;
98
- } , [ slugs . projectSlug , setProject , org . data , user , orgs . data , location , history ] ) ;
113
+ } , [ setProject , org . data , user , orgs . data , location , history , projectInfo ] ) ;
99
114
100
115
return { project, loading } ;
101
116
}
0 commit comments