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
|
"use client";
import * as React from "react";
import { useState } from "react";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button";
import { Alert, AlertDescription } from "@/components/ui/alert";
import { Upload, FolderOpen, Loader2, X, FileText, AlertCircle } from "lucide-react";
import { toast } from "sonner";
import { useFileUploadWithProgress } from "../hooks/use-file-upload-with-progress";
import { uploadFilesWithProgress, type UploadResult } from "../utils/upload-with-progress";
import { FileUploadProgressList } from "../components/file-upload-progress-list";
interface UploadFilesToDetailDialogProps {
open: boolean;
onOpenChange: (open: boolean) => void;
uploadId: string;
drawingNo: string;
revNo: string;
userId: string;
onUploadComplete?: () => void;
}
export function UploadFilesToDetailDialog({
open,
onOpenChange,
uploadId,
drawingNo,
revNo,
userId,
onUploadComplete,
}: UploadFilesToDetailDialogProps) {
const [isUploading, setIsUploading] = useState(false);
// 파일 업로드 훅 사용 (진행도 추적)
const {
fileProgresses,
files: selectedFiles,
removeFile,
clearFiles,
updateFileProgress,
getRootProps,
getInputProps,
isDragActive,
} = useFileUploadWithProgress();
// 다이얼로그 닫을 때 초기화
React.useEffect(() => {
if (!open) {
clearFiles();
}
}, [open, clearFiles]);
// 업로드 처리
const handleUpload = async () => {
if (selectedFiles.length === 0) {
toast.error("파일을 선택해주세요");
return;
}
setIsUploading(true);
try {
// 모든 파일 상태를 uploading으로 변경
selectedFiles.forEach((_, index) => {
updateFileProgress(index, 0, "uploading");
});
// 진행도 추적 업로드 호출
const result: UploadResult = await uploadFilesWithProgress({
uploadId,
userId,
files: selectedFiles,
callbacks: {
onProgress: (fileIndex, progress) => {
updateFileProgress(fileIndex, progress, "uploading");
},
onFileComplete: (fileIndex) => {
updateFileProgress(fileIndex, 100, "completed");
},
onFileError: (fileIndex, error) => {
updateFileProgress(fileIndex, 0, "error", error);
},
},
});
if (result.success) {
toast.success(`${result.uploadedCount}개 파일 업로드 완료`);
onOpenChange(false);
onUploadComplete?.();
} else {
toast.error(result.error || "업로드 실패");
}
} catch (error) {
console.error("업로드 실패:", error);
toast.error(
error instanceof Error ? error.message : "업로드 중 오류가 발생했습니다"
);
} finally {
setIsUploading(false);
}
};
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent className="max-w-2xl">
<DialogHeader>
<DialogTitle>파일 업로드</DialogTitle>
<DialogDescription>
{drawingNo} - Rev. {revNo}에 파일을 업로드합니다
</DialogDescription>
</DialogHeader>
<div className="space-y-4">
{/* 안내 메시지 */}
<Alert>
<AlertCircle className="h-4 w-4" />
<AlertDescription>
선택한 상세도면의 UploadId에 파일을 추가합니다. 파일 업로드 후 자동으로 메타데이터가 저장됩니다.
</AlertDescription>
</Alert>
{/* 파일 선택 영역 */}
<div
{...getRootProps()}
className={`border-2 border-dashed rounded-lg p-8 transition-all duration-200 cursor-pointer ${
isDragActive
? "border-primary bg-primary/5 scale-[1.02]"
: "border-muted-foreground/30 hover:border-muted-foreground/50"
}`}
>
<input {...getInputProps()} />
<div className="flex flex-col items-center justify-center">
<FolderOpen
className={`h-12 w-12 mb-3 transition-colors ${
isDragActive ? "text-primary" : "text-muted-foreground"
}`}
/>
<p
className={`text-sm transition-colors ${
isDragActive
? "text-primary font-medium"
: "text-muted-foreground"
}`}
>
{isDragActive
? "파일을 여기에 놓으세요"
: "클릭하거나 파일을 드래그하여 선택"}
</p>
<p className="text-xs text-muted-foreground mt-1">
여러 파일을 한 번에 업로드할 수 있습니다 (최대 1GB/파일)
</p>
</div>
</div>
{/* 선택된 파일 목록 */}
{selectedFiles.length > 0 && (
<div className="border rounded-lg p-4">
{isUploading ? (
// 업로드 중: 진행도 표시
<FileUploadProgressList fileProgresses={fileProgresses} />
) : (
// 대기 중: 삭제 버튼 표시
<>
<div className="flex items-center justify-between mb-3">
<h4 className="text-sm font-medium">
선택된 파일 ({selectedFiles.length}개)
</h4>
<Button
variant="ghost"
size="sm"
onClick={clearFiles}
>
전체 제거
</Button>
</div>
<div className="max-h-48 overflow-auto space-y-2">
{selectedFiles.map((file, index) => (
<div
key={index}
className="flex items-center justify-between p-2 rounded bg-muted/50"
>
<div className="flex items-center gap-2 flex-1 min-w-0">
<FileText className="h-4 w-4 text-muted-foreground shrink-0" />
<div className="flex-1 min-w-0">
<p className="text-sm truncate">{file.name}</p>
<p className="text-xs text-muted-foreground">
{(file.size / 1024 / 1024).toFixed(2)} MB
</p>
</div>
</div>
<Button
variant="ghost"
size="sm"
onClick={() => removeFile(index)}
>
<X className="h-4 w-4" />
</Button>
</div>
))}
</div>
</>
)}
</div>
)}
</div>
<DialogFooter>
<Button
variant="outline"
onClick={() => onOpenChange(false)}
disabled={isUploading}
>
취소
</Button>
<Button
onClick={handleUpload}
disabled={selectedFiles.length === 0 || isUploading}
>
{isUploading ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
업로드 중...
</>
) : (
<>
<Upload className="mr-2 h-4 w-4" />
업로드 ({selectedFiles.length}개)
</>
)}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
|