All files / src/components/rooms MediaUploadArea.tsx

100% Statements 3/3
100% Branches 16/16
100% Functions 2/2
100% Lines 3/3

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                                        14x   14x                       3x                                                                                            
import { useRef, type ChangeEvent, type DragEvent } from "react";
import { Upload } from "lucide-react";
 
type MediaUploadAreaProps = {
	isUploading: boolean;
	isDragOver: boolean;
	onDragOver: (e: DragEvent<HTMLButtonElement>) => void;
	onDragLeave: (e: DragEvent<HTMLButtonElement>) => void;
	onDrop: (e: DragEvent<HTMLButtonElement>) => void;
	onFileSelect: (e: ChangeEvent<HTMLInputElement>) => void;
};
 
export function MediaUploadArea({
	isUploading,
	isDragOver,
	onDragOver,
	onDragLeave,
	onDrop,
	onFileSelect,
}: MediaUploadAreaProps) {
	const fileInputRef = useRef<HTMLInputElement>(null);
 
	return (
		<button
			type="button"
			aria-label="Upload media - tap or drag and drop"
			className={`mb-4 w-full border-2 border-dashed rounded-lg transition-colors cursor-pointer text-left ${
				isDragOver
					? "border-primary-500 bg-primary-50"
					: "border-border-default hover:border-primary-400 hover:bg-background-muted"
			}`}
			onDragOver={onDragOver}
			onDragLeave={onDragLeave}
			onDrop={onDrop}
			onClick={() => !isUploading && fileInputRef.current?.click()}
		>
			<input
				type="file"
				ref={fileInputRef}
				onChange={onFileSelect}
				accept="image/jpeg,image/png,image/webp,image/gif,image/avif,video/mp4,video/webm,video/quicktime"
				multiple
				className="hidden"
			/>
			<div className="p-4 flex items-center gap-4">
				<div
					className={`w-12 h-12 rounded-lg flex items-center justify-center flex-shrink-0 ${
						isDragOver ? "bg-primary-100" : "bg-background-muted"
					}`}
				>
					{isUploading ? (
						<div className="animate-spin rounded-full h-6 w-6 border-b-2 border-primary-600" />
					) : (
						<Upload
							className={`h-6 w-6 ${isDragOver ? "text-primary-500" : "text-foreground-subtle"}`}
						/>
					)}
				</div>
				<div className="flex-1 min-w-0">
					{isUploading ? (
						<p className="text-sm font-medium text-primary-600">Uploading...</p>
					) : (
						<>
							<p
								className={`text-sm font-medium ${isDragOver ? "text-primary-600" : "text-foreground-default"}`}
							>
								{isDragOver
									? "Drop files here"
									: "Tap to upload or drag and drop"}
							</p>
							<p className="text-xs text-foreground-muted">
								Images (PNG, JPG, WebP, GIF) or Videos (MP4, WebM, MOV)
							</p>
						</>
					)}
				</div>
			</div>
		</button>
	);
}