File size: 6,992 Bytes
7f6dd09 | 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 | import { useState } from "react";
import { Upload, X, Check, FileText, AlertCircle } from "lucide-react";
const SYMBOLS = {
annual_report: { icon: FileText, color: "#f97316" },
gst_3b: { icon: FileText, color: "#22c55e" },
gst_2a: { icon: FileText, color: "#10b981" },
gst_1: { icon: FileText, color: "#34d399" },
gst_filing: { icon: FileText, color: "#22c55e" },
bank_statement: { icon: FileText, color: "#3b82f6" },
itr: { icon: FileText, color: "#eab308" },
mca: { icon: FileText, color: "#8b5cf6" },
};
function formatSize(bytes) {
if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + " KB";
return (bytes / (1024 * 1024)).toFixed(1) + " MB";
}
function truncate(str, max) {
return str.length <= max ? str : str.slice(0, max) + "…";
}
export default function FileDropZone({
fileType,
label,
description,
acceptedFormats,
required,
file,
onFileSelect,
}) {
const [isDragging, setIsDragging] = useState(false);
const [extError, setExtError] = useState(null);
const exts = acceptedFormats.split(",").map((e) => e.trim().toLowerCase());
function validate(f) {
const name = f.name.toLowerCase();
return exts.some((ext) => name.endsWith(ext));
}
function handleDragOver(e) {
e.preventDefault();
setIsDragging(true);
}
function handleDragLeave() {
setIsDragging(false);
}
function handleDrop(e) {
e.preventDefault();
setIsDragging(false);
const dropped = e.dataTransfer.files[0];
if (!dropped) return;
if (!validate(dropped)) {
setExtError(`Invalid file. Accepted: ${acceptedFormats}`);
return;
}
setExtError(null);
onFileSelect(dropped);
}
function handleInputChange(e) {
const picked = e.target.files[0];
if (!picked) return;
if (!validate(picked)) {
setExtError(`Invalid file. Accepted: ${acceptedFormats}`);
return;
}
setExtError(null);
onFileSelect(picked);
e.target.value = "";
}
function openPicker() {
document.getElementById("file-input-" + fileType).click();
}
const { icon: Icon, color } = SYMBOLS[fileType] || {
icon: FileText,
color: "#7a7a85",
};
const zoneClass = isDragging
? "dropzone dragging"
: file
? "dropzone uploaded"
: "dropzone";
return (
<div style={{ display: "flex", flexDirection: "column", height: "100%" }}>
<div
className={zoneClass}
onDragOver={handleDragOver}
onDragLeave={handleDragLeave}
onDrop={handleDrop}
onClick={openPicker}
style={{ flex: 1, minHeight: "160px", display: "flex", flexDirection: "column", justifyContent: "center", position: "relative" }}
>
<input
id={"file-input-" + fileType}
type="file"
accept={acceptedFormats}
style={{ display: "none" }}
onChange={handleInputChange}
/>
{file ? (
<div className="flex items-center gap-md" style={{ textAlign: "left", width: "100%", position: "absolute", left: 0, padding: "0 16px" }}>
<div
style={{
width: "36px",
height: "36px",
borderRadius: "var(--radius-sm)",
background: "var(--success-subtle)",
display: "flex",
alignItems: "center",
justifyContent: "center",
flexShrink: 0,
}}
>
<Check size={18} style={{ color: "var(--success)" }} />
</div>
<div style={{ flex: 1, minWidth: 0, overflow: "hidden" }}>
<p
style={{
fontSize: "13px",
fontWeight: 600,
color: "var(--text-primary)",
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis"
}}
>
{file.name}
</p>
<p style={{ fontSize: "11px", color: "var(--text-muted)", marginTop: "2px" }}>
{formatSize(file.size)}
</p>
</div>
<button
onClick={(e) => {
e.stopPropagation();
onFileSelect(null);
}}
style={{
width: "28px",
height: "28px",
borderRadius: "var(--radius-full)",
background: "transparent",
border: "none",
cursor: "pointer",
display: "flex",
alignItems: "center",
justifyContent: "center",
color: "var(--text-muted)",
transition: "all var(--transition-fast)",
flexShrink: 0,
}}
onMouseEnter={(e) => {
e.currentTarget.style.background = "var(--danger-subtle)";
e.currentTarget.style.color = "var(--danger)";
}}
onMouseLeave={(e) => {
e.currentTarget.style.background = "transparent";
e.currentTarget.style.color = "var(--text-muted)";
}}
>
<X size={14} />
</button>
</div>
) : (
<div className="flex flex-col items-center gap-sm" style={{ padding: "8px 0" }}>
<div
style={{
width: "40px",
height: "40px",
borderRadius: "var(--radius-md)",
background: color + "15",
display: "flex",
alignItems: "center",
justifyContent: "center",
}}
>
<Icon size={20} style={{ color }} />
</div>
<div style={{ textAlign: "center" }}>
<p
style={{
fontSize: "13px",
fontWeight: 600,
color: "var(--text-primary)",
}}
>
{label}
</p>
<p style={{ fontSize: "11px", color: "var(--text-muted)", marginTop: "2px" }}>
{description}
</p>
<p
className="mono"
style={{ fontSize: "10px", color: "var(--text-muted)", marginTop: "4px" }}
>
{acceptedFormats}
</p>
</div>
<span
className={required ? "badge badge-danger" : "badge badge-neutral"}
style={{ fontSize: "10px" }}
>
{required ? "Required" : "Optional"}
</span>
</div>
)}
</div>
{extError && (
<p
className="flex items-center gap-xs"
style={{
color: "var(--danger)",
fontSize: "11px",
marginTop: "6px",
}}
>
<AlertCircle size={12} /> {extError}
</p>
)}
</div>
);
}
|