"use client" import { Select, SelectTrigger, SelectValue, SelectContent, SelectItem } from "@/components/ui/select" import { useState } from "react" import { Card } from "@/components/ui/card" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Table, TableHeader, TableRow, TableHead, TableBody, TableCell } from "@/components/ui/table" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Plus, Pencil, Trash2, Link2 } from "lucide-react" import { toast } from "@/components/ui/use-toast" interface Channel { id: string name: string type: "team" | "other" link?: string } interface TrafficChannelSettingsProps { formData: any onChange: (data: any) => void onNext: () => void onPrev: () => void } function isValidUrl(string: string) { try { new URL(string) return true } catch (_) { return false } } export function TrafficChannelSettings({ formData, onChange, onNext, onPrev }: TrafficChannelSettingsProps) { const [channels, setChannels] = useState(formData.channels || []) const [isAddChannelOpen, setIsAddChannelOpen] = useState(false) const [editingChannel, setEditingChannel] = useState(null) const [newChannel, setNewChannel] = useState>({ name: "", type: "team", link: "", }) const handleAddChannel = () => { if (!newChannel.name) return if (newChannel.link && !isValidUrl(newChannel.link)) { toast({ title: "错误", description: "请输入有效的URL", variant: "destructive", }) return } if (editingChannel) { setChannels( channels.map((channel) => (channel.id === editingChannel.id ? { ...channel, ...newChannel } : channel)), ) } else { setChannels([ ...channels, { id: Date.now().toString(), name: newChannel.name, type: newChannel.type || "team", link: newChannel.link, } as Channel, ]) } setIsAddChannelOpen(false) setNewChannel({ name: "", type: "team", link: "" }) setEditingChannel(null) onChange({ ...formData, channels }) } const handleEditChannel = (channel: Channel) => { setEditingChannel(channel) setNewChannel(channel) setIsAddChannelOpen(true) } const handleDeleteChannel = (channelId: string) => { setChannels(channels.filter((channel) => channel.id !== channelId)) onChange({ ...formData, channels: channels.filter((channel) => channel.id !== channelId) }) } return (

流量通道设置

通道名称 类型 链接 操作 {channels.length === 0 ? ( 暂无数据 ) : ( channels.map((channel) => ( {channel.name} {channel.type === "team" ? "打粉团队" : "其他"} {channel.link && (
{channel.link}
)}
)) )}
{editingChannel ? "编辑通道" : "添加通道"}
setNewChannel({ ...newChannel, name: e.target.value })} placeholder="请输入通道名称" />
setNewChannel({ ...newChannel, link: e.target.value })} placeholder="请输入通道链接" /> {newChannel.link && !isValidUrl(newChannel.link) && (

请输入有效的URL

)}
) }