All files / src/views/Home CDKTypeTabs.tsx

0% Statements 0/13
0% Branches 0/2
0% Functions 0/5
0% Lines 0/13

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                                                                                                                                                                                                                                                                                                                                                                     
import {
  Button,
  Flex,
  forwardRef,
  Heading,
  Tab,
  TabList,
  TabPanelProps,
  TabPanel,
  TabPanels,
  Tabs,
  Text,
} from "@chakra-ui/react";
import type { FunctionComponent } from "react";
import { HOME_ANALYTICS, SECTION_PADDING } from "./constants";
import { PackageGrid } from "./PackageGrid";
import testIds from "./testIds";
import { CatalogSearchSort } from "../../api/catalog-search/constants";
import { NavLink } from "../../components/NavLink";
import { ROUTES } from "../../constants/url";
import { eventName } from "../../contexts/Analytics";
import { useCatalogResults } from "../../hooks/useCatalogResults";
import { useHistoryState } from "../../hooks/useHistoryState";
import { getSearchPath } from "../../util/url";
 
interface PackageTabProps {
  "data-event": string;
  data: ReturnType<typeof useCatalogResults>;
  label: string;
}
 
interface PackageTabPanelProps extends PackageTabProps, TabPanelProps {
  tag: string;
}
 
const tabs = {
  community: {
    "data-event": HOME_ANALYTICS.PUBLISHER.eventName("Community"),
    label: "Community",
    tag: "community",
  },
  aws: {
    "data-event": HOME_ANALYTICS.PUBLISHER.eventName("AWS"),
    label: "AWS",
    tag: "aws-published",
  },
  hashicorp: {
    "data-event": HOME_ANALYTICS.PUBLISHER.eventName("HashiCorp"),
    label: "HashiCorp",
    tag: "hashicorp-published",
  },
};
 
const PackageTab: FunctionComponent<PackageTabProps> = ({
  "data-event": dataEvent,
  data,
  label,
}) => {
  return (
    <Tab
      data-event={eventName(dataEvent, "Tab")}
      data-testid={testIds.cdkTypeTab}
      data-value={label}
      isDisabled={data.page.length < 1}
    >
      {label}
    </Tab>
  );
};
 
const PackageTabPanel = forwardRef<PackageTabPanelProps, "div">(
  ({ "data-event": dataEvent, label, data, tag, ...props }, ref) => {
    return (
      <TabPanel data-testid={testIds.cdkTypeGrid} ref={ref} {...props} p={0}>
        <PackageGrid data-event={dataEvent} packages={data.page} />
 
        <Flex justify="center" w="full">
          <Button
            as={NavLink}
            colorScheme="brand"
            data-event={eventName(dataEvent, "See All")}
            data-testid={testIds.cdkTypeSeeAllButton}
            my={8}
            onClick={() => window.scrollTo(0, 0)}
            to={getSearchPath({
              tags: tag ? [tag] : undefined,
              sort: CatalogSearchSort.DownloadsDesc,
            })}
          >
            See all {label} constructs
          </Button>
        </Flex>
      </TabPanel>
    );
  }
);
 
export const CDKTypeTabs: FunctionComponent = () => {
  const community = useCatalogResults({
    tags: [tabs.community.tag],
    limit: 4,
    sort: CatalogSearchSort.DownloadsDesc,
  });
 
  const aws = useCatalogResults({
    tags: [tabs.aws.tag],
    limit: 4,
    sort: CatalogSearchSort.DownloadsDesc,
  });
  const hashicorp = useCatalogResults({
    tags: [tabs.hashicorp.tag],
    limit: 4,
    sort: CatalogSearchSort.DownloadsDesc,
  });
 
  const [tabIndex, setTabIndex] = useHistoryState("cdkTypeTab", 0);
 
  return (
    <Flex
      bg="bgSecondary"
      color="textPrimary"
      data-testid={testIds.cdkTypeSection}
      direction="column"
      px={SECTION_PADDING.X}
      py={SECTION_PADDING.Y}
      zIndex="0"
    >
      <Heading
        as="h3"
        data-testid={testIds.cdkTypeSectionHeading}
        fontSize="1.5rem"
        fontWeight="semibold"
        lineHeight="lg"
        mb={2}
      >
        Search by publisher
      </Heading>
      <Text
        data-testid={testIds.cdkTypeSectionDescription}
        lineHeight="md"
        mb={5}
      >
        Find constructs published by the open-source community, AWS, and cloud
        technology providers in one location. You can also include your own
        construct libraries on Construct Hub by publishing them on npm registry.
        More concrete guidance can be found in the{" "}
        <NavLink color="link" to={ROUTES.CONTRIBUTE}>
          Contribute
        </NavLink>{" "}
        page.
      </Text>
      <Tabs
        defaultIndex={tabIndex}
        onChange={(index) => setTabIndex(index)}
        variant="line"
      >
        <TabList>
          <PackageTab data={community} {...tabs.community} />
 
          <PackageTab data={aws} {...tabs.aws} />
 
          <PackageTab data={hashicorp} {...tabs.hashicorp} />
        </TabList>
        <TabPanels minH="28.5rem">
          <PackageTabPanel
            data={community}
            {...tabs.community}
            label={tabs.community.label.toLowerCase()}
          />
 
          <PackageTabPanel data={aws} {...tabs.aws} />
 
          <PackageTabPanel data={hashicorp} {...tabs.hashicorp} />
        </TabPanels>
      </Tabs>
    </Flex>
  );
};