jiti-meet/react/features/app/components/AbstractApp.js

447 lines
14 KiB
JavaScript
Raw Normal View History

/* global APP */
import _ from 'lodash';
import PropTypes from 'prop-types';
import React, { Component, Fragment } from 'react';
2017-03-01 02:55:12 +00:00
import { I18nextProvider } from 'react-i18next';
import { Provider } from 'react-redux';
import { compose, createStore } from 'redux';
import Thunk from 'redux-thunk';
2017-03-01 02:55:12 +00:00
import { i18next } from '../../base/i18n';
import {
MiddlewareRegistry,
ReducerRegistry,
StateListenerRegistry
} from '../../base/redux';
import { SoundCollection } from '../../base/sounds';
import { PersistenceRegistry } from '../../base/storage';
import { toURLString } from '../../base/util';
import { OverlayContainer } from '../../overlay';
2017-07-26 19:40:34 +00:00
import { appNavigate, appWillMount, appWillUnmount } from '../actions';
/**
2018-07-12 03:57:44 +00:00
* The default URL to open if no other was specified to {@code AbstractApp} via
* props.
*
* FIXME: This is not at the best place here. This should be either in the
* base/settings feature or a default in base/config.
*/
const DEFAULT_URL = 'https://meet.jit.si';
/**
* Base (abstract) class for main App component.
*
* @abstract
*/
export class AbstractApp extends Component {
/**
2017-07-26 19:40:34 +00:00
* {@code AbstractApp} component's property types.
*
* @static
*/
static propTypes = {
/**
2017-06-09 19:09:23 +00:00
* The default URL {@code AbstractApp} is to open when not in any
* conference/room.
*/
defaultURL: PropTypes.string,
/**
2017-07-26 19:40:34 +00:00
* (Optional) redux store for this app.
*/
store: PropTypes.object,
// XXX Refer to the implementation of loadURLObject: in
// ios/sdk/src/JitsiMeetView.m for further information.
timestamp: PropTypes.any,
/**
* The URL, if any, with which the app was launched.
*/
url: PropTypes.oneOfType([
PropTypes.object,
PropTypes.string
])
};
/**
2017-07-26 19:40:34 +00:00
* Initializes a new {@code AbstractApp} instance.
*
2017-07-26 19:40:34 +00:00
* @param {Object} props - The read-only React {@code Component} props with
* which the new instance is to be initialized.
*/
constructor(props) {
super(props);
this.state = {
2018-07-12 03:57:44 +00:00
/**
* The state of the »possible« async initialization of
* the {@code AbstractApp}.
*/
appAsyncInitialized: false,
/**
2017-07-26 19:40:34 +00:00
* The Route rendered by this {@code AbstractApp}.
*
* @type {Route}
*/
route: {},
/**
2017-07-26 19:40:34 +00:00
* The redux store used by this {@code AbstractApp}.
*
* @type {Store}
*/
store: undefined
};
/**
* Make the mobile {@code AbstractApp} wait until the
* {@code AsyncStorage} implementation of {@code Storage} initializes
* fully.
*
* @private
* @see {@link #_initStorage}
* @type {Promise}
*/
this._init
= this._initStorage()
.catch(() => { /* AbstractApp should always initialize! */ })
.then(() =>
this.setState({
store: this._maybeCreateStore(props)
}));
}
/**
* Initializes the app.
*
* @inheritdoc
*/
componentWillMount() {
this._init.then(() => {
const { dispatch } = this._getStore();
dispatch(appWillMount(this));
// We set the initialized state here and not in the constructor to
// make sure that {@code componentWillMount} gets invoked before
// the app tries to render the actual app content.
this.setState({
appAsyncInitialized: true
});
// If a URL was explicitly specified to this React Component,
// then open it; otherwise, use a default.
this._openURL(toURLString(this.props.url) || this._getDefaultURL());
});
}
/**
2017-07-26 19:40:34 +00:00
* Notifies this mounted React {@code Component} that it will receive new
* props. Makes sure that this {@code AbstractApp} has a redux store to use.
*
* @inheritdoc
2017-07-26 19:40:34 +00:00
* @param {Object} nextProps - The read-only React {@code Component} props
* that this instance will receive.
* @returns {void}
*/
componentWillReceiveProps(nextProps) {
const { props } = this;
this._init.then(() => {
// The consumer of this AbstractApp did not provide a redux store.
if (typeof nextProps.store === 'undefined'
// The consumer of this AbstractApp did provide a redux
// store before. Which means that the consumer changed
// their mind. In such a case this instance should create
// its own internal redux store. If the consumer did not
// provide a redux store before, then this instance is
// using its own internal redux store already.
&& typeof props.store !== 'undefined') {
this.setState({
store: this._maybeCreateStore(nextProps)
});
}
// Deal with URL changes.
let { url } = nextProps;
url = toURLString(url);
if (toURLString(props.url) !== url
// XXX Refer to the implementation of loadURLObject: in
// ios/sdk/src/JitsiMeetView.m for further information.
|| props.timestamp !== nextProps.timestamp) {
this._openURL(url || this._getDefaultURL());
}
});
}
/**
* De-initializes the app.
*
* @inheritdoc
*/
componentWillUnmount() {
2017-09-01 21:25:48 +00:00
const { dispatch } = this._getStore();
dispatch(appWillUnmount(this));
}
2017-03-02 03:33:49 +00:00
/**
2017-07-26 19:40:34 +00:00
* Gets a {@code Location} object from the window with information about the
* current location of the document. Explicitly defined to allow extenders
* to override because React Native does not usually have a location
* property on its window unless debugging remotely in which case the
* browser that is the remote debugger will provide a location property on
* the window.
2017-03-02 03:33:49 +00:00
*
* @public
2017-07-26 19:40:34 +00:00
* @returns {Location} A {@code Location} object with information about the
* current location of the document.
2017-03-02 03:33:49 +00:00
*/
getWindowLocation() {
return undefined;
}
/**
* Delays this {@code AbstractApp}'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() {
const localStorageInitializing = window.localStorage._initializing;
return (
typeof localStorageInitializing === 'undefined'
? Promise.resolve()
: localStorageInitializing);
}
/**
* Implements React's {@link Component#render()}.
*
* @inheritdoc
* @returns {ReactElement}
*/
render() {
2018-07-12 03:57:44 +00:00
const { appAsyncInitialized, route: { component } } = this.state;
if (appAsyncInitialized && component) {
return (
2017-03-01 02:55:12 +00:00
<I18nextProvider i18n = { i18next }>
<Provider store = { this._getStore() }>
<Fragment>
{ this._createElement(component) }
<SoundCollection />
<OverlayContainer />
</Fragment>
</Provider>
</I18nextProvider>
);
}
return null;
}
/**
2017-07-26 19:40:34 +00:00
* 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}.
*
2017-07-26 19:40:34 +00:00
* @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
*/
_createElement(component, props) {
2017-06-09 19:09:23 +00:00
/* eslint-disable no-unused-vars */
const {
// Don't propagate the dispatch and store props because they usually
// come from react-redux and programmers don't really expect them to
// be inherited but rather explicitly connected.
dispatch, // eslint-disable-line react/prop-types
store,
2017-06-09 19:09:23 +00:00
// The following props were introduced to be consumed entirely by
// AbstractApp:
defaultURL,
url,
2017-06-09 19:09:23 +00:00
// The remaining props, if any, are considered suitable for
// propagation to the children of this Component.
...thisProps
} = this.props;
2017-06-09 19:09:23 +00:00
/* eslint-enable no-unused-vars */
return React.createElement(component, {
...thisProps,
...props
});
}
/**
2017-07-26 19:40:34 +00:00
* Initializes a new redux store instance suitable for use by this
* {@code AbstractApp}.
*
* @private
2017-07-26 19:40:34 +00:00
* @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.
let middleware = MiddlewareRegistry.applyMiddleware(Thunk);
// Try to enable Redux DevTools Chrome extension in order to make it
// available for the purposes of facilitating development.
let devToolsExtension;
if (typeof window === 'object'
&& (devToolsExtension = window.devToolsExtension)) {
middleware = compose(middleware, devToolsExtension());
}
2018-01-29 22:20:38 +00:00
return (
createStore(
reducer,
PersistenceRegistry.getPersistedState(),
2018-01-29 22:20:38 +00:00
middleware));
}
2017-01-15 19:05:17 +00:00
/**
2017-07-26 19:40:34 +00:00
* Gets the default URL to be opened when this {@code App} mounts.
2017-01-15 19:05:17 +00:00
*
* @protected
2017-07-26 19:40:34 +00:00
* @returns {string} The default URL to be opened when this {@code App}
* mounts.
2017-01-15 19:05:17 +00:00
*/
_getDefaultURL() {
// If the execution environment provides a Location abstraction, then
// this App at already at that location but it must be made aware of the
// fact.
2017-03-02 03:33:49 +00:00
const windowLocation = this.getWindowLocation();
2017-01-15 19:05:17 +00:00
if (windowLocation) {
const href = windowLocation.toString();
if (href) {
return href;
2017-01-15 19:05:17 +00:00
}
}
return (
this.props.defaultURL
|| this._getStore().getState()['features/base/settings']
2018-07-12 03:57:44 +00:00
.serverURL
|| DEFAULT_URL);
2017-01-15 19:05:17 +00:00
}
/**
2017-07-26 19:40:34 +00:00
* Gets the redux store used by this {@code AbstractApp}.
*
* @protected
2017-07-26 19:40:34 +00:00
* @returns {Store} - The redux store used by this {@code AbstractApp}.
*/
_getStore() {
let store = this.state.store;
if (typeof store === 'undefined') {
store = this.props.store;
}
return store;
}
/**
2017-07-26 19:40:34 +00:00
* Creates a redux store to be used by this {@code AbstractApp} if such as a
* store is not defined by the consumer of this {@code AbstractApp} through
* its read-only React {@code Component} props.
*
2017-07-26 19:40:34 +00:00
* @param {Object} props - The read-only React {@code Component} props that
* will eventually be received by this {@code AbstractApp}.
* @private
2017-07-26 19:40:34 +00:00
* @returns {Store} - The redux store to be used by this
* {@code AbstractApp}.
*/
2018-05-22 03:42:22 +00:00
_maybeCreateStore({ store }) {
2017-07-26 19:40:34 +00:00
// The application Jitsi Meet is architected with redux. However, I do
// not want consumers of the App React Component to be forced into
2017-07-26 19:40:34 +00:00
// dealing with redux. If the consumer did not provide an external redux
// store, utilize an internal redux store.
if (typeof store === 'undefined') {
2018-05-22 03:42:22 +00:00
// eslint-disable-next-line no-param-reassign
store = this._createStore();
// 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;
}
}
// StateListenerRegistry
store && StateListenerRegistry.subscribe(store);
return store;
}
/**
* Navigates to a specific Route.
*
* @param {Route} route - The Route to which to navigate.
* @returns {Promise}
*/
_navigate(route) {
if (_.isEqual(route, this.state.route)) {
return Promise.resolve();
2017-01-19 15:47:22 +00:00
}
2017-01-25 22:11:44 +00:00
if (route.href) {
// This navigation requires loading a new URL in the browser.
window.location.href = route.href;
2017-01-25 22:11:44 +00:00
return Promise.resolve();
}
2017-01-25 22:11:44 +00:00
// 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.
2018-07-12 03:57:44 +00:00
return new Promise(resolve => this.setState({ route }, resolve));
}
/**
2017-07-26 19:40:34 +00:00
* Navigates this {@code AbstractApp} to (i.e. opens) a specific URL.
*
* @param {Object|string} url - The URL to navigate this {@code AbstractApp}
2017-07-26 19:40:34 +00:00
* to (i.e. the URL to open).
* @protected
* @returns {void}
*/
_openURL(url) {
this._getStore().dispatch(appNavigate(toURLString(url)));
}
}