All files / src/views/PackageLatest PackageLatest.tsx

0% Statements 0/16
0% Branches 0/8
0% Functions 0/2
0% Lines 0/16

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                                                                                       
import { Center, Spinner } from "@chakra-ui/react";
import type { FunctionComponent } from "react";
import { Redirect, useParams } from "react-router-dom";
import { Packages } from "../../api/package/packages";
import { getFullPackageName, findPackage } from "../../api/package/util";
import { useCatalog } from "../../hooks/useCatalog";
import NotFound from "../NotFound";
 
interface RouteParams {
  name: string;
  scope?: string;
}
 
const buildRedirectUrl = (catalog: Packages, name: string, scope?: string) => {
  const prefix = "/packages/";
  const packageName = getFullPackageName(name, scope);
  const pkg = findPackage(catalog, packageName);
 
  if (!pkg) {
    return undefined;
  }
 
  const { version } = pkg;
  const suffix = `/v/${version}`;
  return `${prefix}${packageName}${suffix}`;
};
 
export const PackageLatest: FunctionComponent = () => {
  const { name, scope }: RouteParams = useParams();
  const catalog = useCatalog();
 
  if (catalog.isLoading || !catalog.data) {
    return (
      <Center minH="16rem">
        <Spinner size="xl" />
      </Center>
    );
  }
 
  const url = buildRedirectUrl(catalog.data, name, scope);
 
  return url ? <Redirect to={url} /> : <NotFound />;
};