import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { endpointsApi, databasesApi } from '@/services/api'; import { Endpoint } from '@/types'; import { Plus, Search, Edit2, Trash2 } from 'lucide-react'; import toast from 'react-hot-toast'; import EndpointModal from '@/components/EndpointModal'; import Dialog from '@/components/Dialog'; export default function Endpoints() { const queryClient = useQueryClient(); const [search, setSearch] = useState(''); const [showModal, setShowModal] = useState(false); const [editingEndpoint, setEditingEndpoint] = useState(null); const [dialog, setDialog] = useState<{ isOpen: boolean; title: string; message: string; type: 'alert' | 'confirm'; onConfirm?: () => void; }>({ isOpen: false, title: '', message: '', type: 'alert', }); const { data: endpoints, isLoading } = useQuery({ queryKey: ['endpoints', search], queryFn: () => endpointsApi.getAll(search).then(res => res.data), }); const { data: databases } = useQuery({ queryKey: ['databases'], queryFn: () => databasesApi.getAll().then(res => res.data), }); const deleteMutation = useMutation({ mutationFn: (id: string) => endpointsApi.delete(id), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['endpoints'] }); toast.success('Эндпоинт успешно удален'); }, onError: () => toast.error('Не удалось удалить эндпоинт'), }); const handleDelete = (id: string) => { setDialog({ isOpen: true, title: 'Подтверждение', message: 'Вы уверены, что хотите удалить этот эндпоинт?', type: 'confirm', onConfirm: () => { deleteMutation.mutate(id); }, }); }; const handleEdit = (endpoint: Endpoint) => { setEditingEndpoint(endpoint); setShowModal(true); }; const handleCreate = () => { setEditingEndpoint(null); setShowModal(true); }; return (

API Эндпоинты

Управление динамическими API эндпоинтами

setSearch(e.target.value)} className="flex-1 outline-none" />
{isLoading ? (

Загрузка эндпоинтов...

) : (
{endpoints?.map((endpoint) => (

{endpoint.name}

{endpoint.method} {endpoint.is_public && ( Публичный )}

{endpoint.description}

{endpoint.path} {endpoint.folder_name && ( 📁 {endpoint.folder_name} )} {endpoint.parameters && endpoint.parameters.length > 0 && (
Параметры: {endpoint.parameters.map((param: any, idx: number) => ( {param.name} ({param.type}){param.required && '*'} ))}
)}
))} {endpoints?.length === 0 && (

Эндпоинты не найдены. Создайте первый эндпоинт!

)}
)} {showModal && ( setShowModal(false)} /> )} setDialog({ ...dialog, isOpen: false })} title={dialog.title} message={dialog.message} type={dialog.type} onConfirm={dialog.onConfirm} />
); }