-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Expand file tree
/
Copy pathuseGoToDirectMessage.ts
More file actions
39 lines (34 loc) · 1.66 KB
/
useGoToDirectMessage.ts
File metadata and controls
39 lines (34 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { usePermission, useUserSubscriptionByName, useRouter } from '@rocket.chat/ui-contexts';
// TODO: Routes type definitions are declared in-file for most places, so this route doesn't exist in this package
declare module '@rocket.chat/ui-contexts' {
export interface IRouterPaths {
direct: {
pathname: `/direct/:rid${`/${string}` | ''}${`/${string}` | ''}`;
pattern: '/direct/:rid/:tab?/:context?';
};
}
}
/**
* Hook to navigate to a direct message room
* @param targetUser - Object containing the username of the user to navigate to
* @param openRoomId - Optional ID of the room that is already open
* @returns A function to navigate to the direct message room, or undefined if the room is already open or the user doesn't have permission to create direct messages and doesn't have a subscription to the target user
*/
export const useGoToDirectMessage = (targetUser: { username?: string }, openRoomId?: string): (() => void) | undefined => {
const usernameSubscription = useUserSubscriptionByName(targetUser.username ?? '');
const router = useRouter();
const canOpenDirectMessage = usePermission('create-d');
const hasPermissionOrSubscription = usernameSubscription || canOpenDirectMessage;
const alreadyOpen = openRoomId && usernameSubscription?.rid === openRoomId;
const shouldOpen = targetUser.username && hasPermissionOrSubscription && !alreadyOpen;
const openDirectMessage = useEffectEvent(
() =>
targetUser.username &&
router.navigate({
name: 'direct' as const,
params: { rid: targetUser.username },
} as const),
);
return shouldOpen ? openDirectMessage : undefined;
};