Skip to content

Commit

Permalink
chore: remove meteor.startup from unfollow message action (#34078)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso Evangelista <[email protected]>
  • Loading branch information
MartinSchoeler and tassoevan authored Dec 4, 2024
1 parent 6423a2c commit 6cfc6ce
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 62 deletions.
1 change: 0 additions & 1 deletion apps/meteor/app/threads/client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './messageAction/follow';
import './messageAction/unfollow';
import './messageAction/replyInThread';
import './threads.css';
46 changes: 0 additions & 46 deletions apps/meteor/app/threads/client/messageAction/unfollow.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useNewDiscussionMessageAction } from './useNewDiscussionMessageAction';
import { usePermalinkStar } from './usePermalinkStar';
import { usePinMessageAction } from './usePinMessageAction';
import { useStarMessageAction } from './useStarMessageAction';
import { useUnFollowMessageAction } from './useUnFollowMessageAction';
import { useUnstarMessageAction } from './useUnstarMessageAction';
import { useWebDAVMessageAction } from './useWebDAVMessageAction';
import type { MessageActionContext } from '../../../../app/ui-utils/client/lib/MessageAction';
Expand Down Expand Up @@ -98,6 +99,7 @@ const MessageToolbar = ({
useStarMessageAction(message, { room, user });
useUnstarMessageAction(message, { room, user });
usePermalinkStar(message, { subscription, user });
useUnFollowMessageAction(message, { room, user, context });

useJumpToMessageContextAction(message, {
id: 'jump-to-message',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { useSetting, useToastMessageDispatch } from '@rocket.chat/ui-contexts';
import { useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';

import { Messages } from '../../../../app/models/client';
import { MessageAction } from '../../../../app/ui-utils/client';
import type { MessageActionContext } from '../../../../app/ui-utils/client/lib/MessageAction';
import { t } from '../../../../app/utils/lib/i18n';
import { useReactiveQuery } from '../../../hooks/useReactiveQuery';
import { roomsQueryKeys } from '../../../lib/queryKeys';
import { useToggleFollowingThreadMutation } from '../../../views/room/contextualBar/Threads/hooks/useToggleFollowingThreadMutation';

export const useUnFollowMessageAction = (
message: IMessage,
{ room, user, context }: { room: IRoom; user: IUser | undefined; context: MessageActionContext },
) => {
const threadsEnabled = useSetting('Threads_enabled');

const dispatchToastMessage = useToastMessageDispatch();

const queryClient = useQueryClient();

const { mutate: toggleFollowingThread } = useToggleFollowingThreadMutation({
onSuccess: () => {
dispatchToastMessage({
type: 'success',
message: t('You_unfollowed_this_message'),
});
},
});

const { tmid, _id } = message;
const messageQuery = useReactiveQuery(
roomsQueryKeys.message(message.rid, message._id),
() => Messages.findOne({ _id: tmid || _id }, { fields: { replies: 1 } }) ?? null,
);

useEffect(() => {
if (!message || !threadsEnabled || isOmnichannelRoom(room)) {
return;
}

let { replies } = message;

if (tmid || context) {
const parentMessage = messageQuery.data;
if (parentMessage) {
replies = parentMessage.replies || [];
}
}

if (!user?._id) {
return;
}

if (!replies?.includes(user._id)) {
return;
}

MessageAction.addButton({
id: 'unfollow-message',
icon: 'bell-off',
label: 'Unfollow_message',
type: 'interaction',
context: ['message', 'message-mobile', 'threads', 'federated', 'videoconf', 'videoconf-threads'],
action() {
toggleFollowingThread({ tmid: tmid || _id, follow: false, rid: room._id });
},
order: 1,
group: 'menu',
});

return () => MessageAction.removeButton('unfollow-message');
}, [_id, context, message, messageQuery.data, queryClient, room, threadsEnabled, tmid, toggleFollowingThread, user]);
};
1 change: 1 addition & 0 deletions apps/meteor/client/lib/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const roomsQueryKeys = {
messageActions: (rid: IRoom['_id'], mid: IMessage['_id']) => [...roomsQueryKeys.message(rid, mid), 'actions'] as const,
messageActionsWithParameters: (rid: IRoom['_id'], message: IMessage | Serialized<IMessage>) =>
[...roomsQueryKeys.messageActions(rid, message._id), message] as const,
threads: (rid: IRoom['_id']) => [...roomsQueryKeys.room(rid), 'threads'] as const,
};

export const subscriptionsQueryKeys = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { useEndpoint } from '@rocket.chat/ui-contexts';
import type { UseMutationOptions, UseMutationResult } from '@tanstack/react-query';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import { roomsQueryKeys } from '../../../../../lib/queryKeys';

// TODO: its core should be moved to the ChatContext

type UseToggleFollowingThreadMutationVariables = {
Expand Down Expand Up @@ -31,7 +33,8 @@ export const useToggleFollowingThreadMutation = (
{
...options,
onSuccess: async (data, variables, context) => {
await queryClient.invalidateQueries(['rooms', variables.rid, 'threads']);
await queryClient.invalidateQueries(roomsQueryKeys.threads(variables.rid));
await queryClient.invalidateQueries(roomsQueryKeys.message(variables.rid, variables.tmid));
return options?.onSuccess?.(data, variables, context);
},
},
Expand Down
28 changes: 14 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8765,10 +8765,10 @@ __metadata:
"@rocket.chat/icons": "*"
"@rocket.chat/prettier-config": "*"
"@rocket.chat/styled": "*"
"@rocket.chat/ui-avatar": 9.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-avatar": 9.0.0-rc.1
"@rocket.chat/ui-contexts": 13.0.0-rc.1
"@rocket.chat/ui-kit": 0.37.0
"@rocket.chat/ui-video-conf": 13.0.0-rc.0
"@rocket.chat/ui-video-conf": 13.0.0-rc.1
"@tanstack/react-query": "*"
react: ~17.0.2
react-dom: "*"
Expand Down Expand Up @@ -8853,8 +8853,8 @@ __metadata:
"@rocket.chat/fuselage-tokens": "*"
"@rocket.chat/message-parser": 0.31.31
"@rocket.chat/styled": "*"
"@rocket.chat/ui-client": 13.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-client": 13.0.0-rc.1
"@rocket.chat/ui-contexts": 13.0.0-rc.1
katex: "*"
react: "*"
languageName: unknown
Expand Down Expand Up @@ -10089,7 +10089,7 @@ __metadata:
typescript: "npm:~5.7.2"
peerDependencies:
"@rocket.chat/fuselage": "*"
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.1
react: ~17.0.2
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -10139,8 +10139,8 @@ __metadata:
"@rocket.chat/fuselage": "*"
"@rocket.chat/fuselage-hooks": "*"
"@rocket.chat/icons": "*"
"@rocket.chat/ui-avatar": 9.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-avatar": 9.0.0-rc.1
"@rocket.chat/ui-contexts": 13.0.0-rc.1
react: "*"
react-i18next: "*"
languageName: unknown
Expand Down Expand Up @@ -10310,8 +10310,8 @@ __metadata:
"@rocket.chat/fuselage-hooks": "*"
"@rocket.chat/icons": "*"
"@rocket.chat/styled": "*"
"@rocket.chat/ui-avatar": 9.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-avatar": 9.0.0-rc.1
"@rocket.chat/ui-contexts": 13.0.0-rc.1
react: ~17.0.2
react-dom: ^17.0.2
languageName: unknown
Expand Down Expand Up @@ -10367,9 +10367,9 @@ __metadata:
"@rocket.chat/fuselage-hooks": "*"
"@rocket.chat/icons": "*"
"@rocket.chat/styled": "*"
"@rocket.chat/ui-avatar": 9.0.0-rc.0
"@rocket.chat/ui-client": 13.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-avatar": 9.0.0-rc.1
"@rocket.chat/ui-client": 13.0.0-rc.1
"@rocket.chat/ui-contexts": 13.0.0-rc.1
react: ~17.0.2
react-aria: ~3.23.1
react-dom: ^17.0.2
Expand Down Expand Up @@ -10457,7 +10457,7 @@ __metadata:
peerDependencies:
"@rocket.chat/layout": "*"
"@rocket.chat/tools": 0.2.2
"@rocket.chat/ui-contexts": 13.0.0-rc.0
"@rocket.chat/ui-contexts": 13.0.0-rc.1
"@tanstack/react-query": "*"
react: "*"
react-hook-form: "*"
Expand Down

0 comments on commit 6cfc6ce

Please sign in to comment.