Follow-up

This commit is contained in:
Alexander Zinchuk 2021-07-16 20:56:10 +03:00
parent 495ed8b42b
commit d8273a8127
4 changed files with 37 additions and 30 deletions

View File

@ -325,6 +325,10 @@ const MessageInput: FC<OwnProps & StateProps & DispatchProps> = ({
} }
useEffect(() => { useEffect(() => {
if (IS_TOUCH_ENV) {
return;
}
focusInput(); focusInput();
}, [currentChatId, focusInput, replyingToId, shouldSetFocus]); }, [currentChatId, focusInput, replyingToId, shouldSetFocus]);

View File

@ -11,6 +11,7 @@ import parseMessageInput from '../helpers/parseMessageInput';
import getMessageTextAsHtml from '../helpers/getMessageTextAsHtml'; import getMessageTextAsHtml from '../helpers/getMessageTextAsHtml';
import useBackgroundMode from '../../../../hooks/useBackgroundMode'; import useBackgroundMode from '../../../../hooks/useBackgroundMode';
import useBeforeUnload from '../../../../hooks/useBeforeUnload'; import useBeforeUnload from '../../../../hooks/useBeforeUnload';
import { IS_TOUCH_ENV } from '../../../../util/environment';
// Used to avoid running debounced callbacks when chat changes. // Used to avoid running debounced callbacks when chat changes.
let currentChatId: number | undefined; let currentChatId: number | undefined;
@ -66,10 +67,12 @@ export default (
setHtml(getMessageTextAsHtml(draft)); setHtml(getMessageTextAsHtml(draft));
requestAnimationFrame(() => { if (!IS_TOUCH_ENV) {
const messageInput = document.getElementById(EDITABLE_INPUT_ID)!; requestAnimationFrame(() => {
focusEditableElement(messageInput, true); const messageInput = document.getElementById(EDITABLE_INPUT_ID)!;
}); focusEditableElement(messageInput, true);
});
}
}, [chatId, threadId, draft, setHtml, updateDraft, prevChatId, prevThreadId]); }, [chatId, threadId, draft, setHtml, updateDraft, prevChatId, prevThreadId]);
// Update draft when input changes // Update draft when input changes

View File

