import { Button, CopyButton, Group, Text } from '@mantine/core'; import { IconBrandTelegram, IconCheck, IconCopy, IconLink, IconLinkOff } from '@/components/icons'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; import { getJson, sendJson } from '../api/client'; import { ErrorAlert, fmtTs } from '../components/common'; /** GET /api/channels/me: the signed-in person's own linked channels. */ export interface MyChannels { telegram: { linked: boolean; username: string | null; linked_at: string | null } } interface LinkCode { code: string; expires_at: string; bot_username: string | null } const COMMANDS: [string, string][] = [ ['/status', 'what is running and what waits for you'], ['/run Procure-to-Pay quick', 'start a quick simulation (you confirm it first)'], ['Is P2P ready for 26C?', 'any question, answered like on the Ask page'], ['/unlink', 'stop this chat acting as you'], ]; /** Seconds left until `iso`, ticking once a second; 0 when past. */ function useSecondsLeft(iso: string | null): number { const left = () => (iso ? Math.max(0, Math.round((new Date(iso).getTime() - Date.now()) / 1000)) : 0); const [s, setS] = useState(left); useEffect(() => { setS(left()); if (!iso) return undefined; const t = window.setInterval(() => setS(left()), 1000); return () => window.clearInterval(t); }, [iso]); // eslint-disable-line react-hooks/exhaustive-deps -- left() reads iso only return s; } /** * "Astra in Telegram" on the Account page: each person links their own chat with a one-time code sent to the bot. * The chat then acts as them, with their permissions (status, starting runs after a confirm, questions). */ export function TelegramLinkCard() { const qc = useQueryClient(); const [code, setCode] = useState(null); const me = useQuery({ queryKey: ['my-channels'], queryFn: () => getJson('/api/channels/me'), refetchInterval: code ? 3000 : false, // while a code is shown, watch for the /link message }); const linked = me.data?.telegram?.linked ?? false; // an older server (or none yet) answers without it: not linked useEffect(() => { if (linked && code) setCode(null); }, [linked, code]); const secondsLeft = useSecondsLeft(code?.expires_at ?? null); const expired = !!code && secondsLeft === 0; const newCode = useMutation({ mutationFn: () => sendJson('POST', '/api/channels/telegram/link-code'), onSuccess: (c) => setCode(c), }); const unlink = useMutation({ mutationFn: () => sendJson('DELETE', '/api/channels/telegram/link'), onSuccess: () => { void qc.invalidateQueries({ queryKey: ['my-channels'] }); }, }); const tg = me.data?.telegram ?? null; const problem = me.error || newCode.error || unlink.error; const mmss = `${Math.floor(secondsLeft / 60)}:${String(secondsLeft % 60).padStart(2, '0')}`; return (

Astra in Telegram

{linked && tg ? (
Linked as @{tg.username ?? 'unknown'}{tg.linked_at ? ` since ${fmtTs(tg.linked_at)}` : ''}
) : code && code.bot_username === null ? ( Telegram isn't set up yet: ask an administrator. ) : code && !expired ? (
  1. Open t.me/{code.bot_username} in Telegram.
  2. Send /link {code.code} to the bot.
{code.code} {({ copied, copy }) => ( )}
The code works once and expires in {mmss}. This page updates when the bot has linked you.
) : (
Get test-run updates and start runs from Telegram. Your chat acts as you, with your permissions. {expired && That code expired. Get a new one.}
)}

What you can say to the bot

{COMMANDS.map(([say, does]) =>
{say}
{does}
)}
{problem ? : null}
); }