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

73 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-07-25 22:31:04 +00:00
import axios from "axios";
2023-06-02 08:22:14 +00:00
import { Box, SxProps, Theme, Typography, useTheme } from "@mui/material";
2023-07-25 22:31:04 +00:00
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) {
const theme = useTheme();
const downloadFile = async () => {
const { data } = await axios.get<ArrayBuffer>(documentUrl, {
responseType: "arraybuffer",
});
if (!data) {
return;
}
2023-06-02 08:22:14 +00:00
2023-07-28 06:42:44 +00:00
downloadFileToDevice(
`${documentUrl.split("/").pop()?.split(".")?.[0] || "document"}.pdf`,
Buffer.from(data)
);
2023-07-25 22:31:04 +00:00
return;
};
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" }}
2023-07-25 22:31:04 +00:00
onClick={downloadFile}
>
{documentUrl.split("/").pop()?.split(".")?.[0]}
</Typography>
<Document file={documentUrl}>
<Page
pageNumber={1}
width={200}
renderTextLayer={false}
renderAnnotationLayer={false}
/>
</Document>
</>
)}
</Box>
);
}