From 08a29573b461416539c643e3c963224f6ebe2215 Mon Sep 17 00:00:00 2001 From: Gleidson Daniel Silva Date: Tue, 19 Sep 2023 16:49:04 -0300 Subject: [PATCH] chore: improve useVideoConf code (#5214) * improve useVideoConf code * add log --- app/lib/hooks/useVideoConf/index.tsx | 68 +++++++++++++--------------- 1 file changed, 32 insertions(+), 36 deletions(-) diff --git a/app/lib/hooks/useVideoConf/index.tsx b/app/lib/hooks/useVideoConf/index.tsx index 7e07b0c34..99a76cc71 100644 --- a/app/lib/hooks/useVideoConf/index.tsx +++ b/app/lib/hooks/useVideoConf/index.tsx @@ -1,10 +1,11 @@ import { Camera } from 'expo-camera'; -import React from 'react'; +import React, { useMemo } from 'react'; import { useActionSheet } from '../../../containers/ActionSheet'; import i18n from '../../../i18n'; import { getUserSelector } from '../../../selectors/login'; import { compareServerVersion, showErrorAlert } from '../../methods/helpers'; +import log from '../../methods/helpers/log'; import { handleAndroidBltPermission } from '../../methods/videoConf'; import { Services } from '../../services'; import { useAppSelector } from '../useAppSelector'; @@ -17,9 +18,9 @@ const availabilityErrors = { NO_APP: 'no-videoconf-provider-app' } as const; -const handleErrors = (isAdmin: boolean, error: typeof availabilityErrors[keyof typeof availabilityErrors]) => { - if (isAdmin) return showErrorAlert(i18n.t(`admin-${error}-body`), i18n.t(`admin-${error}-header`)); - return showErrorAlert(i18n.t(`${error}-body`), i18n.t(`${error}-header`)); +const handleErrors = (isAdmin: boolean, error: keyof typeof availabilityErrors) => { + const key = isAdmin ? `admin-${error}` : error; + showErrorAlert(i18n.t(`${key}-body`), i18n.t(`${key}-header`)); }; export const useVideoConf = ( @@ -31,47 +32,42 @@ export const useVideoConf = ( const { callEnabled, disabledTooltip } = useVideoConfCall(rid); const [permission, requestPermission] = Camera.useCameraPermissions(); - const { showActionSheet } = useActionSheet(); - const isServer5OrNewer = compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0'); + const isServer5OrNewer = useMemo(() => compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0'), [serverVersion]); - const canInitAnCall = async () => { - if (callEnabled) { - if (isServer5OrNewer) { - try { - await Services.videoConferenceGetCapabilities(); - return true; - } catch (error: any) { - const isAdmin = !!user.roles?.includes('admin'); - switch (error?.error) { - case availabilityErrors.NOT_CONFIGURED: - return handleErrors(isAdmin, availabilityErrors.NOT_CONFIGURED); - case availabilityErrors.NOT_ACTIVE: - return handleErrors(isAdmin, availabilityErrors.NOT_ACTIVE); - case availabilityErrors.NO_APP: - return handleErrors(isAdmin, availabilityErrors.NO_APP); - default: - return handleErrors(isAdmin, availabilityErrors.NOT_CONFIGURED); - } - } + const canInitAnCall = async (): Promise => { + if (!callEnabled) return false; + + if (isServer5OrNewer) { + try { + await Services.videoConferenceGetCapabilities(); + return true; + } catch (error: any) { + const isAdmin = !!user.roles?.includes('admin'); + handleErrors(isAdmin, error?.error || 'NOT_CONFIGURED'); + return false; } - return true; } - return false; + return true; }; const showInitCallActionSheet = async () => { - const canInit = await canInitAnCall(); - if (canInit) { - showActionSheet({ - children: , - snaps: [480] - }); - if (!permission?.granted) { - requestPermission(); - handleAndroidBltPermission(); + try { + const canInit = await canInitAnCall(); + if (canInit) { + showActionSheet({ + children: , + snaps: [480] + }); + + if (!permission?.granted) { + requestPermission(); + handleAndroidBltPermission(); + } } + } catch (error) { + log(error); } };