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 | import { css } from '@emotion/react'; import styled from '@emotion/styled'; import { IconChevronDown, IconChevronUp, IconDownload } from '@tabler/icons-react'; import { Fragment, memo, useCallback, useEffect, useMemo, useState } from 'react'; import { FormattedMessage, useIntl } from 'react-intl'; import { Button, Collapse, Drawer, Group, Skeleton, Stack, Typography, UnstyledButton, useSnackbars, type DrawerProps, } from '@allshares/studio-design-system'; import { dayjs } from '@amalia/ext/dayjs'; import { toError } from '@amalia/ext/typescript'; import { useDownloadGrantDocuments, useListGrantDocuments, useStreamGrantDocument } from '@amalia/lti/state'; import { type LtiGrant, type LtiGrantDocument, type LtiPlan } from '@amalia/lti/types'; import { DownloadsApiClient } from '@amalia/reporting/exports/state'; const PdfIframe = styled.iframe` border: none; flex: 1; width: 100%; min-height: 600px; `; type GrantDocumentsDrawerProps = Pick<DrawerProps, 'isOpen' | 'onClose'> & { readonly planId: LtiPlan['id']; readonly planName: LtiPlan['name']; readonly grantId: LtiGrant['id']; }; const SKELETON_ROWS = [0, 1] as const; export const GrantDocumentsDrawer = memo(function GrantDocumentsDrawer({ planId, planName, grantId, isOpen, onClose, }: GrantDocumentsDrawerProps) { const { formatMessage } = useIntl(); const { snackError } = useSnackbars(); const { data: documents = [], isPending: isLoadingDocuments } = useListGrantDocuments(planId, grantId); const { mutateAsync: downloadGrantDocuments, isPending: isDownloadingGrantDocuments } = useDownloadGrantDocuments( planId, grantId, ); const [expandedDocumentId, setExpandedDocumentId] = useState<LtiGrantDocument['id'] | null>(null); const expandedDocument = useMemo( () => documents.find((document) => document.id === expandedDocumentId) ?? null, [documents, expandedDocumentId], ); useEffect(() => { if (!isOpen || !documents.length) { setExpandedDocumentId(null); return; } setExpandedDocumentId((currentExpandedDocumentId) => { if (currentExpandedDocumentId && documents.some((document) => document.id === currentExpandedDocumentId)) { return currentExpandedDocumentId; } return documents[0].id; }); }, [documents, isOpen]); const { data: expandedDocumentBlob } = useStreamGrantDocument( planId, grantId, expandedDocument?.id, isOpen && !!expandedDocument, ); const documentUrl = useMemo(() => { if (!expandedDocumentBlob) { return null; } return URL.createObjectURL(new Blob([expandedDocumentBlob], { type: 'application/pdf' })); }, [expandedDocumentBlob]); useEffect( () => () => { if (documentUrl) { URL.revokeObjectURL(documentUrl); } }, [documentUrl], ); const handleToggleDocument = useCallback((documentId: LtiGrantDocument['id']) => { setExpandedDocumentId((currentExpandedDocumentId) => currentExpandedDocumentId === documentId ? null : documentId, ); }, []); const handleDownloadDocuments = useCallback(async () => { try { const grantDocumentsZipBlob = await downloadGrantDocuments(); const zipBlob = new Blob([grantDocumentsZipBlob], { type: 'application/zip' }); DownloadsApiClient.downloadFile(zipBlob, planName, 'zip'); } catch (error) { snackError(toError(error).message); } }, [downloadGrantDocuments, planName, snackError]); return ( <Drawer isOpen={isOpen} css={css` width: 1000px; background-color: var(--ds-color-gray-0); display: flex; flex-direction: column; [data-color-scheme='dark'] & { background-color: var(--ds-color-dark-800); } `} onClose={onClose} > <Drawer.Header css={css` padding: 16px 24px 16px 32px; `} drawerActions={ !!documents.length && ( <Button disabled={isDownloadingGrantDocuments} icon={<IconDownload />} size="small" variant="light-text" onClick={handleDownloadDocuments} > <FormattedMessage defaultMessage="Download" /> </Button> ) } > <Typography variant="heading3Bold"> <FormattedMessage defaultMessage="Attached documents" /> </Typography> </Drawer.Header> <Stack css={css` overflow: auto; min-height: 0; border-bottom: 1px solid var(--ds-border-color-100); `} > {isLoadingDocuments ? SKELETON_ROWS.map((row) => ( <Skeleton key={row} css={css` height: 120px; border-radius: 0; `} /> )) : documents.map((document) => { const isExpanded = expandedDocumentId === document.id; return ( <Fragment key={document.id}> <UnstyledButton aria-expanded={isExpanded} css={css` display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 24px 16px 32px; border-bottom: 1px solid var(--ds-border-color-100); `} onClick={() => handleToggleDocument(document.id)} > <Group align="center" gap={8} > {isExpanded ? <IconChevronUp size={20} /> : <IconChevronDown size={20} />} <Typography variant="bodyLargeMedium">{document.metadata.name}</Typography> </Group> <Typography variant="bodySmallRegular" css={css` font-style: italic; color: var(--ds-text-color-500); [data-color-scheme='dark'] & { color: var(--ds-text-color-700); } `} > {formatMessage( { defaultMessage: 'Uploaded {date}' }, { date: dayjs(document.createdAt).format('YYYY/MM/DD') }, )} </Typography> </UnstyledButton> <Collapse lazy isOpen={isExpanded} > <Stack gap={16} css={css` padding: 0 0 16px; border-bottom: 1px solid var(--ds-border-color-100); `} > {documentUrl ? ( <PdfIframe src={documentUrl} title={document.metadata.name} /> ) : ( <Skeleton css={css` min-height: 600px; `} /> )} </Stack> </Collapse> </Fragment> ); })} </Stack> </Drawer> ); }); |