2022-10-20 11:57:30 -04:00
|
|
|
import 'symbol-observable';
|
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
import React from 'react';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { createRoot } from 'react-dom/client';
|
2021-05-30 18:49:33 +02:00
|
|
|
import { I18n } from 'react-polyglot';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { connect, Provider } from 'react-redux';
|
|
|
|
import { HashRouter as Router } from 'react-router-dom';
|
2021-05-31 16:46:41 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
import 'what-input';
|
2021-05-30 18:49:33 +02:00
|
|
|
import { authenticateUser } from './actions/auth';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { loadConfig } from './actions/config';
|
2021-05-30 18:49:33 +02:00
|
|
|
import App from './components/App/App';
|
|
|
|
import './components/EditorWidgets';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { ErrorBoundary } from './components/UI';
|
|
|
|
import { addExtensions } from './extensions';
|
|
|
|
import { getPhrases } from './lib/phrases';
|
2021-05-30 18:49:33 +02:00
|
|
|
import './mediaLibrary';
|
2022-10-20 11:57:30 -04:00
|
|
|
import { selectLocale } from './reducers/config';
|
|
|
|
import { store } from './store';
|
|
|
|
|
|
|
|
import type { AnyAction } from '@reduxjs/toolkit';
|
|
|
|
import type { ConnectedProps } from 'react-redux';
|
2022-11-05 23:55:37 -04:00
|
|
|
import type { BaseField, Config, UnknownField } from './interface';
|
2022-10-20 11:57:30 -04:00
|
|
|
import type { RootState } from './store';
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2018-03-28 15:52:57 -04:00
|
|
|
const ROOT_ID = 'nc-root';
|
2018-03-06 17:14:09 -05:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const TranslatedApp = ({ locale, config }: AppRootProps) => {
|
|
|
|
if (!config) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-11-14 11:25:04 +02:00
|
|
|
return (
|
2022-10-20 11:57:30 -04:00
|
|
|
<I18n locale={locale} messages={getPhrases(locale)}>
|
|
|
|
<ErrorBoundary showBackup config={config}>
|
|
|
|
<Router>
|
|
|
|
<App />
|
|
|
|
</Router>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</I18n>
|
2019-11-14 11:25:04 +02:00
|
|
|
);
|
2022-10-20 11:57:30 -04:00
|
|
|
};
|
2019-11-14 11:25:04 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
function mapDispatchToProps(state: RootState) {
|
|
|
|
return { locale: selectLocale(state.config.config), config: state.config.config };
|
2021-02-08 20:01:21 +02:00
|
|
|
}
|
2019-11-14 11:25:04 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const connector = connect(mapDispatchToProps);
|
|
|
|
export type AppRootProps = ConnectedProps<typeof connector>;
|
2019-11-14 11:25:04 +02:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
const ConnectedTranslatedApp = connector(TranslatedApp);
|
|
|
|
|
2022-11-05 23:55:37 -04:00
|
|
|
function bootstrap<F extends BaseField = UnknownField>(opts?: {
|
|
|
|
config?: Config<F>;
|
|
|
|
autoInitialize?: boolean;
|
|
|
|
}) {
|
2022-10-20 11:57:30 -04:00
|
|
|
const { config, autoInitialize = true } = opts ?? {};
|
2018-03-06 17:14:09 -05:00
|
|
|
|
2018-02-28 15:45:16 -05:00
|
|
|
/**
|
|
|
|
* Log the version number.
|
|
|
|
*/
|
2022-10-02 20:06:20 -04:00
|
|
|
if (typeof STATIC_CMS_CORE_VERSION === 'string') {
|
|
|
|
console.info(`static-cms-core ${STATIC_CMS_CORE_VERSION}`);
|
2018-07-27 14:26:03 -04:00
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
2018-03-28 15:52:57 -04:00
|
|
|
* Get DOM element where app will mount.
|
2018-02-28 15:45:16 -05:00
|
|
|
*/
|
2018-03-28 15:52:57 -04:00
|
|
|
function getRoot() {
|
|
|
|
/**
|
|
|
|
* Return existing root if found.
|
|
|
|
*/
|
|
|
|
const existingRoot = document.getElementById(ROOT_ID);
|
|
|
|
if (existingRoot) {
|
|
|
|
return existingRoot;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If no existing root, create and return a new root.
|
|
|
|
*/
|
|
|
|
const newRoot = document.createElement('div');
|
|
|
|
newRoot.id = ROOT_ID;
|
|
|
|
document.body.appendChild(newRoot);
|
|
|
|
return newRoot;
|
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
2022-10-20 11:57:30 -04:00
|
|
|
if (autoInitialize) {
|
|
|
|
addExtensions();
|
|
|
|
}
|
|
|
|
|
2021-01-18 11:37:12 +02:00
|
|
|
store.dispatch(
|
2022-11-05 23:55:37 -04:00
|
|
|
loadConfig(config as Config | undefined, function onLoad(config) {
|
2022-10-27 12:24:30 -04:00
|
|
|
if (config.backend.name !== 'git-gateway') {
|
|
|
|
store.dispatch(authenticateUser() as unknown as AnyAction);
|
|
|
|
}
|
2022-10-20 11:57:30 -04:00
|
|
|
}) as AnyAction,
|
2021-01-18 11:37:12 +02:00
|
|
|
);
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create connected root component.
|
|
|
|
*/
|
2021-02-08 20:01:21 +02:00
|
|
|
function Root() {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Provider store={store}>
|
|
|
|
<ConnectedTranslatedApp />
|
|
|
|
</Provider>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
2018-02-28 15:45:16 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Render application root.
|
|
|
|
*/
|
2022-10-20 11:57:30 -04:00
|
|
|
const root = createRoot(getRoot());
|
|
|
|
root.render(<Root />);
|
2018-02-28 15:45:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export default bootstrap;
|