import { useState } from "react"; import { Search, PlusCircle, Code2 } from "lucide-react"; import { api } from "../api"; import type { ProductSummary } from "../types"; export default function Home({ onOpen, onContribute, onApi, }: { onOpen: (id: string) => void; onContribute: () => void; onApi: () => void; }) { const [q, setQ] = useState(""); const [brand, setBrand] = useState(""); const [country, setCountry] = useState(""); const [items, setItems] = useState([]); const [total, setTotal] = useState(0); const [searched, setSearched] = useState(false); const [loading, setLoading] = useState(false); const [error, setError] = useState(""); async function run(e?: React.FormEvent) { e?.preventDefault(); setLoading(true); setError(""); try { const res = await api.search(q.trim(), 1, 30, { brand: brand.trim() || undefined, country: country.trim() || undefined, }); setItems(res.items); setTotal(res.total); setSearched(true); } catch (err) { setError(err instanceof Error ? err.message : "搜索失败"); } finally { setLoading(false); } } return (

天工商品档案公共仓

输入商品名称或条码,检索客观、可溯源的商品资料。人人可查,人人可贡献。

setQ(e.target.value)} placeholder="例如:可乐、Nutella、5449000000996" className="flex-1 py-3 outline-none bg-transparent" />
setBrand(e.target.value)} placeholder="按品牌筛选(如 Ferrero)" className="flex-1 min-w-[14rem] bg-white border rounded-lg px-3 py-2 outline-none focus:ring-2 focus:ring-emerald-300" /> setCountry(e.target.value)} placeholder="按产地筛选(如 China)" className="flex-1 min-w-[14rem] bg-white border rounded-lg px-3 py-2 outline-none focus:ring-2 focus:ring-emerald-300" />
{error && (
{error}
)} {searched && (
共 {total} 条结果{q ? `(关键词:${q})` : ""}
{items.length === 0 ? (

没有找到相关商品。

) : (
    {items.map((p) => (
  • ))}
)}
)}
); }