You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

App.native.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React from 'react';
  2. import { Platform, StyleSheet, View } from 'react-native';
  3. import { SafeAreaProvider } from 'react-native-safe-area-context';
  4. import { FullWindowOverlay } from 'react-native-screens';
  5. import SplashScreen from 'react-native-splash-screen';
  6. import { DialogContainer } from '../../base/dialog';
  7. import BottomSheetContainer from '../../base/dialog/components/native/BottomSheetContainer';
  8. import { updateFlags } from '../../base/flags/actions';
  9. import { CALL_INTEGRATION_ENABLED, SERVER_URL_CHANGE_ENABLED } from '../../base/flags/constants';
  10. import { getFeatureFlag } from '../../base/flags/functions';
  11. import { DimensionsDetector, clientResized, setSafeAreaInsets } from '../../base/responsive-ui';
  12. import { updateSettings } from '../../base/settings';
  13. import { _getRouteToRender } from '../getRouteToRender.native';
  14. import logger from '../logger';
  15. import { AbstractApp } from './AbstractApp';
  16. import type { Props as AbstractAppProps } from './AbstractApp';
  17. // Register middlewares and reducers.
  18. import '../middlewares';
  19. import '../reducers';
  20. declare var __DEV__;
  21. const DialogContainerWrapper = Platform.select({
  22. ios: FullWindowOverlay,
  23. default: View
  24. });
  25. /**
  26. * The type of React {@code Component} props of {@link App}.
  27. */
  28. type Props = AbstractAppProps & {
  29. /**
  30. * Identifier for this app on the native side.
  31. */
  32. externalAPIScope: string,
  33. /**
  34. * An object with the feature flags.
  35. */
  36. flags: Object,
  37. /**
  38. * An object with user information (display name, email, avatar URL).
  39. */
  40. userInfo: ?Object
  41. };
  42. /**
  43. * Root app {@code Component} on mobile/React Native.
  44. *
  45. * @augments AbstractApp
  46. */
  47. export class App extends AbstractApp {
  48. /**
  49. * The deferred for the initialisation {{promise, resolve, reject}}.
  50. */
  51. _init: Object;
  52. /**
  53. * Initializes a new {@code App} instance.
  54. *
  55. * @param {Props} props - The read-only React {@code Component} props with
  56. * which the new instance is to be initialized.
  57. */
  58. constructor(props: Props) {
  59. super(props);
  60. // In the Release configuration, React Native will (intentionally) throw
  61. // an unhandled JavascriptException for an unhandled JavaScript error.
  62. // This will effectively kill the app. In accord with the Web, do not
  63. // kill the app.
  64. this._maybeDisableExceptionsManager();
  65. // Bind event handler so it is only bound once per instance.
  66. this._onDimensionsChanged = this._onDimensionsChanged.bind(this);
  67. this._onSafeAreaInsetsChanged = this._onSafeAreaInsetsChanged.bind(this);
  68. }
  69. /**
  70. * Initializes the color scheme.
  71. *
  72. * @inheritdoc
  73. *
  74. * @returns {void}
  75. */
  76. async componentDidMount() {
  77. await super.componentDidMount();
  78. SplashScreen.hide();
  79. }
  80. /**
  81. * Initializes feature flags and updates settings.
  82. *
  83. * @returns {void}
  84. */
  85. async _extraInit() {
  86. const { dispatch, getState } = this.state.store;
  87. // We set these early enough so then we avoid any unnecessary re-renders.
  88. dispatch(updateFlags(this.props.flags));
  89. const route = await _getRouteToRender();
  90. // We need the root navigator to be set early.
  91. await this._navigate(route);
  92. // HACK ALERT!
  93. // Wait until the root navigator is ready.
  94. // We really need to break the inheritance relationship between App,
  95. // AbstractApp and BaseApp, it's very inflexible and cumbersome right now.
  96. const rootNavigationReady = new Promise(resolve => {
  97. const i = setInterval(() => {
  98. const { ready } = getState()['features/app'] || {};
  99. if (ready) {
  100. clearInterval(i);
  101. resolve();
  102. }
  103. }, 50);
  104. });
  105. await rootNavigationReady;
  106. // Check if serverURL is configured externally and not allowed to change.
  107. const serverURLChangeEnabled = getFeatureFlag(getState(), SERVER_URL_CHANGE_ENABLED, true);
  108. if (!serverURLChangeEnabled) {
  109. // As serverURL is provided externally, so we push it to settings.
  110. if (typeof this.props.url !== 'undefined') {
  111. const { serverURL } = this.props.url;
  112. if (typeof serverURL !== 'undefined') {
  113. dispatch(updateSettings({ serverURL }));
  114. }
  115. }
  116. }
  117. dispatch(updateSettings(this.props.userInfo || {}));
  118. // Update settings with feature-flag.
  119. const callIntegrationEnabled = this.props.flags[CALL_INTEGRATION_ENABLED];
  120. if (typeof callIntegrationEnabled !== 'undefined') {
  121. dispatch(updateSettings({ disableCallIntegration: !callIntegrationEnabled }));
  122. }
  123. }
  124. /**
  125. * Overrides the parent method to inject {@link DimensionsDetector} as
  126. * the top most component.
  127. *
  128. * @override
  129. */
  130. _createMainElement(component, props) {
  131. return (
  132. <SafeAreaProvider>
  133. <DimensionsDetector
  134. onDimensionsChanged = { this._onDimensionsChanged }
  135. onSafeAreaInsetsChanged = { this._onSafeAreaInsetsChanged }>
  136. { super._createMainElement(component, props) }
  137. </DimensionsDetector>
  138. </SafeAreaProvider>
  139. );
  140. }
  141. /**
  142. * Attempts to disable the use of React Native
  143. * {@link ExceptionsManager#handleException} on platforms and in
  144. * configurations on/in which the use of the method in questions has been
  145. * determined to be undesirable. For example, React Native will
  146. * (intentionally) throw an unhandled {@code JavascriptException} for an
  147. * unhandled JavaScript error in the Release configuration. This will
  148. * effectively kill the app. In accord with the Web, do not kill the app.
  149. *
  150. * @private
  151. * @returns {void}
  152. */
  153. _maybeDisableExceptionsManager() {
  154. if (__DEV__) {
  155. // As mentioned above, only the Release configuration was observed
  156. // to suffer.
  157. return;
  158. }
  159. if (Platform.OS !== 'android') {
  160. // A solution based on RTCSetFatalHandler was implemented on iOS and
  161. // it is preferred because it is at a later step of the
  162. // error/exception handling and it is specific to fatal
  163. // errors/exceptions which were observed to kill the app. The
  164. // solution implemented below was tested on Android only so it is
  165. // considered safest to use it there only.
  166. return;
  167. }
  168. const oldHandler = global.ErrorUtils.getGlobalHandler();
  169. const newHandler = _handleException;
  170. if (!oldHandler || oldHandler !== newHandler) {
  171. newHandler.next = oldHandler;
  172. global.ErrorUtils.setGlobalHandler(newHandler);
  173. }
  174. }
  175. _onDimensionsChanged: (width: number, height: number) => void;
  176. /**
  177. * Updates the known available size for the app to occupy.
  178. *
  179. * @param {number} width - The component's current width.
  180. * @param {number} height - The component's current height.
  181. * @private
  182. * @returns {void}
  183. */
  184. _onDimensionsChanged(width: number, height: number) {
  185. const { dispatch } = this.state.store;
  186. dispatch(clientResized(width, height));
  187. }
  188. /**
  189. * Updates the safe are insets values.
  190. *
  191. * @param {Object} insets - The insets.
  192. * @param {number} insets.top - The top inset.
  193. * @param {number} insets.right - The right inset.
  194. * @param {number} insets.bottom - The bottom inset.
  195. * @param {number} insets.left - The left inset.
  196. * @private
  197. * @returns {void}
  198. */
  199. _onSafeAreaInsetsChanged(insets) {
  200. const { dispatch } = this.state.store;
  201. dispatch(setSafeAreaInsets(insets));
  202. }
  203. /**
  204. * Renders the platform specific dialog container.
  205. *
  206. * @returns {React$Element}
  207. */
  208. _renderDialogContainer() {
  209. return (
  210. <DialogContainerWrapper
  211. pointerEvents = 'box-none'
  212. style = { StyleSheet.absoluteFill }>
  213. <BottomSheetContainer />
  214. <DialogContainer />
  215. </DialogContainerWrapper>
  216. );
  217. }
  218. }
  219. /**
  220. * Handles a (possibly unhandled) JavaScript error by preventing React Native
  221. * from converting a fatal error into an unhandled native exception which will
  222. * kill the app.
  223. *
  224. * @param {Error} error - The (possibly unhandled) JavaScript error to handle.
  225. * @param {boolean} fatal - If the specified error is fatal, {@code true};
  226. * otherwise, {@code false}.
  227. * @private
  228. * @returns {void}
  229. */
  230. function _handleException(error, fatal) {
  231. if (fatal) {
  232. // In the Release configuration, React Native will (intentionally) throw
  233. // an unhandled JavascriptException for an unhandled JavaScript error.
  234. // This will effectively kill the app. In accord with the Web, do not
  235. // kill the app.
  236. logger.error(error);
  237. } else {
  238. // Forward to the next globalHandler of ErrorUtils.
  239. const { next } = _handleException;
  240. typeof next === 'function' && next(error, fatal);
  241. }
  242. }