@@ -27,7 +27,8 @@ export default function () {
27
27
const [ project , setProject ] = useState < Project | undefined > ( ) ;
28
28
29
29
const [ branches , setBranches ] = useState < Project . BranchDetails [ ] > ( [ ] ) ;
30
- const [ lastPrebuilds , setLastPrebuilds ] = useState < Map < string , PrebuildInfo > > ( new Map ( ) ) ;
30
+ const [ lastPrebuilds , setLastPrebuilds ] = useState < Map < string , PrebuildInfo | undefined > > ( new Map ( ) ) ;
31
+ const [ prebuildLoaders ] = useState < Set < string > > ( new Set ( ) ) ;
31
32
32
33
const [ searchFilter , setSearchFilter ] = useState < string | undefined > ( ) ;
33
34
@@ -55,19 +56,7 @@ export default function () {
55
56
// default branch on top of the rest
56
57
const branches = details . branches . sort ( ( a , b ) => ( b . isDefault as any ) - ( a . isDefault as any ) ) || [ ] ;
57
58
setBranches ( branches ) ;
58
-
59
- for ( const b of branches ) {
60
- const lastPrebuild = await getGitpodService ( ) . server . findPrebuilds ( {
61
- projectId : project . id ,
62
- branch : b . name ,
63
- latest : true ,
64
- } ) ;
65
- if ( lastPrebuild [ 0 ] ) {
66
- setLastPrebuilds ( prev => new Map ( prev ) . set ( b . name , lastPrebuild [ 0 ] ) ) ;
67
- }
68
- }
69
59
}
70
-
71
60
}
72
61
73
62
const branchContextMenu = ( branch : Project . BranchDetails ) => {
@@ -83,7 +72,33 @@ export default function () {
83
72
return entries ;
84
73
}
85
74
86
- const lastPrebuild = ( branch : Project . BranchDetails ) => lastPrebuilds . get ( branch . name ) ;
75
+ const lastPrebuild = ( branch : Project . BranchDetails ) => {
76
+ const lastPrebuild = lastPrebuilds . get ( branch . name ) ;
77
+ if ( ! lastPrebuild ) {
78
+ // do not await here.
79
+ loadPrebuild ( branch ) ;
80
+ }
81
+ return lastPrebuild ;
82
+ }
83
+
84
+ const loadPrebuild = async ( branch : Project . BranchDetails ) => {
85
+ if ( prebuildLoaders . has ( branch . name ) || lastPrebuilds . has ( branch . name ) ) {
86
+ // `lastPrebuilds.has(branch.name)` will be true even if loading finished with no prebuild found.
87
+ // TODO(at): this need to be revised once prebuild events are integrated
88
+ return ;
89
+ }
90
+ if ( ! team || ! project ) {
91
+ return ;
92
+ }
93
+ prebuildLoaders . add ( branch . name ) ;
94
+ const lastPrebuild = await getGitpodService ( ) . server . findPrebuilds ( {
95
+ projectId : project . id ,
96
+ branch : branch . name ,
97
+ latest : true ,
98
+ } ) ;
99
+ setLastPrebuilds ( prev => new Map ( prev ) . set ( branch . name , lastPrebuild [ 0 ] ) ) ;
100
+ prebuildLoaders . delete ( branch . name ) ;
101
+ }
87
102
88
103
const filter = ( branch : Project . BranchDetails ) => {
89
104
if ( searchFilter && `${ branch . changeTitle } ${ branch . name } ` . toLowerCase ( ) . includes ( searchFilter . toLowerCase ( ) ) === false ) {
@@ -137,18 +152,15 @@ export default function () {
137
152
< ItemFieldContextMenu />
138
153
</ ItemField >
139
154
</ Item >
140
- { branches . map ( ( branch , index ) => {
141
- if ( ! filter ( branch ) ) {
142
- return undefined ;
143
- }
155
+ { branches . filter ( filter ) . slice ( 0 , 10 ) . map ( ( branch , index ) => {
144
156
145
157
const branchName = branch . name ;
146
- const prebuild = lastPrebuild ( branch ) ;
158
+ const prebuild = lastPrebuild ( branch ) ; // this might lazily trigger fetching of prebuild details
147
159
148
160
const avatar = branch . changeAuthorAvatar && < img className = "rounded-full w-4 h-4 inline-block align-text-bottom mr-2" src = { branch . changeAuthorAvatar || '' } alt = { branch . changeAuthor } /> ;
149
161
const statusIcon = prebuild ?. status && prebuildStatusIcon ( prebuild . status ) ;
150
162
const status = prebuild ?. status && prebuildStatusLabel ( prebuild . status ) ;
151
- console . log ( `status for ${ branchName } is ${ prebuild ?. status } ( ${ lastPrebuilds . size } )` )
163
+
152
164
return < Item key = { `branch-${ index } -${ branchName } ` } className = "grid grid-cols-3 group" >
153
165
< ItemField className = "flex items-center" >
154
166
< div >
0 commit comments