1- import { attachment , job } from '@/api' ;
1+ import { job } from '@/api' ;
2+ import { downloadFile } from '@/utils/export' ;
23import { LoadingOutlined } from '@ant-design/icons' ;
34import { findByValue } from '@oceanbase/util' ;
45import { useRequest } from 'ahooks' ;
56import { Alert , Button , Modal , Spin , message } from 'antd' ;
6- import { useState } from 'react' ;
7+ import { useEffect , useState } from 'react' ;
78
89interface DownloadModalProps {
910 visible : boolean ;
@@ -14,6 +15,7 @@ interface DownloadModalProps {
1415 attachmentValue : string ;
1516 jobValue ?: any ;
1617 errorLogs ?: any ;
18+ onJobDeleted ?: ( ) => void ; // 新增:job被删除时的回调
1719}
1820
1921export default function DownloadModal ( {
@@ -25,90 +27,32 @@ export default function DownloadModal({
2527 attachmentValue,
2628 jobValue,
2729 errorLogs,
30+ onJobDeleted,
2831} : DownloadModalProps ) {
29- const [ retryCount , setRetryCount ] = useState ( 0 ) ;
30- const [ downloadTimeout , setDownloadTimeout ] = useState < NodeJS . Timeout | null > (
31- null ,
32- ) ;
3332 const [ showErrorDetails , setShowErrorDetails ] = useState ( false ) ;
3433
35- // 集群日志下载
36- const { run : downloadAttachment , loading : downloadLoading } = useRequest (
37- attachment . downloadAttachment ,
38- {
39- manual : true ,
40- onSuccess : ( data ) => {
41- // 处理zip文件下载
42- if ( data ) {
43- try {
44- // 检查数据大小
45- if ( data . size === 0 ) {
46- message . error ( '下载的文件为空,请重试' ) ;
47- return ;
48- }
49-
50- // 创建Blob对象 - 使用tar.gz的MIME类型
51- const blob = new Blob ( [ data ] , { type : 'application/gzip' } ) ;
52-
53- // 检查Blob大小
54- if ( blob . size === 0 ) {
55- message . error ( '文件内容为空,请重试' ) ;
56- return ;
57- }
58-
59- // 创建下载链接
60- const url = window . URL . createObjectURL ( blob ) ;
61- const link = document . createElement ( 'a' ) ;
62- link . href = url ;
63-
64- // 设置文件名
65- const fileName = `${ attachmentValue } ` ;
66- link . download = fileName ;
67-
68- // 触发下载
69- document . body . appendChild ( link ) ;
70- link . click ( ) ;
71-
72- // 清理
73- document . body . removeChild ( link ) ;
74- window . URL . revokeObjectURL ( url ) ;
75-
76- // 显示成功消息
77- message . success ( '文件下载成功' ) ;
78- // 清除超时和重试计数
79- if ( downloadTimeout ) {
80- clearTimeout ( downloadTimeout ) ;
81- setDownloadTimeout ( null ) ;
82- }
83- setRetryCount ( 0 ) ;
84- } catch ( error ) {
85- message . error ( '下载文件失败,请重试' ) ;
86- // 增加重试计数
87- setRetryCount ( ( prev ) => prev + 1 ) ;
88- }
89- } else {
90- message . error ( '下载数据为空' ) ;
91- // 增加重试计数
92- setRetryCount ( ( prev ) => prev + 1 ) ;
93- }
94- onOk ( ) ;
95- } ,
96- onError : ( ) => {
97- message . error ( '下载失败,请重试' ) ;
98- // 增加重试计数
99- setRetryCount ( ( prev ) => prev + 1 ) ;
100- // 清除超时
101- if ( downloadTimeout ) {
102- clearTimeout ( downloadTimeout ) ;
103- setDownloadTimeout ( null ) ;
104- }
105- } ,
106- } ,
107- ) ;
34+ // 当弹窗关闭时清空内部状态
35+ useEffect ( ( ) => {
36+ if ( ! visible ) {
37+ setShowErrorDetails ( false ) ;
38+ }
39+ } , [ visible ] ) ;
40+
41+ // 直接触发浏览器下载
42+ const handleDownload = ( ) => {
43+ if ( attachmentValue ) {
44+ downloadFile ( attachmentValue ) ;
45+ message . success ( '开始下载文件' ) ;
46+ onOk ( ) ;
47+ } else {
48+ message . error ( '文件ID不存在' ) ;
49+ }
50+ } ;
10851
10952 const { run : deleteJob } = useRequest ( job . deleteJob , {
11053 manual : true ,
11154 onSuccess : ( ) => {
55+ onJobDeleted ?.( ) ;
11256 onCancel ( ) ;
11357 } ,
11458 } ) ;
@@ -148,27 +92,8 @@ export default function DownloadModal({
14892 type = "success"
14993 showIcon
15094 action = {
151- < Button
152- size = "small"
153- type = "link"
154- loading = { downloadLoading }
155- onClick = { ( ) => {
156- // 清除之前的超时
157- if ( downloadTimeout ) {
158- clearTimeout ( downloadTimeout ) ;
159- }
160-
161- // 设置下载超时(5分钟)
162- const timeout = setTimeout ( ( ) => {
163- message . error ( '下载超时,请重试' ) ;
164- setRetryCount ( 0 ) ;
165- } , 5 * 60 * 1000 ) ;
166- setDownloadTimeout ( timeout ) ;
167-
168- downloadAttachment ( attachmentValue ) ;
169- } }
170- >
171- { retryCount > 0 ? `重试下载 (${ retryCount } )` : '下载链接' }
95+ < Button size = "small" type = "link" onClick = { handleDownload } >
96+ 下载链接
17297 </ Button >
17398 }
17499 />
@@ -241,14 +166,20 @@ export default function DownloadModal({
241166
242167 return (
243168 < Modal
244- title = { `${ title } ${ findByValue ( modalContent , diagnoseStatus ) . label } ` }
169+ title = { `${ title } ${
170+ findByValue ( modalContent , diagnoseStatus ) . label || '中...'
171+ } `}
245172 open = { visible }
246173 onOk = { onOk }
247- onCancel = { ( ) => deleteJob ( jobValue ?. namespace , jobValue ?. name ) }
174+ onCancel = { ( ) => {
175+ deleteJob ( jobValue ?. namespace , jobValue ?. name ) ;
176+ } }
248177 footer = {
249178 diagnoseStatus === 'running' ? (
250179 < Button
251- onClick = { ( ) => deleteJob ( jobValue ?. namespace , jobValue ?. name ) }
180+ onClick = { ( ) => {
181+ deleteJob ( jobValue ?. namespace , jobValue ?. name ) ;
182+ } }
252183 >
253184 取消
254185 </ Button >
0 commit comments