diff --git a/src/api/gramjs/methods/bots.ts b/src/api/gramjs/methods/bots.ts index a3f748312..43360fae8 100644 --- a/src/api/gramjs/methods/bots.ts +++ b/src/api/gramjs/methods/bots.ts @@ -104,6 +104,7 @@ export async function fetchInlineBotResults({ switchPm: buildBotSwitchPm(result.switchPm), users: result.users.map(buildApiUser).filter(Boolean), results: processInlineBotResult(String(result.queryId), result.results), + cacheTime: result.cacheTime, }; } diff --git a/src/components/middle/composer/hooks/useInlineBotTooltip.ts b/src/components/middle/composer/hooks/useInlineBotTooltip.ts index 19ab05332..a34557c2b 100644 --- a/src/components/middle/composer/hooks/useInlineBotTooltip.ts +++ b/src/components/middle/composer/hooks/useInlineBotTooltip.ts @@ -23,7 +23,7 @@ export default function useInlineBotTooltip( html: string, inlineBots?: Record, ) { - const { queryInlineBot, resetInlineBot } = getActions(); + const { queryInlineBot, resetInlineBot, resetAllInlineBots } = getActions(); const [isOpen, markIsOpen, unmarkIsOpen] = useFlag(); const { @@ -47,6 +47,12 @@ export default function useInlineBotTooltip( } }, [prevQuery, query, unmarkIsOpen]); + useEffect(() => { + if (!isOpen && !username) { + resetAllInlineBots(); + } + }, [isOpen, resetAllInlineBots, username]); + useEffect(() => { if (isAllowed && usernameLowered && chatId) { queryInlineBot({ chatId, username: usernameLowered, query: query! }); diff --git a/src/global/actions/api/bots.ts b/src/global/actions/api/bots.ts index c05d46ffc..191109ef0 100644 --- a/src/global/actions/api/bots.ts +++ b/src/global/actions/api/bots.ts @@ -267,6 +267,7 @@ addActionHandler('queryInlineBot', async (global, actions, payload): Promise { - const { username, tabId = getCurrentTabId() } = payload; + const { username, force, tabId = getCurrentTabId() } = payload; let inlineBotData = selectTabState(global, tabId).inlineBots.byUsername[username]; @@ -358,6 +359,8 @@ addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType return; } + if (!force && Date.now() < inlineBotData.cacheTime) return; + inlineBotData = { id: inlineBotData.id, query: '', @@ -365,12 +368,22 @@ addActionHandler('resetInlineBot', (global, actions, payload): ActionReturnType switchPm: undefined, canLoadMore: true, results: [], + cacheTime: 0, }; global = replaceInlineBotSettings(global, username, inlineBotData, tabId); setGlobal(global); }); +addActionHandler('resetAllInlineBots', (global, actions, payload): ActionReturnType => { + const { tabId = getCurrentTabId() } = payload || {}; + const inlineBots = selectTabState(global, tabId).inlineBots.byUsername; + + Object.keys(inlineBots).forEach((username) => { + actions.resetInlineBot({ username, tabId }); + }); +}); + addActionHandler('startBot', async (global, actions, payload): Promise => { const { botId, param } = payload; @@ -900,6 +913,7 @@ async function searchInlineBot(global: T, { global = replaceInlineBotSettings(global, username, { ...newInlineBotData, help: result.help, + cacheTime: Date.now() + result.cacheTime * 1000, ...(newResults.length && { isGallery: result.isGallery }), ...(result.switchPm && { switchPm: result.switchPm }), canLoadMore: result.results.length > 0 && Boolean(result.nextOffset), diff --git a/src/global/types.ts b/src/global/types.ts index 2eefc30cf..258982e36 100644 --- a/src/global/types.ts +++ b/src/global/types.ts @@ -1893,7 +1893,9 @@ export interface ActionPayloads { } & WithTabId; resetInlineBot: { username: string; + force?: boolean; } & WithTabId; + resetAllInlineBots: WithTabId | undefined; startBot: { botId: string; param?: string; diff --git a/src/types/index.ts b/src/types/index.ts index 8acbab0e0..94a745556 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -391,4 +391,5 @@ export type InlineBotSettings = { results?: (ApiBotInlineResult | ApiBotInlineMediaResult)[]; isGallery?: boolean; switchPm?: ApiBotInlineSwitchPm; + cacheTime: number; };