import { useId, useState } from 'react'; import { CheckCircle2, Info, Search } from 'lucide-react'; import type { AdminChannel } from '@/api/adminApi'; import { Button, CarrierTag, Input, Modal, Select, Tag } from '@/components/ui'; import { formatRateAmount, MONEY_UNITS_PER_YUAN } from '@/utils/currency'; import { normalizeRegion, type NationalRoute, type ProvinceRoute } from './model'; import './RouteConfigModal.css'; type Carrier = 'mobile' | 'unicom' | 'telecom'; export type RouteModalState = { type: 'province' | 'national'; mode: 'create' | 'edit'; route?: ProvinceRoute | NationalRoute; }; const carrierLabels: Record = { mobile: '移动', unicom: '联通', telecom: '电信' }; function isCarrierCompatible(channel: AdminChannel, carrier: Carrier) { return channel.carriers?.length ? channel.carriers.includes(carrier) : !channel.carrier || channel.carrier === 'all' || channel.carrier === carrier; } function connectionLabel(channel: AdminChannel) { if (channel.status === 'deleted') return { text: '已删除', tone: 'neutral' as const }; if (channel.status !== 'active') return { text: '已停用', tone: 'neutral' as const }; const states = channel.connectionStates ?? []; const connected = states.filter( (state) => state.status === 'connected' && state.desiredConnections > 0 && state.currentConnections > 0, ); if (connected.length) { return { text: `已连接 · ${connected.reduce((sum, state) => sum + state.currentConnections, 0)} 条`, tone: 'success' as const, }; } if (!states.length) return { text: '暂无连接回写', tone: 'neutral' as const }; return { text: states.some((state) => state.status === 'connecting') ? '连接中' : '未连接', tone: 'warning' as const, }; } export function RouteConfigModal({ channels, carrier, modal, occupiedChannelIds, onClose, onSubmit, }: { channels: AdminChannel[]; carrier: Carrier; modal: RouteModalState; occupiedChannelIds: string[]; onClose: () => void; onSubmit: (route: ProvinceRoute | NationalRoute) => string | null; }) { const provinceRoute = modal.type === 'province' ? (modal.route as ProvinceRoute | undefined) : undefined; const nationalRoute = modal.type === 'national' ? (modal.route as NationalRoute | undefined) : undefined; const [province, setProvince] = useState(provinceRoute?.province ?? ''); const [channelId, setChannelId] = useState(modal.route?.channelId ?? ''); const [keyword, setKeyword] = useState(''); const [error, setError] = useState(''); const selectionName = useId(); const selected = channels.find((channel) => channel.id === channelId); const dirty = province !== (provinceRoute?.province ?? '') || channelId !== (modal.route?.channelId ?? ''); const provinces = channels .filter( (channel) => channel.status !== 'deleted' && isCarrierCompatible(channel, carrier) && (channel.id === modal.route?.channelId || !occupiedChannelIds.includes(channel.id)), ) .map((channel) => channel.sendRegion) .filter((region): region is string => Boolean(region && normalizeRegion(region) !== '全国')); if (provinceRoute?.province) provinces.push(provinceRoute.province); const provinceOptions = [ { label: '请选择省份', value: '' }, ...Array.from(new Set(provinces)) .sort() .map((region) => ({ label: region, value: region })), ]; function unavailableReason(channel: AdminChannel) { if (channel.status === 'deleted') return '通道已删除'; if (!isCarrierCompatible(channel, carrier)) return `不支持${carrierLabels[carrier]}`; if (channel.id !== modal.route?.channelId && occupiedChannelIds.includes(channel.id)) return '已在当前通道组中配置'; if (modal.type === 'province') { if (!province) return '请先选择省份'; if (normalizeRegion(channel.sendRegion) !== normalizeRegion(province)) return '通道地区与所选省份不匹配'; } return ''; } const query = keyword.trim().toLocaleLowerCase(); const visibleChannels = channels.filter( (channel) => !unavailableReason(channel) && `${channel.name} ${channel.code} ${channel.sendRegion ?? '全国'}`.toLocaleLowerCase().includes(query), ); function submit() { if (modal.type === 'province' && !province) { setError('请选择省份'); return; } if (!selected) { setError(channelId ? '原通道不存在,请重新选择' : '请选择通道'); return; } const reason = unavailableReason(selected); if (reason) { setError(reason); return; } const route = modal.type === 'province' ? { id: provinceRoute?.id ?? `p-${selectionName}`, province, channelId } : { id: nationalRoute?.id ?? `n-${selectionName}`, priority: nationalRoute?.priority ?? 0, channelId }; setError(onSubmit(route) ?? ''); } return ( ( <> )} onClose={onClose} open size="xl" title={modal.mode === 'edit' ? '编辑通道' : '添加通道'} >
{modal.type === 'province' ? ( setKeyword(event.target.value)} placeholder="输入通道名称、编号或地区" prefix={
); }