-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadImageContainer.tsx
More file actions
178 lines (161 loc) · 5.19 KB
/
UploadImageContainer.tsx
File metadata and controls
178 lines (161 loc) · 5.19 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
'use client';
import {
ChangeEvent,
Dispatch,
DragEvent,
SetStateAction,
useState,
} from 'react';
import { FaImage } from 'react-icons/fa';
import UploadedImage from '@/app/entities/post/write/UploadedImage';
import { uploadImageFile } from '@/app/lib/utils/imageUpload';
interface UploadImageContainerProps {
onClick: (link: string) => void;
uploadedImages: string[];
setUploadedImages: Dispatch<SetStateAction<string[]>>;
}
const UploadImageContainer = ({
onClick,
uploadedImages,
setUploadedImages,
}: UploadImageContainerProps) => {
const [isDragging, setIsDragging] = useState(false);
const [isUploading, setIsUploading] = useState(false);
const [uploadProgress, setUploadProgress] = useState({
current: 0,
total: 0,
});
const uploadFiles = async (files: FileList) => {
try {
if (files.length === 0) {
throw new Error('업로드할 파일이 없습니다.');
}
setIsUploading(true);
setUploadProgress({ current: 0, total: files.length });
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (!file.type.startsWith('image/')) {
throw new Error('이미지 파일만 업로드할 수 있습니다.');
}
setUploadProgress({ current: i + 1, total: files.length });
const url = await uploadImageFile(file);
setUploadedImages((prev) => [...prev, url]);
}
return;
} catch (error) {
console.error('업로드 실패:', error);
throw error;
} finally {
setIsUploading(false);
setUploadProgress({ current: 0, total: 0 });
}
};
const uploadToBlob = async (event: ChangeEvent) => {
try {
event.preventDefault();
const target = event.target as HTMLInputElement;
if (!target.files) {
throw new Error('이미지가 선택되지 않았습니다.');
}
await uploadFiles(target.files);
} catch (error) {
console.error('업로드 실패:', error);
throw error;
}
};
const handleDragEnter = (e: DragEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
};
const handleDragLeave = (e: DragEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
};
const handleDragOver = (e: DragEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
};
const handleDrop = async (e: DragEvent<HTMLElement>) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
const files = e.dataTransfer.files;
if (files && files.length > 0) {
await uploadFiles(files);
}
};
return (
<div className={'w-full mt-4'}>
<div className={'flex justify-between my-1'}>
<div>
<span className={'text-xl font-bold text-black dark:text-white'}>
업로드된 이미지
</span>
{isUploading ? (
<p
className={'text-sm text-emerald-600 font-semibold animate-pulse'}
>
업로드 중... ({uploadProgress.current}/{uploadProgress.total})
</p>
) : (
<p className={'text-gray-600 dark:text-gray-400'}>
클릭하여 링크 복사
</p>
)}
</div>
<div
className={
'cursor-pointer relative w-12 h-12 bg-emerald-500 rounded-md overflow-hidden'
}
>
<FaImage
className={
'absolute top-1/2 left-1/2 -translate-y-1/2 -translate-x-1/2 pointer-events-none'
}
/>
<input
type={'file'}
multiple={true}
placeholder={'이미지 업로드'}
onChange={uploadToBlob}
className={
'w-full h-full file:hidden text-transparent px-2 hover:bg-emerald-600'
}
accept={'image/*'}
disabled={isUploading}
></input>
</div>
</div>
<ul
className={`w-full border border-gray-400 px-4 py-4 whitespace-nowrap space-x-4 overflow-x-scroll gap-2 min-h-40 transition-colors ${
isDragging
? 'border-primary-bangladesh border-dashed border-2'
: 'bg-gray-100 dark:bg-gray-800'
} ${isUploading ? 'opacity-70 pointer-events-none' : ''}`}
onDragEnter={handleDragEnter}
onDragLeave={handleDragLeave}
onDragOver={handleDragOver}
onDrop={handleDrop}
>
{uploadedImages.length === 0 && !isUploading && (
<div className="pointer-events-none text-sm text-gray-500 dark:text-gray-400">
{isDragging
? '떨어뜨려!!'
: '업로드된 이미지가 없습니다. 드래그&드랍으로 이미지를 추가하세요.'}
</div>
)}
{isUploading && uploadedImages.length === 0 && (
<div className="pointer-events-none text-sm text-emerald-600 font-semibold">
이미지를 업로드하는 중입니다...
</div>
)}
{uploadedImages.map((imageUrl, index) => (
<UploadedImage key={index} onClick={onClick} imageUrl={imageUrl} />
))}
</ul>
</div>
);
};
export default UploadImageContainer;