您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

App.native.tsx 9.3KB

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