All files / public/details/integrations/components InterfaceItem.tsx

83.67% Statements 41/49
76.19% Branches 32/42
78.57% Functions 11/14
84.78% Lines 39/46

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                                          2x         6x         5x 5x     2x 8x   2x   2x   2x   2x           2x         47x 47x   47x   6x               47x   47x 31x 31x                         47x 19x 17x   2x   2x   2x 8x 8x 8x         8x     2x           8x 2x 2x         2x   2x 2x         2x     47x                                                                                     2x                                                                            
import type { IInterfaceData, ISearchAndFilter, IFilterChip } from "../types";
import type { ICharm } from "../../../../shared/types";
 
import React, { useEffect, useMemo } from "react";
 
import { useSetRecoilState, useRecoilValue } from "recoil";
import { useQuery } from "react-query";
 
import { Row, Col, Spinner, Chip } from "@canonical/react-components";
 
import { filterChipsSelector, filterState } from "../state";
 
import { IntegrationCard } from "@canonical/store-components";
import { Package } from "../../../../publisher-admin/types";
 
interface InterfaceItemProps {
  interfaceType: string;
  interfaceData: IInterfaceData;
  charmName: string;
}
 
const getCharms = async (
  interfaceType: string,
  interfaceName: string,
  charmName: string
): Promise<ICharm[]> => {
  const resp = await fetch(
    `/store.json?${
      interfaceType === "provides" ? "requires" : "provides"
    }=${interfaceName}`
  );
  const json = await resp.json();
  return json.packages.filter((pkg: Package) => pkg.name !== charmName);
};
 
const filterMap = (charm: ICharm, heading: string) => {
  switch (heading) {
    case "Platform":
      return charm.package["platforms"][0] === "vm" ? "Linux" : "Kubernetes";
    case "Stability":
      return charm.package["channel"]["risk"];
    case "Author":
      return charm.publisher["display_name"];
    case "Charm":
      return charm.package["display_name"];
    default:
      return "";
  }
};
 
export const InterfaceItem = ({
  interfaceType,
  interfaceData,
  charmName,
}: InterfaceItemProps) => {
  const setAvailableFilters = useSetRecoilState(filterChipsSelector);
  const filterData = useRecoilValue(filterState);
 
  const { data } = useQuery(
    ["charms", interfaceType, interfaceData.interface],
    () => getCharms(interfaceType, interfaceData.interface, charmName),
    {
      refetchOnMount: false,
      refetchOnWindowFocus: false,
      refetchOnReconnect: false,
    }
  );
 
  const title = `${interfaceData.key} | ${interfaceData.interface}`;
 
  const charms = useMemo(() => {
    Eif (filterData.length === 0 || data?.length === 0) {
      return data;
    }
    return data?.filter((charm: ICharm) => {
      return filterData.every((filter: IFilterChip) => {
        if (filter.lead === "Integration") {
          return true;
        }
        const value = filterMap(charm, filter.lead);
        return value === filter.value;
      });
    });
  }, [data, filterData]);
 
  useEffect(() => {
    if (!charms) {
      return;
    }
    const headings = ["Platform", "Stability", "Author", "Charm"];
    // Add charms
    const filters = charms.reduce<ISearchAndFilter[]>(
      (acc: ISearchAndFilter[], charm: ICharm) => {
        headings.forEach((heading) => {
          const chip: IFilterChip = { lead: heading, value: "" };
          chip.value = filterMap(charm, heading);
          const currentObj = {
            id: heading,
            heading,
            chips: [chip],
          };
          acc.push(currentObj);
        });
 
        return acc;
      },
      [] as ISearchAndFilter[]
    );
 
    // Integrations
    let integrations = filters.find((item) => item.heading === title);
    Eif (!integrations) {
      integrations = {
        id: "Integration",
        heading: "Integration",
        chips: [],
      };
      filters.unshift(integrations);
    }
    Eif (!integrations.chips.find((item) => item.value === title)) {
      integrations.chips.push({
        lead: "Integration",
        value: title,
      });
    }
    setAvailableFilters(filters);
  }, [charms]);
 
  return (
    <>
      <hr />
      <h3 className="p-heading--4 u-no-margin--bottom" id={interfaceData.key}>
        <div style={{ display: "flex", alignItems: "center" }}>
          {interfaceData.key}
          <span className="u-text--muted">&nbsp;endpoint</span>
          {interfaceData.required === true && (
            <Chip
              value="Required"
              appearance="negative"
              className="u-no-margin--bottom"
              style={{ marginLeft: "10px" }}
            />
          )}
        </div>
        <div>
          <a href={`/integrations/${interfaceData.interface}`}>
            {interfaceData.interface}
          </a>
          <span className="u-text--muted">&nbsp;interface</span>
        </div>
      </h3>
      {interfaceData.description && <p>{interfaceData.description}</p>}
 
      {charms && charms?.length > 0 && (
        <>
          <div style={{ paddingTop: "0.5rem" }}>
            <p className="u-fixed-width u-no-margin--bottom">
              The <b>{interfaceData.key}</b> endpoint
              <b>
                {interfaceType === "requires" ? " requires " : " provides "}
              </b>
              an integration over the{" "}
              <a href={`/integrations/${interfaceData.interface}`}>
                {interfaceData.interface}
              </a>{" "}
              interface
            </p>
            <p>This means it can integrate with:</p>
          </div>
          <Row>
            {charms.map((charm: ICharm) => (
              <React.Fragment key={charm.package.name}>
                <Col
                  size={3}
                  style={{ marginBottom: "1.5rem" }}
                  key={charm.package["display_name"]}
                >
                  <IntegrationCard data={charm} />
                </Col>
              </React.Fragment>
            ))}
          </Row>
        </>
      )}
      {!charms && (
        <div className="u-fixed-width">
          <Spinner text={`Loading charms for ${interfaceData.interface}`} />
        </div>
      )}
      {charms && charms.length === 0 && filterData.length === 0 && (
        <div className="u-fixed-width">
          <p>
            No charms found that{" "}
            <b>{interfaceType === "requires" ? "require" : "provide"}</b>{" "}
            {interfaceData.interface}
          </p>
        </div>
      )}
      {charms && charms.length === 0 && filterData.length !== 0 && (
        <div className="u-fixed-width">
          <p>
            No charms found that <b>provide</b> or <b>consume</b>{" "}
            {interfaceData.interface} matching your selected filters.
          </p>
        </div>
      )}
    </>
  );
};