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

69.38% Statements 34/49
50% Branches 21/42
46.15% Functions 6/13
70.83% Lines 34/48

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 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272                                                                            8x 7x 7x 7x 7x 7x 7x   7x   7x   7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x   7x 7x           7x     7x 5x       5x         7x 5x         7x                                                                                                                                                                           1x                                                                                         1x 1x 1x                                                                                                  
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import {
  Strip,
  Row,
  Col,
  Button,
  Icon,
  Notification,
  Accordion,
} from "@canonical/react-components";
 
import CollaboratorFilter from "./CollaboratorFilter";
import Collaborators from "./Collaborators";
import Invites from "./Invites";
import InviteCollaborator from "./InviteCollaborator";
import InviteConfirmationModal from "./InviteConfirmationModal";
 
import { publisherMatchesFilterQuery } from "../../utils";
 
import {
  collaboratorsListState,
  publisherState,
  invitesListState,
  activeInviteEmailState,
  inviteLinkState,
  filterQueryState,
} from "../../state/atoms";
import {
  filteredCollaboratorsListState,
  filteredInvitesListState,
} from "../../state/selectors";
import { useCollaboratorsQuery, useInvitesQuery } from "../../hooks";
import { getUniqueInvites } from "../../utils/getUniqueInvites";
import { useQueryClient } from "react-query";
 
