import { useState, useEffect, useCallback } from 'react'; import { useTranslation } from 'react-i18next'; import { useBlockingStore } from '../../store/blocking'; import { apiClient } from '../../api/client'; const CHECK_COOLDOWN_SECONDS = 5; export default function ChannelSubscriptionScreen() { const { t } = useTranslation(); const { channelInfo, clearBlocking } = useBlockingStore(); const [isChecking, setIsChecking] = useState(false); const [cooldown, setCooldown] = useState(0); const [error, setError] = useState(null); // Cooldown timer useEffect(() => { if (cooldown <= 0) return; const timer = setInterval(() => { setCooldown((prev) => { if (prev <= 1) { clearInterval(timer); return 0; } return prev - 1; }); }, 1000); return () => clearInterval(timer); }, [cooldown]); const openChannel = useCallback(() => { if (channelInfo?.channel_link) { window.open(channelInfo.channel_link, '_blank'); } }, [channelInfo?.channel_link]); const checkSubscription = useCallback(async () => { if (isChecking || cooldown > 0) return; setIsChecking(true); setError(null); try { // Make any authenticated request - if channel check passes, it will succeed await apiClient.get('/cabinet/auth/me'); // If we get here, subscription is valid - reload page clearBlocking(); window.location.reload(); } catch (err: unknown) { // Check if it's still a channel subscription error const error = err as { response?: { status?: number; data?: { detail?: { code?: string } } }; }; if ( error.response?.status === 403 && error.response?.data?.detail?.code === 'channel_subscription_required' ) { setError(t('blocking.channel.notSubscribed')); } else { // Other error - might be network issue setError(t('blocking.channel.checkError')); } } finally { setIsChecking(false); setCooldown(CHECK_COOLDOWN_SECONDS); } }, [isChecking, cooldown, clearBlocking, t]); return (
{/* Icon */}
{/* Title */}

{t('blocking.channel.title')}

{/* Message */}

{channelInfo?.message || t('blocking.channel.defaultMessage')}

{/* Error message */} {error && (

{error}

)} {/* Buttons */}
{/* Open channel button */} {/* Check subscription button */}
{/* Hint */}

{t('blocking.channel.hint')}

); }