All files / libs/lti/components/src/lib/participant-dashboard/drawer LtiParticipantDrawer.tsx

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

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                                                                                                                                                                                                                                                                                 
import { css } from '@emotion/react';
import { IconArrowUpRight, IconFileDescription } from '@tabler/icons-react';
import { sum } from 'lodash-es';
import { memo, useMemo } from 'react';
import { FormattedMessage } from 'react-intl';
import { generatePath } from 'react-router-dom';

import { CountBadge, Divider, Drawer, Group, IconButtonLink, Stack, Typography } from '@allshares/studio-design-system';
import { routes } from '@amalia/core/routes';
import { Link } from '@amalia/ext/react-router-dom';
import { objectToQs } from '@amalia/ext/web';
import { canViewLtiPlans } from '@amalia/kernel/auth/shared';
import { useAbilityContext } from '@amalia/kernel/auth/state';
import { condenseLtiGrantsPerformanceValuesForPlanForUser } from '@amalia/lti/shared';
import { EnrollmentWidgetType, type LtiParticipantGrants } from '@amalia/lti/types';
import { type UserContract } from '@amalia/tenants/users/types';

import { GrantsEventsHistory } from '../../grants/events-history/GrantsEventsHistory';
import { OutcomeWaterfallChart } from '../../grants/OutcomeWaterfallChart/OutcomeWaterfallChart';

type LtiParticipantDrawerProps = {
  readonly grant: LtiParticipantGrants | null;
  readonly isOpen: boolean;
  readonly onClose: () => unknown;
  readonly user: Pick<UserContract, 'id'>;
};

export const LtiParticipantDrawer = memo(function LtiParticipantDrawer({
  grant,
  isOpen,
  onClose,
  user,
}: LtiParticipantDrawerProps) {
  const ability = useAbilityContext();

  // This is true if we can see all grants from all plans (aka I'm an admin).
  const redirectToAdminDocumentsView = canViewLtiPlans(ability);

  const documentsCount = useMemo(() => {
    if (!grant) {
      return null;
    }

    return grant.grants.reduce(
      (acc, curr) =>
        acc +
        sum(
          curr.enrollmentConfigurationVersion?.configuration.sections.flatMap(
            (section) => section.widgets.filter((w) => w.type === EnrollmentWidgetType.DOCUMENT).length,
          ),
        ),
      0,
    );
  }, [grant]);

  return (
    <Drawer
      isOpen={isOpen}
      css={css`
        width: 40%;
        max-width: 600px;
        background-color: var(--ds-color-gray-0);

        [data-color-scheme='dark'] & {
          background-color: var(--ds-color-dark-800);
        }
      `}
      onClose={onClose}
    >
      <Drawer.Header
        css={css`
          padding: 16px 24px 16px 32px;

          & :first-child {
            flex: 1;
          }
        `}
      >
        <Group
          align="center"
          justify="space-between"
        >
          <Typography variant="heading4Bold">{grant?.plan.name ?? ''}</Typography>

          {!!grant?.plan.id && (
            <Group gap={12}>
              <IconButtonLink
                icon={<IconArrowUpRight />}
                label={<FormattedMessage defaultMessage="Open plan" />}
                outline="border"
                variant="classic"
                to={
                  <Link
                    openInNewTab
                    to={`${generatePath(routes.LTI_PLAN_DETAILS, { ltiPlanId: grant.plan.id })}?${objectToQs({ tab: 'outcome' })}`}
                  />
                }
              />

              <IconButtonLink
                countBadge={<CountBadge size="small">{documentsCount}</CountBadge>}
                icon={<IconFileDescription />}
                label={<FormattedMessage defaultMessage="Open documents" />}
                outline="border"
                variant="classic"
                to={
                  <Link
                    openInNewTab
                    to={
                      redirectToAdminDocumentsView
                        ? `${generatePath(routes.LTI_DOCUMENTS, { ltiPlanId: grant.plan.id })}?${objectToQs({ userId: user.id, planId: grant.plan.id })}`
                        : `${generatePath(routes.LTI_PARTICIPANT_DOCUMENTS)}?${objectToQs({ planId: grant.plan.id })}`
                    }
                  />
                }
              />
            </Group>
          )}
        </Group>
      </Drawer.Header>

      {!!grant?.grants.length && (
        <Stack
          css={css`
            flex: 1;
            min-height: 0;
          `}
        >
          <OutcomeWaterfallChart grantsForUser={condenseLtiGrantsPerformanceValuesForPlanForUser(grant.grants)} />
          <Divider.Horizontal grayShade={100} />
          <GrantsEventsHistory grants={grant.grants} />
        </Stack>
      )}
    </Drawer>
  );
});