Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | /**
* File upload input component
* Handles file selection, drag-and-drop, and upload to API
*/
import { useState, useRef } from 'react';
import { Button } from '@/components/ui/button';
import { Upload, X, File, Loader2 } from 'lucide-react';
import { uploadFile } from '@/services/files.service';
import type { FileMetadata } from '@/services/files.service';
import { handleApiError } from '@/lib/api';
interface FileUploadInputProps {
value: FileMetadata | null;
onChange: (value: FileMetadata | null) => void;
disabled?: boolean;
fieldName: string;
}
export default function FileUploadInput({
value,
onChange,
disabled = false,
fieldName,
}: FileUploadInputProps) {
const [isDragging, setIsDragging] = useState(false);
const [isUploading, setIsUploading] = useState(false);
const [error, setError] = useState<string | null>(null);
const fileInputRef = useRef<HTMLInputElement>(null);
const handleFileSelect = async (file: File) => {
setError(null);
setIsUploading(true);
try {
const metadata = await uploadFile(file);
onChange(metadata);
} catch (err) {
setError(handleApiError(err));
} finally {
setIsUploading(false);
}
};
const handleFileInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const file = e.target.files?.[0];
if (file) {
handleFileSelect(file);
}
};
const handleDragOver = (e: React.DragEvent) => {
e.preventDefault();
if (!disabled && !isUploading) {
setIsDragging(true);
}
};
const handleDragLeave = (e: React.DragEvent) => {
e.preventDefault();
setIsDragging(false);
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
setIsDragging(false);
if (disabled || isUploading) return;
const file = e.dataTransfer.files?.[0];
if (file) {
handleFileSelect(file);
}
};
const handleRemove = () => {
onChange(null);
if (fileInputRef.current) {
fileInputRef.current.value = '';
}
};
const handleBrowseClick = () => {
fileInputRef.current?.click();
};
// Parse value if it's a JSON string
const fileMetadata = value
? typeof value === 'string'
? JSON.parse(value)
: value
: null;
return (
<div className="space-y-2">
<input
ref={fileInputRef}
type="file"
onChange={handleFileInputChange}
disabled={disabled || isUploading}
className="hidden"
aria-label={`Upload file for ${fieldName}`}
/>
{fileMetadata ? (
// Display uploaded file
<div className="border rounded-lg p-3 bg-muted/50">
<div className="flex items-start justify-between gap-2">
<div className="flex items-start gap-3 flex-1 min-w-0">
<File className="h-5 w-5 text-muted-foreground shrink-0 mt-0.5" />
<div className="text-sm space-y-1 flex-1 min-w-0">
<div className="font-medium truncate">{fileMetadata.filename}</div>
<div className="text-muted-foreground text-xs space-y-0.5">
<div>Size: {(fileMetadata.size / 1024).toFixed(2)} KB</div>
<div>Type: {fileMetadata.mime_type}</div>
</div>
</div>
</div>
{!disabled && (
<Button
type="button"
variant="ghost"
size="sm"
onClick={handleRemove}
className="shrink-0"
>
<X className="h-4 w-4" />
</Button>
)}
</div>
</div>
) : (
// Upload area
<div
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
className={`
border-2 border-dashed rounded-lg p-6 text-center transition-colors
${isDragging ? 'border-primary bg-primary/5' : 'border-muted-foreground/25'}
${disabled || isUploading ? 'opacity-50 cursor-not-allowed' : 'cursor-pointer hover:border-primary/50'}
`}
onClick={!disabled && !isUploading ? handleBrowseClick : undefined}
>
{isUploading ? (
<div className="flex flex-col items-center gap-2">
<Loader2 className="h-8 w-8 text-primary animate-spin" />
<p className="text-sm text-muted-foreground">Uploading...</p>
</div>
) : (
<div className="flex flex-col items-center gap-2">
<Upload className="h-8 w-8 text-muted-foreground" />
<div className="space-y-1">
<p className="text-sm font-medium">
Drop file here or click to browse
</p>
<p className="text-xs text-muted-foreground">
Max 10MB • Supported: Images, PDF, Text, JSON
</p>
</div>
</div>
)}
</div>
)}
{error && (
<p className="text-sm text-destructive">{error}</p>
)}
</div>
);
}
|