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

83.67% Statements 41/49
80% Branches 36/45
78.57% Functions 11/14
85.1% Lines 40/47

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                                    2x         9x         8x 8x 2x       2x 8x   2x   2x   2x   2x           2x         43x 43x   43x   9x               43x   43x 34x 34x                         43x 22x 20x   2x   2x   2x 8x 8x 8x         8x     2x           8x 2x 2x         2x   2x 2x         2x     43x                                                                                                             2x                                  
import type { IInterfaceData, ISearchAndFilter, IFilterChip } from "../types";
import type { ICharm } from "../../../../shared/types";
 
import { useAtomValue, useSetAtom } from "jotai";
import React, { useEffect, useMemo } from "react";
import { useQuery } from "react-query";
 
import { Row, Col, Spinner, Chip } from "@canonical/react-components";
 
import { filterChipsSelector, filterState } from "../state";
import { InterfaceRow } from "./InterfaceRow";
 
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?extra_fields=default-release.channel.track,default-release.channel.risk&${
      interfaceType === "provides" ? "requires" : "provides"
    }=${interfaceName}`
  );
  const json = await resp.json();
  return (json.packages as ICharm[]).filter(
    (pkg: ICharm) => pkg.package.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 = useSetAtom(filterChipsSelector);
  const filterData = useAtomValue(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 />
      <Row>
        <Col size={5}>
          <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
                  isReadOnly
                  value="Required"
                  appearance="negative"
                  className="u-no-margin--bottom"
                  style={{ marginLeft: "10px" }}
                />
              )}
            </div>
          </h3>
        </Col>
        <Col size={4}>
          <p className="u-fixed-width u-no-margin--bottom">
            This charm{" "}
            <b>{interfaceType === "requires" ? "requires" : "provides"}</b> the{" "}
            <a href={`/integrations/${interfaceData.interface}`}>
              {interfaceData.interface}
            </a>{" "}
            interface.
          </p>
          {charms && charms.length > 0 && <p>It can integrate with:</p>}
          {charms && charms.length === 0 && filterData.length === 0 && (
            <p>
              No publicly listed charms{" "}
              <b>{interfaceType === "requires" ? "provide" : "require"}</b> this
              interface.
            </p>
          )}
          {charms && charms.length === 0 && filterData.length !== 0 && (
            <p>
              No charms found that <b>provide</b> or <b>consume</b>{" "}
              {interfaceData.interface} matching your selected filters.
            </p>
          )}
        </Col>
      </Row>
 
      <Row>
        <Col emptyLarge={2} size={9}>
          {charms && charms?.length > 0 && (
            <ul className="p-list--divided">
              {charms.map((charm: ICharm, idx) => (
                <li key={charm.package.name} className="p-list__item">
                  {idx === 0 && <hr />}
                  <InterfaceRow charm={charm} />
                </li>
              ))}
            </ul>
          )}
        </Col>
      </Row>
      {!charms && (
        <div className="u-fixed-width">
          <Spinner text={`Loading charms for ${interfaceData.interface}`} />
        </div>
      )}
    </>
  );
};