2018-07-11 09:42:43 +00:00
|
|
|
// @flow
|
2017-01-05 21:06:41 +00:00
|
|
|
|
2017-10-13 16:13:46 +00:00
|
|
|
import React from 'react';
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2019-01-22 10:32:18 +00:00
|
|
|
import { setColorScheme } from '../../base/color-scheme';
|
2018-09-03 13:53:24 +00:00
|
|
|
import { DialogContainer } from '../../base/dialog';
|
2020-07-22 10:50:10 +00:00
|
|
|
import { updateFlags } from '../../base/flags/actions';
|
|
|
|
import { CALL_INTEGRATION_ENABLED, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
|
|
|
|
import { getFeatureFlag } from '../../base/flags/functions';
|
2017-02-10 16:13:39 +00:00
|
|
|
import { Platform } from '../../base/react';
|
2020-06-02 09:03:17 +00:00
|
|
|
import { DimensionsDetector, clientResized } from '../../base/responsive-ui';
|
2019-06-11 13:38:33 +00:00
|
|
|
import { updateSettings } from '../../base/settings';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from '../logger';
|
2020-05-20 10:57:03 +00:00
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
import { AbstractApp } from './AbstractApp';
|
2018-07-11 09:42:43 +00:00
|
|
|
import type { Props as AbstractAppProps } from './AbstractApp';
|
|
|
|
|
2020-06-04 14:09:13 +00:00
|
|
|
// Register middlewares and reducers.
|
|
|
|
import '../middlewares';
|
|
|
|
import '../reducers';
|
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
declare var __DEV__;
|
2016-10-05 14:36:59 +00:00
|
|
|
|
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* The type of React {@code Component} props of {@link App}.
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2018-07-11 09:42:43 +00:00
|
|
|
type Props = AbstractAppProps & {
|
|
|
|
|
2019-01-22 10:32:18 +00:00
|
|
|
/**
|
|
|
|
* An object of colors that override the default colors of the app/sdk.
|
|
|
|
*/
|
2019-03-05 11:06:38 +00:00
|
|
|
colorScheme: ?Object,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Identifier for this app on the native side.
|
|
|
|
*/
|
|
|
|
externalAPIScope: string,
|
2019-01-22 10:32:18 +00:00
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
/**
|
2019-05-24 11:06:05 +00:00
|
|
|
* An object with the feature flags.
|
2018-07-11 09:42:43 +00:00
|
|
|
*/
|
2019-06-11 13:38:33 +00:00
|
|
|
flags: Object,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An object with user information (display name, email, avatar URL).
|
|
|
|
*/
|
|
|
|
userInfo: ?Object
|
2018-07-11 09:42:43 +00:00
|
|
|
};
|
2016-12-01 01:52:39 +00:00
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* Root app {@code Component} on mobile/React Native.
|
2018-07-11 09:42:43 +00:00
|
|
|
*
|
|
|
|
* @extends AbstractApp
|
|
|
|
*/
|
|
|
|
export class App extends AbstractApp {
|
2019-01-22 10:32:18 +00:00
|
|
|
_init: Promise<*>;
|
|
|
|
|
2016-10-05 14:36:59 +00:00
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* Initializes a new {@code App} instance.
|
2016-10-05 14:36:59 +00:00
|
|
|
*
|
2018-07-12 16:16:57 +00:00
|
|
|
* @param {Props} props - The read-only React {@code Component} props with
|
|
|
|
* which the new instance is to be initialized.
|
2016-10-05 14:36:59 +00:00
|
|
|
*/
|
2018-07-11 09:42:43 +00:00
|
|
|
constructor(props: Props) {
|
2016-10-05 14:36:59 +00:00
|
|
|
super(props);
|
|
|
|
|
2017-01-05 21:06:41 +00:00
|
|
|
// In the Release configuration, React Native will (intentionally) throw
|
|
|
|
// an unhandled JavascriptException for an unhandled JavaScript error.
|
2018-07-12 16:16:57 +00:00
|
|
|
// This will effectively kill the app. In accord with the Web, do not
|
|
|
|
// kill the app.
|
2017-01-05 21:06:41 +00:00
|
|
|
this._maybeDisableExceptionsManager();
|
2020-06-02 09:03:17 +00:00
|
|
|
|
|
|
|
// Bind event handler so it is only bound once per instance.
|
|
|
|
this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-24 15:53:28 +00:00
|
|
|
* Initializes the color scheme.
|
2016-10-05 14:36:59 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
2019-01-24 15:53:28 +00:00
|
|
|
*
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2019-01-01 23:50:11 +00:00
|
|
|
componentDidMount() {
|
|
|
|
super.componentDidMount();
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2019-01-22 10:32:18 +00:00
|
|
|
this._init.then(() => {
|
2020-07-22 10:50:10 +00:00
|
|
|
const { dispatch, getState } = this.state.store;
|
|
|
|
|
2019-06-11 13:38:33 +00:00
|
|
|
// We set these early enough so then we avoid any unnecessary re-renders.
|
2020-07-22 10:50:10 +00:00
|
|
|
dispatch(setColorScheme(this.props.colorScheme));
|
|
|
|
dispatch(updateFlags(this.props.flags));
|
2020-06-26 09:47:48 +00:00
|
|
|
|
|
|
|
// Check if serverURL is configured externally and not allowed to change.
|
2020-07-22 10:50:10 +00:00
|
|
|
const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
|
2020-06-26 09:47:48 +00:00
|
|
|
|
|
|
|
if (!serverURLChangeEnabled) {
|
|
|
|
// As serverURL is provided externally, so we push it to settings.
|
|
|
|
if (typeof this.props.url !== 'undefined') {
|
|
|
|
const { serverURL } = this.props.url;
|
|
|
|
|
|
|
|
if (typeof serverURL !== 'undefined') {
|
|
|
|
dispatch(updateSettings({ serverURL }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-24 11:06:05 +00:00
|
|
|
|
2019-06-11 13:38:33 +00:00
|
|
|
dispatch(updateSettings(this.props.userInfo || {}));
|
2019-10-18 14:30:59 +00:00
|
|
|
|
|
|
|
// Update settings with feature-flag.
|
|
|
|
const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
|
|
|
|
|
|
|
|
if (typeof callIntegrationEnabled !== 'undefined') {
|
|
|
|
dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
|
|
|
|
}
|
2019-01-22 10:32:18 +00:00
|
|
|
});
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 09:03:17 +00:00
|
|
|
/**
|
|
|
|
* Overrides the parent method to inject {@link DimensionsDetector} as
|
|
|
|
* the top most component.
|
|
|
|
*
|
|
|
|
* @override
|
|
|
|
*/
|
|
|
|
_createMainElement(component, props) {
|
|
|
|
return (
|
|
|
|
<DimensionsDetector
|
|
|
|
onDimensionsChanged = { this._onDimensionsChanged }>
|
|
|
|
{ super._createMainElement(component, props) }
|
|
|
|
</DimensionsDetector>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-05 21:06:41 +00:00
|
|
|
/**
|
|
|
|
* Attempts to disable the use of React Native
|
|
|
|
* {@link ExceptionsManager#handleException} on platforms and in
|
|
|
|
* configurations on/in which the use of the method in questions has been
|
|
|
|
* determined to be undesirable. For example, React Native will
|
2018-07-12 16:16:57 +00:00
|
|
|
* (intentionally) throw an unhandled {@code JavascriptException} for an
|
2017-01-05 21:06:41 +00:00
|
|
|
* unhandled JavaScript error in the Release configuration. This will
|
2018-07-12 16:16:57 +00:00
|
|
|
* effectively kill the app. In accord with the Web, do not kill the app.
|
2017-01-05 21:06:41 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_maybeDisableExceptionsManager() {
|
|
|
|
if (__DEV__) {
|
|
|
|
// As mentioned above, only the Release configuration was observed
|
|
|
|
// to suffer.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (Platform.OS !== 'android') {
|
|
|
|
// A solution based on RTCSetFatalHandler was implemented on iOS and
|
|
|
|
// it is preferred because it is at a later step of the
|
|
|
|
// error/exception handling and it is specific to fatal
|
2018-07-12 16:16:57 +00:00
|
|
|
// errors/exceptions which were observed to kill the app. The
|
|
|
|
// solution implemented bellow was tested on Android only so it is
|
|
|
|
// considered safest to use it there only.
|
2017-01-05 21:06:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const oldHandler = global.ErrorUtils.getGlobalHandler();
|
|
|
|
const newHandler = _handleException;
|
|
|
|
|
|
|
|
if (!oldHandler || oldHandler !== newHandler) {
|
|
|
|
newHandler.next = oldHandler;
|
|
|
|
global.ErrorUtils.setGlobalHandler(newHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 09:03:17 +00:00
|
|
|
_onDimensionsChanged: (width: number, height: number) => void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates the known available size for the app to occupy.
|
|
|
|
*
|
|
|
|
* @param {number} width - The component's current width.
|
|
|
|
* @param {number} height - The component's current height.
|
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onDimensionsChanged(width: number, height: number) {
|
|
|
|
const { dispatch } = this.state.store;
|
|
|
|
|
|
|
|
dispatch(clientResized(width, height));
|
|
|
|
}
|
|
|
|
|
2018-09-03 13:53:24 +00:00
|
|
|
/**
|
|
|
|
* Renders the platform specific dialog container.
|
|
|
|
*
|
|
|
|
* @returns {React$Element}
|
|
|
|
*/
|
|
|
|
_renderDialogContainer() {
|
|
|
|
return (
|
|
|
|
<DialogContainer />
|
|
|
|
);
|
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|
2017-01-05 21:06:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a (possibly unhandled) JavaScript error by preventing React Native
|
|
|
|
* from converting a fatal error into an unhandled native exception which will
|
2018-07-12 16:16:57 +00:00
|
|
|
* kill the app.
|
2017-01-05 21:06:41 +00:00
|
|
|
*
|
|
|
|
* @param {Error} error - The (possibly unhandled) JavaScript error to handle.
|
2018-07-12 16:16:57 +00:00
|
|
|
* @param {boolean} fatal - If the specified error is fatal, {@code true};
|
|
|
|
* otherwise, {@code false}.
|
2017-01-05 21:06:41 +00:00
|
|
|
* @private
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function _handleException(error, fatal) {
|
|
|
|
if (fatal) {
|
|
|
|
// In the Release configuration, React Native will (intentionally) throw
|
|
|
|
// an unhandled JavascriptException for an unhandled JavaScript error.
|
2018-07-12 16:16:57 +00:00
|
|
|
// This will effectively kill the app. In accord with the Web, do not
|
|
|
|
// kill the app.
|
2018-07-30 14:38:25 +00:00
|
|
|
logger.error(error);
|
2017-01-05 21:06:41 +00:00
|
|
|
} else {
|
|
|
|
// Forward to the next globalHandler of ErrorUtils.
|
2018-07-12 16:16:57 +00:00
|
|
|
const { next } = _handleException;
|
2017-01-05 21:06:41 +00:00
|
|
|
|
|
|
|
typeof next === 'function' && next(error, fatal);
|
|
|
|
}
|
|
|
|
}
|