@ -1,4 +1,4 @@
import { useCallback, useEffect, useState } from '../../../../lib/teact/teact'; import { useCallback, useEffect } from '../../../../lib/teact/teact';
import { getDispatch } from '../../../../lib/teact/teactn'; import { getDispatch } from '../../../../lib/teact/teactn';
import { InlineBotSettings } from '../../../../types'; import { InlineBotSettings } from '../../../../types';
import useFlag from '../../../../hooks/useFlag'; import useFlag from '../../../../hooks/useFlag';
@ -15,12 +15,20 @@ export default function useInlineBotTooltip(
inlineBots?: Record<string, false | InlineBotSettings>, inlineBots?: Record<string, false | InlineBotSettings>,
) { ) {
const [isOpen, markIsOpen, unmarkIsOpen] = useFlag(); const [isOpen, markIsOpen, unmarkIsOpen] = useFlag();
const [botSettings, setBotSettings] = useState<undefined | false | InlineBotSettings>();
const text = getPlainText(html); const text = getPlainText(html);
const { queryInlineBot, resetInlineBot } = getDispatch(); const { queryInlineBot, resetInlineBot } = getDispatch();
const { username, query, canShowHelp } = parseStartWithUsernameString(text); const { username, query, canShowHelp } = parseStartWithUsernameString(text);
const usernameLowered = username.toLowerCase(); const usernameLowered = username.toLowerCase();
const prevUsername = usePrevious(username); const prevUsername = usePrevious(username);
const inlineBotData = inlineBots && inlineBots[usernameLowered];
const {
id: botId,
switchPm,
offset,
results,
isGallery,
help,
} = inlineBotData || {};
useEffect(() => { useEffect(() => {
if (isAllowed && usernameLowered && chatId) { if (isAllowed && usernameLowered && chatId) {
@ -30,26 +38,17 @@ export default function useInlineBotTooltip(
const loadMore = useCallback(() => { const loadMore = useCallback(() => {
queryInlineBot({ queryInlineBot({
chatId, username: usernameLowered, query, offset: botSettings && botSettings.offset, chatId, username: usernameLowered, query, offset,
}); });
}, [botSettings, chatId, query, queryInlineBot, usernameLowered]); }, [offset, chatId, query, queryInlineBot, usernameLowered]);
const inlineBotData = inlineBots && inlineBots[usernameLowered];
useEffect(() => { useEffect(() => {
setBotSettings(inlineBotData); if (isAllowed && botId && (switchPm || (results && results.length))) {
}, [inlineBotData]);
useEffect(() => {
if (
isAllowed && botSettings && botSettings.id
&& (botSettings.switchPm || (botSettings.results && botSettings.results.length))
) {
markIsOpen(); markIsOpen();
} else { } else {
unmarkIsOpen(); unmarkIsOpen();
} }
}, [botSettings, isAllowed, markIsOpen, unmarkIsOpen]); }, [botId, isAllowed, markIsOpen, results, switchPm, unmarkIsOpen]);
if (prevUsername !== username) { if (prevUsername !== username) {
resetInlineBot({ username: prevUsername }); resetInlineBot({ username: prevUsername });
@ -60,11 +59,11 @@ export default function useInlineBotTooltip(
closeTooltip: unmarkIsOpen, closeTooltip: unmarkIsOpen,
loadMore, loadMore,
username, username,
id: botSettings ? botSettings.id : undefined, id: botId,
isGallery: botSettings ? botSettings.isGallery : undefined, isGallery,
switchPm: botSettings ? botSettings.switchPm : undefined, switchPm,
results: botSettings ? botSettings.results : undefined, results,
help: canShowHelp && botSettings && botSettings.help ? `@${username} ${botSettings.help}` : undefined, help: canShowHelp && help ? `@${username} ${help}` : undefined,
}; };
} }

View File

@ -12,11 +12,11 @@ import {
} from '../../selectors'; } from '../../selectors';
import { addChats, addUsers } from '../../reducers'; import { addChats, addUsers } from '../../reducers';
import { buildCollectionByKey } from '../../../util/iteratees'; import { buildCollectionByKey } from '../../../util/iteratees';
import { throttle } from '../../../util/schedulers'; import { debounce } from '../../../util/schedulers';
import { replaceInlineBotSettings, replaceInlineBotsIsLoading } from '../../reducers/bots'; import { replaceInlineBotSettings, replaceInlineBotsIsLoading } from '../../reducers/bots';
const TOP_PEERS_REQUEST_COOLDOWN = 60000; // 1 min const TOP_PEERS_REQUEST_COOLDOWN = 60000; // 1 min
const runThrottledForSearch = throttle((cb) => cb(), 500, false); const runDebouncedForSearch = debounce((cb) => cb(), 500, false);
addReducer('clickInlineButton', (global, actions, payload) => { addReducer('clickInlineButton', (global, actions, payload) => {
const { button } = payload; const { button } = payload;
@ -144,7 +144,7 @@ addReducer('queryInlineBot', ((global, actions, payload) => {
return; return;
} }
void runThrottledForSearch(() => { void runDebouncedForSearch(() => {
searchInlineBot({ searchInlineBot({
username, username,
inlineBotData: inlineBotData as InlineBotSettings, inlineBotData: inlineBotData as InlineBotSettings,
@ -220,11 +220,12 @@ async function searchInlineBot({
return; return;
} }
const shouldReplaceSettings = inlineBotData.query !== query;
global = replaceInlineBotsIsLoading(global, true); global = replaceInlineBotsIsLoading(global, true);
global = replaceInlineBotSettings(global, username, { global = replaceInlineBotSettings(global, username, {
...inlineBotData, ...inlineBotData,
query, query,
...(inlineBotData && inlineBotData.query !== query && { offset: undefined }), ...(shouldReplaceSettings && { offset: undefined, results: [] }),
}); });
setGlobal(global); setGlobal(global);
@ -232,7 +233,7 @@ async function searchInlineBot({
bot, bot,
chat, chat,
query, query,
offset, offset: shouldReplaceSettings ? undefined : offset,
}); });
const newInlineBotData = global.inlineBots.byUsername[username]; const newInlineBotData = global.inlineBots.byUsername[username];
@ -249,7 +250,7 @@ async function searchInlineBot({
...newInlineBotData, ...newInlineBotData,
help: result.help, help: result.help,
isGallery: result.isGallery, isGallery: result.isGallery,
switchPm: result.switchPm, ...(result.switchPm && { switchPm: result.switchPm }),
canLoadMore: result.results.length > 0 && Boolean(result.nextOffset), canLoadMore: result.results.length > 0 && Boolean(result.nextOffset),
results: newInlineBotData.offset === '' || newInlineBotData.offset === result.nextOffset results: newInlineBotData.offset === '' || newInlineBotData.offset === result.nextOffset
? result.results ? result.results