import { Button, Center, Stack, Text, Title } 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"; 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(null); useEffect(() => { api .get(`/shares/${shareId}/files/${fileId}?download=false`) .then((res) => setText(res.data)); }, [shareId, fileId]); 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;