Forum: Fix jumping focus when going to message in topic (#2830)

This commit is contained in:
Alexander Zinchuk 2023-03-19 22:33:34 -05:00
parent 2948c395b1
commit 55d711e683
6 changed files with 15 additions and 8 deletions

View File

@ -36,6 +36,7 @@ import ActionMessageSuggestedAvatar from './ActionMessageSuggestedAvatar';
type OwnProps = { type OwnProps = {
message: ApiMessage; message: ApiMessage;
threadId?: number;
observeIntersectionForReading?: ObserveFn; observeIntersectionForReading?: ObserveFn;
observeIntersectionForLoading?: ObserveFn; observeIntersectionForLoading?: ObserveFn;
observeIntersectionForPlaying?: ObserveFn; observeIntersectionForPlaying?: ObserveFn;
@ -234,7 +235,7 @@ const ActionMessage: FC<OwnProps & StateProps> = ({
}; };
export default memo(withGlobal<OwnProps>( export default memo(withGlobal<OwnProps>(
(global, { message }): StateProps => { (global, { message, threadId }): StateProps => {
const { byId: usersById } = global.users; const { byId: usersById } = global.users;
const userId = message.senderId; const userId = message.senderId;
const { targetUserIds, targetChatId } = message.content.action || {}; const { targetUserIds, targetChatId } = message.content.action || {};
@ -243,7 +244,7 @@ export default memo(withGlobal<OwnProps>(
? selectChatMessage(global, message.chatId, targetMessageId) ? selectChatMessage(global, message.chatId, targetMessageId)
: undefined; : undefined;
const isFocused = selectIsMessageFocused(global, message); const isFocused = threadId ? selectIsMessageFocused(global, message, threadId) : false;
const { const {
direction: focusDirection, direction: focusDirection,
noHighlight: noFocusHighlight, noHighlight: noFocusHighlight,

View File

@ -146,6 +146,7 @@ const MessageListContent: FC<OwnProps> = ({
<ActionMessage <ActionMessage
key={message.id} key={message.id}
message={message} message={message}
threadId={threadId}
isInsideTopic={Boolean(threadId && threadId !== MAIN_THREAD_ID)} isInsideTopic={Boolean(threadId && threadId !== MAIN_THREAD_ID)}
observeIntersectionForReading={observeIntersectionForReading} observeIntersectionForReading={observeIntersectionForReading}
observeIntersectionForLoading={observeIntersectionForLoading} observeIntersectionForLoading={observeIntersectionForLoading}

View File

@ -1308,8 +1308,8 @@ export default memo(withGlobal<OwnProps>(
const uploadProgress = selectUploadProgress(global, message); const uploadProgress = selectUploadProgress(global, message);
const isFocused = messageListType === 'thread' && ( const isFocused = messageListType === 'thread' && (
album album
? album.messages.some((m) => selectIsMessageFocused(global, m)) ? album.messages.some((m) => selectIsMessageFocused(global, m, threadId))
: selectIsMessageFocused(global, message) : selectIsMessageFocused(global, message, threadId)
); );
const { const {

View File

@ -416,12 +416,12 @@ addActionHandler('focusMessage', (global, actions, payload): ActionReturnType =>
} }
blurTimeout = window.setTimeout(() => { blurTimeout = window.setTimeout(() => {
global = getGlobal(); global = getGlobal();
global = updateFocusedMessage(global, undefined, undefined, undefined, undefined, tabId); global = updateFocusedMessage(global, undefined, undefined, undefined, undefined, undefined, tabId);
global = updateFocusDirection(global, undefined, tabId); global = updateFocusDirection(global, undefined, tabId);
setGlobal(global); setGlobal(global);
}, noHighlight ? FOCUS_NO_HIGHLIGHT_DURATION : FOCUS_DURATION); }, noHighlight ? FOCUS_NO_HIGHLIGHT_DURATION : FOCUS_DURATION);
global = updateFocusedMessage(global, chatId, messageId, noHighlight, isResizingContainer, tabId); global = updateFocusedMessage(global, chatId, messageId, threadId, noHighlight, isResizingContainer, tabId);
global = updateFocusDirection(global, undefined, tabId); global = updateFocusDirection(global, undefined, tabId);
if (replyMessageId) { if (replyMessageId) {

View File

@ -501,13 +501,15 @@ function updateScheduledMessages<T extends GlobalState>(
} }
export function updateFocusedMessage<T extends GlobalState>( export function updateFocusedMessage<T extends GlobalState>(
global: T, chatId?: string, messageId?: number, noHighlight = false, isResizingContainer = false, global: T, chatId?: string, messageId?: number, threadId = MAIN_THREAD_ID, noHighlight = false,
isResizingContainer = false,
...[tabId = getCurrentTabId()]: TabArgs<T> ...[tabId = getCurrentTabId()]: TabArgs<T>
): T { ): T {
return updateTabState(global, { return updateTabState(global, {
focusedMessage: { focusedMessage: {
...selectTabState(global, tabId).focusedMessage, ...selectTabState(global, tabId).focusedMessage,
chatId, chatId,
threadId,
messageId, messageId,
noHighlight, noHighlight,
isResizingContainer, isResizingContainer,

View File

@ -367,10 +367,13 @@ export function selectFocusedMessageId<T extends GlobalState>(
} }
export function selectIsMessageFocused<T extends GlobalState>( export function selectIsMessageFocused<T extends GlobalState>(
global: T, message: ApiMessage, global: T, message: ApiMessage, currentThreadId: number,
...[tabId = getCurrentTabId()]: TabArgs<T> ...[tabId = getCurrentTabId()]: TabArgs<T>
) { ) {
const focusedId = selectFocusedMessageId(global, message.chatId, tabId); const focusedId = selectFocusedMessageId(global, message.chatId, tabId);
const threadId = selectTabState(global, tabId).focusedMessage?.threadId;
if (currentThreadId !== threadId) return false;
return focusedId ? focusedId === message.id || focusedId === message.previousLocalId : false; return focusedId ? focusedId === message.id || focusedId === message.previousLocalId : false;
} }