doctor-mail/apps/web/src/components/common/uploader/TusUploader.tsx

186 lines
4.5 KiB
TypeScript
Raw Normal View History

2025-01-25 22:39:22 +08:00
import { useCallback, useState } from "react";
2025-01-25 02:28:28 +08:00
import {
UploadOutlined,
CheckCircleOutlined,
DeleteOutlined,
} from "@ant-design/icons";
2025-01-26 19:33:45 +08:00
import { Upload, Progress, Button } from "antd";
2025-01-25 02:28:28 +08:00
import type { UploadFile } from "antd";
import { useTusUpload } from "@web/src/hooks/useTusUpload";
2025-01-26 18:32:47 +08:00
import toast from "react-hot-toast";
2025-01-26 20:26:12 +08:00
import { getCompressedImageUrl } from "@nice/utils";
2025-01-25 02:28:28 +08:00
export interface TusUploaderProps {
value?: string[];
onChange?: (value: string[]) => void;
}
2025-01-26 19:33:45 +08:00
2025-01-25 02:28:28 +08:00
interface UploadingFile {
name: string;
progress: number;
status: "uploading" | "done" | "error";
fileId?: string;
2025-01-25 23:19:03 +08:00
fileKey?: string;
2025-01-25 02:28:28 +08:00
}
export const TusUploader = ({ value = [], onChange }: TusUploaderProps) => {
2025-01-25 23:19:03 +08:00
const { handleFileUpload, uploadProgress } = useTusUpload();
2025-01-25 02:28:28 +08:00
const [uploadingFiles, setUploadingFiles] = useState<UploadingFile[]>([]);
2025-01-25 22:39:22 +08:00
const [completedFiles, setCompletedFiles] = useState<UploadingFile[]>(
() =>
value?.map((fileId) => ({
2025-01-25 23:19:03 +08:00
name: `文件 ${fileId}`,
progress: 100,
2025-01-25 22:39:22 +08:00
status: "done" as const,
fileId,
})) || []
2025-01-25 02:28:28 +08:00
);
2025-01-26 19:33:45 +08:00
// 恢复使用 uploadResults 状态跟踪最新结果
2025-01-25 02:28:28 +08:00
const [uploadResults, setUploadResults] = useState<string[]>(value || []);
const handleRemoveFile = useCallback(
(fileId: string) => {
setCompletedFiles((prev) =>
prev.filter((f) => f.fileId !== fileId)
);
2025-01-26 19:33:45 +08:00
// 使用函数式更新保证获取最新状态
setUploadResults((prev) => {
const newValue = prev.filter((id) => id !== fileId);
onChange?.(newValue); // 同步更新父组件
return newValue;
});
2025-01-25 02:28:28 +08:00
},
2025-01-26 19:33:45 +08:00
[onChange]
2025-01-25 02:28:28 +08:00
);
2025-01-26 19:33:45 +08:00
const handleBeforeUpload = useCallback(
(file: File) => {
const fileKey = `${file.name}-${Date.now()}`;
2025-01-25 22:39:22 +08:00
2025-01-26 19:33:45 +08:00
setUploadingFiles((prev) => [
...prev,
{
name: file.name,
progress: 0,
status: "uploading",
fileKey,
},
]);
2025-01-25 02:28:28 +08:00
2025-01-26 19:33:45 +08:00
handleFileUpload(
file,
(result) => {
setCompletedFiles((prev) => [
...prev,
{
name: file.name,
progress: 100,
status: "done",
fileId: result.fileId,
},
]);
2025-01-25 23:19:03 +08:00
2025-01-26 19:33:45 +08:00
setUploadingFiles((prev) =>
prev.filter((f) => f.fileKey !== fileKey)
);
2025-01-25 02:28:28 +08:00
2025-01-26 19:33:45 +08:00
// 正确的状态更新方式
setUploadResults((prev) => {
const newValue = [...prev, result.fileId];
onChange?.(newValue); // 传递值而非函数
return newValue;
});
},
(error) => {
console.error("上传错误:", error);
toast.error(
`上传失败: ${
error instanceof Error ? error.message : "未知错误"
}`
);
setUploadingFiles((prev) =>
prev.map((f) =>
f.fileKey === fileKey
? { ...f, status: "error" }
: f
)
2025-01-25 02:28:28 +08:00
);
2025-01-26 19:33:45 +08:00
},
fileKey
);
2025-01-25 02:28:28 +08:00
return false;
},
2025-01-26 19:33:45 +08:00
[handleFileUpload, onChange]
2025-01-25 02:28:28 +08:00
);
return (
2025-01-25 22:39:22 +08:00
<div className="space-y-1">
2025-01-25 02:28:28 +08:00
<Upload.Dragger
name="files"
2025-01-26 19:33:45 +08:00
multiple
2025-01-25 02:28:28 +08:00
showUploadList={false}
2025-01-26 18:24:16 +08:00
style={{ background: "transparent", borderStyle: "none" }}
2025-01-26 19:33:45 +08:00
beforeUpload={handleBeforeUpload}>
2025-01-25 02:28:28 +08:00
<p className="ant-upload-drag-icon">
<UploadOutlined />
</p>
<p className="ant-upload-text">
2025-01-25 22:39:22 +08:00
2025-01-25 02:28:28 +08:00
</p>
2025-01-25 22:39:22 +08:00
<p className="ant-upload-hint"></p>
2025-01-25 23:19:03 +08:00
2025-01-26 19:33:45 +08:00
{/* 上传状态展示 */}
<div className="px-2 py-0 rounded mt-1">
{/* 上传中的文件 */}
{uploadingFiles.map((file) => (
<div
key={file.fileKey}
className="flex flex-col gap-1 mb-2">
<div className="flex items-center gap-2">
<span className="text-sm">{file.name}</span>
</div>
<Progress
percent={
file.status === "done"
? 100
: Math.round(
uploadProgress?.[
file.fileKey!
] || 0
)
}
status={
file.status === "error"
? "exception"
: "active"
}
/>
</div>
))}
{/* 已完成的文件 */}
{completedFiles.map((file) => (
<div
key={file.fileId}
className="flex items-center justify-between gap-2 mb-2">
<div className="flex items-center gap-2">
<CheckCircleOutlined className="text-green-500" />
<span className="text-sm">{file.name}</span>
2025-01-25 23:19:03 +08:00
</div>
2025-01-26 19:33:45 +08:00
<Button
type="text"
danger
icon={<DeleteOutlined />}
onClick={(e) => {
e.stopPropagation();
if (file.fileId)
handleRemoveFile(file.fileId);
}}
/>
</div>
))}
</div>
2025-01-25 02:28:28 +08:00
</Upload.Dragger>
</div>
);
2025-01-25 00:46:59 +08:00
};