All files / publisher/pages/Builds Builds.tsx

94.11% Statements 16/17
86.66% Branches 13/15
100% Functions 2/2
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85                                        5x 5x 5x 5x     5x 5x     2x   2x       2x   2x   2x 1x 1x   1x         5x   5x                                                                  
import { useState } from "react";
import { useParams } from "react-router-dom";
import { useQuery } from "react-query";
import { useRecoilState } from "recoil";
import { Strip } from "@canonical/react-components";
 
import SectionNav from "../../components/SectionNav";
import LoggedOut from "./LoggedOut";
import RepoNotConnected from "./RepoNotConnected";
import RepoConnected from "./RepoConnected";
 
import {
  buildLoggedInState,
  buildRepoConnectedState,
  githubDataState,
} from "../../state/atoms";
 
import { setPageTitle } from "../../utils";
 
function Builds(): React.JSX.Element {
  const { snapId } = useParams();
  const [githubData, setGithubData] = useRecoilState(githubDataState);
  const [loggedIn, setLoggedIn] = useRecoilState(buildLoggedInState);
  const [repoConnected, setRepoConnected] = useRecoilState(
    buildRepoConnectedState,
  );
  const [autoTriggerBuild, setAutoTriggerBuild] = useState<boolean>(false);
  const { isLoading } = useQuery({
    queryKey: ["githubData"],
    queryFn: async () => {
      const response = await fetch(`/api/${snapId}/repo`);
 
      Iif (!response.ok) {
        setGithubData(null);
      }
 
      const responseData = await response.json();
 
      const githubData = responseData.data;
 
      setLoggedIn(githubData.github_user !== null);
      setRepoConnected(githubData.github_repository !== null);
      setGithubData(responseData.data);
 
      return responseData.data;
    },
    retry: 0,
  });
 
  setPageTitle(`Builds for ${snapId}`);
 
  return (
    <>
      <h1 className="p-heading--4" aria-live="polite">
        <a href="/snaps">My snaps</a> / <a href={`/${snapId}`}>{snapId}</a> /
        Builds
      </h1>
 
      <SectionNav snapName={snapId} activeTab="builds" />
 
      {isLoading && (
        <Strip shallow>
          <p>
            <i className="p-icon--spinner u-animation--spin"></i>&nbsp;Loading{" "}
            {snapId} builds data
          </p>
        </Strip>
      )}
 
      {!isLoading && !loggedIn && <LoggedOut />}
      {!isLoading && loggedIn && !repoConnected && (
        <RepoNotConnected setAutoTriggerBuild={setAutoTriggerBuild} />
      )}
      {githubData && loggedIn && repoConnected && (
        <RepoConnected
          autoTriggerBuild={autoTriggerBuild}
          setAutoTriggerBuild={setAutoTriggerBuild}
        />
      )}
    </>
  );
}
 
export default Builds;