Skip to content

Commit

Permalink
Tidy hook APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Shirley <andrew.shirley@sainsburys.co.uk>
Signed-off-by: blam <ben@blam.sh>
  • Loading branch information
ashirley authored and benjdlambert committed Jul 14, 2021
1 parent 9fde1b3 commit dc05501
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const BuildWithStepsView = () => {
const { jobName, buildNumber } = useRouteRefParams(buildRouteRef);
const classes = useStyles();

const [{ value }] = useBuildWithSteps(jobName, buildNumber);
const [{ value }] = useBuildWithSteps({ jobName, buildNumber });

return (
<div className={classes.root}>
Expand Down
2 changes: 1 addition & 1 deletion plugins/jenkins/src/components/Cards/Cards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ export const LatestRunCard = ({
branch: string;
variant?: InfoCardVariants;
}) => {
const [{ projects, loading, error }] = useBuilds(branch);
const [{ projects, loading, error }] = useBuilds({ branch });
const latestRun = projects?.[0];
return (
<InfoCard title={`Latest ${branch} build`} variant={variant}>
Expand Down
14 changes: 10 additions & 4 deletions plugins/jenkins/src/components/useBuildWithSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { useCallback, useMemo } from 'react';
import { useCallback } from 'react';
import { useAsyncRetry } from 'react-use';
import { jenkinsApiRef } from '../api';
import { useAsyncPolling } from './useAsyncPolling';
Expand All @@ -28,20 +28,26 @@ const INTERVAL_AMOUNT = 1500;
* @param jobName the full name of the project (job with builds, not a folder). e.g. "department-A/team-1/project-foo/master"
* @param buildNumber the number of the build. e.g. "13"
*/
export function useBuildWithSteps(jobName: string, buildNumber: string) {
export function useBuildWithSteps({
jobName,
buildNumber,
}: {
jobName: string;
buildNumber: string;
}) {
const api = useApi(jenkinsApiRef);
const errorApi = useApi(errorApiRef);
const { entity } = useEntity();
const entityName = useMemo(() => getEntityName(entity), [entity]);

const getBuildWithSteps = useCallback(async () => {
try {
const entityName = await getEntityName(entity);
return api.getBuild(entityName, jobName, buildNumber);
} catch (e) {
errorApi.post(e);
return Promise.reject(e);
}
}, [buildNumber, jobName, entityName, api, errorApi]);
}, [buildNumber, jobName, entity, api, errorApi]);

const { loading, value, retry } = useAsyncRetry(() => getBuildWithSteps(), [
getBuildWithSteps,
Expand Down
2 changes: 1 addition & 1 deletion plugins/jenkins/src/components/useBuilds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export enum ErrorType {
*
* TODO: deprecate branch and add a generic filter concept.
*/
export function useBuilds(branch?: string) {
export function useBuilds({ branch }: { branch?: string } = {}) {
const { entity } = useEntity();
const entityName = getEntityName(entity);
const api = useApi(jenkinsApiRef);
Expand Down

0 comments on commit dc05501

Please sign in to comment.