Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression: Jump to message not working #28163

Merged
merged 15 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 3 additions & 21 deletions apps/meteor/app/ui-utils/client/lib/openRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import type { RoomType } from '@rocket.chat/core-typings';

import { appLayout } from '../../../../client/lib/appLayout';
import { waitUntilFind } from '../../../../client/lib/utils/waitUntilFind';
import { Messages, ChatSubscription, Rooms, Subscriptions } from '../../../models/client';
import { ChatSubscription, Rooms, Subscriptions } from '../../../models/client';
import { settings } from '../../../settings/client';
import { callbacks } from '../../../../lib/callbacks';
import { callWithErrorHandling } from '../../../../client/lib/utils/callWithErrorHandling';
import { call } from '../../../../client/lib/utils/call';
import { RoomManager, RoomHistoryManager } from '..';
import { RoomManager } from '..';
import { fireGlobalEvent } from '../../../../client/lib/utils/fireGlobalEvent';
import { roomCoordinator } from '../../../../client/lib/rooms/roomCoordinator';
import MainLayout from '../../../../client/views/root/MainLayout';
Expand Down Expand Up @@ -44,9 +44,7 @@ export async function openRoom(type: RoomType, name: string, render = true) {

c.stop();

const messageId = FlowRouter.getQueryParam('msg');

if (room._id === Session.get('openedRoom') && !messageId) {
if (room._id === Session.get('openedRoom')) {
return;
}

Expand Down Expand Up @@ -76,22 +74,6 @@ export async function openRoom(type: RoomType, name: string, render = true) {
await callWithErrorHandling('openRoom', room._id);
}

if (messageId) {
const msg = { _id: messageId, rid: room._id };

const message = Messages.findOne({ _id: msg._id }) || (await callWithErrorHandling('getMessages', [msg._id]))[0];

if (message && (message.tmid || message.tcount)) {
FlowRouter.withReplaceState(() => {
FlowRouter.setParams({ tab: 'thread', context: message.tmid || message._id });
});
return;
}

RoomHistoryManager.getSurroundingMessages(msg);
FlowRouter.setQueryParams({ msg: null });
}

return callbacks.run('enter-room', sub);
} catch (error) {
c.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import { ChatRoom } from '../../../app/models/client';
import { RoomHistoryManager } from '../../../app/ui-utils/client';
import { goToRoomById } from './goToRoomById';

export const jumpToMessage = async (message: IMessage) => {
export const legacyJumpToMessage = async (message: IMessage) => {
FlowRouter.setQueryParams({ msg: null });

if (matchMedia('(max-width: 500px)').matches) {
(Template.instance() as any)?.tabBar?.close();
}

if (isThreadMessage(message)) {
if (isThreadMessage(message) || message.tcount) {
const { route, queryParams } = FlowRouter.current();
FlowRouter.go(
route?.name ?? '/',
{
tab: 'thread',
context: message.tmid,
context: message.tmid || message._id,
rid: message.rid,
name: ChatRoom.findOne({ _id: message.rid })?.name ?? '',
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { FlowRouter } from 'meteor/kadira:flow-router';

export const setMessageJumpQueryStringParameter = async (msg: IMessage['_id']) => {
FlowRouter.setQueryParams({ msg });
};
4 changes: 2 additions & 2 deletions apps/meteor/client/startup/actionButtons/jumpToMessage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meteor } from 'meteor/meteor';

import { MessageAction } from '../../../app/ui-utils/client';
import { jumpToMessage } from '../../lib/utils/jumpToMessage';
import { messageArgs } from '../../lib/utils/messageArgs';
import { setMessageJumpQueryStringParameter } from '../../lib/utils/setMessageJumpQueryStringParameter';

Meteor.startup(() => {
MessageAction.addButton({
Expand All @@ -12,7 +12,7 @@ Meteor.startup(() => {
context: ['mentions', 'threads'],
action(_, props) {
const { message = messageArgs(this).msg } = props;
jumpToMessage(message);
setMessageJumpQueryStringParameter(message._id);
},
order: 100,
group: ['message', 'menu'],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meteor } from 'meteor/meteor';

import { MessageAction } from '../../../app/ui-utils/client';
import { jumpToMessage } from '../../lib/utils/jumpToMessage';
import { messageArgs } from '../../lib/utils/messageArgs';
import { setMessageJumpQueryStringParameter } from '../../lib/utils/setMessageJumpQueryStringParameter';

Meteor.startup(() => {
MessageAction.addButton({
Expand All @@ -12,7 +12,7 @@ Meteor.startup(() => {
context: ['pinned', 'message-mobile', 'direct'],
action(_, props) {
const { message = messageArgs(this).msg } = props;
jumpToMessage(message);
setMessageJumpQueryStringParameter(message._id);
},
condition({ subscription }) {
return !!subscription;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meteor } from 'meteor/meteor';

import { MessageAction } from '../../../app/ui-utils/client';
import { jumpToMessage } from '../../lib/utils/jumpToMessage';
import { messageArgs } from '../../lib/utils/messageArgs';
import { setMessageJumpQueryStringParameter } from '../../lib/utils/setMessageJumpQueryStringParameter';

Meteor.startup(() => {
MessageAction.addButton({
Expand All @@ -12,7 +12,7 @@ Meteor.startup(() => {
context: ['search'],
async action(_, props) {
const { message = messageArgs(this).msg } = props;
await jumpToMessage(message);
setMessageJumpQueryStringParameter(message._id);
},
order: 100,
group: 'menu',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Meteor } from 'meteor/meteor';

import { settings } from '../../../app/settings/client';
import { MessageAction } from '../../../app/ui-utils/client';
import { jumpToMessage } from '../../lib/utils/jumpToMessage';
import { messageArgs } from '../../lib/utils/messageArgs';
import { setMessageJumpQueryStringParameter } from '../../lib/utils/setMessageJumpQueryStringParameter';

Meteor.startup(() => {
MessageAction.addButton({
Expand All @@ -13,7 +13,7 @@ Meteor.startup(() => {
context: ['starred', 'threads', 'message-mobile'],
action(_, props) {
const { message = messageArgs(this).msg } = props;
jumpToMessage(message);
setMessageJumpQueryStringParameter(message._id);
},
condition({ message, subscription, user }) {
if (subscription == null || !settings.get('Message_AllowStarring')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import UnreadMessagesIndicator from './UnreadMessagesIndicator';
import UploadProgressIndicator from './UploadProgressIndicator';
import ComposerContainer from './composer/ComposerContainer';
import { useFileUploadDropTarget } from './useFileUploadDropTarget';
import { useJumpToMessage } from './useJumpToMessage';
import { useRetentionPolicy } from './useRetentionPolicy';
import { useUnreadMessages } from './useUnreadMessages';

Expand All @@ -63,10 +64,11 @@ const RoomBody = (): ReactElement => {
const hideFlexTab = useUserPreference<boolean>('hideFlexTab');
const hideUsernames = useUserPreference<boolean>('hideUsernames');
const displayAvatars = useUserPreference<boolean>('displayAvatars');
const msgParameter = useQueryStringParameter('msg');

const wrapperRef = useRef<HTMLDivElement | null>(null);
const messagesBoxRef = useRef<HTMLDivElement | null>(null);
const atBottomRef = useRef(!useQueryStringParameter('msg'));
const atBottomRef = useRef(!msgParameter);
const lastScrollTopRef = useRef(0);

const chat = useChat();
Expand Down Expand Up @@ -203,6 +205,8 @@ const RoomBody = (): ReactElement => {

const retentionPolicy = useRetentionPolicy(room);

useJumpToMessage(msgParameter);
gabriellsh marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
callbacks.add(
'streamNewMessage',
Expand Down
31 changes: 31 additions & 0 deletions apps/meteor/client/views/room/components/body/useJumpToMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';

import { legacyJumpToMessage } from '../../../../lib/utils/legacyJumpToMessage';

export const useJumpToMessage = (msgId?: IMessage['_id']) => {
const queryClient = useQueryClient();
const getMessage = useEndpoint('GET', '/v1/chat.getMessage');

useEffect(() => {
if (!msgId) {
return;
}
queryClient
.fetchQuery({
queryKey: ['chat.getMessage', msgId],
queryFn: () => {
return getMessage({ msgId });
},
})
.then(({ message }) => {
// Serialized IMessage dates are strings. For this function, only ts is needed
legacyJumpToMessage({ ...message, ts: new Date(message.ts) } as any as IMessage);
})
.catch((error) => {
console.warn(error);
});
}, [msgId, queryClient, getMessage]);
};