"use client"; import { useEffect, useState } from "react"; import type { MothershipAddressCandidate } from "@/lib/frontend/types"; import { PrimaryButton } from "@/components/ui/primary-button"; export interface AddressDisambiguationModalProps { open: boolean; pickupCandidates: MothershipAddressCandidate[]; deliveryCandidates: MothershipAddressCandidate[]; onConfirm: (pickup: MothershipAddressCandidate, delivery: MothershipAddressCandidate) => void; onCancel: () => void; } function isCandidateSelectable(item: MothershipAddressCandidate): boolean { return item.selectable !== false; } function pickDefaultId( candidates: MothershipAddressCandidate[], ): string | null { if (candidates.length === 0) { return null; } const selectable = candidates.filter(isCandidateSelectable); if (selectable.length === 1) { return selectable[0]!.option_id; } return null; } function findSelectableById( candidates: MothershipAddressCandidate[], id: string | null, ): MothershipAddressCandidate | null { if (!id) { return null; } const hit = candidates.find((c) => c.option_id === id); if (!hit || !isCandidateSelectable(hit)) { return null; } return hit; } function CandidateGroup({ title, candidates, selectedId, onSelect, }: { title: string; candidates: MothershipAddressCandidate[]; selectedId: string | null; onSelect: (id: string) => void; }) { if (candidates.length === 0) { return null; } const multi = candidates.length > 1; return (
{title}

{multi ? "MotherShip 提供多个精确坐标,请选择与实际位置一致的一项" : "请确认以下 MotherShip 解析结果是否与实际位置一致"}

); } export function AddressDisambiguationModal({ open, pickupCandidates, deliveryCandidates, onConfirm, onCancel, }: AddressDisambiguationModalProps) { const [pickupId, setPickupId] = useState(null); const [deliveryId, setDeliveryId] = useState(null); useEffect(() => { if (!open) { return; } setPickupId(pickDefaultId(pickupCandidates)); setDeliveryId(pickDefaultId(deliveryCandidates)); }, [open, pickupCandidates, deliveryCandidates]); if (!open) { return null; } const pickup = findSelectableById(pickupCandidates, pickupId); const delivery = findSelectableById(deliveryCandidates, deliveryId); const canConfirm = pickup != null && delivery != null; const handleConfirm = () => { if (!pickup || !delivery) { return; } onConfirm(pickup, delivery); }; const hasMultiple = pickupCandidates.length > 1 || deliveryCandidates.length > 1; return (

确认 MotherShip 精确地址

{hasMultiple ? "系统检测到 MotherShip 对您的地址有多个精确坐标。错误选择会导致报价不准,请逐项确认后再获取报价。" : "请确认 MotherShip 解析的提货/派送地址是否准确,确认无误后再获取报价。"}

确认并获取报价
); }