All files / store/pages/Explore Explore.tsx

94.44% Statements 17/18
86.66% Branches 13/15
100% Functions 5/5
94.44% Lines 17/18

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                            8x 8x 8x                                                                   8x       24x     15x       15x       15x   15x               8x 14x     8x   8x 8x 24x 9x         8x                                                                                                                                                                                                            
import { useRef } from "react";
import { useQueries } from "react-query";
import { Strip, Row, Col } from "@canonical/react-components";
 
import Banner from "../../components/Banner";
import RecommendationsSection from "./RecommendationsSection";
// import EditorialSection from "./EditorialSection";
import ListSection from "./ListSection";
import Categories from "./Categories";
 
import type { UseQueryResult } from "react-query";
import type { RecommendationData } from "../../types";
 
function Explore(): JSX.Element {
  const searchRef = useRef<HTMLInputElement | null>(null);
  const searchSummaryRef = useRef<HTMLDivElement | null>(null);
  const categories: string[] = ["popular", "recent", "trending"];
  // const sliceIds: string[] = ["our_picks", "must_have_snaps"];
 
  // const slices = useQueries(
  //   sliceIds.map((sliceId) => ({
  //     queryKey: ["slices", sliceId],
  //     queryFn: async () => {
  //       const response = await fetch(
  //         `https://recommendations.snapcraft.io/api/slice/${sliceId}`,
  //       );
  //
  //       if (!response.ok) {
  //         throw Error(`Unable to fetch ${sliceId} data`);
  //       }
  //
  //       const responseData = await response.json();
  //
  //       return responseData;
  //     },
  //   })),
  // );
 
  // const slicesLoading: boolean = slices.some((s) => s.isLoading);
 
  // const slicesData: Record<string, SlicesData> = {};
 
  // if (slices) {
  //   slices.forEach((slice) => {
  //     if (slice.data) {
  //       slicesData[slice.data.slice.id] = slice.data;
  //     }
  //   });
  // }
 
  const recommendations: UseQueryResult<{
    name: string;
    snaps: RecommendationData[];
  }>[] = useQueries(
    categories.map((category) => ({
      queryKey: ["recommendations", category],
      queryFn: async () => {
        const response = await fetch(
          `https://recommendations.snapcraft.io/api/category/${category}`,
        );
 
        Iif (!response.ok) {
          throw Error(`Unable to fetch ${category} data`);
        }
 
        const responseData = await response.json();
 
        return {
          name: category,
          snaps: responseData,
        };
      },
    })),
  );
 
  const recommendationsLoading: boolean = recommendations.some(
    (r) => r.isLoading,
  );
 
  const snaps: Record<string, RecommendationData[]> = {};
 
  Eif (recommendations) {
    recommendations.forEach((recommendation) => {
      if (recommendation.data) {
        snaps[recommendation.data.name] = recommendation.data.snaps.slice(0, 8);
      }
    });
  }
 
  return (
    <>
      <Banner searchRef={searchRef} searchSummaryRef={searchSummaryRef} />
 
      {/* slices && (
        <>
          {slicesData.our_picks && (
            <Strip className="u-no-padding--bottom">
              <EditorialSection
                isLoading={slicesLoading}
                slice={slicesData.our_picks}
                gradient="purplePink"
              />
            </Strip>
          )}
        </>
      ) */}
 
      {recommendations && (
        <>
          {snaps.recent && (
            <Strip shallow className="u-no-padding--bottom">
              <RecommendationsSection
                snaps={snaps.recent}
                title="Recently updated snaps"
                isLoading={recommendationsLoading}
                highlight={true}
              />
            </Strip>
          )}
 
          {snaps.popular && (
            <Strip shallow className="u-no-padding--bottom">
              <ListSection
                snaps={snaps.popular}
                title="Most popular snaps"
                isLoading={recommendationsLoading}
              />
            </Strip>
          )}
        </>
      )}
 
      <Strip shallow className="u-no-padding--top">
        <Categories />
      </Strip>
 
      {/* Placeholder until content is decided */}
      {/* slices && (
        <>
          {slicesData.must_have_snaps && (
            <Strip shallow className="u-no-padding--top u-no-padding--bottom">
              <EditorialSection
                isLoading={slicesLoading}
                slice={slicesData.must_have_snaps}
                gradient="blueGreen"
              />
            </Strip>
          )}
        </>
      ) */}
 
      {recommendations && snaps.trending && (
        <Strip shallow>
          <RecommendationsSection
            snaps={snaps.trending}
            title="Trending snaps"
            isLoading={recommendationsLoading}
          />
        </Strip>
      )}
 
      <Strip className="u-no-padding--top">
        <div
          style={{
            backgroundImage:
              "url('https://assets.ubuntu.com/v1/e888a79f-suru.png')",
            backgroundPosition: "top right",
            backgroundSize: "contain",
            backgroundRepeat: "no-repeat",
            backgroundColor: "#f3f3f3",
            padding: "67px",
          }}
        >
          <Row>
            <Col size={6}>
              <h2>Learn how to snap in 30 minutes</h2>
              <p className="p-heading--4">
                Find out how to build and publish snaps
              </p>
              <a className="p-button--positive" href="/docs/get-started">
                Get started
              </a>
            </Col>
          </Row>
        </div>
      </Strip>
    </>
  );
}
 
export default Explore;