static-cms/core/src/bootstrap.tsx

117 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-10-20 11:57:30 -04:00
import 'symbol-observable';
import React from 'react';
2022-10-20 11:57:30 -04:00
import { createRoot } from 'react-dom/client';
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';
2022-10-20 11:57:30 -04:00
import 'what-input';
import { authenticateUser } from './actions/auth';
2022-10-20 11:57:30 -04:00
import { loadConfig } from './actions/config';
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';
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';
import type { Config } from './interface';
import type { RootState } from './store';
const ROOT_ID = 'nc-root';
2022-10-20 11:57:30 -04:00
const TranslatedApp = ({ locale, config }: AppRootProps) => {
if (!config) {
return null;
}
return (
2022-10-20 11:57:30 -04:00
<I18n locale={locale} messages={getPhrases(locale)}>
<ErrorBoundary showBackup config={config}>
<Router>
<App />
</Router>
</ErrorBoundary>
</I18n>
);
2022-10-20 11:57:30 -04:00
};
2022-10-20 11:57:30 -04:00
function mapDispatchToProps(state: RootState) {
return { locale: selectLocale(state.config.config), config: state.config.config };
}
2022-10-20 11:57:30 -04:00
const connector = connect(mapDispatchToProps);
export type AppRootProps = ConnectedProps<typeof connector>;
2022-10-20 11:57:30 -04:00
const ConnectedTranslatedApp = connector(TranslatedApp);
function bootstrap(opts?: { config?: Config; autoInitialize?: boolean }) {
const { config, autoInitialize = true } = opts ?? {};
/**
* 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}`);
}
/**
* Get DOM element where app will mount.
*/
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;
}
2022-10-20 11:57:30 -04:00
if (autoInitialize) {
addExtensions();
}
store.dispatch(
loadConfig(config, function onLoad(config) {
if (config.backend.name !== 'git-gateway') {
store.dispatch(authenticateUser() as unknown as AnyAction);
}
2022-10-20 11:57:30 -04:00
}) as AnyAction,
);
/**
* Create connected root component.
*/
function Root() {
return (
<>
<Provider store={store}>
<ConnectedTranslatedApp />
</Provider>
</>
);
}
/**
* Render application root.
*/
2022-10-20 11:57:30 -04:00
const root = createRoot(getRoot());
root.render(<Root />);
}
export default bootstrap;