"use client"; import { useState, useCallback } from 'react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Progress } from '@/components/ui/progress'; import { Download, FileText, Image, Video, Music, Archive } from 'lucide-react'; import { FileInfo, TransferProgress } from '@/types'; interface FileReceiveProps { onJoinRoom: (code: string) => void; files: FileInfo[]; onDownloadFile: (fileId: string) => void; transferProgresses: TransferProgress[]; isConnected: boolean; isConnecting: boolean; } const getFileIcon = (mimeType: string) => { if (mimeType.startsWith('image/')) return ; if (mimeType.startsWith('video/')) return ; if (mimeType.startsWith('audio/')) return ; if (mimeType.includes('zip') || mimeType.includes('rar')) return ; return ; }; const formatFileSize = (bytes: number): string => { if (bytes === 0) return '0 Bytes'; const k = 1024; const sizes = ['Bytes', 'KB', 'MB', 'GB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; }; export function FileReceive({ onJoinRoom, files, onDownloadFile, transferProgresses, isConnected, isConnecting }: FileReceiveProps) { const [pickupCode, setPickupCode] = useState(''); const handleSubmit = useCallback((e: React.FormEvent) => { e.preventDefault(); if (pickupCode.length === 6) { onJoinRoom(pickupCode.toUpperCase()); } }, [pickupCode, onJoinRoom]); const handleInputChange = useCallback((e: React.ChangeEvent) => { const value = e.target.value.replace(/[^A-Z0-9]/g, '').toUpperCase(); if (value.length <= 6) { setPickupCode(value); } }, []); // 如果已经连接并且有文件列表,显示文件列表 if (files.length > 0) { return ( 可下载文件 {isConnected ? ( ✅ 已连接,可以下载文件 ) : ( ⏳ 正在建立连接... )} {files.length} 个文件 {files.map((file) => { const progress = transferProgresses.find(p => p.originalFileId === file.id); const isDownloading = progress && progress.status === 'downloading'; const isCompleted = progress && progress.status === 'completed'; return ( {getFileIcon(file.type)} {file.name} {formatFileSize(file.size)} {isCompleted && ( ✅ 下载完成 )} onDownloadFile(file.id)} disabled={!isConnected || isDownloading || isCompleted} className={`px-6 py-2 rounded-lg font-medium shadow-lg transition-all duration-200 hover:shadow-xl ${ isCompleted ? 'bg-slate-300 text-slate-500 cursor-not-allowed' : 'bg-gradient-to-r from-emerald-500 to-teal-500 hover:from-emerald-600 hover:to-teal-600 text-white' }`} > {isDownloading ? '下载中...' : isCompleted ? '已完成' : '下载'} {progress && (progress.status === 'downloading' || progress.status === 'completed') && ( {progress.status === 'completed' ? '下载完成' : '正在下载...'} {progress.progress.toFixed(1)}% {formatFileSize(progress.receivedSize)} / {formatFileSize(progress.totalSize)} {progress.status === 'downloading' && ( 预计还需 {Math.ceil((progress.totalSize - progress.receivedSize) / 1024 / 1024)} MB )} )} ); })} ); } // 显示取件码输入界面 return ( 输入取件码 请输入6位取件码来获取文件 {[...Array(6)].map((_, i) => ( ))} {pickupCode.length}/6 位 {isConnecting ? ( 连接中... ) : ( 开始接收 )} {/* 使用提示 */} 💡 提示:取件码由发送方提供,有效期为24小时 ); }
{isConnected ? ( ✅ 已连接,可以下载文件 ) : ( ⏳ 正在建立连接... )}
{file.name}
{formatFileSize(file.size)}
✅ 下载完成
请输入6位取件码来获取文件
{pickupCode.length}/6 位
💡 提示:取件码由发送方提供,有效期为24小时