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

70.27% Statements 26/37
78.12% Branches 25/32
54.54% Functions 6/11
72.22% Lines 26/36

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 213 214                                                              139x 139x 139x     139x 139x 139x 139x 139x   139x 139x   139x 233x       139x 233x           139x 118x       118x     139x                                                                                     111x 111x                                           3x 3x 3x 3x         2x       2x                                                                                                                                            
import { useAtom, useAtomValue, useSetAtom } from "jotai";
import { type ReactElement, useState } from "react";
import { useParams } from "react-router-dom";
import { useQueryClient } from "react-query";
import {
  Input,
  Button,
  Icon,
  Spinner,
  Panel,
} from "@canonical/react-components";
 
import { isPending } from "../../utils";
import {
  activeInviteEmailState,
  invitesListState,
  inviteLinkState,
  publisherState,
} from "../../state/atoms";
 
import { generateInviteToken } from "../../utils/generateInviteToken";
import { Invite } from "../../types";
import { filteredCollaboratorsListState } from "../../state/selectors";
 
type Props = {
  setShowSidePanel: (showSidePanel: boolean) => void;
  setShowInviteSuccess: (showInviteSuccess: boolean) => void;
  setShowInviteError: (showInviteError: boolean) => void;
};
 
function InviteCollaborator({ setShowSidePanel }: Props): ReactElement {
  const { packageName } = useParams();
  const queryClient = useQueryClient();
  const [activeInviteEmail, setActiveInviteEmail] = useAtom(
    activeInviteEmailState
  );
  const setInviteLink = useSetAtom(inviteLinkState);
  const invitesList = useAtomValue(invitesListState);
  const inviteLink = useAtomValue(inviteLinkState);
  const publisher = useAtomValue(publisherState);
  const collaboratorsList = useAtomValue(filteredCollaboratorsListState);
 
  const [copied, setCopied] = useState(false);
  const [loadingInviteLink, setLoadingInviteLink] = useState(false);
 
  const hasPendingInvite = (email: string | undefined) =>
    invitesList?.some(
      (invite: Invite) => invite.email === email && isPending(invite)
    );
 
  const isCollaborator = (email: string | undefined) =>
    !!email &&
    (collaboratorsList.some(
      (collaborator) => collaborator?.account?.email === email
    ) ||
      publisher?.email === email);
 
  const isEligibleForInvite = (email: string | undefined) => {
    Iif (!email) {
      return true;
    }
 
    return !hasPendingInvite(email) && !isCollaborator(email);
  };
 
  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>
        <div className="p-notification--caution">
          <div className="p-notification__content">
            <h5 className="p-notification__title">A collaborator can:</h5>
            <div className="p-notification__message">
              <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>
            </div>
          </div>
        </div>
        <Input
          type="email"
          id="collaborator-email"
          label={<strong>1. Email</strong>}
          placeholder="colleague@company.com"
          help="Collaborator email linked to the Ubuntu One account"
          value={activeInviteEmail}
          onInput={(e) => {
            setActiveInviteEmail(e.currentTarget.value);
            setInviteLink("");
          }}
          success={inviteLink ? "Invite sent successfully" : undefined}
          error={
            activeInviteEmail && isCollaborator(activeInviteEmail)
              ? "This email address is already a collaborator"
              : activeInviteEmail && hasPendingInvite(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 ||
              !isEligibleForInvite(activeInviteEmail) ||
              loadingInviteLink
            }
            onClick={async () => {
              Eif (activeInviteEmail && isEligibleForInvite(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..." />
              </>
            ) : (
              "Send invite"
            )}
          </Button>
          <p className="u-text--muted">
            An email will be sent to the collaborator, it will expire after 30
            days.
          </p>
          <p className="u-text--muted">
            Alternatively, you can share the link below:
          </p>
          {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">
            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;