Skip to content

Commit

Permalink
Avoid loading data from Jenkins twice. Don't load data when navigatin…
Browse files Browse the repository at this point in the history
…g thought the pages as all data from all pages is already loaded.
  • Loading branch information
Fox32 committed Dec 3, 2020
1 parent b570f78 commit b2a07d2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-zoos-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@backstage/plugin-jenkins': patch
---

Avoid loading data from Jenkins twice. Don't load data when navigating thought the pages as all data from all pages is already loaded.
43 changes: 17 additions & 26 deletions plugins/jenkins/src/components/useBuilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/
import { errorApiRef, useApi } from '@backstage/core';
import { useCallback, useEffect, useState } from 'react';
import { useState } from 'react';
import { useAsyncRetry } from 'react-use';
import { jenkinsApiRef } from '../api';

Expand All @@ -26,21 +26,6 @@ export function useBuilds(owner: string, repo: string, branch?: string) {
const [page, setPage] = useState(0);
const [pageSize, setPageSize] = useState(5);

const getBuilds = useCallback(async () => {
try {
let build;
if (branch) {
build = await api.getLastBuild(`${owner}/${repo}/${branch}`);
} else {
build = await api.getFolder(`${owner}/${repo}`);
}
return build;
} catch (e) {
errorApi.post(e);
return Promise.reject(e);
}
}, [api, branch, errorApi, owner, repo]);

const restartBuild = async (buildName: string) => {
try {
await api.retry(buildName);
Expand All @@ -49,18 +34,24 @@ export function useBuilds(owner: string, repo: string, branch?: string) {
}
};

useEffect(() => {
getBuilds().then(b => {
const size = Array.isArray(b) ? b?.[0].build_num! : 1;
const { loading, value: builds, retry } = useAsyncRetry(async () => {
try {
let builds;
if (branch) {
builds = await api.getLastBuild(`${owner}/${repo}/${branch}`);
} else {
builds = await api.getFolder(`${owner}/${repo}`);
}

const size = Array.isArray(builds) ? builds?.[0].build_num! : 1;
setTotal(size);
});
}, [repo, getBuilds]);

const { loading, value: builds, retry } = useAsyncRetry(
() =>
getBuilds().then(retrievedBuilds => retrievedBuilds ?? [], restartBuild),
[page, pageSize, getBuilds],
);
return builds || [];
} catch (e) {
errorApi.post(e);
throw e;
}
}, [api, errorApi, owner, repo, branch]);

const projectName = `${owner}/${repo}`;
return [
Expand Down

0 comments on commit b2a07d2

Please sign in to comment.