All files / react-app/src/features/package/package-activity index.tsx

94.28% Statements 33/35
83.33% Branches 30/36
100% Functions 14/14
94.11% Lines 32/34

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 273 274                                                5x                                       2x 2x 1x     2x                                     5x 9x               9x 9x                                                                             1x 1x 1x     1x                                                     5x 63x 63x                                                       5x 12x     12x   12x 3x     9x 63x       63x     9x 1x       1x 1x 1x     1x                                                           5x 73x                                                                      
import { useMemo } from "react";
import { opensearch } from "shared-types";
import { ItemResult } from "shared-types/opensearch/changelog";
import { formatDateToET, getPackageActivityLabel } from "shared-utils";
 
import {
  Accordion,
  AccordionContent,
  AccordionItem,
  AccordionTrigger,
  Button,
  DetailsSection,
  Table,
  TableBody,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "@/components";
import { BLANK_VALUE } from "@/consts";
import { sendGAEvent } from "@/utils";
 
import { Attachments, useAttachmentService } from "./hook";
 
const attachmentStatusMessageClassName = "text-sm font-normal text-red-700";
 
type AttachmentDetailsProps = {
  id: string;
  packageId: string;
  attachments: opensearch.changelog.Document["attachments"];
  onClick: (attachment: Attachments[number]) => Promise<string | undefined>;
};
 
const AttachmentDetails = ({ id, packageId, attachments, onClick }: AttachmentDetailsProps) => (
  <TableBody>
    {attachments.map((attachment) => {
      return (
        <TableRow key={`${id}-${attachment.key}`}>
          <TableCell>{attachment.title}</TableCell>
          <TableCell>
            <Button
              className="ml-[-15px] align-left text-left min-h-fit"
              variant="link"
              onClick={() => {
                onClick(attachment).then((url) => {
                  if (url) {
                    window.open(url);
                  }
                });
                sendGAEvent("attachment_download", {
                  document_type: attachment.title,
                  package_id: packageId,
                });
              }}
            >
              {attachment.filename}
            </Button>
          </TableCell>
        </TableRow>
      );
    })}
  </TableBody>
);
 
type SubmissionProps = {
  packageActivity: opensearch.changelog.Document;
};
 
const Submission = ({ packageActivity }: SubmissionProps) => {
  const { attachments = [], id, packageId, additionalInformation } = packageActivity;
  const {
    archiveErrorMessage,
    archiveWarningMessage,
    attachmentErrorMessage,
    onArchive,
    onUrl,
    loading,
  } = useAttachmentService({ packageId });
  const archiveMessage = archiveErrorMessage || archiveWarningMessage;
 
  return (
    <div className="flex flex-col gap-6">
      <div>
        <h2 className="font-bold text-lg mb-2">Attachments</h2>
 
        {attachments && attachments?.length > 0 ? (
          <Table>
            <TableHeader>
              <TableRow>
                <TableHead className="w-[300px]">Document Type</TableHead>
                <TableHead>Attached File</TableHead>
              </TableRow>
            </TableHeader>
 
            <AttachmentDetails
              attachments={attachments}
              id={id}
              packageId={packageId}
              onClick={onUrl}
            />
          </Table>
        ) : (
          <p>No information submitted</p>
        )}
        {attachmentErrorMessage && (
          <p role="alert" className={`mt-2 ${attachmentStatusMessageClassName}`}>
            {attachmentErrorMessage}
          </p>
        )}
      </div>
      {attachments && attachments.length > 1 && (
        <>
          <Button
            variant="outline"
            className="w-max"
            loading={loading}
            onClick={() => {
              onArchive({ scope: "section", sectionId: id }).then((url) => {
                Eif (url) {
                  window.open(url);
                }
              });
              sendGAEvent("section_attachments_download", {
                number_attachments: attachments.length,
                package_id: packageId,
              });
            }}
          >
            Download section attachments
          </Button>
          {archiveMessage && (
            <p role="alert" className={attachmentStatusMessageClassName}>
              {archiveMessage}
            </p>
          )}
        </>
      )}
      <div>
        <h2 className="font-bold text-lg mb-2">Additional Information</h2>
        <p className="whitespace-pre-line">{additionalInformation || "No information submitted"}</p>
      </div>
    </div>
  );
};
 
type PackageActivityProps = {
  packageActivity: opensearch.changelog.Document;
};
 
const PackageActivity = ({ packageActivity }: PackageActivityProps) => {
  const label = useMemo(() => {
    return getPackageActivityLabel(packageActivity.event) || BLANK_VALUE;
  }, [packageActivity.event]);
 
  return (
    <AccordionItem value={packageActivity.id}>
      <AccordionTrigger className="bg-gray-100 px-3" showPlusMinus>
        <p className="flex flex-row gap-2 text-gray-600">
          <strong className="text-left">
            {label} {packageActivity.submitterName ? `By ${packageActivity.submitterName}` : ""}
          </strong>
          {" - "}
          <span className="text-right">
            {packageActivity.timestamp ? formatDateToET(packageActivity.timestamp) : "Unknown"}
          </span>
        </p>
      </AccordionTrigger>
      <AccordionContent className="p-4">
        <Submission packageActivity={packageActivity} />
      </AccordionContent>
    </AccordionItem>
  );
};
 
type DownloadAllButtonProps = {
  packageId: string;
  submissionChangelog: ItemResult[];
};
 
const DownloadAllButton = ({ packageId, submissionChangelog }: DownloadAllButtonProps) => {
  const { archiveErrorMessage, archiveWarningMessage, loading, onArchive } = useAttachmentService({
    packageId,
  });
  const archiveMessage = archiveErrorMessage || archiveWarningMessage;
 
  if (submissionChangelog?.length === 0) {
    return null;
  }
 
  const attachmentsAggregate = submissionChangelog.reduce<Attachments>((acc, changelogItem) => {
    Iif (!changelogItem._source.attachments) {
      return acc;
    }
 
    return acc.concat(changelogItem._source.attachments);
  }, []);
 
  const onDownloadAll = () => {
    Iif (attachmentsAggregate.length === 0) {
      return;
    }
 
    onArchive({ scope: "all" }).then((url) => {
      Eif (url) {
        window.open(url);
      }
    });
    sendGAEvent("all_attachments_download", {
      number_attachments: attachmentsAggregate.length,
      package_id: packageId,
    });
  };
 
  return (
    <>
      <Button
        loading={loading}
        onClick={onDownloadAll}
        variant="outline"
        className="max-w-fit min-h-fit justify-self-end"
      >
        Download all attachments
      </Button>
      {archiveMessage && (
        <p role="alert" className={`justify-self-end ${attachmentStatusMessageClassName}`}>
          {archiveMessage}
        </p>
      )}
    </>
  );
};
 
type PackageActivitiesProps = {
  id: string;
  changelog: ItemResult[];
};
 
export const PackageActivities = ({ id, changelog }: PackageActivitiesProps) => {
  const changelogWithoutAdminChanges = changelog.filter((item) => !item._source.isAdminChange);
 
  return (
    <DetailsSection
      id="package_activity"
      title={
        <div className="flex justify-between">
          Package Activity ({changelogWithoutAdminChanges.length || 0})
          <DownloadAllButton submissionChangelog={changelogWithoutAdminChanges} packageId={id} />
        </div>
      }
    >
      {changelogWithoutAdminChanges.length > 0 ? (
        <Accordion
          // `changelogWithoutAdminChanges[0]._source.id` to re-render the `defaultValue` whenever `keyAndDefaultValue` changes
          key={changelogWithoutAdminChanges[0]._source.id}
          type="multiple"
          className="flex flex-col gap-2"
          defaultValue={[changelogWithoutAdminChanges[0]._source.id]}
          asChild
        >
          <ol>
            {changelogWithoutAdminChanges.map(({ _source: packageActivity }) => (
              <li key={packageActivity.id}>
                <PackageActivity packageActivity={packageActivity} />
              </li>
            ))}
          </ol>
        </Accordion>
      ) : (
        <p className="text-gray-500">No package activity recorded</p>
      )}
    </DetailsSection>
  );
};