-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Expand file tree
/
Copy pathuseMediaCallInternalHistoryActions.ts
More file actions
84 lines (72 loc) · 2.17 KB
/
useMediaCallInternalHistoryActions.ts
File metadata and controls
84 lines (72 loc) · 2.17 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { useEffectEvent } from '@rocket.chat/fuselage-hooks';
import { useGoToDirectMessage } from '@rocket.chat/ui-client';
import { useRouter, useUserAvatarPath } from '@rocket.chat/ui-contexts';
import { useWidgetExternalControls, usePeekMediaSessionState } from '@rocket.chat/ui-voip';
import { useMemo } from 'react';
export type InternalCallHistoryContact = {
_id: string;
name?: string;
username: string;
displayName?: string;
voiceCallExtension?: string;
avatarUrl?: string;
};
type UseMediaCallInternalHistoryActionsBaseOptions = {
contact: InternalCallHistoryContact;
messageId?: string;
openRoomId?: string;
messageRoomId?: string;
openUserInfo?: (userId: string) => void;
};
export const useMediaCallInternalHistoryActions = ({
contact,
messageId,
openRoomId,
messageRoomId,
openUserInfo,
}: UseMediaCallInternalHistoryActionsBaseOptions) => {
const state = usePeekMediaSessionState();
const { toggleWidget } = useWidgetExternalControls();
const router = useRouter();
const getAvatarUrl = useUserAvatarPath();
const voiceCall = useEffectEvent(() => {
if (state !== 'available') {
return;
}
toggleWidget({
userId: contact._id,
displayName: contact.displayName ?? '',
username: contact.username,
avatarUrl: getAvatarUrl({ username: contact.username }),
callerId: contact.voiceCallExtension,
});
});
const goToDirectMessage = useGoToDirectMessage({ username: contact.username }, openRoomId ?? '');
const jumpToMessage = useEffectEvent(() => {
const rid = messageRoomId || openRoomId;
if (!messageId || !rid) {
return;
}
const { msg: _, ...searchParams } = router.getSearchParameters();
router.navigate({
name: 'direct',
params: { rid },
search: { ...searchParams, msg: messageId },
});
});
const userInfo = useEffectEvent(() => {
if (!openUserInfo) {
return;
}
openUserInfo(contact._id);
});
return useMemo(
() => ({
voiceCall,
directMessage: goToDirectMessage,
jumpToMessage: messageId && messageRoomId ? jumpToMessage : undefined,
userInfo: openUserInfo ? () => userInfo() : undefined,
}),
[voiceCall, goToDirectMessage, messageId, messageRoomId, jumpToMessage, openUserInfo, userInfo],
);
};