|
1
|
import { ArrowDownTray } from "@medusajs/icons" |
|
2
|
import { Text, clx } from "@medusajs/ui" |
|
3
|
import { ChangeEvent, DragEvent, useRef, useState } from "react" |
|
4
|
|
|
5
|
export interface FileType { |
|
6
|
id: string |
|
7
|
url: string |
|
8
|
file: File |
|
9
|
} |
|
10
|
|
|
11
|
export interface RejectedFile { |
|
12
|
file: File |
|
13
|
reason: "size" | "format" |
|
14
|
} |
|
15
|
|
|
16
|
export interface FileUploadProps { |
|
17
|
label: string |
|
18
|
multiple?: boolean |
|
19
|
hint?: string |
|
20
|
hasError?: boolean |
|
21
|
formats: string[] |
|
22
|
maxFileSize?: number // in bytes, defaults to 1MB. Set to Infinity to disable. |
|
23
|
onUploaded: (files: FileType[], rejectedFiles?: RejectedFile[]) => void |
|
24
|
} |
|
25
|
|
|
26
|
const DEFAULT_MAX_FILE_SIZE = __MAX_UPLOAD_FILE_SIZE__ ?? 1024 * 1024 // 1MB fallback |
|
27
|
|
|
28
|
export const FileUpload = ({ |
|
29
|
label, |
|
30
|
hint, |
|
31
|
multiple = true, |
|
32
|
hasError, |
|
33
|
formats, |
|
34
|
maxFileSize = DEFAULT_MAX_FILE_SIZE, |
|
35
|
onUploaded, |
|
36
|
}: FileUploadProps) => { |
|
37
|
const [isDragOver, setIsDragOver] = useState<boolean>(false) |
|
38
|
const inputRef = useRef<HTMLInputElement>(null) |
|
39
|
const dropZoneRef = useRef<HTMLButtonElement>(null) |
|
40
|
|
|
41
|
const handleOpenFileSelector = () => { |
|
42
|
inputRef.current?.click() |
|
43
|
} |
|
44
|
|
|
45
|
const handleDragEnter = (event: DragEvent) => { |
|
46
|
event.preventDefault() |
|
47
|
event.stopPropagation() |
|
48
|
|
|
49
|
const files = event.dataTransfer?.files |
|
50
|
if (!files) { |
|
51
|
return |
|
52
|
} |
|
53
|
|
|
54
|
setIsDragOver(true) |
|
55
|
} |
|
56
|
|
|
57
|
const handleDragLeave = (event: DragEvent) => { |
|
58
|
event.preventDefault() |
|
59
|
event.stopPropagation() |
|
60
|
|
|
61
|
if ( |
|
62
|
!dropZoneRef.current || |
|
63
|
dropZoneRef.current.contains(event.relatedTarget as Node) |
|
64
|
) { |
|
65
|
return |
|
66
|
} |
|
67
|
|
|
68
|
setIsDragOver(false) |
|
69
|
} |
|
70
|
|
|
71
|
const handleUploaded = (files: FileList | null) => { |
|
72
|
if (!files) { |
|
73
|
return |
|
74
|
} |
|
75
|
|
|
76
|
const fileList = Array.from(files) |
|
77
|
const validFiles: FileType[] = [] |
|
78
|
const rejectedFiles: RejectedFile[] = [] |
|
79
|
const normalizedMaxFileSize = Math.min(maxFileSize, Infinity) |
|
80
|
|
|
81
|
fileList.forEach((file) => { |
|
82
|
if (file.size > normalizedMaxFileSize) { |
|
83
|
rejectedFiles.push({ file, reason: "size" }) |
|
84
|
return |
|
85
|
} |
|
86
|
|
|
87
|
const id = Math.random().toString(36).substring(7) |
|
88
|
const previewUrl = URL.createObjectURL(file) |
|
89
|
validFiles.push({ |
|
90
|
id: id, |
|
91
|
url: previewUrl, |
|
92
|
file, |
|
93
|
}) |
|
94
|
}) |
|
95
|
|
|
96
|
onUploaded(validFiles, rejectedFiles) |
|
97
|
} |
|
98
|
|
|
99
|
const handleDrop = (event: DragEvent) => { |
|
100
|
event.preventDefault() |
|
101
|
event.stopPropagation() |
|
102
|
|
|
103
|
setIsDragOver(false) |
|
104
|
|
|
105
|
handleUploaded(event.dataTransfer?.files) |
|
106
|
} |
|
107
|
|
|
108
|
const handleFileChange = async (event: ChangeEvent<HTMLInputElement>) => { |
|
109
|
handleUploaded(event.target.files) |
|
110
|
} |
|
111
|
|
|
112
|
return ( |
|
113
|
<div> |
|
114
|
<button |
|
115
|
ref={dropZoneRef} |
|
116
|
type="button" |
|
117
|
onClick={handleOpenFileSelector} |
|
118
|
onDrop={handleDrop} |
|
119
|
onDragOver={(e) => e.preventDefault()} |
|
120
|
onDragEnter={handleDragEnter} |
|
121
|
onDragLeave={handleDragLeave} |
|
122
|
className={clx( |
|
123
|
"bg-ui-bg-component border-ui-border-strong transition-fg group flex w-full flex-col items-center gap-y-2 rounded-lg border border-dashed p-8", |
|
124
|
"hover:border-ui-border-interactive focus:border-ui-border-interactive", |
|
125
|
"focus:shadow-borders-focus outline-none focus:border-solid", |
|
126
|
{ |
|
127
|
"!border-ui-border-error": hasError, |
|
128
|
"!border-ui-border-interactive": isDragOver, |
|
129
|
} |
|
130
|
)} |
|
131
|
> |
|
132
|
<div className="text-ui-fg-subtle group-disabled:text-ui-fg-disabled flex items-center gap-x-2"> |
|
133
|
<ArrowDownTray /> |
|
134
|
<Text>{label}</Text> |
|
135
|
</div> |
|
136
|
{!!hint && ( |
|
137
|
<Text |
|
138
|
size="small" |
|
139
|
leading="compact" |
|
140
|
className="text-ui-fg-muted group-disabled:text-ui-fg-disabled" |
|
141
|
> |
|
142
|
{hint} |
|
143
|
</Text> |
|
144
|
)} |
|
145
|
</button> |
|
146
|
<input |
|
147
|
hidden |
|
148
|
ref={inputRef} |
|
149
|
onChange={handleFileChange} |
|
150
|
type="file" |
|
151
|
accept={formats.join(",")} |
|
152
|
multiple={multiple} |
|
153
|
/> |
|
154
|
</div> |
|
155
|
) |
|
156
|
} |