import { Collapse, Modal, Progress, Select, Switch, Typography, message, } from "antd"; import { useEffect, useRef, useState } from "react"; import { get } from "../../request"; import type { ArticleType } from "../article/Article" import ExportSettingLayout from "./ExportSettingLayout"; const { Text } = Typography; interface IExportResponse { ok: boolean; message?: string; data: string; } interface IStatus { progress: number; message: string; log?: string[]; } interface IExportStatusResponse { ok: boolean; message?: string; data: { url?: string; status: IStatus; }; } interface IWidget { type?: ArticleType; articleId?: string; book?: string | null; para?: string | null; channelId?: string | null; anthologyId?: string | null; open?: boolean; onClose?: Function; } const ExportModalWidget = ({ type, ___book, ___para, channelId, articleId, anthologyId, open = false, onClose, }: IWidget) => { const [isModalOpen, setIsModalOpen] = useState(open); const [filename, setFilename] = useState(); const [url, setUrl] = useState(); const [format, setFormat] = useState("docx"); const [exportStatus, setExportStatus] = useState(); const [exportStart, setExportStart] = useState(false); const [hasOrigin, setHasOrigin] = useState(false); const [hasTranslation, setHasTranslation] = useState(true); const filenameRef = useRef(filename); useEffect(() => { // 及时更新 count 值 filenameRef.current = filename; }); const queryStatus = () => { if (typeof filenameRef.current === "undefined") { return; } const url = `/v2/export/${filenameRef.current}`; console.info("api request export", url); get(url) .then((json) => { console.info("api response export", json); if (json.ok) { setExportStatus(json.data.status); if (json.data.status.progress === 1) { setFilename(undefined); setUrl(json.data.url); } } else { console.error(json.message); } }) .catch((e) => console.error(e)); }; useEffect(() => { const interval = setInterval(() => queryStatus(), 3000); return () => clearInterval(interval); }, []); const getUrl = (): string => { if (!articleId) { throw new Error("id error"); } let url = `/v2/export?type=${type}&id=${articleId}&format=${format}`; url += channelId ? `&channel=${channelId}` : ""; url += "&origin=" + (hasOrigin ? "true" : "false"); url += "&translation=" + (hasTranslation ? "true" : "false"); switch (type) { case "chapter": const para = articleId?.split("-").map((item) => parseInt(item)); if (para?.length === 2) { url += `&book=${para[0]}&par=${para[1]}`; } else { throw new Error("段落编号错误"); } if (!channelId) { throw new Error("请选择版本"); } break; case "article": url += `&id=${articleId}`; url += anthologyId ? `&anthology=${anthologyId}` : ""; break; default: throw new Error("此类型暂时无法导出" + type); break; } return url; }; const exportRun = (): void => { const url = getUrl(); console.info("api request", url); setExportStart(true); get(url) .then((json) => { console.info("api response", json); if (json.ok) { const filename = json.data; console.log("filename", filename); setFilename(filename); } else { } }) .catch((_e) => {}); }; const closeModal = () => { if (typeof onClose !== "undefined") { onClose(); } }; useEffect(() => setIsModalOpen(open), [open]); return ( { console.log("type", type); try { exportRun(); } catch (error) { message.error((error as Error).message); console.error(error); } }} onCancel={closeModal} okText={"导出"} okButtonProps={{ disabled: exportStart }} > setFormat(value)} /> } /> setHasOrigin(checked)} /> } /> setHasTranslation(checked)} /> } /> setFormat(value)} /> } />
{exportStatus ? exportStatus.message : "正在生成……"} {url ? ( {"下载"} ) : ( <> )}
} key="1" >
{exportStatus?.log?.map((item, id) => { return
{item}
; })}
); }; export default ExportModalWidget;