mattermost-mobile/app/products/calls/hooks.ts
Elias Nahum 7aa5bd0611
Update Dependencies and bug fixes (#7000)
* update dependencies

* update dependencies

* feedback review

* update @mattermost/react-native-turbo-mailer
2023-01-24 09:14:23 +02:00

102 lines
3.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// Check if calls is enabled. If it is, then run fn; if it isn't, show an alert and set
// msgPostfix to ' (Not Available)'.
import {useCallback, useEffect, useState} from 'react';
import {useIntl} from 'react-intl';
import {Alert, Platform} from 'react-native';
import Permissions from 'react-native-permissions';
import {initializeVoiceTrack} from '@calls/actions/calls';
import {setMicPermissionsGranted} from '@calls/state';
import {errorAlert} from '@calls/utils';
import {useServerUrl} from '@context/server';
import {useAppState} from '@hooks/device';
import NetworkManager from '@managers/network_manager';
import type {Client} from '@client/rest';
import type ClientError from '@client/rest/error';
export const useTryCallsFunction = (fn: () => void) => {
const intl = useIntl();
const serverUrl = useServerUrl();
const [msgPostfix, setMsgPostfix] = useState('');
const [clientError, setClientError] = useState('');
let client: Client | undefined;
if (!clientError) {
try {
client = NetworkManager.getClient(serverUrl);
} catch (error) {
setClientError((error as ClientError).message);
}
}
const tryFn = useCallback(async () => {
if (client && await client.getEnabled()) {
setMsgPostfix('');
fn();
return;
}
if (clientError) {
errorAlert(clientError, intl);
return;
}
const title = intl.formatMessage({
id: 'mobile.calls_not_available_title',
defaultMessage: 'Calls is not enabled',
});
const message = intl.formatMessage({
id: 'mobile.calls_not_available_msg',
defaultMessage: 'Please contact your System Admin to enable the feature.',
});
const ok = intl.formatMessage({
id: 'mobile.calls_ok',
defaultMessage: 'OK',
});
const notAvailable = intl.formatMessage({
id: 'mobile.calls_not_available_option',
defaultMessage: '(Not available)',
});
Alert.alert(
title,
message,
[
{
text: ok,
style: 'cancel',
},
],
);
setMsgPostfix(` ${notAvailable}`);
}, [client, fn, clientError, intl]);
return [tryFn, msgPostfix] as [() => Promise<void>, string];
};
const micPermission = Platform.select({
ios: Permissions.PERMISSIONS.IOS.MICROPHONE,
default: Permissions.PERMISSIONS.ANDROID.RECORD_AUDIO,
});
export const usePermissionsChecker = (micPermissionsGranted: boolean) => {
const appState = useAppState();
useEffect(() => {
const asyncFn = async () => {
if (appState === 'active') {
const hasPermission = (await Permissions.check(micPermission)) === Permissions.RESULTS.GRANTED;
if (hasPermission) {
initializeVoiceTrack();
setMicPermissionsGranted(hasPermission);
}
}
};
if (!micPermissionsGranted) {
asyncFn();
}
}, [appState]);
};