{"version":3,"file":"AttachmentTransaction.mjs","names":["React","useState","useEffect","useImperativeHandle","useMemo","useCallback","AmbientAlert","Divider","EmptyStateDisplay","FileViewer","LinearProgress","AttachmentTransactionAttachmentList","apiMutate","ViewIcon","makeStyles","useAxiosGet","useTranslation","ConfigService","AttachmentTransaction","props","transaction","attachmentRef","selectedAttachment","setSelectedAttachment","attachments","udpTransactionUdpTransactionAttachment","t","data","attachmentFileInfo","refetchattachmentFileInfo","productV1ApiUrl","transactionAttachmentId","attachmentFile","loading","loadingFile","error","downloadError","refetchAttachmentFile","responseType","blobUrl","pdfBlob","Blob","type","URL","createObjectURL","getErrorMessage","status","response","defaultValue","message","fileDataObj","filePath","fileExtension","extension","fixedHeight","useStyles","theme","sidesheet","display","flexDirection","flex","height","attachmentListContainer","textAlign","width","attachmentListHeader","backgroundColor","justifyItems","paddingLeft","attachmentListHeaderPrimaryText","fontSize","fontWeight","color","attachmentListHeaderSecondaryText","paddingBottom","attachemntFileInfoCol","overflowY","attachmentFileNameHeader","attachmentFileName","marginTop","marginBottom","justifySelf","fileViewer","marginLeft","marginRight","divider","classStyles","downloadSelectedFile","url","link","document","createElement","href","download","fileName","name","body","appendChild","click","removeChild","e","console","handleOpenInNewTab","preventDefault","window","open","refetchAttachment","length","count","attachmentName"],"sources":["../../../../../src/udp/pages/UdpTransactionsPage/sidesheets/AttachmentTransaction.jsx"],"sourcesContent":["import React, {\n  useState,\n  useEffect,\n  useImperativeHandle,\n  useMemo,\n  useCallback\n} from 'react';\nimport {\n  AmbientAlert,\n  Divider,\n  EmptyStateDisplay,\n  FileViewer,\n  LinearProgress\n} from '../../../../UI';\nimport AttachmentTransactionAttachmentList from '../components/AttachmentTransactionAttachmentList';\nimport { apiMutate } from '../../../../utilities';\nimport { ViewIcon } from '@fluentui/react-icons';\nimport { makeStyles } from '@material-ui/core';\nimport { useAxiosGet } from '../../../../utilities';\nimport { useTranslation } from 'react-i18next';\nimport { ConfigService } from '../../../../configService';\n\nconst AttachmentTransaction = (props) => {\n  const { transaction, attachmentRef } = props;\n  const [selectedAttachment, setSelectedAttachment] = useState('');\n\n  const attachments = transaction?.udpTransactionUdpTransactionAttachment || [];\n\n  const { t } = useTranslation();\n\n  const [{ data: attachmentFileInfo }, refetchattachmentFileInfo] = useAxiosGet(\n    `${ConfigService.productV1ApiUrl}`,\n    `UdpTransactionAttachment/${selectedAttachment?.transactionAttachmentId}/udpDriveFile/download`,\n    {},\n    true\n  );\n\n  const [\n    { data: attachmentFile, loading: loadingFile, error: downloadError },\n    refetchAttachmentFile\n  ] = useAxiosGet(\n    `${ConfigService.productV1ApiUrl}`,\n    `UdpTransactionAttachment/${selectedAttachment?.transactionAttachmentId}/download`,\n    {\n      responseType: 'blob'\n    },\n    true\n  );\n\n  const blobUrl = useMemo(() => {\n    if (!attachmentFile) return null;\n    const pdfBlob = new Blob([attachmentFile], { type: 'application/pdf' });\n    return URL.createObjectURL(pdfBlob);\n  }, [attachmentFile]);\n\n  useEffect(() => {\n    if (selectedAttachment) {\n      refetchattachmentFileInfo();\n      refetchAttachmentFile();\n    }\n  }, [selectedAttachment, refetchattachmentFileInfo, refetchAttachmentFile]);\n\n  const getErrorMessage = () => {\n    if (!downloadError) return null;\n\n    const status = downloadError.response?.status;\n    if (status === 404)\n      return t('transactions.attachments.notFoundError', {\n        defaultValue: 'The requested attachment could not be found.'\n      });\n    if (status >= 500)\n      return t('transactions.attachments.serverError', {\n        defaultValue: 'A server error occured. Please try again later.'\n      });\n\n    return (\n      downloadError.message ||\n      t('transactions.attachments.unexpectedError', {\n        defaultValue: 'An unexpected error occured.'\n      })\n    );\n  };\n\n  let fileDataObj = {\n    filePath: blobUrl,\n    fileExtension: attachmentFileInfo?.extension\n  };\n\n  const fixedHeight = 'calc(100vh - 140px)';\n  const useStyles = makeStyles((theme) => ({\n    sidesheet: {\n      display: 'flex',\n      flexDirection: 'row',\n      flex: 1,\n      height: fixedHeight\n    },\n    attachmentListContainer: {\n      textAlign: 'left',\n      width: '35%',\n      height: fixedHeight\n    },\n    attachmentListHeader: {\n      backgroundColor: 'white',\n      justifyItems: 'left',\n      paddingLeft: '5%'\n    },\n    attachmentListHeaderPrimaryText: {\n      fontSize: 'var(--h6-font-size)',\n      fontWeight: 'var(--body1-font-weight)',\n      color: 'var(--text-main)'\n    },\n    attachmentListHeaderSecondaryText: {\n      fontSize: 'var(--body2-font-size)',\n      color: 'var(--text-secondary)',\n      paddingBottom: '2%'\n    },\n    attachemntFileInfoCol: {\n      width: '50%',\n      display: 'flex',\n      flexDirection: 'column',\n      flex: 1,\n      overflowY: 'auto',\n      height: 'calc(100vh - 50px)'\n    },\n    attachmentFileNameHeader: {\n      backgroundColor: 'white'\n    },\n    attachmentFileName: {\n      color: 'var(--text-main)',\n      fontSize: 'var(--h6-font-size)',\n      fontWeight: 'var(--body1-font-weight)',\n      marginTop: '1%',\n      marginBottom: '1%',\n      justifySelf: 'center'\n    },\n    fileViewer: {\n      marginLeft: 'var(--spacing-02)',\n      marginRight: 'var(--spacing-02)'\n    },\n    divider: {\n      '& MuiDivider-root': {\n        backgroundColor: 'var(--gray-02)'\n      }\n    }\n  }));\n\n  const classStyles = useStyles();\n\n  useImperativeHandle(attachmentRef, () => ({\n    downloadSelectedFile: async () => {\n      if (!selectedAttachment) return;\n\n      try {\n        const response = await apiMutate(\n          ConfigService.productV1ApiUrl,\n          `UdpTransactionAttachment/${selectedAttachment.transactionAttachmentId}/udpDriveFile/download`,\n          {}\n        );\n        const url = response.data.link;\n        const link = document.createElement('a');\n        link.href = url;\n        link.download = selectedAttachment.fileName ?? selectedAttachment.name;\n        document.body.appendChild(link);\n        link.click();\n        document.body.removeChild(link);\n      } catch (e) {\n        console.error('Download failed', e);\n      }\n    }\n  }));\n\n  const handleOpenInNewTab = useCallback(\n    (e) => {\n      e?.preventDefault();\n\n      if (blobUrl) {\n        window.open(blobUrl, '_blank');\n      }\n    },\n    [blobUrl]\n  );\n\n  const refetchAttachment = () => {\n    refetchattachmentFileInfo();\n    refetchAttachmentFile();\n  };\n\n  return (\n    <>\n      <div className={classStyles.sidesheet}>\n        <div className={classStyles.attachmentListContainer}>\n          <div className={classStyles.attachmentListHeader}>\n            <div className={classStyles.attachmentListHeaderPrimaryText}>\n              {t('transactions.attachments.attachmentsTitle', {\n                defaultValue: 'Attachments'\n              })}\n            </div>\n            <div className={classStyles.attachmentListHeaderSecondaryText}>\n              {attachments?.length === 1\n                ? t('transactions.attachments.filesAttachedOne', {\n                    defaultValue: '{{count}} file attached',\n                    count: attachments?.length\n                  })\n                : t('transactions.attachments.filesAttachedOther', {\n                    defaultValue: '{{count}} files attached',\n                    count: attachments?.length ?? 0\n                  })}\n            </div>\n          </div>\n          <div className={classStyles.divider}>\n            <Divider />\n          </div>\n          <AttachmentTransactionAttachmentList\n            transaction={transaction}\n            attachments={attachments}\n            setSelectedAttachment={setSelectedAttachment}\n          />\n        </div>\n\n        <div className={classStyles.attachemntFileInfoCol}>\n          <div className={classStyles.attachmentFileNameHeader}>\n            {selectedAttachment && (\n              <p className={classStyles.attachmentFileName}>\n                {selectedAttachment.attachmentName}\n              </p>\n            )}\n            <div className={classStyles.divider}>\n              <Divider />\n            </div>\n          </div>\n\n          {downloadError && (\n            <AmbientAlert\n              showAlert={!!downloadError}\n              alertSeverity={'error'}\n              alertTitle={t('transactions.attachments.previewLoadFailed', {\n                defaultValue: 'Failed to load preview'\n              })}\n              alertMessage={getErrorMessage()}\n              alertAction={true}\n              alertActionButton={t('common.retry', { defaultValue: 'Retry' })}\n              alertHandleAction={() => refetchAttachment()}\n            />\n          )}\n\n          {loadingFile && !downloadError ? (\n            <LinearProgress />\n          ) : selectedAttachment && attachmentFileInfo && blobUrl ? (\n            <div className={classStyles.fileViewer}>\n              <FileViewer\n                data={\n                  attachmentFileInfo?.extension == 'pdf'\n                    ? fileDataObj\n                    : [fileDataObj]\n                }\n                onClickDownload={handleOpenInNewTab}\n              />\n            </div>\n          ) : (\n            !selectedAttachment && (\n              <EmptyStateDisplay\n                icon={ViewIcon}\n                primaryText={t('transactions.attachments.selectToPreview', {\n                  defaultValue: 'Select an attachment to preview'\n                })}\n                secondaryText={t(\n                  'transactions.attachments.selectToPreviewHint',\n                  {\n                    defaultValue:\n                      'Choose a file from the attachments list to view its contents'\n                  }\n                )}\n              />\n            )\n          )}\n        </div>\n      </div>\n    </>\n  );\n};\nexport default AttachmentTransaction;\n"],"mappings":";;;;;;;;;;;;;;AAsBA,MAAMkB,yBAAyBC,UAAU;CACvC,MAAM,EAAEC,aAAaC,kBAAkBF;CACvC,MAAM,CAACG,oBAAoBC,yBAAyBtB,SAAS,GAAG;CAEhE,MAAMuB,cAAcJ,aAAaK,0CAA0C,EAAE;CAE7E,MAAM,EAAEC,MAAMV,gBAAgB;CAE9B,MAAM,CAAC,EAAEW,MAAMC,sBAAsBC,6BAA6Bd,YAChE,GAAGE,cAAca,mBACjB,4BAA4BR,oBAAoBS,wBAAuB,yBACvE,EAAE,EACF,KACD;CAED,MAAM,CACJ,EAAEJ,MAAMK,gBAAgBC,SAASC,aAAaC,OAAOC,iBACrDC,yBACEtB,YACF,GAAGE,cAAca,mBACjB,4BAA4BR,oBAAoBS,wBAAuB,YACvE,EACEO,cAAc,QACf,EACD,KACD;CAED,MAAMC,UAAUnC,cAAc;AAC5B,MAAI,CAAC4B,eAAgB,QAAO;EAC5B,MAAMQ,UAAU,IAAIC,KAAK,CAACT,eAAe,EAAE,EAAEU,MAAM,mBAAmB,CAAC;AACvE,SAAOC,IAAIC,gBAAgBJ,QAAQ;IAClC,CAACR,eAAe,CAAC;AAEpB9B,iBAAgB;AACd,MAAIoB,oBAAoB;AACtBO,8BAA2B;AAC3BQ,0BAAuB;;IAExB;EAACf;EAAoBO;EAA2BQ;EAAsB,CAAC;CAE1E,MAAMQ,wBAAwB;AAC5B,MAAI,CAACT,cAAe,QAAO;EAE3B,MAAMU,SAASV,cAAcW,UAAUD;AACvC,MAAIA,WAAW,IACb,QAAOpB,EAAE,0CAA0C,EACjDsB,cAAc,gDACf,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