2018-07-11 09:42:43 +00:00
|
|
|
// @flow
|
|
|
|
|
2020-07-09 07:17:23 +00:00
|
|
|
import { jitsiLocalStorage } from '@jitsi/js-utils';
|
2018-07-11 09:42:43 +00:00
|
|
|
import _ from 'lodash';
|
|
|
|
import React, { Component, Fragment } from 'react';
|
|
|
|
import { I18nextProvider } from 'react-i18next';
|
|
|
|
import { Provider } from 'react-redux';
|
|
|
|
import { compose, createStore } from 'redux';
|
|
|
|
import Thunk from 'redux-thunk';
|
|
|
|
|
|
|
|
import { i18next } from '../../i18n';
|
|
|
|
import {
|
|
|
|
MiddlewareRegistry,
|
2020-06-03 14:56:08 +00:00
|
|
|
PersistenceRegistry,
|
2018-07-11 09:42:43 +00:00
|
|
|
ReducerRegistry,
|
|
|
|
StateListenerRegistry
|
|
|
|
} from '../../redux';
|
|
|
|
import { SoundCollection } from '../../sounds';
|
2022-01-07 13:18:24 +00:00
|
|
|
import { createDeferred } from '../../util';
|
2018-07-11 09:42:43 +00:00
|
|
|
import { appWillMount, appWillUnmount } from '../actions';
|
2019-08-21 14:50:00 +00:00
|
|
|
import logger from '../logger';
|
2019-04-26 18:11:53 +00:00
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
declare var APP: Object;
|
|
|
|
|
2018-07-12 16:16:57 +00:00
|
|
|
/**
|
|
|
|
* The type of the React {@code Component} state of {@link BaseApp}.
|
|
|
|
*/
|
2018-07-11 09:42:43 +00:00
|
|
|
type State = {
|
|
|
|
|
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* The {@code Route} rendered by the {@code BaseApp}.
|
2018-07-11 09:42:43 +00:00
|
|
|
*/
|
|
|
|
route: Object,
|
|
|
|
|
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* The redux store used by the {@code BaseApp}.
|
2018-07-11 09:42:43 +00:00
|
|
|
*/
|
|
|
|
store: Object
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base (abstract) class for main App component.
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
export default class BaseApp extends Component<*, State> {
|
2022-01-07 13:18:24 +00:00
|
|
|
/**
|
|
|
|
* The deferred for the initialisation {{promise, resolve, reject}}.
|
|
|
|
*/
|
|
|
|
_init: Object;
|
2018-07-11 09:42:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new {@code BaseApp} instance.
|
|
|
|
*
|
|
|
|
* @param {Object} props - The read-only React {@code Component} props with
|
|
|
|
* which the new instance is to be initialized.
|
|
|
|
*/
|
|
|
|
constructor(props: Object) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
route: {},
|
|
|
|
store: undefined
|
|
|
|
};
|
2019-01-01 23:50:11 +00:00
|
|
|
}
|
2018-07-11 09:42:43 +00:00
|
|
|
|
2019-01-01 23:50:11 +00:00
|
|
|
/**
|
|
|
|
* Initializes the app.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
2022-01-07 13:18:24 +00:00
|
|
|
*/
|
|
|
|
async componentDidMount() {
|
2018-07-11 09:42:43 +00:00
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* Make the mobile {@code BaseApp} wait until the {@code AsyncStorage}
|
|
|
|
* implementation of {@code Storage} initializes fully.
|
2018-07-11 09:42:43 +00:00
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @see {@link #_initStorage}
|
|
|
|
* @type {Promise}
|
|
|
|
*/
|
2022-01-07 13:18:24 +00:00
|
|
|
this._init = createDeferred();
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this._initStorage();
|
|
|
|
|
|
|
|
const setStatePromise = new Promise(resolve => {
|
2019-01-01 23:50:11 +00:00
|
|
|
this.setState({
|
|
|
|
store: this._createStore()
|
|
|
|
}, resolve);
|
2019-04-26 18:11:53 +00:00
|
|
|
});
|
2022-01-07 13:18:24 +00:00
|
|
|
|
|
|
|
await setStatePromise;
|
|
|
|
|
|
|
|
await this._extraInit();
|
|
|
|
} catch (err) {
|
|
|
|
/* BaseApp should always initialize! */
|
|
|
|
logger.error(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.state.store.dispatch(appWillMount(this));
|
|
|
|
|
|
|
|
this._init.resolve();
|
2018-07-11 09:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-12 16:16:57 +00:00
|
|
|
* De-initializes the app.
|
2018-07-11 09:42:43 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
componentWillUnmount() {
|
2018-07-12 16:16:57 +00:00
|
|
|
this.state.store.dispatch(appWillUnmount(this));
|
2018-07-11 09:42:43 +00:00
|
|
|
}
|
|
|
|
|
2021-08-31 17:00:37 +00:00
|
|
|
/**
|
|
|
|
* Logs for errors that were not caught.
|
|
|
|
*
|
|
|
|
* @param {Error} error - The error that was thrown.
|
|
|
|
* @param {Object} info - Info about the error(stack trace);.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
componentDidCatch(error: Error, info: Object) {
|
|
|
|
logger.error(error, info);
|
|
|
|
}
|
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
/**
|
|
|
|
* Delays this {@code BaseApp}'s startup until the {@code Storage}
|
|
|
|
* implementation of {@code localStorage} initializes. While the
|
|
|
|
* initialization is instantaneous on Web (with Web Storage API), it is
|
|
|
|
* asynchronous on mobile/react-native.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_initStorage(): Promise<*> {
|
2020-05-01 19:48:08 +00:00
|
|
|
const _initializing = jitsiLocalStorage.getItem('_initializing');
|
2018-07-11 09:42:43 +00:00
|
|
|
|
|
|
|
return _initializing || Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2022-01-07 13:18:24 +00:00
|
|
|
/**
|
|
|
|
* Extra initialisation that subclasses might require.
|
|
|
|
*
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_extraInit() {
|
|
|
|
// To be implemented by subclass.
|
|
|
|
}
|
|
|
|
|
2018-07-11 09:42:43 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2020-06-29 07:45:58 +00:00
|
|
|
const { route: { component, props }, store } = this.state;
|
2018-07-11 09:42:43 +00:00
|
|
|
|
2019-04-09 15:53:12 +00:00
|
|
|
if (store) {
|
2018-07-11 09:42:43 +00:00
|
|
|
return (
|
|
|
|
<I18nextProvider i18n = { i18next }>
|
|
|
|
<Provider store = { store }>
|
|
|
|
<Fragment>
|
2020-06-29 07:45:58 +00:00
|
|
|
{ this._createMainElement(component, props) }
|
2018-07-11 09:42:43 +00:00
|
|
|
<SoundCollection />
|
|
|
|
{ this._createExtraElement() }
|
2018-09-03 13:53:24 +00:00
|
|
|
{ this._renderDialogContainer() }
|
2018-07-11 09:42:43 +00:00
|
|
|
</Fragment>
|
|
|
|
</Provider>
|
|
|
|
</I18nextProvider>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-16 15:59:33 +00:00
|
|
|
* Creates an extra {@link ReactElement}s to be added (unconditionally)
|
2018-07-11 09:42:43 +00:00
|
|
|
* alongside the main element.
|
|
|
|
*
|
|
|
|
* @returns {ReactElement}
|
|
|
|
* @abstract
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
_createExtraElement() {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a {@link ReactElement} from the specified component, the
|
|
|
|
* specified props and the props of this {@code AbstractApp} which are
|
|
|
|
* suitable for propagation to the children of this {@code Component}.
|
|
|
|
*
|
|
|
|
* @param {Component} component - The component from which the
|
|
|
|
* {@code ReactElement} is to be created.
|
|
|
|
* @param {Object} props - The read-only React {@code Component} props with
|
|
|
|
* which the {@code ReactElement} is to be initialized.
|
|
|
|
* @returns {ReactElement}
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
_createMainElement(component, props) {
|
2019-04-09 15:53:12 +00:00
|
|
|
return component ? React.createElement(component, props || {}) : null;
|
2018-07-11 09:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes a new redux store instance suitable for use by this
|
|
|
|
* {@code AbstractApp}.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @returns {Store} - A new redux store instance suitable for use by
|
|
|
|
* this {@code AbstractApp}.
|
|
|
|
*/
|
|
|
|
_createStore() {
|
|
|
|
// Create combined reducer from all reducers in ReducerRegistry.
|
|
|
|
const reducer = ReducerRegistry.combineReducers();
|
|
|
|
|
|
|
|
// Apply all registered middleware from the MiddlewareRegistry and
|
|
|
|
// additional 3rd party middleware:
|
|
|
|
// - Thunk - allows us to dispatch async actions easily. For more info
|
|
|
|
// @see https://github.com/gaearon/redux-thunk.
|
2022-03-28 08:16:01 +00:00
|
|
|
const middleware = MiddlewareRegistry.applyMiddleware(Thunk);
|
|
|
|
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
|
|
|
const store = createStore(reducer, PersistenceRegistry.getPersistedState(), composeEnhancers(middleware));
|
2018-07-11 09:42:43 +00:00
|
|
|
|
|
|
|
// StateListenerRegistry
|
|
|
|
StateListenerRegistry.subscribe(store);
|
|
|
|
|
|
|
|
// This is temporary workaround to be able to dispatch actions from
|
|
|
|
// non-reactified parts of the code (conference.js for example).
|
|
|
|
// Don't use in the react code!!!
|
|
|
|
// FIXME: remove when the reactification is finished!
|
|
|
|
if (typeof APP !== 'undefined') {
|
|
|
|
APP.store = store;
|
|
|
|
}
|
|
|
|
|
|
|
|
return store;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigates to a specific Route.
|
|
|
|
*
|
|
|
|
* @param {Route} route - The Route to which to navigate.
|
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
|
|
|
_navigate(route): Promise<*> {
|
|
|
|
if (_.isEqual(route, this.state.route)) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (route.href) {
|
|
|
|
// This navigation requires loading a new URL in the browser.
|
|
|
|
window.location.href = route.href;
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX React's setState is asynchronous which means that the value of
|
|
|
|
// this.state.route above may not even be correct. If the check is
|
|
|
|
// performed before setState completes, the app may not navigate to the
|
|
|
|
// expected route. In order to mitigate the problem, _navigate was
|
|
|
|
// changed to return a Promise.
|
|
|
|
return new Promise(resolve => {
|
|
|
|
this.setState({ route }, resolve);
|
|
|
|
});
|
|
|
|
}
|
2018-09-03 13:53:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Renders the platform specific dialog container.
|
|
|
|
*
|
|
|
|
* @returns {React$Element}
|
|
|
|
*/
|
2021-11-04 21:10:43 +00:00
|
|
|
_renderDialogContainer: () => React$Element<*>;
|
2018-07-11 09:42:43 +00:00
|
|
|
}
|