diff --git a/src/components/download/Download.tsx b/src/components/download/Download.tsx new file mode 100644 index 0000000..d4e7b06 --- /dev/null +++ b/src/components/download/Download.tsx @@ -0,0 +1,58 @@ +import React, { useEffect, useRef, useState } from 'react' +import { LoadingOutlined } from '@ant-design/icons' + +export interface DownloadProps { + children?: React.ReactNode; + className?: string; + style?: React.CSSProperties; + server?: () => Promise + fileName?: string + + [key: string]: any +} + +const Download = ({ server, style, className, fileName, children, ...props }: DownloadProps) => { + + const [ isLoading, setLoading ] = useState(false) + const serverRef = useRef(server) + serverRef.current = server + + useEffect(() => { + serverRef.current = server + }, [ server ]) + + return ( +
{ + if (isLoading) return + serverRef.current && serverRef.current()?.then(res => { + + if (!res) { + return res + } + + const downloadUrl = window.URL.createObjectURL(new Blob([ res ])) + const a = document.createElement('a') + a.style.display = 'none' + a.href = downloadUrl + a.download = fileName || '' + const event = new MouseEvent('click') + a.dispatchEvent(event) + window.URL.revokeObjectURL(downloadUrl) + setTimeout(() => a.remove()) + return res + })?.finally(() => { + setLoading(false) + + }) + }} + > + {isLoading && } + {children} +
+ ) +} + +export default Download \ No newline at end of file diff --git a/src/components/download/index.ts b/src/components/download/index.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/pages/websites/ssl/index.tsx b/src/pages/websites/ssl/index.tsx index ccebf0b..b9504ab 100644 --- a/src/pages/websites/ssl/index.tsx +++ b/src/pages/websites/ssl/index.tsx @@ -30,6 +30,7 @@ import SSLDetail from './components/Detail.tsx' import { detailAtom } from './components/store.ts' import Upload from './components/Upload.tsx' import { FormInstance } from 'antd/lib' +import Download from '@/components/download/Download.tsx' const SSL = () => { @@ -261,14 +262,12 @@ const SSL = () => { , - { - - }} + { + }} > - {t('actions.download', '下载')} - , + {t('actions.download', '下载')} + , { }> , -