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,47 +32,42 @@ 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 () => { const canInitAnCall = async (): Promise<boolean> => {
if (callEnabled) { if (!callEnabled) return false;
if (isServer5OrNewer) {
try { if (isServer5OrNewer) {
await Services.videoConferenceGetCapabilities(); try {
return true; await Services.videoConferenceGetCapabilities();
} catch (error: any) { return true;
const isAdmin = !!user.roles?.includes('admin'); } catch (error: any) {
switch (error?.error) { const isAdmin = !!user.roles?.includes('admin');
case availabilityErrors.NOT_CONFIGURED: handleErrors(isAdmin, error?.error || 'NOT_CONFIGURED');
return handleErrors(isAdmin, availabilityErrors.NOT_CONFIGURED); return false;
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 false; return true;
}; };
const showInitCallActionSheet = async () => { const showInitCallActionSheet = async () => {
const canInit = await canInitAnCall(); try {
if (canInit) { const canInit = await canInitAnCall();
showActionSheet({ if (canInit) {
children: <StartACallActionSheet rid={rid} />, showActionSheet({
snaps: [480] children: <StartACallActionSheet rid={rid} />,
}); snaps: [480]
if (!permission?.granted) { });
requestPermission();
handleAndroidBltPermission(); if (!permission?.granted) {
requestPermission();
handleAndroidBltPermission();
}
} }
} catch (error) {
log(error);
} }
}; };