All files / publisher/components/PublishedSnapList PublishedSnapList.tsx

100% Statements 7/7
91.3% Branches 21/23
100% Functions 4/4
100% Lines 6/6

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                                              9x   9x 30x                                                                                                       9x   9x                                                                                     1x                      
import { Link } from "react-router-dom";
import { MainTable, Pagination, Strip } from "@canonical/react-components";
import SnapNameEntry from "./SnapNameEntry";
import NewSnapNotification from "../NewSnapNotification";
import EmptySnapList from "../EmptySnapList";
import { ITEMS_PER_PAGE } from "../../constants";
 
import type { ISnap } from "../../types";
 
function PublishedSnapList({
  snaps,
  currentUser,
  currentPage,
  setCurrentPage,
  totalItems,
}: {
  snaps: ISnap[];
  currentUser: string;
  currentPage: number;
  setCurrentPage: (page: number) => void;
  totalItems: number;
}): React.JSX.Element {
  const shouldShowNewSnapNotification =
    snaps && snaps.length === 1 && snaps[0].is_new;
 
  const getData = () => {
    return snaps.map((snap) => ({
      columns: [
        {
          content: <SnapNameEntry snap={snap} />,
          role: "rowheader",
          className: "p-table--mobile-card__header",
        },
        {
          content: snap.unlisted
            ? "Unlisted"
            : snap.private
              ? "Private"
              : "Public",
          "aria-label": "Visibility",
        },
        {
          content:
            snap.publisher.username === currentUser
              ? "You"
              : snap.publisher["display-name"],
          "aria-label": "Owner",
        },
        {
          content: (
            <div>
              {snap.latest_release?.status == "Published" ? (
                snap.latest_release?.channels[0]
              ) : (
                <Link to={`/${snap.snapName}/releases`}>Not released</Link>
              )}
            </div>
          ),
          "aria-label": "Latest release",
        },
        {
          content: (
            <div>
              {snap.latest_release?.status == "Published" ? (
                <Link to={`/${snap.snapName}/releases`}>
                  {snap.latest_release?.version}
                </Link>
              ) : (
                ""
              )}
            </div>
          ),
          "aria-label": "Version",
        },
      ],
    }));
  };
 
  const isEmpty = snaps && snaps.length === 0;
 
  return (
    <Strip element="section" shallow>
      {isEmpty && <EmptySnapList />}
 
      {!isEmpty && (
        <div className="u-fixed-width">
          <h2 className="p-heading--4">My published snaps</h2>
        </div>
      )}
 
      {shouldShowNewSnapNotification && <NewSnapNotification snap={snaps[0]} />}
 
      {snaps.length > 0 && (
        <div className="u-fixed-width">
          <MainTable
            headers={[
              {
                content: "Name",
                width: "20%",
              },
              {
                content: "Visibility",
                width: "10%",
              },
              {
                content: "Owner",
                width: "10%",
              },
              {
                content: "Latest release",
                width: "10%",
              },
              {
                content: "Version",
                width: "10%",
              },
            ]}
            rows={getData()}
          />
          <Pagination
            currentPage={currentPage}
            itemsPerPage={ITEMS_PER_PAGE}
            paginate={(page) => {
              setCurrentPage(page);
            }}
            totalItems={totalItems}
          />
        </div>
      )}
    </Strip>
  );
}
 
export default PublishedSnapList;