-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFileUploader.tsx
More file actions
165 lines (151 loc) · 4.89 KB
/
Copy pathFileUploader.tsx
File metadata and controls
165 lines (151 loc) · 4.89 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
'use client';
import { ethers } from 'ethers';
import { useState, useCallback } from 'react';
import { useAccount } from 'wagmi';
// TODO: 1. import synapse-sdk
export function FileUploader() {
const [file, setFile] = useState<File | null>(null);
const [isDragging, setIsDragging] = useState(false);
const { isConnected } = useAccount();
const [status, setStatus] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [progress, setProgress] = useState(0);
const handleDrag = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
}, []);
const handleDragIn = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(true);
}, []);
const handleDragOut = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
}, []);
const handleDrop = useCallback((e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setIsDragging(false);
const files = e.dataTransfer.files;
if (files && files.length > 0) {
setFile(files[0]);
}
}, []);
const handleSubmit = async () => {
if (!file) return;
try {
setIsLoading(true);
setProgress(0);
setStatus('Preparing upload...');
const provider = new ethers.BrowserProvider(window.ethereum);
const signer = await provider.getSigner();
// TODO: 2. upload file to Filecoin using synapse-sdk
setStatus('✅ File uploaded successfully to Filecoin!');
} catch (err: any) {
console.error(err);
setStatus(`❌ ${err.message || 'Upload failed. Please try again.'}`);
} finally {
setIsLoading(false);
setProgress(0);
}
};
const handleReset = () => {
setFile(null);
setStatus('');
setProgress(0);
};
if (!isConnected) {
return null;
}
return (
<div className="w-full max-w-md">
<div
className={`border-2 border-dashed rounded-lg p-8 text-center cursor-pointer transition-colors ${
isDragging
? 'border-blue-500 bg-blue-50'
: 'border-gray-300 hover:border-gray-400'
}`}
onDragEnter={handleDragIn}
onDragLeave={handleDragOut}
onDragOver={handleDrag}
onDrop={handleDrop}
onClick={() => document.getElementById('fileInput')?.click()}
>
<input
id="fileInput"
type="file"
onChange={(e) => e.target.files && setFile(e.target.files[0])}
className="hidden"
/>
<div className="flex flex-col items-center gap-2">
<svg
className={`w-10 h-10 ${isDragging ? 'text-blue-500' : 'text-gray-400'}`}
fill="none"
stroke="currentColor"
viewBox="0 0 24 24"
>
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M7 16a4 4 0 01-.88-7.903A5 5 0 1115.9 6L16 6a5 5 0 011 9.9M15 13l-3-3m0 0l-3 3m3-3v12"
/>
</svg>
<p className="text-lg font-medium">
{file ? file.name : 'Drop your file here, or click to select'}
</p>
{!file && (
<p className="text-sm text-gray-500">
Drag and drop your file, or click to browse
</p>
)}
</div>
</div>
<div className="flex justify-center gap-4 mt-4">
<button
onClick={handleSubmit}
disabled={!file || isLoading}
className={`px-6 py-2 rounded-[20px] text-center border-2 border-black transition-all ${
!file || isLoading
? 'bg-gray-200 border-gray-200 text-gray-400 cursor-not-allowed'
: 'bg-black text-white hover:bg-white hover:text-black'
}`}
>
{isLoading ? 'Uploading...' : 'Submit'}
</button>
<button
onClick={handleReset}
disabled={!file || isLoading}
className={`px-6 py-2 rounded-[20px] text-center border-2 transition-all ${
!file || isLoading
? 'border-gray-200 text-gray-400 cursor-not-allowed'
: 'border-black text-black hover:bg-black hover:text-white'
}`}
>
Reset
</button>
</div>
{status && (
<div className="mt-4 text-center">
<p className={`text-sm ${
status.includes('❌') ? 'text-red-500' :
status.includes('✅') ? 'text-green-500' :
'text-gray-500'
}`}>
{status}
</p>
{isLoading && (
<div className="w-full bg-gray-200 rounded-full h-2.5 mt-2">
<div
className="bg-black h-2.5 rounded-full transition-all duration-500"
style={{ width: `${progress}%` }}
></div>
</div>
)}
</div>
)}
</div>
);
}