import { IconClose, IconSearch, IconTickCircle } from '@douyinfe/semi-icons'; import { Button, Input, Tag } from '@douyinfe/semi-ui'; import { useQuery } from '@tanstack/react-query'; import { useDeferredValue, useMemo, useState } from 'react'; import { api } from '../../api/client'; import type { VehicleRow } from '../../api/types'; import { InlineError, PanelLoading } from './AsyncState'; const VEHICLE_SELECTION_LIMIT = 500; function uniqueVehicles(rows: VehicleRow[]) { const byVIN = new Map(); for (const row of rows) { const current = byVIN.get(row.vin); if (!current || (!current.online && row.online)) byVIN.set(row.vin, row); } return Array.from(byVIN.values()); } export function VehicleQuickSelector({ value, protocol, onChange }: { value: string[]; protocol?: string; onChange: (value: string[]) => void }) { const [search, setSearch] = useState(''); const deferredSearch = useDeferredValue(search.trim()); const params = useMemo(() => { const next = new URLSearchParams({ limit: '12', offset: '0' }); if (deferredSearch) next.set('keyword', deferredSearch); if (protocol) next.set('protocol', protocol); return next; }, [deferredSearch, protocol]); const vehicles = useQuery({ queryKey: ['automation-vehicle-options', protocol ?? '', deferredSearch], queryFn: ({ signal }) => api.vehicles(params, signal), staleTime: 30_000 }); const options = useMemo(() => uniqueVehicles(vehicles.data?.items ?? []), [vehicles.data?.items]); const selected = useMemo(() => new Set(value), [value]); const toggle = (vin: string) => { if (selected.has(vin)) { onChange(value.filter((item) => item !== vin)); return; } onChange([...value, vin].slice(0, VEHICLE_SELECTION_LIMIT)); }; const selectOnline = () => { const next = new Set(value); for (const vehicle of options) if (vehicle.online) next.add(vehicle.vin); onChange(Array.from(next).slice(0, VEHICLE_SELECTION_LIMIT)); }; return
快速选择车辆按车牌或 VIN 搜索;不选择表示覆盖当前协议下的全部授权车辆{value.length ? `已选 ${value.length} 辆` : '全部车辆'}
} showClear value={search} onChange={setSearch} aria-label="搜索要应用自动化的车辆" placeholder="搜索车牌 / VIN / 终端号" />{value.length ? : null}
{value.length ?
{value.map((vin) => toggle(vin)}>{vin})}
: null} {vehicles.isPending ? : vehicles.isError ? vehicles.refetch()} /> :
{options.map((vehicle) => )} {!options.length ?

{deferredSearch ? '没有匹配车辆,请换一个车牌或 VIN。' : '当前协议下没有可快捷选择的车辆。'}

: null}
}
; }