import { Button, Center, Stack, Text, Title, useMantineTheme, } from "@mantine/core"; import { modals } from "@mantine/modals"; import Link from "next/link"; import React, { Dispatch, SetStateAction, useEffect, useState } from "react"; import { FormattedMessage } from "react-intl"; import api from "../../services/api.service"; import Markdown from "markdown-to-jsx"; const webroot = process.env.WEBROOT || ""; const FilePreviewContext = React.createContext<{ shareId: string; fileId: string; mimeType: string; setIsNotSupported: Dispatch>; }>({ shareId: "", fileId: "", mimeType: "", setIsNotSupported: () => {}, }); const FilePreview = ({ shareId, fileId, mimeType, }: { shareId: string; fileId: string; mimeType: string; }) => { const [isNotSupported, setIsNotSupported] = useState(false); if (isNotSupported) return ; return ( ); }; const FileDecider = () => { const { mimeType, setIsNotSupported } = React.useContext(FilePreviewContext); if (mimeType == "application/pdf") { return ; } else if (mimeType.startsWith("video/")) { return ; } else if (mimeType.startsWith("image/")) { return ; } else if (mimeType.startsWith("audio/")) { return ; } else if (mimeType.startsWith("text/")) { return ; } else { setIsNotSupported(true); return null; } }; const AudioPreview = () => { const { shareId, fileId, setIsNotSupported } = React.useContext(FilePreviewContext); return (
); }; const VideoPreview = () => { const { shareId, fileId, setIsNotSupported } = React.useContext(FilePreviewContext); return ( ); }; const ImagePreview = () => { const { shareId, fileId, setIsNotSupported } = React.useContext(FilePreviewContext); return ( // eslint-disable-next-line @next/next/no-img-element {`${fileId}_preview`} setIsNotSupported(true)} /> ); }; const TextPreview = () => { const { shareId, fileId } = React.useContext(FilePreviewContext); const [text, setText] = useState(""); const { colorScheme } = useMantineTheme(); useEffect(() => { api .get(`/shares/${shareId}/files/${fileId}?download=false`) .then((res) => setText(res.data ?? "Preview couldn't be fetched.")); }, [shareId, fileId]); const options = { overrides: { pre: { props: { style: { backgroundColor: colorScheme == "dark" ? "rgba(50, 50, 50, 0.5)" : "rgba(220, 220, 220, 0.5)", padding: "0.75em", whiteSpace: "pre-wrap", }, }, }, table: { props: { className: "md", }, }, }, }; return {text}; }; const PdfPreview = () => { const { shareId, fileId } = React.useContext(FilePreviewContext); if (typeof window !== "undefined") { window.location.href = `/api/shares/${shareId}/files/${fileId}?download=false`; } return null; }; const UnSupportedFile = () => { return (
<FormattedMessage id="share.modal.file-preview.error.not-supported.title" />
); }; export default FilePreview;