import { useState } from "react"; import { Search, PlusCircle, ScanBarcode, ShieldCheck, ArrowRight } from "lucide-react"; import { api } from "../api"; import type { ProductSummary } from "../types"; const EXAMPLES = ["可乐", "Nutella", "牛奶", "5449000000996"]; export default function Home({ onOpen, onContribute, }: { onOpen: (id: string) => void; onContribute: () => void; }) { const [q, setQ] = 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(term?: string, e?: React.FormEvent) { e?.preventDefault(); const keyword = (term ?? q).trim(); if (term !== undefined) setQ(term); setLoading(true); setError(""); try { const res = await api.search(keyword, 1, 30); setItems(res.items); setTotal(res.total); setSearched(true); } catch (err) { setError(err instanceof Error ? err.message : "搜索失败"); } finally { setLoading(false); } } return (
公益 · 开放 · 可溯源

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

run(undefined, e)} className="mx-auto mt-7 flex w-full max-w-2xl flex-col gap-2 sm:flex-row" >
setQ(e.target.value)} placeholder="例如:可乐、Nutella、5449000000996" className="flex-1 bg-transparent py-3.5 outline-none" />
试试: {EXAMPLES.map((ex) => ( ))}
{error && (
{error}
)} {searched && (
{total} 条结果 {q ? `(关键词:${q})` : ""}
{items.length === 0 ? (

没有找到相关商品。

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