import React, { useEffect, useRef, useState } from 'react';
import { Button, Drawer, Space, Spin } from 'antd';
import { DownloadOutlined, CloseOutlined } from '@ant-design/icons';
import loadable from '@loadable/component';
import { renderAsync } from 'docx-preview';
import { CommonFilePath } from "../../tmpl/interface";
import HCService from "../../tmpl/hcservice-v3";
import "./index.less";
const Loading = ;
// 懒加载 FileViewer
const FileViewer = loadable(() => import('react-file-viewer'), { fallback: Loading });
interface DrawerFileViewProps {
filePath: CommonFilePath;
serverKey?: string;
downloadable?: boolean;
contentType?: string;
}
const DrawerFileView: React.FC = (props) => {
const { filePath, serverKey, downloadable = true, contentType } = props;
const [drawerOpen, setDrawerOpen] = useState(false);
const [drawerSize, setDrawerSize] = useState("1200px");
const containerRef = useRef(null);
const loadDocx = async () => {
try {
const response = await fetch(filePath.path);
const blob = await response.blob();
await renderAsync(blob, containerRef.current);
} catch (error) {
console.error('Failed to load document:', error);
}
};
useEffect(() => {
if (contentType === "docx" && filePath?.path && drawerOpen) {
loadDocx();
}
}, [filePath, drawerOpen]);
const onClose = () => {
setDrawerOpen(false);
};
const handleDownload = () => {
HCService.downloadDir(filePath.path, filePath.fileName);
};
const handleOpenDrawer = () => {
setDrawerOpen(true);
};
const onError = (e) => {
console.log(e, 'error in file-viewer');
}
return (
{downloadable ? (
) : null}
{drawerOpen && (
setDrawerSize(newSize)) }}
closable={false}
onClose={onClose}
open={drawerOpen}
styles={{ body: { minWidth: "1000px" } }}
extra={
{downloadable ? (
) : null}
}
className='drawer-file-view'
>
{contentType === "docx" ?
:
}
)}
);
};
export default DrawerFileView;