Skip to content

Commit e24a246

Browse files
AlexTugarevroboquat
authored andcommitted
[projects] return all branches and filter on frontend
1 parent 90d5372 commit e24a246

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

components/dashboard/src/projects/Project.tsx

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export default function () {
2727
const [project, setProject] = useState<Project | undefined>();
2828

2929
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());
3132

3233
const [searchFilter, setSearchFilter] = useState<string | undefined>();
3334

@@ -55,19 +56,7 @@ export default function () {
5556
// default branch on top of the rest
5657
const branches = details.branches.sort((a, b) => (b.isDefault as any) - (a.isDefault as any)) || [];
5758
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-
}
6959
}
70-
7160
}
7261

7362
const branchContextMenu = (branch: Project.BranchDetails) => {
@@ -83,7 +72,33 @@ export default function () {
8372
return entries;
8473
}
8574

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+
}
87102

88103
const filter = (branch: Project.BranchDetails) => {
89104
if (searchFilter && `${branch.changeTitle} ${branch.name}`.toLowerCase().includes(searchFilter.toLowerCase()) === false) {
@@ -137,18 +152,15 @@ export default function () {
137152
<ItemFieldContextMenu />
138153
</ItemField>
139154
</Item>
140-
{branches.map((branch, index) => {
141-
if (!filter(branch)) {
142-
return undefined;
143-
}
155+
{branches.filter(filter).slice(0, 10).map((branch, index) => {
144156

145157
const branchName = branch.name;
146-
const prebuild = lastPrebuild(branch);
158+
const prebuild = lastPrebuild(branch); // this might lazily trigger fetching of prebuild details
147159

148160
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} />;
149161
const statusIcon = prebuild?.status && prebuildStatusIcon(prebuild.status);
150162
const status = prebuild?.status && prebuildStatusLabel(prebuild.status);
151-
console.log(`status for ${branchName} is ${prebuild?.status} (${lastPrebuilds.size})`)
163+
152164
return <Item key={`branch-${index}-${branchName}`} className="grid grid-cols-3 group">
153165
<ItemField className="flex items-center">
154166
<div>

components/server/src/projects/projects-service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export class ProjectsService {
8282
});
8383
}
8484
result.sort((a, b) => (b.changeDate || "").localeCompare(a.changeDate || ""));
85-
return result.slice(0, 30);
85+
return result;
8686
}
8787

8888
async createProject({ name, cloneUrl, teamId, userId, appInstallationId }: CreateProjectParams): Promise<Project> {

0 commit comments

Comments
 (0)