"use client"; import { useMemo, useState } from "react"; import catalog from "./data/vehicleCatalog.json"; type Row = { year: number; make: string; model: string; trim: string }; function uniq(arr: T[]) { return Array.from(new Set(arr)); } export default function ShopByVehicle({ initial, onSearch, vertical = false, backgroundImage, }: { initial?: { year?: string; make?: string; model?: string; trim?: string }; onSearch: (sel: { year: string; make: string; model: string; trim: string }) => void; vertical?: boolean; backgroundImage?: string; }) { const rows = catalog as Row[]; const [year, setYear] = useState(initial?.year ?? ""); const [make, setMake] = useState(initial?.make ?? ""); const [model, setModel] = useState(initial?.model ?? ""); const [trim, setTrim] = useState(initial?.trim ?? ""); const years = useMemo(() => uniq(rows.map(r => String(r.year))).sort((a, b) => Number(b) - Number(a)), [rows]); const makes = useMemo(() => { if (!year) return []; return uniq(rows.filter(r => String(r.year) === year).map(r => r.make)).sort(); }, [rows, year]); const models = useMemo(() => { if (!year || !make) return []; return uniq(rows.filter(r => String(r.year) === year && r.make === make).map(r => r.model)).sort(); }, [rows, year, make]); const trims = useMemo(() => { if (!year || !make || !model) return []; return uniq(rows.filter(r => String(r.year) === year && r.make === make && r.model === model).map(r => r.trim)).sort(); }, [rows, year, make, model]); function resetDownstream(from: "year" | "make" | "model") { if (from === "year") { setMake(""); setModel(""); setTrim(""); } if (from === "make") { setModel(""); setTrim(""); } if (from === "model") { setTrim(""); } } const wrapStyle = backgroundImage ? { backgroundImage: `url(${backgroundImage})`, backgroundSize: "cover", backgroundPosition: "center" } : {}; const isActive = Boolean(year && make && model && trim); return (
Note: vehicle options come from data/vehicleCatalog.json (top 12 list for 1970-1991, full US catalog for 1992-present).
); }