Composer: Fix incorrect preview for some URLs (#3223)

This commit is contained in:
Alexander Zinchuk 2023-05-28 14:32:38 +02:00
parent 6218fbfd56
commit a49374a666
4 changed files with 28 additions and 13 deletions

View File

@ -1140,9 +1140,15 @@ export async function searchMessagesGlobal({
}; };
} }
export async function fetchWebPagePreview({ message }: { message: string }) { export async function fetchWebPagePreview({
text,
}: {
text: ApiFormattedText;
}) {
const textWithEntities = buildInputTextWithEntities(text);
const preview = await invokeRequest(new GramJs.messages.GetWebPagePreview({ const preview = await invokeRequest(new GramJs.messages.GetWebPagePreview({
message, message: textWithEntities.text,
entities: textWithEntities.entities,
})); }));
return preview && buildWebPage(preview); return preview && buildWebPage(preview);

View File

@ -1,11 +1,15 @@
import type { Signal } from '../../../util/signals'; import React, {
import type { FC } from '../../../lib/teact/teact'; memo, useCallback, useEffect, useRef,
import React, { memo, useCallback, useEffect } from '../../../lib/teact/teact'; } from '../../../lib/teact/teact';
import { getActions, withGlobal } from '../../../global'; import { getActions, withGlobal } from '../../../global';
import type { ApiMessage, ApiMessageEntityTextUrl, ApiWebPage } from '../../../api/types'; import type { FC } from '../../../lib/teact/teact';
import type {
ApiFormattedText, ApiMessage, ApiMessageEntityTextUrl, ApiWebPage,
} from '../../../api/types';
import { ApiMessageEntityTypes } from '../../../api/types'; import { ApiMessageEntityTypes } from '../../../api/types';
import type { ISettings } from '../../../types'; import type { ISettings } from '../../../types';
import type { Signal } from '../../../util/signals';
import { RE_LINK_TEMPLATE } from '../../../config'; import { RE_LINK_TEMPLATE } from '../../../config';
import { selectTabState, selectNoWebPage, selectTheme } from '../../../global/selectors'; import { selectTabState, selectNoWebPage, selectTheme } from '../../../global/selectors';
@ -54,27 +58,32 @@ const WebPagePreview: FC<OwnProps & StateProps> = ({
toggleMessageWebPage, toggleMessageWebPage,
} = getActions(); } = getActions();
const formattedTextWithLinkRef = useRef<ApiFormattedText>();
const detectLinkDebounced = useDebouncedResolver(() => { const detectLinkDebounced = useDebouncedResolver(() => {
const { text, entities } = parseMessageInput(getHtml()); const formattedText = parseMessageInput(getHtml());
const linkEntity = entities?.find((entity): entity is ApiMessageEntityTextUrl => ( const linkEntity = formattedText.entities?.find((entity): entity is ApiMessageEntityTextUrl => (
entity.type === ApiMessageEntityTypes.TextUrl entity.type === ApiMessageEntityTypes.TextUrl
)); ));
return linkEntity?.url || text.match(RE_LINK)?.[0]; formattedTextWithLinkRef.current = formattedText;
return linkEntity?.url || formattedText.text.match(RE_LINK)?.[0];
}, [getHtml], DEBOUNCE_MS, true); }, [getHtml], DEBOUNCE_MS, true);
const getLink = useDerivedSignal(detectLinkDebounced, [detectLinkDebounced, getHtml], true); const getLink = useDerivedSignal(detectLinkDebounced, [detectLinkDebounced, getHtml], true);
useEffect(() => { useEffect(() => {
const link = getLink(); const link = getLink();
const formattedText = formattedTextWithLinkRef.current;
if (link) { if (link) {
loadWebPagePreview({ text: link }); loadWebPagePreview({ text: formattedText! });
} else { } else {
clearWebPagePreview(); clearWebPagePreview();
toggleMessageWebPage({ chatId, threadId }); toggleMessageWebPage({ chatId, threadId });
} }
}, [getLink, chatId, threadId, clearWebPagePreview, loadWebPagePreview, toggleMessageWebPage]); }, [getLink, chatId, threadId]);
useSyncEffect(() => { useSyncEffect(() => {
clearWebPagePreview(); clearWebPagePreview();

View File

@ -664,7 +664,7 @@ addActionHandler('markMessagesRead', (global, actions, payload): ActionReturnTyp
addActionHandler('loadWebPagePreview', async (global, actions, payload): Promise<void> => { addActionHandler('loadWebPagePreview', async (global, actions, payload): Promise<void> => {
const { text, tabId = getCurrentTabId() } = payload; const { text, tabId = getCurrentTabId() } = payload;
const webPagePreview = await callApi('fetchWebPagePreview', { message: text }); const webPagePreview = await callApi('fetchWebPagePreview', { text });
global = getGlobal(); global = getGlobal();
global = updateTabState(global, { global = updateTabState(global, {

View File

@ -1022,7 +1022,7 @@ export interface ActionPayloads {
// misc // misc
loadWebPagePreview: { loadWebPagePreview: {
text: string; text: ApiFormattedText;
} & WithTabId; } & WithTabId;
clearWebPagePreview: WithTabId | undefined; clearWebPagePreview: WithTabId | undefined;
loadWallpapers: undefined; loadWallpapers: undefined;