All files / publisher/components/RegisteredSnaps RegisteredSnaps.tsx

45.83% Statements 11/24
66.66% Branches 8/12
42.85% Functions 3/7
45.83% Lines 11/24

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                                              6x     6x 6x   6x       6x 6x 34x 34x   34x                                                                                                                                       6x                                               6x                                                                                                          
import { useState } from "react";
import { Link } from "react-router-dom";
import {
  Accordion,
  ConfirmationModal,
  MainTable,
  Notification,
  Row,
  Tooltip,
} from "@canonical/react-components";
import { ITEMS_PER_PAGE } from "../../constants";
 
import type { ISnap } from "../../types";
 
function RegisteredSnaps({
  snaps,
  currentUser,
  refetchSnaps,
}: {
  snaps: ISnap[];
  currentUser: string;
  refetchSnaps: () => void;
}): React.JSX.Element {
  const [unregisterSnapModal, setUnregisterSnapModal] = useState<string | null>(
    null,
  );
  const [isError, setIsError] = useState<boolean>(false);
  const [unregisterLoading, setUnregisterLoading] = useState<boolean>(false);
 
  const closeModal = (): void => {
    setUnregisterSnapModal(null);
  };
 
  const getData = () => {
    return snaps.map((snap) => {
      const isDisputePending = snap.status === "DisputePending";
      const isUsersSnap = snap.publisher.username === currentUser;
 
      return {
        columns: [
          {
            width: "25%",
            content: (
              <>
                {snap.snapName}
                {isDisputePending && (
                  <>
                    &nbsp;
                    <i
                      className="p-icon--warning p-snapcraft-dispute-list__icon"
                      aria-label="Name dispute in progress"
                    ></i>
                  </>
                )}
              </>
            ),
          },
          {
            content: "",
          },
          {
            content: "",
          },
          {
            content: isUsersSnap ? (
              <button
                className="p-button--base u-no-margin--bottom is-dense"
                onClick={() => {
                  setUnregisterSnapModal(snap.snapName);
                }}
              >
                Unregister
              </button>
            ) : (
              <>
                <Tooltip
                  message={"Snaps can only be unregistered by their owner."}
                >
                  <button
                    className="u-no-margin--bottom u-no-margin--right is-dense"
                    disabled
                  >
                    Unregister
                  </button>
                </Tooltip>
              </>
            ),
          },
          {
            content: isDisputePending ? (
              <span className="p-snapcraft-dispute-list__muted">
                (Name dispute in progress)
              </span>
            ) : (
              <Link to="/docs/releasing-your-app" target="_blank">
                Publish to this name
              </Link>
            ),
            className: "u-align--right",
          },
        ],
        className: "p-snapcraft-dispute-list__item",
      };
    });
  };
 
  const unregisterPackage = async () => {
    setUnregisterLoading(true);
    setIsError(false);
    try {
      const response = await fetch(`/packages/${unregisterSnapModal}`, {
        method: "DELETE",
        headers: {
          "Content-Type": "application/json",
          "X-CSRFToken": window.CSRF_TOKEN,
        },
      });
      if (response.ok) {
        void refetchSnaps();
      } else {
        setIsError(true);
      }
    } catch (_) {
      setIsError(true);
    } finally {
      setUnregisterLoading(false);
      closeModal();
    }
  };
 
  return (
    <>
      {unregisterSnapModal && (
        <ConfirmationModal
          title={
            <div className="p-snap-list__confirmation-modal">
              <i className="p-icon--warning p-snap-list__confirmation-modal-icon"></i>
              Unregister “<span>{unregisterSnapModal}</span>”
            </div>
          }
          confirmButtonLabel="Unregister"
          onConfirm={() => {
            void unregisterPackage();
          }}
          close={closeModal}
          confirmButtonLoading={unregisterLoading}
        >
          <p>
            Are you sure you want to unregister “
            <span>{unregisterSnapModal}</span>”?
            <br />
            This name will be removed from your registered names and become
            available to others. This action is permanent and cannot be undone.
          </p>
        </ConfirmationModal>
      )}
 
      {isError && (
        <div className="u-fixed-width">
          <Notification severity="negative" title="Error:">
            Something went wrong. Please try again later.
          </Notification>
        </div>
      )}
 
      <Row>
        <Accordion
          className="accordion-bold-titles"
          sections={[
            {
              key: "registered-snap-names",
              title: `Registered snap names (${snaps.length})`,
              content: <MainTable rows={getData()} paginate={ITEMS_PER_PAGE} />,
            },
          ]}
          expanded="registered-snap-names"
        />
      </Row>
    </>
  );
}
 
export default RegisteredSnaps;