phase-2: asset register, detail overview, asset picker, attachments

This commit is contained in:
Atharv1705
2026-11-12 12:38:50 +05:30
parent e58232a657
commit ee437e3adf
27 changed files with 4442 additions and 14 deletions
+229
View File
@@ -0,0 +1,229 @@
import { useState, useEffect, useRef } from "react";
import { Search, X, ShieldCheck, Ban } from "lucide-react";
import { assetsApi, type AssetLookupItem } from "@/api/assets";
import { formatDate } from "@/lib/format";
import { CoverageBadge, AssetStatusBadge } from "./StatusBadge";
interface AssetPickerProps {
selectedAsset: AssetLookupItem | null;
onSelect: (asset: AssetLookupItem | null) => void;
departmentId?: number;
disabled?: boolean;
placeholder?: string;
}
export function AssetPicker({
selectedAsset,
onSelect,
departmentId,
disabled = false,
placeholder = "Search by asset code, equipment name, or serial no...",
}: AssetPickerProps) {
const [query, setQuery] = useState("");
const [debouncedQuery, setDebouncedQuery] = useState("");
const [results, setResults] = useState<AssetLookupItem[]>([]);
const [loading, setLoading] = useState(false);
const [open, setOpen] = useState(false);
const containerRef = useRef<HTMLDivElement>(null);
useEffect(() => {
const timer = setTimeout(() => {
setDebouncedQuery(query);
}, 250);
return () => clearTimeout(timer);
}, [query]);
useEffect(() => {
if (!open || disabled) return;
let cancelled = false;
setLoading(true);
assetsApi
.lookup({ q: debouncedQuery, department_id: departmentId, limit: 25 })
.then((res) => {
if (!cancelled) setResults(res.items);
})
.catch(() => {
if (!cancelled) setResults([]);
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [debouncedQuery, departmentId, open, disabled]);
useEffect(() => {
function handleOutsideClick(e: MouseEvent) {
if (containerRef.current && !containerRef.current.contains(e.target as Node)) {
setOpen(false);
}
}
document.addEventListener("mousedown", handleOutsideClick);
return () => document.removeEventListener("mousedown", handleOutsideClick);
}, []);
if (selectedAsset) {
const isUnderWarranty =
selectedAsset.warranty_status === "active" || selectedAsset.warranty_status === "expiring";
const isCondemned = selectedAsset.status === "condemned";
return (
<div className="rounded-lg border border-blue-200 bg-blue-50/40 p-4 space-y-3">
<div className="flex items-start justify-between gap-2">
<div>
<div className="flex items-center gap-2 flex-wrap">
<span className="font-mono text-sm font-bold text-[#1e3a8a]">
{selectedAsset.asset_code}
</span>
<span className="font-semibold text-gray-900">{selectedAsset.name}</span>
<AssetStatusBadge status={selectedAsset.status} />
{isUnderWarranty && (
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-emerald-100 text-emerald-800 border border-emerald-300">
<ShieldCheck className="h-3 w-3" />
Under Warranty
</span>
)}
{isCondemned && (
<span className="inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-xs font-medium bg-red-100 text-red-800 border border-red-300">
<Ban className="h-3 w-3" />
Condemned Asset
</span>
)}
</div>
{selectedAsset.serial_no && (
<div className="text-xs text-gray-500 mt-0.5">
Serial No: <span className="font-mono">{selectedAsset.serial_no}</span>
</div>
)}
</div>
{!disabled && (
<button
type="button"
onClick={() => {
onSelect(null);
setQuery("");
}}
aria-label="Clear selected asset"
className="inline-flex items-center gap-1 text-xs text-gray-600 hover:text-gray-900 px-2 py-1 rounded border border-gray-200 bg-white"
>
<X className="h-3.5 w-3.5" />
Change
</button>
)}
</div>
<div className="grid grid-cols-2 sm:grid-cols-3 gap-3 text-xs">
<div>
<div className="text-gray-500">Category</div>
<div className="font-medium text-gray-800">{selectedAsset.category_name || "—"}</div>
</div>
<div>
<div className="text-gray-500">Department</div>
<div className="font-medium text-gray-800">{selectedAsset.department_name || "—"}</div>
</div>
<div>
<div className="text-gray-500">Location</div>
<div className="font-medium text-gray-800">{selectedAsset.location_name || "—"}</div>
</div>
<div>
<div className="text-gray-500">Warranty</div>
<div className="mt-0.5 flex items-center gap-1.5 flex-wrap">
<CoverageBadge status={selectedAsset.warranty_status} />
{selectedAsset.warranty_end_date && (
<span className="text-gray-600">
till {formatDate(selectedAsset.warranty_end_date)}
</span>
)}
</div>
</div>
<div>
<div className="text-gray-500">Contract</div>
<div className="font-medium text-gray-800">
{selectedAsset.contract_summary ||
(selectedAsset.contract_type
? `${selectedAsset.contract_type} (${selectedAsset.contract_status})`
: "None")}
</div>
</div>
<div>
<div className="text-gray-500">Insurance</div>
<div className="font-medium text-gray-800">
{selectedAsset.insurance_summary || selectedAsset.insurance_status || "None"}
</div>
</div>
</div>
</div>
);
}
return (
<div ref={containerRef} className="relative">
<div className="relative">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
<input
type="text"
disabled={disabled}
value={query}
onFocus={() => setOpen(true)}
onChange={(e) => {
setQuery(e.target.value);
setOpen(true);
}}
placeholder={placeholder}
className="w-full pl-9 pr-4 py-2 text-sm border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 bg-white disabled:bg-gray-100"
/>
</div>
{open && !disabled && (
<div className="absolute left-0 right-0 mt-1 max-h-72 overflow-y-auto bg-white border border-gray-200 rounded-md shadow-lg z-30 divide-y divide-gray-100">
{loading ? (
<div className="px-4 py-3 text-xs text-gray-500">Searching assets...</div>
) : results.length === 0 ? (
<div className="px-4 py-3 text-xs text-gray-500">No matching assets found.</div>
) : (
results.map((item) => {
const underWarranty =
item.warranty_status === "active" || item.warranty_status === "expiring";
const condemned = item.status === "condemned";
return (
<button
key={item.id}
type="button"
onClick={() => {
onSelect(item);
setOpen(false);
}}
className="w-full text-left px-3 py-2.5 hover:bg-blue-50/60 transition-colors flex items-center justify-between gap-2"
>
<div className="min-w-0">
<div className="text-sm font-medium text-gray-900 truncate">
<span className="font-mono text-[#1e3a8a]">{item.asset_code}</span> —{" "}
{item.name}
{item.serial_no ? ` (${item.serial_no})` : ""}
</div>
<div className="text-xs text-gray-500 truncate">
{item.department_name || "—"} • {item.location_name || "—"}
</div>
</div>
<div className="flex items-center gap-1.5 shrink-0">
{underWarranty && (
<span className="px-2 py-0.5 rounded-full text-[11px] font-medium bg-emerald-100 text-emerald-800">
Under Warranty
</span>
)}
{condemned && (
<span className="px-2 py-0.5 rounded-full text-[11px] font-medium bg-red-100 text-red-800">
Condemned
</span>
)}
</div>
</button>
);
})
)}
</div>
)}
</div>
);
}