-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReportExport.tsx
More file actions
158 lines (143 loc) · 4.93 KB
/
ReportExport.tsx
File metadata and controls
158 lines (143 loc) · 4.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// src/components/ReportExport.tsx
// 报告导出对话框组件
import { useState } from 'react';
import { AnalysisResult } from '../types';
import { exportReport } from '../utils/reportGenerator';
interface ReportExportProps {
result: AnalysisResult;
onClose: () => void;
}
export default function ReportExport({ result, onClose }: ReportExportProps) {
// 默认文件名:包名 + 时间戳
const defaultFilename = `${result.basic.packageName.replace(/\./g, '_')}_${new Date().toISOString().slice(0, 10).replace(/-/g, '')}`;
const [filename, setFilename] = useState(defaultFilename);
const [format, setFormat] = useState<'html' | 'json'>('html');
const [includeTimestamp, setIncludeTimestamp] = useState(true);
const [prettyPrint, setPrettyPrint] = useState(true);
const [error, setError] = useState<string>('');
// 验证并清理文件名
const sanitizeFilename = (name: string): string => {
// 移除非法字符: / \ : * ? " < > |
return name.replace(/[\/\\:*?"<>|]/g, '_').trim();
};
// 处理文件名输入
const handleFilenameChange = (value: string) => {
setFilename(value);
setError('');
};
// 处理导出
const handleExport = () => {
const sanitizedFilename = sanitizeFilename(filename || defaultFilename);
if (!sanitizedFilename) {
setError('文件名不能为空');
return;
}
try {
exportReport(result, {
format,
filename: sanitizedFilename,
includeTimestamp,
prettyPrint: format === 'json' ? prettyPrint : true,
});
onClose();
} catch (error) {
console.error('导出失败:', error);
setError('导出失败,请重试');
}
};
// 点击遮罩层关闭
const handleOverlayClick = (e: React.MouseEvent<HTMLDivElement>) => {
if (e.target === e.currentTarget) {
onClose();
}
};
return (
<div className="modal-overlay" onClick={handleOverlayClick}>
<div className="modal-content" onClick={(e) => e.stopPropagation()}>
{/* 标题栏 */}
<div className="modal-header">
<h2>📊 导出报告</h2>
<button className="modal-close" onClick={onClose}>
✕
</button>
</div>
{/* 表单内容 */}
<div className="modal-body">
{/* 文件名 */}
<div className="form-group">
<label htmlFor="filename">文件名</label>
<input
id="filename"
type="text"
className={`form-input ${error ? 'input-error' : ''}`}
value={filename}
onChange={(e) => handleFilenameChange(e.target.value)}
placeholder="请输入文件名"
/>
{error && <p className="error-message">{error}</p>}
<p className="hint-text">不支持字符: / \ : * ? " {'<'} {'>'} |</p>
</div>
{/* 导出格式 */}
<div className="form-group">
<label>导出格式</label>
<div className="radio-group">
<label className="radio-label">
<input
type="radio"
name="format"
value="html"
checked={format === 'html'}
onChange={(e) => setFormat(e.target.value as 'html')}
/>
<span>HTML (完整报告 + 样式)</span>
</label>
<label className="radio-label">
<input
type="radio"
name="format"
value="json"
checked={format === 'json'}
onChange={(e) => setFormat(e.target.value as 'json')}
/>
<span>JSON (原始数据)</span>
</label>
</div>
</div>
{/* 选项 */}
<div className="form-group">
<label>选项</label>
<div className="checkbox-group">
<label className="checkbox-label">
<input
type="checkbox"
checked={includeTimestamp}
onChange={(e) => setIncludeTimestamp(e.target.checked)}
/>
<span>包含时间戳</span>
</label>
{format === 'json' && (
<label className="checkbox-label">
<input
type="checkbox"
checked={prettyPrint}
onChange={(e) => setPrettyPrint(e.target.checked)}
/>
<span>美化输出</span>
</label>
)}
</div>
</div>
</div>
{/* 操作按钮 */}
<div className="modal-footer">
<button className="button button-secondary" onClick={onClose}>
取消
</button>
<button className="button" onClick={handleExport}>
导出
</button>
</div>
</div>
</div>
);
}