'use client'; import { useCallback, useEffect, useMemo, useState } from 'react'; import { useRouter, useSearchParams } from 'next/navigation'; import { Bot, ChevronRight, Loader2, Plus } from 'lucide-react'; import { usePublicSettings } from '@/components/id/public-settings-provider'; import { Button } from '@/components/ui/button'; import { BotCreateMiniAppContent } from '@/app/mini-apps/bot-create/content'; import { BotManageMiniAppContent } from '@/app/mini-apps/bot-manage/content'; import { useMiniAppAuth } from '@/hooks/use-mini-app-auth'; import { fetchMyBots, getApiErrorMessage, type ManagedBot } from '@/lib/api'; import { useToast } from '@/components/id/toast-provider'; type View = 'list' | 'create' | 'manage'; export function BotFatherMiniAppContent() { const searchParams = useSearchParams(); const router = useRouter(); const { projectName } = usePublicSettings(); const { showToast } = useToast(); const { effectiveToken, canUse, authReady, waitingForBridge, isLoading } = useMiniAppAuth(); const initialBotId = searchParams.get('botId'); const initialMode = searchParams.get('mode'); const [view, setView] = useState(() => { if (initialBotId) return 'manage'; if (initialMode === 'create') return 'create'; return 'list'; }); const [selectedBotId, setSelectedBotId] = useState(initialBotId); const [bots, setBots] = useState([]); const [loadingBots, setLoadingBots] = useState(false); const syncUrl = useCallback( (nextView: View, botId?: string | null) => { const params = new URLSearchParams(); if (nextView === 'create') params.set('mode', 'create'); if (nextView === 'manage' && botId) params.set('botId', botId); const query = params.toString(); router.replace(query ? `/mini-apps/bot-father?${query}` : '/mini-apps/bot-father'); }, [router] ); const loadBots = useCallback(async () => { if (!effectiveToken) return; setLoadingBots(true); try { const response = await fetchMyBots(effectiveToken); setBots((response.bots ?? []).filter((bot) => !bot.isSystemBot)); } catch (error) { showToast(getApiErrorMessage(error, 'Не удалось загрузить список ботов') ?? 'Ошибка'); } finally { setLoadingBots(false); } }, [effectiveToken, showToast]); useEffect(() => { if (!canUse) return; if (view === 'list') void loadBots(); }, [canUse, loadBots, view]); const selectedBot = useMemo( () => bots.find((bot) => bot.id === selectedBotId) ?? null, [bots, selectedBotId] ); function openManage(botId: string) { setSelectedBotId(botId); setView('manage'); syncUrl('manage', botId); } function openCreate() { setView('create'); syncUrl('create'); } function backToList() { setSelectedBotId(null); setView('list'); syncUrl('list'); void loadBots(); } if ((isLoading || waitingForBridge || !authReady) && !effectiveToken) { return (
Загрузка...
); } if (!canUse) { return (
Войдите в {projectName}, чтобы управлять ботами
); } if (view === 'create') { return ( openManage(botId)} /> ); } if (view === 'manage' && selectedBotId) { return ; } return (

BotFather

Создание и настройка ваших ботов

Мои боты

{loadingBots ? (
Загрузка...
) : bots.length === 0 ? (

У вас пока нет ботов

) : (
{bots.map((bot) => ( ))}
)}
); }