All files / libs/lti/components/src/lib/documents/drawers DocumentDrawer.tsx

0% Statements 0/135
0% Branches 0/1
0% Functions 0/1
0% Lines 0/135

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                                                                                                                                                                                                                                                                               
import { css } from '@emotion/react';
import styled from '@emotion/styled';
import { memo, useCallback, useEffect, useMemo, type ReactNode } from 'react';
import { useIntl } from 'react-intl';
import { match, P } from 'ts-pattern';

import { Drawer, Group, Skeleton, Stack, Typography, useSnackbars } from '@allshares/studio-design-system';
import { type LtiGrantDocument, type LtiPlanDocument } from '@amalia/lti/types';

import { DocumentSignatureRequiredBadge } from '../badges/DocumentSignatureRequiredBadge';
import { DocumentSignedBadge } from '../badges/DocumentSignedBadge';

const PdfIframe = styled.iframe`
  border: none;
  flex: 1;
  width: 100%;
`;

type DocumentDrawerProps = Partial<Pick<LtiGrantDocument, 'signedAt'>> &
  Pick<LtiPlanDocument, 'isSignatureRequired' | 'name'> & {
    readonly documentBlob?: Blob;

    readonly isOpen: boolean;
    readonly onClose: () => void;

    readonly actions?: ReactNode;
  };

export const DocumentDrawer = memo(function DocumentDrawer({
  isSignatureRequired,
  name,
  signedAt,
  documentBlob,

  actions,

  isOpen,
  onClose,
}: DocumentDrawerProps) {
  const { formatMessage } = useIntl();
  const { snackError } = useSnackbars();

  const documentUrl = useMemo(
    () => (documentBlob ? URL.createObjectURL(new Blob([documentBlob], { type: 'application/pdf' })) : null),
    [documentBlob],
  );

  useEffect(
    () => () => {
      if (documentUrl) {
        URL.revokeObjectURL(documentUrl);
      }
    },
    [documentUrl],
  );

  const handleIframeError = useCallback(() => {
    snackError(formatMessage({ defaultMessage: 'Unable to load document.' }));
  }, [formatMessage, snackError]);

  return (
    <Drawer
      isOpen={isOpen}
      css={(theme) => css`
        width: 1000px;
        background-color: ${theme.ds.colors.gray[0]};
        display: flex;
        flex-direction: column;

        [data-color-scheme='dark'] & {
          background-color: ${theme.ds.colors.dark[800]};
        }
      `}
      onClose={onClose}
    >
      <Drawer.Header
        css={(theme) => css`
          padding: 24px 32px;
          flex-shrink: 0;

          [data-color-scheme='dark'] & {
            border-bottom-color: ${theme.ds.colors.dark[700]};
          }
        `}
      >
        <Group
          align="center"
          gap={16}
        >
          <Typography variant="heading3Bold">{name}</Typography>

          {match({ signedAt, isSignatureRequired })
            .with({ signedAt: P.nonNullable }, (props) => <DocumentSignedBadge signedAt={props.signedAt} />)
            .with({ isSignatureRequired: true }, () => <DocumentSignatureRequiredBadge />)
            .otherwise(() => null)}
        </Group>
      </Drawer.Header>

      <Stack
        gap={16}
        css={css`
          flex: 1;
          padding: 24px 32px;
          min-height: 0;
        `}
      >
        {documentUrl ? (
          <PdfIframe
            src={documentUrl}
            title={name}
            onError={handleIframeError}
          />
        ) : (
          <Skeleton
            css={css`
              flex: 1;
            `}
          />
        )}
      </Stack>

      <Stack
        align="flex-end"
        css={css`
          border-top: ${actions ? `1px solid var(--ds-border-color-100)` : 'none'};
          padding: 16px 32px;
          flex-shrink: 0;
          min-height: 50px;
        `}
      >
        {actions}
      </Stack>
    </Drawer>
  );
});