Spaces:
Sleeping
Sleeping
File size: 6,938 Bytes
61ca2b1 | 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 | import { useState } from 'react'
import { Card, CardContent, CardHeader, CardTitle } from '../components/ui/card'
import { FileUpload } from '../components/ui/file-upload'
import { parseStockDataFile, ParseResult } from '../utils/fileParser'
import { CheckCircle, AlertCircle, FileText, Database } from 'lucide-react'
export default function FileTest() {
const [uploadedFile, setUploadedFile] = useState<File | null>(null)
const [parseResult, setParseResult] = useState<ParseResult | null>(null)
const [parsing, setParsing] = useState(false)
const handleFileSelect = async (file: File) => {
setUploadedFile(file)
setParsing(true)
setParseResult(null)
try {
const result = await parseStockDataFile(file)
setParseResult(result)
if (result.success) {
console.log('解析成功:', result)
} else {
console.error('解析失败:', result.error)
}
} catch (error) {
console.error('解析异常:', error)
setParseResult({
success: false,
error: `解析异常: ${error}`
})
} finally {
setParsing(false)
}
}
const handleFileRemove = () => {
setUploadedFile(null)
setParseResult(null)
}
return (
<div className="w-full">
<div className="mb-6">
<h1 className="text-2xl font-bold mb-2">文件解析测试</h1>
<p className="text-muted-foreground">
测试EXCEL、CSV、JSON文件的解析功能
</p>
</div>
<div className="grid gap-6">
{/* 文件上传区域 */}
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<FileText className="h-5 w-5" />
文件上传
</CardTitle>
</CardHeader>
<CardContent>
<FileUpload
onFileSelect={handleFileSelect}
onFileRemove={handleFileRemove}
selectedFile={uploadedFile}
acceptedTypes={['.csv', '.xlsx', '.xls', '.json']}
maxSize={50}
/>
</CardContent>
</Card>
{/* 解析状态 */}
{parsing && (
<Card>
<CardContent className="pt-6">
<div className="flex items-center gap-2 text-blue-600">
<div className="animate-spin rounded-full h-4 w-4 border-b-2 border-blue-600"></div>
<span>正在解析文件...</span>
</div>
</CardContent>
</Card>
)}
{/* 解析结果 */}
{parseResult && (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Database className="h-5 w-5" />
解析结果
{parseResult.success ? (
<CheckCircle className="h-5 w-5 text-green-500" />
) : (
<AlertCircle className="h-5 w-5 text-red-500" />
)}
</CardTitle>
</CardHeader>
<CardContent>
{parseResult.success ? (
<div className="space-y-4">
<div className="grid grid-cols-2 gap-4">
<div>
<label className="text-sm font-medium text-muted-foreground">股票代码</label>
<p className="text-lg font-semibold">{parseResult.symbol || '未识别'}</p>
</div>
<div>
<label className="text-sm font-medium text-muted-foreground">股票名称</label>
<p className="text-lg font-semibold">{parseResult.symbolName || '未识别'}</p>
</div>
<div>
<label className="text-sm font-medium text-muted-foreground">数据条数</label>
<p className="text-lg font-semibold">{parseResult.totalRecords || 0} 条</p>
</div>
<div>
<label className="text-sm font-medium text-muted-foreground">日期范围</label>
<p className="text-lg font-semibold">
{parseResult.dateRange ?
`${parseResult.dateRange.start} ~ ${parseResult.dateRange.end}` :
'未识别'
}
</p>
</div>
</div>
{/* 数据预览 */}
{parseResult.data && parseResult.data.length > 0 && (
<div>
<h4 className="text-sm font-medium text-muted-foreground mb-2">数据预览(前5条)</h4>
<div className="overflow-x-auto">
<table className="w-full text-sm border-collapse border border-gray-200">
<thead>
<tr className="bg-gray-50">
<th className="border border-gray-200 px-2 py-1 text-left">日期</th>
<th className="border border-gray-200 px-2 py-1 text-left">开盘</th>
<th className="border border-gray-200 px-2 py-1 text-left">最高</th>
<th className="border border-gray-200 px-2 py-1 text-left">最低</th>
<th className="border border-gray-200 px-2 py-1 text-left">收盘</th>
<th className="border border-gray-200 px-2 py-1 text-left">成交量</th>
</tr>
</thead>
<tbody>
{parseResult.data.slice(0, 5).map((row, index) => (
<tr key={index}>
<td className="border border-gray-200 px-2 py-1">{row.date}</td>
<td className="border border-gray-200 px-2 py-1">{row.open}</td>
<td className="border border-gray-200 px-2 py-1">{row.high}</td>
<td className="border border-gray-200 px-2 py-1">{row.low}</td>
<td className="border border-gray-200 px-2 py-1">{row.close}</td>
<td className="border border-gray-200 px-2 py-1">{row.volume}</td>
</tr>
))}
</tbody>
</table>
</div>
</div>
)}
</div>
) : (
<div className="text-red-600">
<p className="font-medium">解析失败</p>
<p className="text-sm mt-1">{parseResult.error}</p>
</div>
)}
</CardContent>
</Card>
)}
</div>
</div>
)
}
|