chore: improve useVideoConf code (#5214)

* improve useVideoConf code

* add log
This commit is contained in:
Gleidson Daniel Silva 2023-09-19 16:49:04 -03:00 committed by GitHub
parent 0aa8a57630
commit 08a29573b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 32 additions and 36 deletions

View File

@ -1,10 +1,11 @@
import { Camera } from 'expo-camera'; import { Camera } from 'expo-camera';
import React from 'react'; import React, { useMemo } from 'react';
import { useActionSheet } from '../../../containers/ActionSheet'; import { useActionSheet } from '../../../containers/ActionSheet';
import i18n from '../../../i18n'; import i18n from '../../../i18n';
import { getUserSelector } from '../../../selectors/login'; import { getUserSelector } from '../../../selectors/login';
import { compareServerVersion, showErrorAlert } from '../../methods/helpers'; import { compareServerVersion, showErrorAlert } from '../../methods/helpers';
import log from '../../methods/helpers/log';
import { handleAndroidBltPermission } from '../../methods/videoConf'; import { handleAndroidBltPermission } from '../../methods/videoConf';
import { Services } from '../../services'; import { Services } from '../../services';
import { useAppSelector } from '../useAppSelector'; import { useAppSelector } from '../useAppSelector';
@ -17,9 +18,9 @@ const availabilityErrors = {
NO_APP: 'no-videoconf-provider-app' NO_APP: 'no-videoconf-provider-app'
} as const; } as const;
const handleErrors = (isAdmin: boolean, error: typeof availabilityErrors[keyof typeof availabilityErrors]) => { const handleErrors = (isAdmin: boolean, error: keyof typeof availabilityErrors) => {
if (isAdmin) return showErrorAlert(i18n.t(`admin-${error}-body`), i18n.t(`admin-${error}-header`)); const key = isAdmin ? `admin-${error}` : error;
return showErrorAlert(i18n.t(`${error}-body`), i18n.t(`${error}-header`)); showErrorAlert(i18n.t(`${key}-body`), i18n.t(`${key}-header`));
}; };
export const useVideoConf = ( export const useVideoConf = (
@ -31,48 +32,43 @@ export const useVideoConf = (
const { callEnabled, disabledTooltip } = useVideoConfCall(rid); const { callEnabled, disabledTooltip } = useVideoConfCall(rid);
const [permission, requestPermission] = Camera.useCameraPermissions(); const [permission, requestPermission] = Camera.useCameraPermissions();
const { showActionSheet } = useActionSheet(); const { showActionSheet } = useActionSheet();
const isServer5OrNewer = compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0'); const isServer5OrNewer = useMemo(() => compareServerVersion(serverVersion, 'greaterThanOrEqualTo', '5.0.0'), [serverVersion]);
const canInitAnCall = async (): Promise<boolean> => {
if (!callEnabled) return false;
const canInitAnCall = async () => {
if (callEnabled) {
if (isServer5OrNewer) { if (isServer5OrNewer) {
try { try {
await Services.videoConferenceGetCapabilities(); await Services.videoConferenceGetCapabilities();
return true; return true;
} catch (error: any) { } catch (error: any) {
const isAdmin = !!user.roles?.includes('admin'); const isAdmin = !!user.roles?.includes('admin');
switch (error?.error) { handleErrors(isAdmin, error?.error || 'NOT_CONFIGURED');
case availabilityErrors.NOT_CONFIGURED: return false;
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);
}
} }
} }
return true; return true;
}
return false;
}; };
const showInitCallActionSheet = async () => { const showInitCallActionSheet = async () => {
try {
const canInit = await canInitAnCall(); const canInit = await canInitAnCall();
if (canInit) { if (canInit) {
showActionSheet({ showActionSheet({
children: <StartACallActionSheet rid={rid} />, children: <StartACallActionSheet rid={rid} />,
snaps: [480] snaps: [480]
}); });
if (!permission?.granted) { if (!permission?.granted) {
requestPermission(); requestPermission();
handleAndroidBltPermission(); handleAndroidBltPermission();
} }
} }
} catch (error) {
log(error);
}
}; };
return { showInitCallActionSheet, callEnabled, disabledTooltip }; return { showInitCallActionSheet, callEnabled, disabledTooltip };