'use client' import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Textarea } from '@/components/ui/textarea' import { Switch } from '@/components/ui/switch' import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table' import DataSourceManagement from './DataSourceManagement' import type { APIEndpoint } from '@/types/admin' import { authenticatedFetch } from '@/lib/auth' import { DndContext, closestCenter, KeyboardSensor, PointerSensor, useSensor, useSensors, DragEndEvent, } from '@dnd-kit/core' import { arrayMove, SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy, } from '@dnd-kit/sortable' import { useSortable, } from '@dnd-kit/sortable' import { CSS } from '@dnd-kit/utilities' import { GripVertical } from 'lucide-react' interface EndpointsTabProps { endpoints: APIEndpoint[] onCreateEndpoint: (data: Partial) => void onUpdateEndpoints: () => void } // 可拖拽的表格行组件 function SortableTableRow({ endpoint, onManageDataSources }: { endpoint: APIEndpoint onManageDataSources: (endpoint: APIEndpoint) => void }) { const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: endpoint.id }) const style = { transform: CSS.Transform.toString(transform), transition, opacity: isDragging ? 0.5 : 1, } return (
{endpoint.name} {endpoint.url} {endpoint.is_active ? '启用' : '禁用'} {endpoint.show_on_homepage ? '显示' : '隐藏'} {new Date(endpoint.created_at).toLocaleDateString()}
) } export default function EndpointsTab({ endpoints, onCreateEndpoint, onUpdateEndpoints }: EndpointsTabProps) { const [showCreateForm, setShowCreateForm] = useState(false) const [selectedEndpoint, setSelectedEndpoint] = useState(null) const [formData, setFormData] = useState({ name: '', url: '', description: '', is_active: true, show_on_homepage: true }) const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }) ) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() onCreateEndpoint(formData) setFormData({ name: '', url: '', description: '', is_active: true, show_on_homepage: true }) setShowCreateForm(false) } const loadEndpointDataSources = async (endpointId: number) => { try { const response = await authenticatedFetch(`/api/admin/endpoints/${endpointId}/data-sources`) if (response.ok) { const data = await response.json() const endpoint = endpoints.find(e => e.id === endpointId) if (endpoint) { endpoint.data_sources = data.data || [] setSelectedEndpoint({ ...endpoint }) } } } catch (error) { console.error('Failed to load data sources:', error) } } const handleManageDataSources = (endpoint: APIEndpoint) => { setSelectedEndpoint(endpoint) loadEndpointDataSources(endpoint.id) } // 处理拖拽结束事件 const handleDragEnd = async (event: DragEndEvent) => { const { active, over } = event if (!over || active.id === over.id) { return } const oldIndex = endpoints.findIndex(endpoint => endpoint.id === active.id) const newIndex = endpoints.findIndex(endpoint => endpoint.id === over.id) if (oldIndex === -1 || newIndex === -1) { return } // 创建新的排序数组 const newEndpoints = arrayMove(endpoints, oldIndex, newIndex) // 更新排序值 const endpointOrders = newEndpoints.map((endpoint, index) => ({ id: endpoint.id, sort_order: index })) try { const response = await authenticatedFetch('/api/admin/endpoints/sort-order', { method: 'PUT', body: JSON.stringify({ endpoint_orders: endpointOrders }), }) if (response.ok) { onUpdateEndpoints() } else { alert('更新排序失败') } } catch (error) { console.error('Failed to update sort order:', error) alert('更新排序失败') } } return (

API端点管理

{showCreateForm && (

创建新端点

setFormData({ ...formData, name: e.target.value })} required />
setFormData({ ...formData, url: e.target.value })} placeholder="例如: pic/anime" required />