-
Notifications
You must be signed in to change notification settings - Fork 782
/
uploader.tsx
204 lines (195 loc) · 6.63 KB
/
uploader.tsx
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
"use client";
import { useState, useCallback, useMemo, ChangeEvent } from "react";
import { toast } from "sonner";
import LoadingDots from "@/components/icons/loading-dots";
export default function Uploader() {
const [data, setData] = useState<{
image: string | null;
}>({
image: null,
});
const [file, setFile] = useState<File | null>(null);
const [dragActive, setDragActive] = useState(false);
const onChangePicture = useCallback(
(event: ChangeEvent<HTMLInputElement>) => {
const file = event.currentTarget.files && event.currentTarget.files[0];
if (file) {
if (file.size / 1024 / 1024 > 50) {
toast.error("File size too big (max 50MB)");
} else {
setFile(file);
const reader = new FileReader();
reader.onload = (e) => {
setData((prev) => ({ ...prev, image: e.target?.result as string }));
};
reader.readAsDataURL(file);
}
}
},
[setData],
);
const [saving, setSaving] = useState(false);
const saveDisabled = useMemo(() => {
return !data.image || saving;
}, [data.image, saving]);
return (
<form
className="grid gap-6"
onSubmit={async (e) => {
e.preventDefault();
setSaving(true);
fetch("/api/upload", {
method: "POST",
headers: { "content-type": file?.type || "application/octet-stream" },
body: file,
}).then(async (res) => {
if (res.status === 200) {
const { url } = await res.json();
toast(
<div className="relative">
<div className="p-2">
<p className="font-semibold text-gray-900">File uploaded!</p>
<p className="mt-1 text-sm text-gray-500">
Your file has been uploaded to{" "}
<a
className="font-medium text-gray-900 underline"
href={url}
target="_blank"
rel="noopener noreferrer"
>
{url}
</a>
</p>
</div>
</div>,
);
} else {
const error = await res.text();
toast.error(error);
}
setSaving(false);
});
}}
>
<div>
<div className="mb-4 space-y-1">
<h2 className="text-xl font-semibold">Upload a file</h2>
<p className="text-sm text-gray-500">
Accepted formats: .png, .jpg, .gif, .mp4
</p>
</div>
<label
htmlFor="image-upload"
className="group relative mt-2 flex h-72 cursor-pointer flex-col items-center justify-center rounded-md border border-gray-300 bg-white shadow-sm transition-all hover:bg-gray-50"
>
<div
className="absolute z-[5] h-full w-full rounded-md"
onDragOver={(e) => {
e.preventDefault();
e.stopPropagation();
setDragActive(true);
}}
onDragEnter={(e) => {
e.preventDefault();
e.stopPropagation();
setDragActive(true);
}}
onDragLeave={(e) => {
e.preventDefault();
e.stopPropagation();
setDragActive(false);
}}
onDrop={(e) => {
e.preventDefault();
e.stopPropagation();
setDragActive(false);
const file = e.dataTransfer.files && e.dataTransfer.files[0];
if (file) {
if (file.size / 1024 / 1024 > 50) {
toast.error("File size too big (max 50MB)");
} else {
setFile(file);
const reader = new FileReader();
reader.onload = (e) => {
setData((prev) => ({
...prev,
image: e.target?.result as string,
}));
};
reader.readAsDataURL(file);
}
}
}}
/>
<div
className={`${
dragActive ? "border-2 border-black" : ""
} absolute z-[3] flex h-full w-full flex-col items-center justify-center rounded-md px-10 transition-all ${
data.image
? "bg-white/80 opacity-0 hover:opacity-100 hover:backdrop-blur-md"
: "bg-white opacity-100 hover:bg-gray-50"
}`}
>
<svg
className={`${
dragActive ? "scale-110" : "scale-100"
} h-7 w-7 text-gray-500 transition-all duration-75 group-hover:scale-110 group-active:scale-95`}
xmlns="http://www.w3.org/2000/svg"
width="24"
height="24"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
>
<path d="M4 14.899A7 7 0 1 1 15.71 8h1.79a4.5 4.5 0 0 1 2.5 8.242"></path>
<path d="M12 12v9"></path>
<path d="m16 16-4-4-4 4"></path>
</svg>
<p className="mt-2 text-center text-sm text-gray-500">
Drag and drop or click to upload.
</p>
<p className="mt-2 text-center text-sm text-gray-500">
Max file size: 50MB
</p>
<span className="sr-only">Photo upload</span>
</div>
{data.image && (
// eslint-disable-next-line @next/next/no-img-element
<img
src={data.image}
alt="Preview"
className="h-full w-full rounded-md object-cover"
/>
)}
</label>
<div className="mt-1 flex rounded-md shadow-sm">
<input
id="image-upload"
name="image"
type="file"
accept="image/*"
className="sr-only"
onChange={onChangePicture}
/>
</div>
</div>
<button
disabled={saveDisabled}
className={`${
saveDisabled
? "cursor-not-allowed border-gray-200 bg-gray-100 text-gray-400"
: "border-black bg-black text-white hover:bg-white hover:text-black"
} flex h-10 w-full items-center justify-center rounded-md border text-sm transition-all focus:outline-none`}
>
{saving ? (
<LoadingDots color="#808080" />
) : (
<p className="text-sm">Confirm upload</p>
)}
</button>
</form>
);
}