import { useEffect, useState } from "react"; import { Search, X } from "lucide-react"; import { customerGetList, rowsOf } from "../lib/api/bos"; import { useLoad } from "../lib/useLoad"; import { SearchInput } from "./ui/search-input"; export function PartnerCustomerPicker({ selectedIds, blockedIds, onChange, disabled }: { selectedIds: string[]; blockedIds: ReadonlySet; onChange: (ids: string[]) => void; disabled: boolean; }) { const [search, setSearch] = useState(""); const [knownNames, setKnownNames] = useState(new Map()); const keyword = search.trim(); const results = useLoad(async () => { const list = await customerGetList({ page: 0, page_size: 20, keyword: keyword || undefined }); return { keyword, customers: rowsOf(list).map((row) => ({ id: row.customer_id, name: row.customer_name || row.customer_id })), }; }, [keyword]); useEffect(() => { const matches = results.data?.customers; if (!matches?.length) return; setKnownNames((current) => { const next = new Map(current); for (const { id, name } of matches) next.set(id, name); return next; }); }, [results.data]); const matches = results.data?.keyword === keyword ? results.data.customers : []; const loading = results.loading || (!results.error && results.data?.keyword !== keyword); return (

指定客户

可选客户

{loading ?

正在查找客户…

: results.error ? (

{results.error.message || "客户搜索失败。"}

) : matches.length ? (
可选客户
    {matches.map(({ id, name }) => { const selected = selectedIds.includes(id); const blocked = blockedIds.has(id) && !selected; return (
  • ); })}
) :

{keyword ? "未找到匹配的客户。" : "暂无可选客户。"}

}

已选客户

{selectedIds.length}
{selectedIds.length ? (
    {selectedIds.map((id) => { const name = knownNames.get(id) ?? id; return (
  • {name}{name !== id && {id}}
  • ); })}
) :

还没有指定客户。

}
); }