chore: Remove hideRoom client meteor method (#33766)
parent
3a66c37edd
commit
8420101e2c
@ -0,0 +1,95 @@ |
||||
import type { RoomType } from '@rocket.chat/core-typings'; |
||||
import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; |
||||
import type { TranslationKey } from '@rocket.chat/ui-contexts'; |
||||
import { useEndpoint, useSetModal, useToastMessageDispatch, useRouter, useUserId } from '@rocket.chat/ui-contexts'; |
||||
import { useMutation } from '@tanstack/react-query'; |
||||
import React from 'react'; |
||||
import { useTranslation } from 'react-i18next'; |
||||
|
||||
import { useDontAskAgain } from './useDontAskAgain'; |
||||
import { UiTextContext } from '../../definition/IRoomTypeConfig'; |
||||
import { GenericModalDoNotAskAgain } from '../components/GenericModal'; |
||||
import { updateSubscription } from '../lib/mutationEffects/updateSubscription'; |
||||
import { roomCoordinator } from '../lib/rooms/roomCoordinator'; |
||||
|
||||
type HideRoomProps = { |
||||
rid: string; |
||||
type: RoomType; |
||||
name: string; |
||||
}; |
||||
|
||||
type HideRoomOptions = { |
||||
redirect?: boolean; |
||||
}; |
||||
|
||||
const CLOSE_ENDPOINTS_BY_ROOM_TYPE = { |
||||
p: '/v1/groups.close', // private
|
||||
c: '/v1/channels.close', // channel
|
||||
d: '/v1/im.close', // direct message
|
||||
v: '/v1/channels.close', // omnichannel voip
|
||||
l: '/v1/channels.close', // livechat
|
||||
} as const; |
||||
|
||||
export const useHideRoomAction = ({ rid: roomId, type, name }: HideRoomProps, { redirect = true }: HideRoomOptions = {}) => { |
||||
const { t } = useTranslation(); |
||||
const setModal = useSetModal(); |
||||
const closeModal = useEffectEvent(() => setModal()); |
||||
const dispatchToastMessage = useToastMessageDispatch(); |
||||
const dontAskHideRoom = useDontAskAgain('hideRoom'); |
||||
const router = useRouter(); |
||||
const userId = useUserId(); |
||||
|
||||
const hideRoomEndpoint = useEndpoint('POST', CLOSE_ENDPOINTS_BY_ROOM_TYPE[type]); |
||||
|
||||
const hideRoom = useMutation({ |
||||
mutationFn: () => hideRoomEndpoint({ roomId }), |
||||
onMutate: async () => { |
||||
closeModal(); |
||||
|
||||
if (userId) { |
||||
return updateSubscription(roomId, userId, { alert: false, open: false }); |
||||
} |
||||
}, |
||||
onSuccess: () => { |
||||
if (redirect) { |
||||
router.navigate('/home'); |
||||
} |
||||
}, |
||||
onError: async (error, _, rollbackDocument) => { |
||||
dispatchToastMessage({ type: 'error', message: error }); |
||||
|
||||
if (userId && rollbackDocument) { |
||||
const { alert, open } = rollbackDocument; |
||||
updateSubscription(roomId, userId, { alert, open }); |
||||
} |
||||
}, |
||||
}); |
||||
|
||||
const handleHide = useEffectEvent(async () => { |
||||
const warnText = roomCoordinator.getRoomDirectives(type).getUiText(UiTextContext.HIDE_WARNING); |
||||
|
||||
if (dontAskHideRoom) { |
||||
hideRoom.mutate(); |
||||
return; |
||||
} |
||||
|
||||
setModal( |
||||
<GenericModalDoNotAskAgain |
||||
variant='danger' |
||||
confirmText={t('Yes_hide_it')} |
||||
cancelText={t('Cancel')} |
||||
onClose={closeModal} |
||||
onCancel={closeModal} |
||||
onConfirm={() => hideRoom.mutate()} |
||||
dontAskAgain={{ |
||||
action: 'hideRoom', |
||||
label: t('Hide_room'), |
||||
}} |
||||
> |
||||
{t(warnText as TranslationKey, { postProcess: 'sprintf', sprintf: [name] })} |
||||
</GenericModalDoNotAskAgain>, |
||||
); |
||||
}); |
||||
|
||||
return handleHide; |
||||
}; |
||||
@ -0,0 +1,11 @@ |
||||
import type { ISubscription } from '@rocket.chat/core-typings'; |
||||
|
||||
import { Subscriptions } from '../../../app/models/client'; |
||||
|
||||
export const updateSubscription = (roomId: string, userId: string, data: Partial<ISubscription>) => { |
||||
const oldDocument = Subscriptions.findOne({ 'rid': roomId, 'u._id': userId }); |
||||
|
||||
Subscriptions.update({ 'rid': roomId, 'u._id': userId }, { $set: data }); |
||||
|
||||
return oldDocument; |
||||
}; |
||||
@ -1,25 +0,0 @@ |
||||
import type { ServerMethods } from '@rocket.chat/ddp-client'; |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { Subscriptions } from '../../app/models/client'; |
||||
|
||||
Meteor.methods<ServerMethods>({ |
||||
async hideRoom(rid) { |
||||
if (!Meteor.userId()) { |
||||
return 0; |
||||
} |
||||
|
||||
return Subscriptions.update( |
||||
{ |
||||
rid, |
||||
'u._id': Meteor.userId(), |
||||
}, |
||||
{ |
||||
$set: { |
||||
alert: false, |
||||
open: false, |
||||
}, |
||||
}, |
||||
); |
||||
}, |
||||
}); |
||||
@ -1,43 +0,0 @@ |
||||
import type { IRoom } from '@rocket.chat/core-typings'; |
||||
import { useMutableCallback } from '@rocket.chat/fuselage-hooks'; |
||||
import type { TranslationKey } from '@rocket.chat/ui-contexts'; |
||||
import { useSetModal, useToastMessageDispatch, useMethod, useTranslation, useRouter } from '@rocket.chat/ui-contexts'; |
||||
import React from 'react'; |
||||
|
||||
import { UiTextContext } from '../../../../../../../definition/IRoomTypeConfig'; |
||||
import WarningModal from '../../../../../../components/WarningModal'; |
||||
import { roomCoordinator } from '../../../../../../lib/rooms/roomCoordinator'; |
||||
|
||||
export const useRoomHide = (room: IRoom) => { |
||||
const t = useTranslation(); |
||||
const setModal = useSetModal(); |
||||
const dispatchToastMessage = useToastMessageDispatch(); |
||||
const hideRoom = useMethod('hideRoom'); |
||||
const router = useRouter(); |
||||
|
||||
const handleHide = useMutableCallback(async () => { |
||||
const hide = async () => { |
||||
try { |
||||
await hideRoom(room._id); |
||||
router.navigate('/home'); |
||||
} catch (error) { |
||||
dispatchToastMessage({ type: 'error', message: error }); |
||||
} |
||||
setModal(null); |
||||
}; |
||||
|
||||
const warnText = roomCoordinator.getRoomDirectives(room.t).getUiText(UiTextContext.HIDE_WARNING); |
||||
|
||||
setModal( |
||||
<WarningModal |
||||
text={t(warnText as TranslationKey, room.fname || room.name)} |
||||
confirmText={t('Yes_hide_it')} |
||||
close={() => setModal(null)} |
||||
cancelText={t('Cancel')} |
||||
confirm={hide} |
||||
/>, |
||||
); |
||||
}); |
||||
|
||||
return handleHide; |
||||
}; |
||||
Loading…
Reference in new issue