"use client"; import { useEffect, useState } from "react"; import { useRouter } from "next/navigation"; import { Button } from "@/components/ui/button"; import { pairingLinkAction } from "@/app/(app)/actions"; /** * PEP-100: opens the user's bot (with the pairing code) in a new tab / the Telegram app and keeps * Peppa open — a plain redirect used to take the page away mid-steps. While the user presses * Start, the page refreshes itself so the step turns done. */ export function OpenBotButton({ label, variant = "default", size = "sm" }: { label: string; variant?: "default" | "ghost" | "outline"; size?: "sm" | "xs" }) { const router = useRouter(); const [waiting, setWaiting] = useState(false); const [error, setError] = useState(null); const [link, setLink] = useState(null); useEffect(() => { if (!waiting) return; const t = setInterval(() => router.refresh(), 3000); const stop = setTimeout(() => setWaiting(false), 3 * 60_000); return () => { clearInterval(t); clearTimeout(stop); }; }, [waiting, router]); async function open() { setError(null); // Opened synchronously in the click, so the browser doesn't block it as a popup. const tab = window.open("about:blank", "_blank"); const r = await pairingLinkAction(); if (!r.url) { tab?.close(); setError(r.error ?? "Не получилось открыть бота"); return; } // Popups blocked: never take Peppa away — show a link that opens in a new tab instead. if (tab) tab.location.href = r.url; else setLink(r.url); setWaiting(true); } return ( {link && ( Браузер не открыл новую вкладку — нажмите здесь, чтобы открыть бота )} {waiting && Нажмите Start в Telegram и вернитесь сюда — шаг отметится сам.} {error && {error}} ); }