Download web/src/notify/TelegramLinkCard.tsx from EnergyVenom/patchwise-agent-studio: direct link, hf CLI and curl.
- Browser
- Download file 6.27 kB
-
https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/web/src/notify/TelegramLinkCard.tsx
- Command line
-
hf download hf://spaces/EnergyVenom/patchwise-agent-studio/web/src/notify/TelegramLinkCard.tsx
-
curl -L -o TelegramLinkCard.tsx https://huggingface.co/spaces/EnergyVenom/patchwise-agent-studio/resolve/main/web/src/notify/TelegramLinkCard.tsx
6.27 kB
| 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<LinkCode | null>(null); | |
| const me = useQuery({ | |
| queryKey: ['my-channels'], queryFn: () => getJson<MyChannels>('/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<LinkCode>('POST', '/api/channels/telegram/link-code'), | |
| onSuccess: (c) => setCode(c), | |
| }); | |
| const unlink = useMutation({ | |
| mutationFn: () => sendJson<unknown>('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 ( | |
| <section className="pw-card pw-bot" aria-labelledby="acc-tg-h" data-testid="tg-link-card"> | |
| <h2 id="acc-tg-h" className="trio-card-h"><IconBrandTelegram size={18} aria-hidden /> Astra in Telegram</h2> | |
| <div aria-live="polite" className="pw-bot-state"> | |
| {linked && tg ? ( | |
| <div className="pw-bot-linked" data-testid="tg-linked"> | |
| <span className="pw-tg-ok">Linked as <b>@{tg.username ?? 'unknown'}</b>{tg.linked_at ? ` since ${fmtTs(tg.linked_at)}` : ''}</span> | |
| <Button variant="subtle" leftSection={<IconLinkOff size={15} aria-hidden />} loading={unlink.isPending} data-testid="tg-unlink" | |
| onClick={() => { if (window.confirm('Unlink Telegram? That chat will stop getting updates and can no longer act as you.')) unlink.mutate(); }}> | |
| Unlink | |
| </Button> | |
| </div> | |
| ) : code && code.bot_username === null ? ( | |
| <Text size="sm" data-testid="tg-not-setup">Telegram isn't set up yet: ask an administrator.</Text> | |
| ) : code && !expired ? ( | |
| <div className="pw-bot-code" data-testid="tg-code-step"> | |
| <ol className="pw-bot-steps"> | |
| <li>Open <a href={`https://t.me/${code.bot_username}`} target="_blank" rel="noreferrer">t.me/{code.bot_username}</a> in Telegram.</li> | |
| <li>Send <code>/link {code.code}</code> to the bot.</li> | |
| </ol> | |
| <div className="pw-bot-codebox" role="group" aria-label="Your one-time link code"> | |
| <span className="pw-bot-codeval" data-testid="tg-code">{code.code}</span> | |
| <CopyButton value={`/link ${code.code}`} timeout={2000}> | |
| {({ copied, copy }) => ( | |
| <Button size="xs" variant="default" onClick={copy} leftSection={copied ? <IconCheck size={14} aria-hidden /> : <IconCopy size={14} aria-hidden />}> | |
| {copied ? 'Copied' : 'Copy the code'} | |
| </Button> | |
| )} | |
| </CopyButton> | |
| </div> | |
| <Text size="xs" c="dimmed">The code works once and expires in {mmss}. This page updates when the bot has linked you.</Text> | |
| </div> | |
| ) : ( | |
| <div className="pw-bot-intro"> | |
| <Text size="sm">Get test-run updates and start runs from Telegram. Your chat acts as you, with your permissions.</Text> | |
| {expired && <Text size="sm" role="status" data-testid="tg-expired">That code expired. Get a new one.</Text>} | |
| <Group mt="xs"> | |
| <Button leftSection={<IconLink size={15} aria-hidden />} onClick={() => newCode.mutate()} loading={newCode.isPending} data-testid="tg-link"> | |
| Link my Telegram | |
| </Button> | |
| </Group> | |
| </div> | |
| )} | |
| </div> | |
| <div className="pw-bot-help"> | |
| <h3 className="pw-bot-help-h">What you can say to the bot</h3> | |
| <dl className="pw-bot-cmds"> | |
| {COMMANDS.map(([say, does]) => <div key={say}><dt><code>{say}</code></dt><dd>{does}</dd></div>)} | |
| </dl> | |
| </div> | |
| {problem ? <ErrorAlert error={problem} title="Telegram linking did not work" /> : null} | |
| </section> | |
| ); | |
| } | |