Skip to content

Commit

Permalink
chore: remove meteor.startup from unstar-message (#34018)
Browse files Browse the repository at this point in the history
Co-authored-by: Tasso <[email protected]>
  • Loading branch information
MartinSchoeler and tassoevan authored Nov 23, 2024
1 parent fe9be01 commit 0d5f794
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { useToastMessageDispatch, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQueryClient, useMutation } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';

import { toggleStarredMessage } from '../../../lib/mutationEffects/starredMessage';
import { roomsQueryKeys } from '../../../lib/queryKeys';

export const useStarMessageMutation = () => {
const { t } = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

const queryClient = useQueryClient();
Expand All @@ -18,6 +20,7 @@ export const useStarMessageMutation = () => {
},
onSuccess: (_data, message) => {
toggleStarredMessage(message, true);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_starred') });
},
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { IMessage } from '@rocket.chat/core-typings';
import { useToastMessageDispatch, useEndpoint } from '@rocket.chat/ui-contexts';
import { useQueryClient, useMutation } from '@tanstack/react-query';
import { useTranslation } from 'react-i18next';

import { toggleStarredMessage } from '../../../lib/mutationEffects/starredMessage';
import { roomsQueryKeys } from '../../../lib/queryKeys';

export const useUnstarMessageMutation = () => {
const { t } = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

const queryClient = useQueryClient();

const unstarMessage = useEndpoint('POST', '/v1/chat.unStarMessage');

return useMutation({
mutationFn: async (message: IMessage) => {
await unstarMessage({ messageId: message._id });
},
onSuccess: (_data, message) => {
toggleStarredMessage(message, false);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_unstarred') });
},
onError: (error) => {
dispatchToastMessage({ type: 'error', message: error });
},
onSettled: (_data, _error, message) => {
queryClient.invalidateQueries(roomsQueryKeys.starredMessages(message.rid));
queryClient.invalidateQueries(roomsQueryKeys.messageActions(message.rid, message._id));
},
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import MessageActionMenu from './MessageActionMenu';
import MessageToolbarStarsActionMenu from './MessageToolbarStarsActionMenu';
import { useNewDiscussionMessageAction } from './useNewDiscussionMessageAction';
import { useStarMessageAction } from './useStarMessageAction';
import { useUnstarMessageAction } from './useUnstarMessageAction';
import { useWebDAVMessageAction } from './useWebDAVMessageAction';
import type { MessageActionContext } from '../../../../app/ui-utils/client/lib/MessageAction';
import { MessageAction } from '../../../../app/ui-utils/client/lib/MessageAction';
Expand Down Expand Up @@ -90,6 +91,7 @@ const MessageToolbar = ({
useWebDAVMessageAction();
useNewDiscussionMessageAction();
useStarMessageAction(message, { room, user });
useUnstarMessageAction(message, { room, user });

const actionsQueryResult = useQuery({
queryKey: roomsQueryKeys.messageActionsWithParameters(room._id, message),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useStarMessageAction = (message: IMessage, { room, user }: { room:
return;
}

if (Array.isArray(message.starred) && message.starred.find((star) => star._id === user?._id)) {
if (Array.isArray(message.starred) && message.starred.some((star) => star._id === user?._id)) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { IMessage, IRoom, IUser } from '@rocket.chat/core-typings';
import { isOmnichannelRoom } from '@rocket.chat/core-typings';
import { useSetting } from '@rocket.chat/ui-contexts';
import { useEffect } from 'react';

import { MessageAction } from '../../../../app/ui-utils/client/lib/MessageAction';
import { useUnstarMessageMutation } from '../hooks/useUnstarMessageMutation';

export const useUnstarMessageAction = (message: IMessage, { room, user }: { room: IRoom; user: IUser | undefined }) => {
const allowStarring = useSetting('Message_AllowStarring');

const { mutateAsync: unstarMessage } = useUnstarMessageMutation();

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

if (!Array.isArray(message.starred) || message.starred.every((star) => star._id !== user?._id)) {
return;
}

MessageAction.addButton({
id: 'unstar-message',
icon: 'star',
label: 'Unstar_Message',
type: 'interaction',
context: ['starred', 'message', 'message-mobile', 'threads', 'federated', 'videoconf', 'videoconf-threads'],
async action() {
await unstarMessage(message);
},
order: 3,
group: 'menu',
});

return () => {
MessageAction.removeButton('unstar-message');
};
}, [allowStarring, message, room, unstarMessage, user?._id]);
};
4 changes: 0 additions & 4 deletions apps/meteor/client/lib/mutationEffects/starredMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import type { IMessage } from '@rocket.chat/core-typings';
import { Meteor } from 'meteor/meteor';

import { Messages } from '../../../app/models/client';
import { t } from '../../../app/utils/lib/i18n';
import { dispatchToastMessage } from '../toast';

export const toggleStarredMessage = (message: IMessage, starred: boolean) => {
const uid = Meteor.userId()!;
Expand All @@ -17,7 +15,6 @@ export const toggleStarredMessage = (message: IMessage, starred: boolean) => {
},
},
);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_starred') });
return;
}

Expand All @@ -29,5 +26,4 @@ export const toggleStarredMessage = (message: IMessage, starred: boolean) => {
},
},
);
dispatchToastMessage({ type: 'success', message: t('Message_has_been_unstarred') });
};
1 change: 0 additions & 1 deletion apps/meteor/client/startup/actionButtons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,3 @@ import './permalinkPinned';
import './permalinkStar';
import './pinMessage';
import './unpinMessage';
import './unstarMessage';
39 changes: 0 additions & 39 deletions apps/meteor/client/startup/actionButtons/unstarMessage.ts

This file was deleted.

0 comments on commit 0d5f794

Please sign in to comment.