front-hub/src/pages/AccountSettings/DocumentsDialog/DocumentItem.tsx

73 lines
1.4 KiB
TypeScript
Raw Normal View History

2023-11-05 23:33:40 +00:00
import axios from "axios"
import { Box, SxProps, Theme, Typography, useTheme } from "@mui/material"
import { Document, Page } from "react-pdf"
import { Buffer } from "buffer"
import { downloadFileToDevice } from "@root/utils/downloadFileToDevice"
2023-06-02 08:22:14 +00:00
interface Props {
2023-07-25 22:31:04 +00:00
text: string;
documentUrl: string;
sx?: SxProps<Theme>;
2023-06-02 08:22:14 +00:00
}
2023-07-25 22:31:04 +00:00
export default function DocumentItem({ text, documentUrl = "", sx }: Props) {
2023-11-05 23:33:40 +00:00
const theme = useTheme()
2023-07-25 22:31:04 +00:00
2023-11-05 23:33:40 +00:00
const downloadFile = async () => {
const { data } = await axios.get<ArrayBuffer>(documentUrl, {
responseType: "arraybuffer",
})
2023-07-25 22:31:04 +00:00
2023-11-05 23:33:40 +00:00
if (!data) {
return
}
2023-06-02 08:22:14 +00:00
2023-11-05 23:33:40 +00:00
downloadFileToDevice(
`${documentUrl.split("/").pop()?.split(".")?.[0] || "document"}.pdf`,
Buffer.from(data)
)
2023-07-25 22:31:04 +00:00
2023-11-05 23:33:40 +00:00
return
}
2023-07-25 22:31:04 +00:00
2023-11-05 23:33:40 +00:00
return (
<Box
sx={{
display: "flex",
flexDirection: "column",
alignItems: "start",
gap: "10px",
...sx,
}}
>
<Typography
sx={{
color: "#4D4D4D",
fontWeight: 500,
fontVariantNumeric: "tabular-nums",
}}
>
{text}
</Typography>
{documentUrl && (
<>
<Typography
sx={{ color: theme.palette.purple.main, cursor: "pointer" }}
onClick={downloadFile}
>
{documentUrl.split("/").pop()?.split(".")?.[0]}
</Typography>
<Document file={documentUrl}>
<Page
pageNumber={1}
width={200}
renderTextLayer={false}
renderAnnotationLayer={false}
/>
</Document>
</>
)}
</Box>
)
2023-07-25 22:31:04 +00:00
}