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 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,48 +32,43 @@ 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 (): Promise<boolean> => {
if (!callEnabled) return false;
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);
}
handleErrors(isAdmin, error?.error || 'NOT_CONFIGURED');
return false;
}
}
return true;
}
return false;
};
const showInitCallActionSheet = async () => {
try {
const canInit = await canInitAnCall();
if (canInit) {
showActionSheet({
children: <StartACallActionSheet rid={rid} />,
snaps: [480]
});
if (!permission?.granted) {
requestPermission();
handleAndroidBltPermission();
}
}
} catch (error) {
log(error);
}
};
return { showInitCallActionSheet, callEnabled, disabledTooltip };