-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
346 lines (311 loc) · 10.8 KB
/
Copy pathApp.tsx
File metadata and controls
346 lines (311 loc) · 10.8 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
// src/App.tsx
// 主应用组件 - 状态管理和组件编排
import { useState, useEffect } from 'react';
import { AnalysisResult, AnalysisProgress } from './types';
import { analyzeApk } from './services/apkAnalyzer';
import { loadConfig, type AppConfig } from './config';
import { useTextOverflowDetection } from './hooks/useTextOverflowDetection';
import { loadRules } from './services/rulesLoader';
import FileUploader from './components/FileUploader';
import AnalysisProgressComponent from './components/AnalysisProgress';
import ResultTabs from './components/ResultTabs';
import ReportExport from './components/ReportExport';
import AnalysisHistory from './components/AnalysisHistory';
import './styles/App.css';
// 应用状态类型
type AppState = 'idle' | 'analyzing' | 'completed' | 'error' | 'history';
// 最近分析记录类型
interface RecentAnalysis {
id: number;
fileName: string;
fileSize: string;
packageName: string;
analyzeTime: string;
result: AnalysisResult;
}
export default function App() {
// 配置状态
const [config, setConfig] = useState<AppConfig | null>(null);
// 应用状态
const [state, setState] = useState<AppState>('idle');
const [progress, setProgress] = useState<AnalysisProgress | null>(null);
const [result, setResult] = useState<AnalysisResult | null>(null);
const [error, setError] = useState<string | null>(null);
const [showExportModal, setShowExportModal] = useState(false);
// 自动检测页脚信息框是否被遮挡
useTextOverflowDetection({
containerSelector: '.footer-info',
textSelector: '.info-list',
minPaddingBottom: 20,
checkInterval: 1000,
debug: false,
});
// 初始化:加载配置和规则
useEffect(() => {
const initializeApp = async () => {
// 1. 加载配置
const appConfig = await loadConfig();
setConfig(appConfig);
// 2. 预加载规则库(背景加载,不阻塞 UI)
try {
console.log('⏳ 预加载规则库...');
const rules = await loadRules();
if (rules) {
console.log(`✓ 规则库预加载完成 (${rules.totalRules} 个规则)`);
}
} catch (err) {
console.warn('规则库预加载失败:', err);
// 不影响应用正常运行
}
};
initializeApp();
}, []);
// 最近分析列表状态
const [recentAnalyses, setRecentAnalyses] = useState<RecentAnalysis[]>(() => {
// 从 localStorage 加载最近的分析记录
try {
const stored = localStorage.getItem('recentAnalyses');
return stored ? JSON.parse(stored) : [];
} catch {
return [];
}
});
// 文件大小验证错误状态
const [fileSizeError, setFileSizeError] = useState<string | null>(null);
// 删除确认弹窗状态
const [deletingRecordId, setDeletingRecordId] = useState<number | null>(null);
// 处理文件选择
const handleFileSelect = async (file: File) => {
setFileSizeError(null);
setState('analyzing');
setError(null);
setProgress({
stage: 'extracting',
progress: 0,
message: '正在提取 APK 文件...',
});
try {
// 调用分析服务
const analysisResult = await analyzeApk(file, (progressUpdate) => {
setProgress(progressUpdate);
});
// 分析完成
setResult(analysisResult);
setState('completed');
setProgress(null);
// 添加到最近分析列表
const fileSizeFormatted = (file.size / 1024 / 1024).toFixed(2) + ' MB';
const newRecord: RecentAnalysis = {
id: Date.now(),
fileName: file.name,
fileSize: fileSizeFormatted,
packageName: analysisResult.basic.packageName,
analyzeTime: new Date().toLocaleString('zh-CN'),
result: analysisResult,
};
// 根据 bundle id 去重:移除相同 packageName 的旧记录
const deduplicatedList = recentAnalyses.filter(
(record) => record.packageName !== analysisResult.basic.packageName
);
// 将新记录添加到列表顶部,并限制列表大小为 10
const updated = [newRecord, ...deduplicatedList].slice(0, 10);
setRecentAnalyses(updated);
localStorage.setItem('recentAnalyses', JSON.stringify(updated));
} catch (err) {
console.error('分析失败:', err);
setState('error');
setError(err instanceof Error ? err.message : '分析过程中发生未知错误');
setProgress(null);
}
};
// 处理文件验证错误
const handleFileValidationError = (errorMessage: string) => {
setFileSizeError(errorMessage);
};
// 查看历史分析结果
const handleQuickReanalyze = (record: RecentAnalysis) => {
setFileSizeError(null);
// 直接加载缓存的分析结果并跳转到结果页面
setResult(record.result);
setState('completed');
};
// 查看历史记录
const handleViewHistory = () => {
setState('history');
};
// 清空所有历史记录
const handleClearAllHistory = () => {
setRecentAnalyses([]);
localStorage.setItem('recentAnalyses', JSON.stringify([]));
};
// 删除分析记录
const handleDeleteRecord = (recordId: number) => {
const updated = recentAnalyses.filter(record => record.id !== recordId);
setRecentAnalyses(updated);
localStorage.setItem('recentAnalyses', JSON.stringify(updated));
setDeletingRecordId(null);
};
// 重置状态(返回上传页面)
const handleReset = () => {
setState('idle');
setResult(null);
setError(null);
setProgress(null);
setShowExportModal(false);
setFileSizeError(null);
};
// 打开导出对话框
const handleExport = () => {
setShowExportModal(true);
};
// 关闭导出对话框
const handleCloseExport = () => {
setShowExportModal(false);
};
return (
<div className="app">
{/* 全局 Header - 根据状态显示不同内容 */}
<header className="app-header">
<div className="header-content">
<div>
{state === 'history' ? (
<>
<h1>📋 分析历史</h1>
<p className="subtitle">查看和管理您的 APK 分析记录</p>
</>
) : (
<>
<h1>🔍 APK SDK 分析工具</h1>
<p className="subtitle">快速识别 Android 应用中的 SDK 和第三方库</p>
</>
)}
</div>
</div>
{/* 结果页面显示操作按钮 */}
{state === 'completed' && (
<div className="header-actions">
<button className="btn btn-sm btn-secondary" onClick={handleExport}>
导出报告
</button>
<button className="btn btn-sm btn-secondary" onClick={handleReset}>
重新分析
</button>
</div>
)}
</header>
{/* 主内容区域 */}
<main className="app-main">
{/* 空闲状态 - 显示上传界面 */}
{state === 'idle' && (
<div className="upload-analyze-container">
<FileUploader
onFileSelect={handleFileSelect}
disabled={false}
recentAnalyses={recentAnalyses}
onQuickReanalyze={handleQuickReanalyze}
onViewHistory={handleViewHistory}
onDeleteRecord={handleDeleteRecord}
fileValidationError={fileSizeError}
onValidationError={handleFileValidationError}
deletingRecordId={deletingRecordId}
onSetDeletingRecordId={setDeletingRecordId}
/>
</div>
)}
{/* 分析中 - 显示进度 */}
{state === 'analyzing' && progress && (
<div className="upload-analyze-container">
<AnalysisProgressComponent progress={progress} />
</div>
)}
{/* 分析完成 - 显示结果 */}
{state === 'completed' && result && (
<div className="result-container">
<ResultTabs
result={result}
onExport={handleExport}
onReset={handleReset}
/>
</div>
)}
{/* 错误状态 - 显示错误信息 */}
{state === 'error' && (
<div className="upload-analyze-container">
<div className="card error-card">
<div className="error-icon">❌</div>
<h2>分析失败</h2>
<p className="error-message">{error}</p>
<button className="button" onClick={handleReset}>
重新上传
</button>
</div>
</div>
)}
{/* 历史记录页面 */}
{state === 'history' && (
<div className="upload-analyze-container">
<AnalysisHistory
recentAnalyses={recentAnalyses}
onQuickReanalyze={handleQuickReanalyze}
onBackToUpload={handleReset}
onDeleteRecord={handleDeleteRecord}
onClearAllHistory={handleClearAllHistory}
/>
</div>
)}
</main>
{/* 页脚 */}
<footer className="app-footer">
{config && (
<>
<div className="footer-content">
{/* 项目链接 - 如果配置了 URL 才显示 */}
{config.footer.projectUrl && config.footer.projectLabel && (
<div className="footer-project">
<p>
基于{' '}
<a
href={config.footer.projectUrl}
target="_blank"
rel="noopener noreferrer"
title={config.footer.projectUrl}
>
{config.footer.projectLabel}
</a>{' '}
| 支持识别 2800+ SDK
</p>
</div>
)}
{/* ICP 备案信息 - 只有启用且有值才显示 */}
{config.footer.icp?.enabled &&
config.footer.icp.number &&
config.footer.icp.url && (
<div className="footer-icp">
<a
href={config.footer.icp.url}
target="_blank"
rel="noopener noreferrer"
title={config.footer.icp.label || 'ICP 备案号'}
className="icp-link"
>
{config.footer.icp.number}
</a>
</div>
)}
</div>
{/* 版权信息 - 只有值存在才显示 */}
{config.footer.copyright && config.footer.copyright.trim() && (
<div className="footer-copyright">
{config.footer.copyright}
</div>
)}
</>
)}
</footer>
{/* 导出对话框 */}
{showExportModal && result && (
<ReportExport result={result} onClose={handleCloseExport} />
)}
</div>
);
}