All files / publisher-admin/pages/Collaboration InviteCollaborator.tsx

66.66% Statements 24/36
69.56% Branches 16/23
44.44% Functions 4/9
68.57% Lines 24/35

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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                                            130x 130x 130x     130x 130x 130x   130x 130x   130x 250x 13x     237x       237x       237x 237x           130x                                                                                   111x 111x                                     3x 3x 3x 3x         3x       3x                                                                                                                                                      
import { SyntheticEvent, useState } from "react";
import { useParams } from "react-router-dom";
import { useQueryClient } from "react-query";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import {
  Notification,
  Input,
  Button,
  Icon,
  Spinner,
  Panel,
} from "@canonical/react-components";
 
import { isPending } from "../../utils";
import {
  activeInviteEmailState,
  invitesListState,
  inviteLinkState,
} from "../../state/atoms";
 
import { generateInviteToken } from "../../utils/generateInviteToken";
import { Invite } from "../../types";
 
type Props = {
  setShowSidePanel: (showSidePanel: boolean) => void;
  setShowInviteSuccess: (showInviteSuccess: boolean) => void;
  setShowInviteError: (showInviteError: boolean) => void;
};
 
function InviteCollaborator({ setShowSidePanel }: Props): JSX.Element {
  const { packageName } = useParams();
  const queryClient = useQueryClient();
  const [activeInviteEmail, setActiveInviteEmail] = useRecoilState(
    activeInviteEmailState
  );
  const setInviteLink = useSetRecoilState(inviteLinkState);
  const invitesList = useRecoilValue(invitesListState);
  const inviteLink = useRecoilValue(inviteLinkState);
 
  const [copied, setCopied] = useState(false);
  const [loadingInviteLink, setLoadingInviteLink] = useState(false);
 
  const isUnique = (email: string | undefined) => {
    if (!email) {
      return true;
    }
 
    Iif (!invitesList) {
      return false;
    }
 
    const existingInvites = invitesList.filter((invite: Invite) => {
      return invite.email === email && isPending(invite);
    });
 
    Eif (!existingInvites.length) {
      return true;
    }
 
    return false;
  };
 
  return (
    <Panel
      wrapContent={false}
      header={
        <div className="p-panel__header u-no-padding--left u-no-padding--right">
          <h4 className="p-panel__title">Add new collaborator</h4>
          <div className="p-panel__controls">
            <Button
              hasIcon
              type="button"
              className="p-button--base u-no-margin--bottom"
              onClick={() => {
                setShowSidePanel(false);
                queryClient.invalidateQueries("invitesData");
              }}
            >
              <Icon name="close" />
            </Button>
          </div>
        </div>
      }
    >
      <div>
        <Notification severity="caution" title="A collaborator can:">
          <ul>
            <li>Access and modify this charm</li>
            <li>Publish new versions and manage releases</li>
            <li>Represent the charm alongside you as a publisher</li>
          </ul>
        </Notification>
        <Input
          type="email"
          id="collaborator-email"
          label={<strong>1. Email</strong>}
          placeholder="yourname@example.com"
          help="Collaborator email linked to the Ubuntu One account"
          value={activeInviteEmail}
          onInput={(
            e: SyntheticEvent<HTMLInputElement> & {
              target: HTMLInputElement;
            }
          ) => {
            setActiveInviteEmail(e.target.value);
            setInviteLink("");
          }}
          error={
            !isUnique(activeInviteEmail)
              ? "There is already a pending invite for this email address"
              : ""
          }
        />
        <div className="p-panel__content u-no-padding--top u-no-padding--bottom">
          <h5>2. Invite Link</h5>
          <Button
            type="button"
            appearance="positive"
            disabled={
              !activeInviteEmail ||
              !isUnique(activeInviteEmail) ||
              loadingInviteLink
            }
            onClick={async () => {
              Eif (activeInviteEmail && isUnique(activeInviteEmail)) {
                try {
                  setLoadingInviteLink(true);
                  const { inviteLink } = await generateInviteToken(
                    activeInviteEmail,
                    packageName!,
                    window.CSRF_TOKEN
                  );
                  setInviteLink(inviteLink);
                } catch (err) {
                  console.error("Error generating invite preview:", err);
                } finally {
                  setLoadingInviteLink(false);
                }
              }
            }}
          >
            {loadingInviteLink ? (
              <>
                <Spinner text="Loading..." />
              </>
            ) : (
              "Generate invite link"
            )}
          </Button>
          {inviteLink ? (
            <div className="grid-row">
              <div className="grid-col-6">
                <pre className="p-code-snippet__block">
                  <code>{inviteLink}</code>
                </pre>
              </div>
              <div className="grid-col-2">
                <Button
                  type="button"
                  appearance="base"
                  className="p-button"
                  onClick={() => {
                    navigator.clipboard.writeText(inviteLink);
                    setCopied(true);
                    setTimeout(() => setCopied(false), 2000);
                  }}
                >
                  {copied ? "Copied!" : "Copy"}
                </Button>
              </div>
            </div>
          ) : (
            <pre className="p-code-snippet__block">
              <code>Enter email to generate a unique invitation link</code>
            </pre>
          )}
          <p className="u-text--muted">
            Important: This link will <strong>NOT</strong> be automatically
            sent.
          </p>
          <div className="u-text--muted">
            After generating, you'll need to:
            <ul>
              <li>Copy the link</li>
              <li>Share it with your collaborator</li>
              <li>The link expires 30 days after generation</li>
            </ul>
          </div>
          <p className="u-text--muted">
            Once your collaborator uses the link, they'll gain access to the
            charm.
          </p>
        </div>
      </div>
      <div className="p-panel__footer u-align--right">
        <Button
          type="button"
          className="u-no-margin--bottom"
          onClick={() => {
            setShowSidePanel(false);
            queryClient.invalidateQueries("invitesData");
          }}
        >
          Done
        </Button>
      </div>
    </Panel>
  );
}
 
export default InviteCollaborator;