1+ import sandwatchLottie from '@assets/sandwatch.json'
12import {
23 Block ,
34 Icon ,
45 PageLayout ,
6+ StickerPlayer ,
57 TelegramBackButton ,
68 TelegramMainButton ,
79 Text ,
10+ useToast ,
811} from '@components'
912import { useAppNavigation , useInterval } from '@hooks'
1013import { ROUTES_NAME } from '@routes'
@@ -20,10 +23,16 @@ const webApp = window.Telegram.WebApp
2023export const AddBotToChatPage = ( ) => {
2124 const { appNavigate } = useAppNavigation ( )
2225
23- const { fetchAdminUserChatsAction } = useChatActions ( )
26+ const { fetchAdminUserChatsAction, fetchChatAction } = useChatActions ( )
27+ const [ checkingTries , setCheckingTries ] = useState ( 0 )
2428
2529 const [ currentChats , setCurrentChats ] = useState < AdminChat [ ] > ( [ ] )
26- const [ isCheckingChats , setIsCheckingChats ] = useState ( false )
30+ const [ isCheckingNewChat , setIsCheckingNewChat ] = useState ( false )
31+ const [ isCheckingChatPermissions , setIsCheckingChatPermissions ] =
32+ useState ( false )
33+ const [ newChat , setNewChat ] = useState < AdminChat | null > ( null )
34+
35+ const { showToast } = useToast ( )
2736
2837 const navigateToMainPage = useCallback ( ( ) => {
2938 appNavigate ( { path : ROUTES_NAME . MAIN } )
@@ -33,69 +42,160 @@ export const AddBotToChatPage = () => {
3342 webApp . openTelegramLink (
3443 `${ config . botLink } ?startgroup=&admin=restrict_members+invite_users`
3544 )
36- setIsCheckingChats ( true )
45+ setIsCheckingNewChat ( true )
3746 } , [ ] )
3847
39- const poollingChats = async ( ) => {
48+ const fetchAdminUserChats = async ( ) => {
4049 try {
4150 const data = await fetchAdminUserChatsAction ( )
51+ setCurrentChats ( data )
52+ } catch ( error ) {
53+ console . error ( error )
54+ }
55+ }
56+
57+ const poollingNewChat = async ( ) => {
58+ try {
59+ const data = await fetchAdminUserChatsAction ( )
60+
4261 const noNewChats = data ?. length === currentChats ?. length
62+ setCheckingTries ( checkingTries + 1 )
4363 if ( noNewChats ) return
4464
4565 const newChat = findNewChat ( data , currentChats , 'slug' )
4666
4767 if ( newChat . length ) {
48- setIsCheckingChats ( false )
49- appNavigate ( {
50- path : ROUTES_NAME . CHECKING_BOT_ADDED ,
51- params : { chatSlug : newChat [ 0 ] . slug } ,
52- } )
68+ setNewChat ( newChat [ 0 ] )
69+ setIsCheckingNewChat ( false )
70+ setIsCheckingChatPermissions ( true )
5371 }
5472 } catch ( error ) {
5573 console . error ( error )
5674 }
5775 }
5876
59- const fetchAdminUserChats = async ( ) => {
77+ const poollingChatPermissions = async ( chatSlug : string ) => {
78+ if ( ! chatSlug ) return
6079 try {
61- const data = await fetchAdminUserChatsAction ( )
62- setCurrentChats ( data )
80+ const { chat } = await fetchChatAction ( chatSlug )
81+
82+ if ( ! chat ?. insufficientPrivileges ) {
83+ appNavigate ( {
84+ path : ROUTES_NAME . BOT_ADDED_SUCCESS ,
85+ params : { chatSlug } ,
86+ } )
87+ setIsCheckingChatPermissions ( false )
88+ }
6389 } catch ( error ) {
6490 console . error ( error )
6591 }
6692 }
6793
6894 useInterval (
6995 ( ) => {
70- poollingChats ( )
96+ poollingNewChat ( )
97+ } ,
98+ 1500 ,
99+ {
100+ enabled : isCheckingNewChat ,
101+ immediate : false ,
102+ }
103+ )
104+
105+ useInterval (
106+ ( ) => {
107+ if ( ! newChat ) return
108+ poollingChatPermissions ( newChat ?. slug )
71109 } ,
72110 1500 ,
73111 {
74- enabled : isCheckingChats ,
112+ enabled : isCheckingChatPermissions ,
75113 immediate : false ,
76114 }
77115 )
78116
117+ useEffect ( ( ) => {
118+ if ( checkingTries > 10 ) {
119+ resetPolling ( )
120+ setCheckingTries ( 0 )
121+ showToast ( {
122+ message : 'Please check if the bot was added to the group or channel' ,
123+ type : 'error' ,
124+ } )
125+ }
126+ } , [ checkingTries ] )
127+
79128 useEffect ( ( ) => {
80129 fetchAdminUserChats ( )
81130 } , [ ] )
82131
132+ let Component = null
133+
134+ if ( isCheckingNewChat || isCheckingChatPermissions ) {
135+ const title =
136+ ( isCheckingNewChat &&
137+ 'Checking If the Bot Was Added to Your Group or Channel' ) ||
138+ ( isCheckingChatPermissions && 'Checking Bot Permissions' )
139+ Component = (
140+ < >
141+ < StickerPlayer lottie = { sandwatchLottie } />
142+ < Block margin = "top" marginValue = { 16 } >
143+ < Text type = "title" align = "center" weight = "bold" >
144+ { title }
145+ </ Text >
146+ </ Block >
147+ < Block margin = "top" marginValue = { 12 } >
148+ < Text type = "text" align = "center" >
149+ This may take a moment — the check usually doesn't take long
150+ </ Text >
151+ </ Block >
152+ </ >
153+ )
154+ } else {
155+ Component = (
156+ < >
157+ < Icon name = "gatewayBot" size = { 112 } />
158+ < Block margin = "top" marginValue = { 16 } >
159+ < Text type = "title" align = "center" weight = "bold" >
160+ Add Gateway Bot to The Group or Channel
161+ </ Text >
162+ </ Block >
163+ < Block margin = "top" marginValue = { 12 } >
164+ < Text align = "center" type = "text" >
165+ Gateway bot require admin access to control who can join the group
166+ or channel.Telegram bots can’t read messages inside the group chat.
167+ </ Text >
168+ </ Block >
169+ </ >
170+ )
171+ }
172+
173+ const resetPolling = ( ) => {
174+ setIsCheckingNewChat ( false )
175+ setIsCheckingChatPermissions ( false )
176+ setNewChat ( null )
177+ }
178+
179+ const handleClick = ( ) => {
180+ if ( isCheckingNewChat || isCheckingChatPermissions ) {
181+ resetPolling ( )
182+ } else {
183+ addGatewayBot ( )
184+ }
185+ }
186+
83187 return (
84188 < PageLayout center >
85189 < TelegramBackButton onClick = { navigateToMainPage } />
86- < TelegramMainButton text = "Add Group or Channel" onClick = { addGatewayBot } />
87- < Icon name = "gatewayBot" size = { 112 } />
88- < Block margin = "top" marginValue = { 16 } >
89- < Text type = "title" align = "center" weight = "bold" >
90- Add Gateway Bot to The Group or Channel
91- </ Text >
92- </ Block >
93- < Block margin = "top" marginValue = { 12 } >
94- < Text align = "center" type = "text" >
95- Gateway bot require admin access to control who can join the group or
96- channel.Telegram bots can’t read messages inside the group chat.
97- </ Text >
98- </ Block >
190+ < TelegramMainButton
191+ text = {
192+ isCheckingNewChat || isCheckingChatPermissions
193+ ? 'Cancel'
194+ : 'Add Group or Channel'
195+ }
196+ onClick = { handleClick }
197+ />
198+ { Component }
99199 </ PageLayout >
100200 )
101201}
0 commit comments