All files / interfaces/components/InterfaceDetails InterfaceDetails.tsx

64.28% Statements 18/28
81.25% Branches 52/64
50% Functions 2/4
64.28% Lines 18/28

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213                                1x                                                   4x 4x 5x 1x     4x     4x   4x                   4x   4x   4x 1x     4x 4x     4x   4x 2x     4x                                                                                                                                                                                                                                                                      
import { useParams } from "react-router-dom";
import { useQuery } from "react-query";
import { formatDistance } from "date-fns";
import { Strip, Row, Col, Notification } from "@canonical/react-components";
 
import InterfaceDetailsNav from "../InterfaceDetailsNav";
import InterfaceDiscussion from "../InterfaceDiscussion";
import CanonicalRelationsMeta from "../CanonicalRelationsMeta";
import CommunityRelationsMeta from "../CommunityRelationsMeta";
import InterfaceDetailsLinks from "../InterfaceDetailsLinks";
import DeveloperDocumentation from "../DeveloperDocumentation";
import ProvidingCharms from "../ProvidingCharms";
import RequiringCharms from "../RequiringCharms";
 
import type { InterfaceData } from "../../types";
 
const getInterface = async (
  interfaceName: string | undefined,
  interfaceStatus?: string | undefined
): Promise<InterfaceData> => {
  if (interfaceName) {
    if (interfaceStatus) {
      const response = await fetch(`./${interfaceStatus}.json`);
      if (response.status === 200) {
        return response.json();
      }
    } else {
      const response = await fetch(`./${interfaceName}.json`);
      if (response.status === 200) {
        return response.json();
      }
    }
  }
 
  throw new Error("Interface is not a tested interface.");
};
 
type Props = {
  interfaceItem?: InterfaceData | null;
};
 
function InterfaceDetails({ interfaceItem }: Props) {
  const { interfaceName, interfaceStatus } = useParams();
  const shouldFetchData = () => {
    if (interfaceItem && interfaceItem.name === interfaceName) {
      return false;
    }
 
    return true;
  };
 
  let isCommunity = false;
 
  const query = useQuery(
    ["interface", interfaceName],
    () => getInterface(interfaceName, interfaceStatus),
    {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,
      enabled: shouldFetchData(),
    }
  );
  const { error: interfaceError, isLoading: interfaceIsLoading } = query;
 
  let { data: interfaceData } = query;
 
  if (interfaceItem && interfaceItem.name === interfaceName) {
    interfaceData = interfaceItem;
  }
 
  const error = interfaceError as Error;
  const isLoading = interfaceIsLoading;
 
  const hasDeveloperDocumentation =
    interfaceData && interfaceData.body ? true : false;
 
  if (!hasDeveloperDocumentation) {
    isCommunity = true;
  }
 
  return (
    <>
      <Strip type="light" shallow>
        <h1>
          {interfaceData?.name && <>{interfaceData?.name}</>}
          {!interfaceData?.name && interfaceName}
        </h1>
      </Strip>
      <Strip>
        {error && (
          <div className="u-fixed-width">
            <Notification severity="negative" title="Error">
              There was a problem fetching this interface. {error.message}
              {isCommunity && (
                <>
                  <br />
                  <a href="https://discourse.charmhub.io/t/implementing-relations/1051">
                    Discuss this interface on discourse.
                  </a>
                </>
              )}
            </Notification>
          </div>
        )}
 
        {isLoading && shouldFetchData() && (
          <div className="u-fixed-width u-align--center">
            Fetching interface...
          </div>
        )}
 
        {interfaceData && !isLoading && (
          <Row>
            <Col size={3} className="interface-sidebar">
              <div className="u-hide--small u-hide--medium">
                <InterfaceDetailsNav
                  hasDeveloperDocumentation={hasDeveloperDocumentation}
                />
              </div>
              <InterfaceDetailsLinks
                isCommunity={isCommunity}
                interfaceName={interfaceName}
                interfaceVersion={interfaceData.version}
              />
              {!isCommunity && (
                <CanonicalRelationsMeta
                  interfaceName={interfaceName}
                  interfaceVersion={interfaceData.version}
                />
              )}
              {isCommunity && <CommunityRelationsMeta />}
              <InterfaceDiscussion />
            </Col>
            <Col size={9} className="interface-content">
              <div className="u-hide--large">
                <InterfaceDetailsNav
                  hasDeveloperDocumentation={hasDeveloperDocumentation}
                />
              </div>
 
              {!!(
                !interfaceData?.charms?.providers?.length &&
                !interfaceData?.other_charms?.providers?.length &&
                !interfaceData?.charms?.requirers?.length &&
                !interfaceData?.other_charms?.requirers?.length
              ) && (
                <Notification severity="information">
                  No charms found that Provide or Require {interfaceName}
                </Notification>
              )}
 
              {!!(
                interfaceData?.charms?.providers?.length ||
                interfaceData?.other_charms?.providers?.length ||
                interfaceData?.charms?.requirers?.length ||
                interfaceData?.other_charms?.requirers?.length
              ) && (
                <Strip className="u-no-padding--top" bordered shallow>
                  <h2 id="charms">Charms</h2>
                </Strip>
              )}
 
              {!!(
                interfaceData?.charms?.providers?.length ||
                interfaceData?.other_charms?.providers?.length
              ) && (
                <ProvidingCharms
                  interfaceData={interfaceData}
                  isCommunity={isCommunity}
                />
              )}
 
              {!!(
                interfaceData?.charms?.requirers?.length ||
                interfaceData?.other_charms?.requirers?.length
              ) && (
                <RequiringCharms
                  interfaceData={interfaceData}
                  isCommunity={isCommunity}
                />
              )}
 
              {hasDeveloperDocumentation && (
                <DeveloperDocumentation interfaceData={interfaceData} />
              )}
 
              <Notification severity="information">
                <>
                  {interfaceData.last_modified &&
                    `Last updated ${formatDistance(
                      new Date(interfaceData.last_modified),
                      new Date(),
                      {
                        addSuffix: true,
                      }
                    )}.`}{" "}
                  <a href="https://github.com/canonical/charm-relation-interfaces">
                    Help us improve this page
                  </a>
                  .
                </>
              </Notification>
            </Col>
          </Row>
        )}
      </Strip>
    </>
  );
}
 
export default InterfaceDetails;