2026-02-26 17:48:16 +01:00
|
|
|
|
'use client';
|
|
|
|
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
|
import { Upload, X, FileImage, HardDrive } from 'lucide-react';
|
|
|
|
|
|
import { cn } from '@/lib/utils/cn';
|
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
|
|
|
|
|
|
|
export interface FaviconFileUploadProps {
|
|
|
|
|
|
onFileSelect: (file: File) => void;
|
|
|
|
|
|
onFileRemove: () => void;
|
|
|
|
|
|
selectedFile?: File | null;
|
|
|
|
|
|
disabled?: boolean;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function FaviconFileUpload({
|
|
|
|
|
|
onFileSelect,
|
|
|
|
|
|
onFileRemove,
|
|
|
|
|
|
selectedFile,
|
|
|
|
|
|
disabled = false,
|
|
|
|
|
|
}: FaviconFileUploadProps) {
|
|
|
|
|
|
const [isDragging, setIsDragging] = React.useState(false);
|
|
|
|
|
|
const [dimensions, setDimensions] = React.useState<string | null>(null);
|
|
|
|
|
|
const fileInputRef = React.useRef<HTMLInputElement>(null);
|
|
|
|
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
|
if (selectedFile) {
|
|
|
|
|
|
const img = new Image();
|
|
|
|
|
|
img.onload = () => {
|
|
|
|
|
|
setDimensions(`${img.width} × ${img.height}`);
|
|
|
|
|
|
URL.revokeObjectURL(img.src);
|
|
|
|
|
|
};
|
|
|
|
|
|
img.src = URL.createObjectURL(selectedFile);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
setDimensions(null);
|
|
|
|
|
|
}
|
|
|
|
|
|
}, [selectedFile]);
|
|
|
|
|
|
|
|
|
|
|
|
const handleDragEnter = (e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
if (!disabled) setIsDragging(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDragLeave = (e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
setIsDragging(false);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDragOver = (e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleDrop = (e: React.DragEvent) => {
|
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
|
setIsDragging(false);
|
|
|
|
|
|
|
|
|
|
|
|
if (disabled) return;
|
|
|
|
|
|
|
|
|
|
|
|
const files = Array.from(e.dataTransfer.files);
|
|
|
|
|
|
if (files.length > 0 && files[0].type.startsWith('image/')) {
|
|
|
|
|
|
onFileSelect(files[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleFileInput = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
|
const files = Array.from(e.target.files || []);
|
|
|
|
|
|
if (files.length > 0 && files[0].type.startsWith('image/')) {
|
|
|
|
|
|
onFileSelect(files[0]);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
const handleClick = () => {
|
|
|
|
|
|
if (!disabled) fileInputRef.current?.click();
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="w-full space-y-3">
|
|
|
|
|
|
<input
|
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
|
type="file"
|
|
|
|
|
|
className="hidden"
|
|
|
|
|
|
accept="image/*"
|
|
|
|
|
|
onChange={handleFileInput}
|
|
|
|
|
|
disabled={disabled}
|
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
|
|
{selectedFile ? (
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<div className="border border-border rounded-xl p-4 bg-card/50 backdrop-blur-sm">
|
|
|
|
|
|
<div className="flex items-start gap-3">
|
|
|
|
|
|
<div className="p-2 bg-primary/10 rounded-lg shrink-0">
|
|
|
|
|
|
<FileImage className="h-5 w-5 text-primary" />
|
2026-02-26 17:48:16 +01:00
|
|
|
|
</div>
|
|
|
|
|
|
<div className="flex-1 min-w-0">
|
|
|
|
|
|
<div className="flex items-start justify-between gap-2">
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<p className="text-sm font-medium text-foreground truncate" title={selectedFile.name}>
|
2026-02-26 17:48:16 +01:00
|
|
|
|
{selectedFile.name}
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
variant="ghost"
|
|
|
|
|
|
size="icon-xs"
|
|
|
|
|
|
onClick={onFileRemove}
|
|
|
|
|
|
disabled={disabled}
|
2026-02-27 12:35:02 +01:00
|
|
|
|
className="rounded-full hover:bg-destructive/10 hover:text-destructive shrink-0"
|
2026-02-26 17:48:16 +01:00
|
|
|
|
>
|
|
|
|
|
|
<X className="h-3.5 w-3.5" />
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<div className="mt-1.5 flex gap-3 text-[10px] text-muted-foreground">
|
|
|
|
|
|
<div className="flex items-center gap-1">
|
2026-02-26 17:48:16 +01:00
|
|
|
|
<HardDrive className="h-3 w-3" />
|
|
|
|
|
|
<span>{(selectedFile.size / 1024).toFixed(1)} KB</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{dimensions && (
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<div className="flex items-center gap-1">
|
2026-02-26 17:48:16 +01:00
|
|
|
|
<FileImage className="h-3 w-3" />
|
|
|
|
|
|
<span>{dimensions}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div
|
|
|
|
|
|
onClick={handleClick}
|
|
|
|
|
|
onDragEnter={handleDragEnter}
|
|
|
|
|
|
onDragOver={handleDragOver}
|
|
|
|
|
|
onDragLeave={handleDragLeave}
|
|
|
|
|
|
onDrop={handleDrop}
|
|
|
|
|
|
className={cn(
|
2026-02-27 12:35:02 +01:00
|
|
|
|
'border-2 border-dashed rounded-xl p-8 text-center cursor-pointer transition-all duration-200',
|
|
|
|
|
|
'hover:border-primary/40 hover:bg-primary/5',
|
2026-02-26 17:48:16 +01:00
|
|
|
|
{
|
|
|
|
|
|
'border-primary bg-primary/10 scale-[0.98]': isDragging,
|
2026-02-27 12:35:02 +01:00
|
|
|
|
'border-border/50': !isDragging,
|
2026-02-26 17:48:16 +01:00
|
|
|
|
'opacity-50 cursor-not-allowed': disabled,
|
|
|
|
|
|
}
|
|
|
|
|
|
)}
|
|
|
|
|
|
>
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<div className="bg-primary/10 w-12 h-12 rounded-full flex items-center justify-center mx-auto mb-3">
|
|
|
|
|
|
<Upload className="h-6 w-6 text-primary" />
|
2026-02-26 17:48:16 +01:00
|
|
|
|
</div>
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<p className="text-sm font-medium text-foreground mb-0.5">
|
2026-02-26 17:48:16 +01:00
|
|
|
|
Drop icon source here
|
|
|
|
|
|
</p>
|
2026-02-27 12:35:02 +01:00
|
|
|
|
<p className="text-[10px] text-muted-foreground">
|
|
|
|
|
|
512x512 PNG or SVG recommended
|
2026-02-26 17:48:16 +01:00
|
|
|
|
</p>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|