-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added enable/disable voice call button to NavBarV2
- Loading branch information
1 parent
1dccef4
commit 5dd0a22
Showing
2 changed files
with
74 additions
and
0 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
69 changes: 69 additions & 0 deletions
69
apps/meteor/client/NavBarV2/NavBarSettingsToolbar/UserMenu/hooks/useVoiceCallItems.tsx
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,69 @@ | ||
import { Box } from '@rocket.chat/fuselage'; | ||
import { useToastMessageDispatch } from '@rocket.chat/ui-contexts'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import React, { useMemo } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import type { GenericMenuItemProps } from '../../../../components/GenericMenu/GenericMenuItem'; | ||
import { useVoiceCallAPI } from '../../../../providers/VoiceCallProvider/hooks/useVoiceCallAPI'; | ||
import { useVoiceCallState } from '../../../../providers/VoiceCallProvider/hooks/useVoiceCallState'; | ||
|
||
export const useVoiceCallItems = (): GenericMenuItemProps[] => { | ||
const { t } = useTranslation(); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
|
||
const { error, isEnabled, isReady, isRegistered } = useVoiceCallState(); | ||
const { register, unregister } = useVoiceCallAPI(); | ||
|
||
const toggleVoiceCall = useMutation({ | ||
mutationFn: async () => { | ||
if (!isRegistered) { | ||
await register(); | ||
return true; | ||
} | ||
|
||
await unregister(); | ||
return false; | ||
}, | ||
onSuccess: (isEnabled: boolean) => { | ||
dispatchToastMessage({ | ||
type: 'success', | ||
message: isEnabled ? t('Voice_calling_enabled') : t('Voice_calling_disabled'), | ||
}); | ||
}, | ||
}); | ||
|
||
const tooltip = useMemo(() => { | ||
if (error) { | ||
return error.message; | ||
} | ||
|
||
if (!isReady || toggleVoiceCall.isLoading) { | ||
return t('Loading'); | ||
} | ||
|
||
return ''; | ||
}, [error, isReady, toggleVoiceCall.isLoading, t]); | ||
|
||
return useMemo(() => { | ||
if (!isEnabled) { | ||
return []; | ||
} | ||
|
||
return [ | ||
{ | ||
id: 'toggle-voice-call', | ||
icon: isRegistered ? 'phone-disabled' : 'phone', | ||
disabled: !isReady || toggleVoiceCall.isLoading, | ||
onClick: () => toggleVoiceCall.mutate(), | ||
content: ( | ||
<Box is='span' title={tooltip}> | ||
{isRegistered ? t('Disable_voice_calling') : t('Enable_voice_calling')} | ||
</Box> | ||
), | ||
}, | ||
]; | ||
}, [isEnabled, isRegistered, isReady, tooltip, t, toggleVoiceCall]); | ||
}; | ||
|
||
export default useVoiceCallItems; |