File size: 9,153 Bytes
c0ee8b0 |
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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
import { useState, useRef, useCallback } from "react";
import { Upload, X, File, AlertCircle, Link as LinkIcon } from "lucide-react";
import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { FileConstraints } from "@/server/types/plugin";
interface FileUploadProps {
name: string;
description: string;
fileConstraints?: FileConstraints;
acceptUrl?: boolean;
onFileChange: (file: File | null) => void;
onUrlChange?: (url: string) => void;
disabled?: boolean;
}
export function FileUpload({
name,
description,
fileConstraints,
acceptUrl = false,
onFileChange,
onUrlChange,
disabled = false,
}: FileUploadProps) {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [fileUrl, setFileUrl] = useState<string>("");
const [error, setError] = useState<string | null>(null);
const [dragActive, setDragActive] = useState(false);
const [mode, setMode] = useState<"upload" | "url">("upload");
const fileInputRef = useRef<HTMLInputElement>(null);
const formatFileSize = (bytes: number): string => {
if (bytes === 0) return "0 Bytes";
const k = 1024;
const sizes = ["Bytes", "KB", "MB", "GB"];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return Math.round(bytes / Math.pow(k, i) * 100) / 100 + " " + sizes[i];
};
const validateFile = (file: File): string | null => {
if (!fileConstraints) return null;
if (fileConstraints.maxSize && file.size > fileConstraints.maxSize) {
return `File size (${formatFileSize(file.size)}) exceeds maximum allowed size (${formatFileSize(fileConstraints.maxSize)})`;
}
if (fileConstraints.acceptedTypes && fileConstraints.acceptedTypes.length > 0) {
if (!fileConstraints.acceptedTypes.includes(file.type)) {
return `File type ${file.type} is not accepted. Allowed types: ${fileConstraints.acceptedTypes.join(", ")}`;
}
}
if (fileConstraints.acceptedExtensions && fileConstraints.acceptedExtensions.length > 0) {
const extension = "." + file.name.split(".").pop()?.toLowerCase();
if (!fileConstraints.acceptedExtensions.includes(extension)) {
return `File extension ${extension} is not accepted. Allowed extensions: ${fileConstraints.acceptedExtensions.join(", ")}`;
}
}
return null;
};
const handleFileSelect = useCallback((file: File) => {
const validationError = validateFile(file);
if (validationError) {
setError(validationError);
setSelectedFile(null);
onFileChange(null);
return;
}
setError(null);
setSelectedFile(file);
onFileChange(file);
}, [fileConstraints, onFileChange]);
const handleFileChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
handleFileSelect(file);
}
};
const handleDrag = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
if (e.type === "dragenter" || e.type === "dragover") {
setDragActive(true);
} else if (e.type === "dragleave") {
setDragActive(false);
}
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
e.stopPropagation();
setDragActive(false);
if (e.dataTransfer.files && e.dataTransfer.files[0]) {
handleFileSelect(e.dataTransfer.files[0]);
}
};
const handleRemoveFile = () => {
setSelectedFile(null);
setError(null);
onFileChange(null);
if (fileInputRef.current) {
fileInputRef.current.value = "";
}
};
const handleUrlChange = (url: string) => {
setFileUrl(url);
if (onUrlChange) {
onUrlChange(url);
}
};
const getAcceptAttribute = (): string | undefined => {
if (!fileConstraints) return undefined;
const types = [];
if (fileConstraints.acceptedTypes) {
types.push(...fileConstraints.acceptedTypes);
}
if (fileConstraints.acceptedExtensions) {
types.push(...fileConstraints.acceptedExtensions);
}
return types.length > 0 ? types.join(",") : undefined;
};
const renderContent = () => {
if (!acceptUrl) {
return (
<div className="space-y-3">
{renderUploadArea()}
{renderConstraints()}
</div>
);
}
return (
<Tabs value={mode} onValueChange={(v) => setMode(v as "upload" | "url")} className="w-full">
<TabsList className="grid w-full grid-cols-2 bg-black/30">
<TabsTrigger value="upload" className="data-[state=active]:bg-purple-500/20">
<Upload className="w-4 h-4 mr-2" />
Upload File
</TabsTrigger>
<TabsTrigger value="url" className="data-[state=active]:bg-purple-500/20">
<LinkIcon className="w-4 h-4 mr-2" />
Use URL
</TabsTrigger>
</TabsList>
<TabsContent value="upload" className="mt-3 space-y-3">
{renderUploadArea()}
{renderConstraints()}
</TabsContent>
<TabsContent value="url" className="mt-3 space-y-3">
<Input
type="url"
placeholder="https://example.com/file.jpg"
value={fileUrl}
onChange={(e) => handleUrlChange(e.target.value)}
disabled={disabled}
className="bg-black/50 border-white/10 text-white focus:border-purple-500"
/>
<p className="text-xs text-gray-500">
Enter the URL of the file you want to process
</p>
{renderConstraints()}
</TabsContent>
</Tabs>
);
};
const renderUploadArea = () => (
<>
<div
className={`relative border-2 border-dashed rounded-lg p-8 transition-all ${
dragActive
? "border-purple-500 bg-purple-500/10"
: "border-white/20 bg-black/30"
} ${disabled ? "opacity-50 cursor-not-allowed" : "cursor-pointer hover:border-purple-500/50"}`}
onDragEnter={handleDrag}
onDragLeave={handleDrag}
onDragOver={handleDrag}
onDrop={handleDrop}
onClick={() => !disabled && fileInputRef.current?.click()}
>
<input
ref={fileInputRef}
type="file"
onChange={handleFileChange}
accept={getAcceptAttribute()}
disabled={disabled}
className="hidden"
/>
{selectedFile ? (
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="p-2 bg-purple-500/20 rounded">
<File className="w-6 h-6 text-purple-400" />
</div>
<div>
<p className="text-sm font-medium text-white">{selectedFile.name}</p>
<p className="text-xs text-gray-400">{formatFileSize(selectedFile.size)}</p>
</div>
</div>
<Button
type="button"
variant="ghost"
size="sm"
onClick={(e) => {
e.stopPropagation();
handleRemoveFile();
}}
disabled={disabled}
className="text-red-400 hover:text-red-300 hover:bg-red-500/10"
>
<X className="w-4 h-4" />
</Button>
</div>
) : (
<div className="text-center">
<Upload className="w-12 h-12 text-gray-400 mx-auto mb-3" />
<p className="text-sm text-gray-300 mb-1">
{dragActive ? "Drop file here" : "Click to upload or drag and drop"}
</p>
<p className="text-xs text-gray-500">{description}</p>
</div>
)}
</div>
{error && (
<div className="flex items-start gap-2 p-3 bg-red-500/10 border border-red-500/30 rounded-lg">
<AlertCircle className="w-5 h-5 text-red-400 flex-shrink-0 mt-0.5" />
<p className="text-sm text-red-300">{error}</p>
</div>
)}
</>
);
const renderConstraints = () => {
if (!fileConstraints) return null;
return (
<div className="space-y-2">
{fileConstraints.maxSize && (
<p className="text-xs text-gray-500">
• Max file size: {formatFileSize(fileConstraints.maxSize)}
</p>
)}
{fileConstraints.acceptedTypes && fileConstraints.acceptedTypes.length > 0 && (
<p className="text-xs text-gray-500">
• Accepted types: {fileConstraints.acceptedTypes.join(", ")}
</p>
)}
{fileConstraints.acceptedExtensions && fileConstraints.acceptedExtensions.length > 0 && (
<p className="text-xs text-gray-500">
• Accepted extensions: {fileConstraints.acceptedExtensions.join(", ")}
</p>
)}
</div>
);
};
return (
<div className="space-y-2">
<label className="block text-sm text-gray-300">
{name}
<span className="text-xs text-gray-500 ml-2">(file)</span>
</label>
{renderContent()}
</div>
);
} |