-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
33dbe1d
commit ab5b1a4
Showing
18 changed files
with
307 additions
and
483 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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, useRouter, useSetModal, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { LegacyRoomManager } from '../../../app/ui-utils/client'; | ||
import { UiTextContext } from '../../../definition/IRoomTypeConfig'; | ||
import WarningModal from '../../components/WarningModal'; | ||
import { roomCoordinator } from '../../lib/rooms/roomCoordinator'; | ||
|
||
const leaveEndpoints = { | ||
p: '/v1/groups.leave', | ||
c: '/v1/channels.leave', | ||
d: '/v1/im.leave', | ||
v: '/v1/channels.leave', | ||
l: '/v1/groups.leave', | ||
} as const; | ||
|
||
type LeaveRoomProps = { | ||
rid: string; | ||
type: RoomType; | ||
name: string; | ||
roomOpen?: boolean; | ||
}; | ||
|
||
// TODO: check leaving modal for teams | ||
export const useLeaveRoomAction = ({ rid, type, name, roomOpen }: LeaveRoomProps) => { | ||
const { t } = useTranslation(); | ||
const setModal = useSetModal(); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const router = useRouter(); | ||
|
||
const leaveRoom = useEndpoint('POST', leaveEndpoints[type]); | ||
|
||
const handleLeave = useEffectEvent(() => { | ||
const leave = async (): Promise<void> => { | ||
try { | ||
await leaveRoom({ roomId: rid }); | ||
if (roomOpen) { | ||
router.navigate('/home'); | ||
} | ||
LegacyRoomManager.close(rid); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: error }); | ||
} finally { | ||
setModal(null); | ||
} | ||
}; | ||
|
||
const warnText = roomCoordinator.getRoomDirectives(type).getUiText(UiTextContext.LEAVE_WARNING); | ||
|
||
setModal( | ||
<WarningModal | ||
text={t(warnText as TranslationKey, name)} | ||
confirmText={t('Leave_room')} | ||
close={() => setModal(null)} | ||
cancelText={t('Cancel')} | ||
confirm={leave} | ||
/>, | ||
); | ||
}); | ||
|
||
return handleLeave; | ||
}; |
18 changes: 18 additions & 0 deletions
18
apps/meteor/client/hooks/menuActions/useToggleFavoriteAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { IRoom } from '@rocket.chat/core-typings'; | ||
import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; | ||
import { useEndpoint, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
|
||
export const useToggleFavoriteAction = ({ rid, isFavorite }: { rid: IRoom['_id']; isFavorite: boolean }) => { | ||
const toggleFavorite = useEndpoint('POST', '/v1/rooms.favorite'); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
|
||
const handleToggleFavorite = useEffectEvent(async () => { | ||
try { | ||
await toggleFavorite({ roomId: rid, favorite: !isFavorite }); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: error }); | ||
} | ||
}); | ||
|
||
return handleToggleFavorite; | ||
}; |
48 changes: 48 additions & 0 deletions
48
apps/meteor/client/hooks/menuActions/useToggleReadAction.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import type { ISubscription } from '@rocket.chat/core-typings'; | ||
import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; | ||
import { useEndpoint, useMethod, useRouter, useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
|
||
import { LegacyRoomManager } from '../../../app/ui-utils/client'; | ||
|
||
type ToggleReadActionProps = { | ||
rid: string; | ||
isUnread?: boolean; | ||
subscription?: ISubscription; | ||
}; | ||
|
||
export const useToggleReadAction = ({ rid, isUnread, subscription }: ToggleReadActionProps) => { | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const queryClient = useQueryClient(); | ||
const router = useRouter(); | ||
|
||
const readMessages = useEndpoint('POST', '/v1/subscriptions.read'); | ||
const unreadMessages = useMethod('unreadMessages'); | ||
|
||
const handleToggleRead = useEffectEvent(async () => { | ||
try { | ||
queryClient.invalidateQueries({ | ||
queryKey: ['sidebar/search/spotlight'], | ||
}); | ||
|
||
if (isUnread) { | ||
await readMessages({ rid, readThreads: true }); | ||
return; | ||
} | ||
|
||
if (subscription == null) { | ||
return; | ||
} | ||
|
||
LegacyRoomManager.close(subscription.t + subscription.name); | ||
|
||
router.navigate('/home'); | ||
|
||
await unreadMessages(undefined, rid); | ||
} catch (error) { | ||
dispatchToastMessage({ type: 'error', message: error }); | ||
} | ||
}); | ||
|
||
return handleToggleRead; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import type { RoomType } from '@rocket.chat/core-typings'; | ||
import type { GenericMenuItemProps } from '@rocket.chat/ui-client'; | ||
import { usePermission, useSetting, useUserSubscription } from '@rocket.chat/ui-contexts'; | ||
import type { Fields } from '@rocket.chat/ui-contexts'; | ||
import { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import { useLeaveRoomAction } from './menuActions/useLeaveRoom'; | ||
import { useToggleFavoriteAction } from './menuActions/useToggleFavoriteAction'; | ||
import { useToggleReadAction } from './menuActions/useToggleReadAction'; | ||
import { useHideRoomAction } from './useHideRoomAction'; | ||
import { useOmnichannelPrioritiesMenu } from '../omnichannel/hooks/useOmnichannelPrioritiesMenu'; | ||
|
||
const fields: Fields = { | ||
f: true, | ||
t: true, | ||
name: true, | ||
}; | ||
|
||
type RoomMenuActionsProps = { | ||
rid: string; | ||
type: RoomType; | ||
name: string; | ||
isUnread?: boolean; | ||
cl?: boolean; | ||
roomOpen?: boolean; | ||
hideDefaultOptions: boolean; | ||
}; | ||
|
||
export const useRoomMenuActions = ({ | ||
rid, | ||
type, | ||
name, | ||
isUnread, | ||
cl, | ||
roomOpen, | ||
hideDefaultOptions, | ||
}: RoomMenuActionsProps): { title: string; items: GenericMenuItemProps[] }[] => { | ||
const { t } = useTranslation(); | ||
const subscription = useUserSubscription(rid, fields); | ||
|
||
const isFavorite = Boolean(subscription?.f); | ||
const canLeaveChannel = usePermission('leave-c'); | ||
const canLeavePrivate = usePermission('leave-p'); | ||
const canFavorite = useSetting('Favorite_Rooms') as boolean; | ||
|
||
const canLeave = ((): boolean => { | ||
if (type === 'c' && !canLeaveChannel) { | ||
return false; | ||
} | ||
if (type === 'p' && !canLeavePrivate) { | ||
return false; | ||
} | ||
return !((cl != null && !cl) || ['d', 'l'].includes(type)); | ||
})(); | ||
|
||
const handleHide = useHideRoomAction({ rid, type, name }, { redirect: false }); | ||
const handleToggleFavorite = useToggleFavoriteAction({ rid, isFavorite }); | ||
const handleToggleRead = useToggleReadAction({ rid, isUnread, subscription }); | ||
const handleLeave = useLeaveRoomAction({ rid, type, name, roomOpen }); | ||
|
||
const isOmnichannelRoom = type === 'l'; | ||
const prioritiesMenu = useOmnichannelPrioritiesMenu(rid); | ||
|
||
const menuOptions = useMemo( | ||
() => | ||
!hideDefaultOptions | ||
? [ | ||
!isOmnichannelRoom && { | ||
id: 'hideRoom', | ||
icon: 'eye-off', | ||
content: t('Hide'), | ||
onClick: handleHide, | ||
}, | ||
{ | ||
id: 'toggleRead', | ||
icon: 'flag', | ||
content: isUnread ? t('Mark_read') : t('Mark_unread'), | ||
onClick: handleToggleRead, | ||
}, | ||
canFavorite && { | ||
id: 'toggleFavorite', | ||
icon: isFavorite ? 'star-filled' : 'star', | ||
content: isFavorite ? t('Unfavorite') : t('Favorite'), | ||
onClick: handleToggleFavorite, | ||
}, | ||
canLeave && { | ||
id: 'leaveRoom', | ||
icon: 'sign-out', | ||
content: t('Leave_room'), | ||
onClick: handleLeave, | ||
}, | ||
] | ||
: [], | ||
[ | ||
hideDefaultOptions, | ||
t, | ||
handleHide, | ||
isUnread, | ||
handleToggleRead, | ||
canFavorite, | ||
isFavorite, | ||
handleToggleFavorite, | ||
canLeave, | ||
handleLeave, | ||
isOmnichannelRoom, | ||
], | ||
); | ||
|
||
if (isOmnichannelRoom && prioritiesMenu.length > 0) { | ||
return [ | ||
{ title: '', items: menuOptions.filter(Boolean) as GenericMenuItemProps[] }, | ||
{ title: t('Priorities'), items: prioritiesMenu }, | ||
]; | ||
} | ||
|
||
return [{ title: '', items: menuOptions.filter(Boolean) as GenericMenuItemProps[] }]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.