All files / publisher-admin/utils buildInviteTableRows.tsx

4.34% Statements 1/23
0% Branches 0/32
16.66% Functions 1/6
4.54% Lines 1/22

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                                              39x                                                                                                                                                                                                                                            
import { format } from "date-fns";
import { Button, MainTable } from "@canonical/react-components";
 
import type { Dispatch, SetStateAction } from "react";
import type { Invite } from "../types";
import { generateInviteToken } from "./generateInviteToken";
 
type MainTableProps = Parameters<typeof MainTable>[0];
type MainTableRow = NonNullable<MainTableProps["rows"]>[number];
type MainTableCell = NonNullable<MainTableRow["columns"]>[number];
 
function buildInviteTableRows(
  invites: Array<Invite>,
  status: "Pending" | "Expired" | "Revoked",
  packageName: string,
  setShowRevokeModal: (showRevokeModal: boolean) => void,
  setShowReopenModal: (showReopenModal: boolean) => void,
  setActiveInviteEmail: (email: string) => void,
  loadingInviteUrl: string | null,
  setLoadingInviteUrl: Dispatch<SetStateAction<string | null>>,
  copiedInviteUrl: string | null,
  setCopiedInviteUrl: Dispatch<SetStateAction<string | null>>
) {
  return invites.map((invite: Invite, index) => {
    let columns: MainTableCell[] = [];
    let statusColumn: MainTableCell | undefined;
 
    if (invites.length > 0 && index === 0) {
      statusColumn = {
        content: (
          <>
            {status === "Pending" && <i className="p-icon--status-waiting" />}
            {status === "Expired" && <i className="p-icon--warning" />}
            {status === "Revoked" && <i className="p-icon--error" />}
            &nbsp;
            {status}
          </>
        ),
        rowSpan: invites.length,
      };
    }
 
    const remainingColumns: MainTableCell[] = [
      {
        content: invite?.email,
        className: "u-truncate",
      },
      {
        content: invite?.["created-by"]?.["display-name"],
      },
      {
        content:
          invite?.["expires-at"] &&
          format(new Date(invite?.["expires-at"]), "dd/MM/yyyy"),
      },
      {
        content:
          status === "Pending" ? (
            <Button
              type="button"
              dense
              onClick={() => {
                setActiveInviteEmail(invite?.email || "");
                setShowRevokeModal(true);
              }}
            >
              Revoke
            </Button>
          ) : null,
        className: "u-align--right",
      },
      {
        content:
          status === "Pending" ? (
            <Button
              type="button"
              dense
              disabled={
                loadingInviteUrl === invite.email ||
                copiedInviteUrl === invite.email
              }
              onClick={async () => {
                try {
                  setLoadingInviteUrl(invite.email || "");
                  setCopiedInviteUrl(null);
 
                  const { inviteLink } = await generateInviteToken(
                    invite.email!,
                    packageName,
                    window.CSRF_TOKEN
                  );
 
                  await navigator.clipboard.writeText(inviteLink);
                  setLoadingInviteUrl(null);
                  setCopiedInviteUrl(invite.email || "");
                  setTimeout(() => setCopiedInviteUrl(null), 2000);
                } catch (err) {
                  console.error("Failed to generate or copy invite link:", err);
                }
              }}
            >
              {loadingInviteUrl === invite.email ? (
                <i
                  className="p-icon--spinner u-animation--spin"
                  aria-label="Loading"
                />
              ) : copiedInviteUrl === invite.email ? (
                "URL copied!"
              ) : (
                "Copy invite URL"
              )}
            </Button>
          ) : (
            <Button
              type="button"
              dense
              onClick={() => {
                setActiveInviteEmail(invite?.email || "");
                setShowReopenModal(true);
              }}
            >
              Reopen
            </Button>
          ),
        className: "u-align--right",
      },
    ];
 
    if (statusColumn) {
      columns = columns.concat([statusColumn], remainingColumns);
    } else {
      columns = remainingColumns;
    }
 
    return {
      columns,
    };
  });
}
 
export default buildInviteTableRows;