Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

1.7 KB
Raw Blame
'use client';

import { useTranslations } from 'next-intl';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import {
  getTelegramAccount,
  startTelegramLink,
  unlinkTelegramAccount,
} from '@/lib/api/endpoints/telegram';
import { qk } from '@/services/queryKeys';

// The session user's Telegram account link. Shared rather than feature-local: the
// account page manages the link, and a project's notification preferences show which
// account its Telegram notifications will reach.

// `pollMs` turns on polling while a connection is in progress: the bot writes the
// link out of band, so the page only learns about it by asking again.
export function useTelegramAccountQuery(pollMs?: number) {
  return useQuery({
    queryKey: qk.telegramAccount,
    queryFn: () => getTelegramAccount(),
    refetchInterval: pollMs ?? false,
  });
}

// Starts a connection: the API mints a one-time code and returns the bot deep link
// that completes it when opened.
export function useStartTelegramLink() {
  return useMutation({ mutationFn: () => startTelegramLink() });
}

export function useDisconnectTelegram() {
  const qc = useQueryClient();
  return useMutation({
    mutationFn: () => unlinkTelegramAccount(),
    onSuccess: () => qc.invalidateQueries({ queryKey: qk.telegramAccount }),
  });
}

// How to name a connected account: the @username when Telegram has one, otherwise
// the first name, otherwise a plain statement that it is connected.
export function useTelegramAccountLabel() {
  const t = useTranslations('account.accounts');
  return (link: { username: string | null; firstName: string | null }) =>
    link.username ? `@${link.username}` : (link.firstName ?? t('telegramConnectedLabel'));
}