Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
feat: extract render logic to prevent hook mismatch
Browse files Browse the repository at this point in the history
  • Loading branch information
ZackarySantana committed Mar 18, 2024
1 parent 119e3c4 commit 63e2d5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
28 changes: 16 additions & 12 deletions src/pages/task/metadata/Stepback.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { useEffect } from "react";
import styled from "@emotion/styled";
import Badge from "@leafygreen-ui/badge";
import Tooltip from "@leafygreen-ui/tooltip";
Expand All @@ -12,27 +11,32 @@ import { useLastPassingTask } from "hooks/useLastPassingTask";

type Props = {
taskId: string;
task: TaskQuery["task"];
};

export const Stepback: React.FC<Props> = ({ task, taskId }) => {
const { loading, task: breakingTask } = useBreakingTask(taskId);
const { task: lastPassingTask } = useLastPassingTask(taskId);

// lastFailingStepbackTaskId is set only in the middle of stepback (not the first task).
const inStepback =
/**
* Returns whether the task is in stepback.
* @param task The task to check if it is in stepback.
* @returns Whether the task is in stepback.
*/
export function inStepback(task: TaskQuery["task"]) {
// lastFailingStepbackTaskId is set only in the middle/last of stepback (not the first task).
const stepback =
task?.stepbackInfo?.lastFailingStepbackTaskId !== undefined &&
task?.stepbackInfo?.lastFailingStepbackTaskId !== "";

// nextStepbackTaskId is set when the next task in stepback in known, in the beginning
// of stepback, it is known right away. In the rest of stepback, it is not.
const beginningStepback =
task?.stepbackInfo?.nextStepbackTaskId !== undefined &&
task?.stepbackInfo?.nextStepbackTaskId !== "";

// If the task is not in stepback or beginning stepback, it should not show the stepback metadata.
if (!inStepback && !beginningStepback) {
return;
}
// If the task is in stepback or beginning stepback, it is counted as in stepback.
return stepback || beginningStepback;
}

export const Stepback: React.FC<Props> = ({ taskId }) => {
const { loading, task: breakingTask } = useBreakingTask(taskId);
const { task: lastPassingTask } = useLastPassingTask(taskId);

// The last stepback task has an undefined last passing task (it is passing itself).
const isLastStepbackTask = lastPassingTask === undefined;
Expand Down
5 changes: 3 additions & 2 deletions src/pages/task/metadata/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { AbortMessage } from "./AbortMessage";
import { DependsOn } from "./DependsOn";
import ETATimer from "./ETATimer";
import RuntimeTimer from "./RuntimeTimer";
import { Stepback } from "./Stepback";
import { Stepback, inStepback } from "./Stepback";

const { applyStrictRegex, msToDuration, shortenGithash } = string;
const { red } = palette;
Expand Down Expand Up @@ -107,6 +107,7 @@ export const Metadata: React.FC<Props> = ({ error, loading, task, taskId }) => {
Cookies.set(SEEN_HONEYCOMB_GUIDE_CUE, "true", { expires: 365 });
setOpenGuideCue(false);
};
const stepback = inStepback(task);

return (
<MetadataCard error={error} loading={loading}>
Expand Down Expand Up @@ -404,7 +405,7 @@ export const Metadata: React.FC<Props> = ({ error, loading, task, taskId }) => {
</StyledLink>
</MetadataItem>
)}
<Stepback task={task} taskId={taskId} />
{stepback && <Stepback taskId={taskId} />}
</MetadataCard>
);
};
Expand Down

0 comments on commit 63e2d5b

Please sign in to comment.