function Collaboration() {
  const queryClient = useQueryClient();
  const { packageName } = useParams();
  const [showRevokeSuccess, setShowRevokeSuccess] = useState<boolean>(false);
  const [showRevokeError, setShowRevokeError] = useState<boolean>(false);
  const [showInviteSuccess, setShowInviteSuccess] = useState<boolean>(false);
  const [showInviteError, setShowInviteError] = useState<boolean>(false);
  const [showSidePanel, setShowSidePanel] = useState<boolean>(false);
  const [showRevokeCollaboratorModal, setShowRevokeCollaboratorModal] =
    useState<boolean>(false);
  const [showRevokeInviteModal, setShowRevokeInviteModal] =
    useState<boolean>(false);
  const [showReopenInviteModal, setShowReopenInviteModal] =
    useState<boolean>(false);
  const setCollaboratorsList = useSetRecoilState(collaboratorsListState);
  const collaboratorsList = useRecoilValue(filteredCollaboratorsListState);
  const [publisher, setPublisher] = useRecoilState(publisherState);
  const [copied, setCopied] = useState(false);
  const setInvitesList = useSetRecoilState(invitesListState);
  const invitesList = useRecoilValue(filteredInvitesListState);
  const activeInviteEmail = useRecoilValue(activeInviteEmailState);
  const inviteLink = useRecoilValue(inviteLinkState);
  const filterQuery = useRecoilValue(filterQueryState);
  const { data: collaboratorsData } = useCollaboratorsQuery(packageName);
  const { data: invitesData } = useInvitesQuery(packageName);
 
  const getCollaboratorsCount = () => {
    Iif (publisher) {
      if (!filterQuery || publisherMatchesFilterQuery(publisher, filterQuery)) {
        return collaboratorsList.length + 1;
      }
    }
 
    return collaboratorsList.length;
  };
 
  useEffect(() => {
    Iif (collaboratorsData && collaboratorsData.collaborators) {
      setCollaboratorsList(collaboratorsData.collaborators);
    }
 
    Iif (collaboratorsData && collaboratorsData.publisher) {
      setPublisher(collaboratorsData.publisher);
    }
  }, [collaboratorsData]);
 
  useEffect(() => {
    Iif (invitesData && invitesData.length) {
      setInvitesList(invitesData);
    }
  }, [invitesData]);
 
  return (
    <>
      <div className="l-application">
        <div className="l-main">
          <div className="p-panel">
            {showRevokeSuccess && (
              <Notification
                severity="positive"
                onDismiss={() => {
                  setShowRevokeSuccess(false);
                }}
              >
                The invite for <strong>{activeInviteEmail}</strong> has been
                revoked.
              </Notification>
            )}
 
            {showRevokeError && (
              <Notification
                severity="negative"
                onDismiss={() => {
                  setShowRevokeError(false);
                }}
              >
                There was a problem revoking the invite for{" "}
                <strong>{activeInviteEmail}</strong>.
              </Notification>
            )}
 
            {showInviteSuccess && (
              <Notification
                severity="positive"
                title={`Invite for "${activeInviteEmail}" re-opened`}
                onDismiss={() => {
                  setShowInviteSuccess(false);
                }}
              >
                <p>
                  Copy the invite URL and send it to the collaborator to grant
                  access
                </p>
                <div className="grid-row">
                  <div className="grid-col-7">
                    <pre className="p-code-snippet__block">
                      <code>{inviteLink}</code>
                    </pre>
                  </div>
                  <div className="grid-col-1">
                    <Button
                      type="button"
                      appearance="base"
                      className="p-button"
                      onClick={() => {
                        navigator.clipboard.writeText(inviteLink as string);
                        setCopied(true);
                        setTimeout(() => setCopied(false), 2000);
                      }}
                    >
                      {copied ? "Copied!" : "Copy"}
                    </Button>
                  </div>
                </div>
              </Notification>
            )}
 
            {showInviteError && (
              <Notification
                severity="negative"
                onDismiss={() => {
                  setShowInviteError(false);
                }}
              >
                There was a problem creating an invite for{" "}
                <strong>{activeInviteEmail}</strong>.
              </Notification>
            )}
 
            <Row>
              <Col size={6}>
                <CollaboratorFilter />
              </Col>
              <Col size={6} className="u-align--right">
                <Button
                  appearance="positive"
                  hasIcon
                  onClick={() => {
                    setShowSidePanel(true);
                  }}
                >
                  <Icon name="plus" light />
                  <span>Add new collaborator</span>
                </Button>
              </Col>
            </Row>
            <Strip shallow>
              <div className="u-fixed-width">
                <Accordion
                  expanded="collaborators"
                  sections={[
                    {
                      key: "collaborators",
                      title: `Active shares (${getCollaboratorsCount()})`,
                      content: (
                        <Collaborators
                          setShowRevokeModal={setShowRevokeCollaboratorModal}
                        />
                      ),
                    },
                    {
                      key: "invites",
                      title: `Invites (${getUniqueInvites(invitesList).length})`,
                      content: (
                        <Invites
                          setShowRevokeModal={setShowRevokeInviteModal}
                          setShowReopenModal={setShowReopenInviteModal}
                        />
                      ),
                    },
                  ]}
                />
              </div>
            </Strip>
          </div>
        </div>
        <div
          className={`l-aside__overlay ${!showSidePanel && "u-hide"}`}
          onClick={() => {
            setShowSidePanel(false);
            queryClient.invalidateQueries("invitesData");
          }}
          onKeyDown={(e) => {
            Eif (e.key === "Enter" || e.key === "Escape") {
              setShowSidePanel(false);
              queryClient.invalidateQueries("invitesData");
            }
          }}
          role="button"
          tabIndex={0}
          aria-label="Close side panel"
        ></div>
        <aside className={`l-aside ${!showSidePanel && "is-collapsed"}`}>
          <InviteCollaborator
            setShowSidePanel={setShowSidePanel}
            setShowInviteSuccess={setShowInviteSuccess}
            setShowInviteError={setShowInviteError}
          />
        </aside>
 
        {showRevokeCollaboratorModal && (
          <InviteConfirmationModal
            action="Revoke"
            setShowModal={setShowRevokeCollaboratorModal}
            setShowSuccess={setShowRevokeSuccess}
            setShowError={setShowRevokeError}
            queryKey="collaboratorsData"
          />
        )}
 
        {showRevokeInviteModal && (
          <InviteConfirmationModal
            action="Revoke"
            setShowModal={setShowRevokeInviteModal}
            setShowSuccess={setShowRevokeSuccess}
            setShowError={setShowRevokeError}
            queryKey="invitesData"
          />
        )}
 
        {showReopenInviteModal && (
          <InviteConfirmationModal
            action="Reopen"
            setShowModal={setShowReopenInviteModal}
            setShowSuccess={setShowInviteSuccess}
            setShowError={setShowInviteError}
          />
        )}
      </div>
    </>
  );
}
 
export default Collaboration;