soapbox/app/soapbox/containers/soapbox.tsx

262 wiersze
8.5 KiB
TypeScript
Czysty Zwykły widok Historia

2020-03-27 20:59:38 +00:00
'use strict';
import classNames from 'classnames';
2022-04-21 16:47:28 +00:00
import React, { useState, useEffect } from 'react';
import { IntlProvider } from 'react-intl';
2022-04-21 17:19:33 +00:00
import { Provider } from 'react-redux';
import { BrowserRouter, Switch, Redirect, Route } from 'react-router-dom';
// @ts-ignore: it doesn't have types
import { ScrollContext } from 'react-router-scroll-4';
import { loadInstance } from 'soapbox/actions/instance';
2020-05-28 22:52:07 +00:00
import { fetchMe } from 'soapbox/actions/me';
import { loadSoapboxConfig, getSoapboxConfig } from 'soapbox/actions/soapbox';
2022-03-21 18:09:01 +00:00
import { fetchVerificationConfig } from 'soapbox/actions/verification';
2022-04-21 17:19:33 +00:00
import * as BuildConfig from 'soapbox/build_config';
import Helmet from 'soapbox/components/helmet';
import LoadingScreen from 'soapbox/components/loading-screen';
2022-03-21 18:09:01 +00:00
import AuthLayout from 'soapbox/features/auth_layout';
import PublicLayout from 'soapbox/features/public_layout';
import BundleContainer from 'soapbox/features/ui/containers/bundle_container';
import {
ModalContainer,
NotificationsContainer,
OnboardingWizard,
2022-05-20 18:29:17 +00:00
WaitlistPage,
} from 'soapbox/features/ui/util/async-components';
import { createGlobals } from 'soapbox/globals';
2022-05-12 00:19:08 +00:00
import { useAppSelector, useAppDispatch, useOwnAccount, useFeatures, useSoapboxConfig, useSettings, useSystemTheme } from 'soapbox/hooks';
2022-04-21 16:47:28 +00:00
import MESSAGES from 'soapbox/locales/messages';
import { useCachedLocationHandler } from 'soapbox/utils/redirect';
import { generateThemeCss } from 'soapbox/utils/theme';
2022-05-02 20:55:52 +00:00
import { checkOnboardingStatus } from '../actions/onboarding';
2022-01-10 22:01:24 +00:00
import { preload } from '../actions/preload';
import ErrorBoundary from '../components/error_boundary';
2022-01-10 22:01:24 +00:00
import UI from '../features/ui';
import { store } from '../store';
2020-03-27 20:59:38 +00:00
2022-04-21 16:47:28 +00:00
/** Ensure the given locale exists in our codebase */
2022-04-21 17:19:33 +00:00
const validLocale = (locale: string): boolean => Object.keys(MESSAGES).includes(locale);
2020-06-05 02:48:45 +00:00
// Configure global functions for developers
createGlobals(store);
2022-04-19 23:15:42 +00:00
// Preload happens synchronously
2022-04-21 17:19:33 +00:00
store.dispatch(preload() as any);
2022-05-02 20:55:52 +00:00
// This happens synchronously
store.dispatch(checkOnboardingStatus() as any);
2022-04-19 23:15:42 +00:00
/** Load initial data from the backend */
const loadInitial = () => {
2022-04-21 17:19:33 +00:00
// @ts-ignore
2022-04-19 23:15:42 +00:00
return async(dispatch, getState) => {
// Await for authenticated fetch
await dispatch(fetchMe());
// Await for feature detection
await dispatch(loadInstance());
// Await for configuration
await dispatch(loadSoapboxConfig());
2022-04-19 23:15:42 +00:00
const state = getState();
const soapboxConfig = getSoapboxConfig(state);
const pepeEnabled = soapboxConfig.getIn(['extensions', 'pepe', 'enabled']) === true;
2022-03-21 18:09:01 +00:00
if (pepeEnabled && !state.me) {
await dispatch(fetchVerificationConfig());
2022-03-21 18:09:01 +00:00
}
2022-04-19 23:15:42 +00:00
};
};
2022-03-21 18:09:01 +00:00
2022-05-20 17:30:29 +00:00
/** Highest level node with the Redux store. */
2022-04-21 16:47:28 +00:00
const SoapboxMount = () => {
useCachedLocationHandler();
2022-04-21 17:19:33 +00:00
const dispatch = useAppDispatch();
2020-03-27 20:59:38 +00:00
2022-04-21 16:47:28 +00:00
const me = useAppSelector(state => state.me);
2022-04-30 17:02:30 +00:00
const instance = useAppSelector(state => state.instance);
2022-04-21 16:47:28 +00:00
const account = useOwnAccount();
const settings = useSettings();
const soapboxConfig = useSoapboxConfig();
2022-04-30 17:02:30 +00:00
const features = useFeatures();
const swUpdating = useAppSelector(state => state.meta.swUpdating);
2020-03-27 20:59:38 +00:00
2022-04-21 16:47:28 +00:00
const locale = validLocale(settings.get('locale')) ? settings.get('locale') : 'en';
2020-03-27 20:59:38 +00:00
const waitlisted = account && !account.source.get('approved', true);
2022-05-02 20:55:52 +00:00
const needsOnboarding = useAppSelector(state => state.onboarding.needsOnboarding);
const showOnboarding = account && !waitlisted && needsOnboarding;
2022-04-21 16:47:28 +00:00
const singleUserMode = soapboxConfig.singleUserMode && soapboxConfig.singleUserModeProfile;
2020-06-05 02:22:58 +00:00
2022-04-21 17:19:33 +00:00
const [messages, setMessages] = useState<Record<string, string>>({});
2022-04-21 16:47:28 +00:00
const [localeLoading, setLocaleLoading] = useState(true);
const [isLoaded, setIsLoaded] = useState(false);
2020-06-05 02:22:58 +00:00
2022-05-12 00:19:08 +00:00
const systemTheme = useSystemTheme();
2022-05-04 13:08:40 +00:00
const userTheme = settings.get('themeMode');
2022-05-12 00:19:08 +00:00
const darkMode = userTheme === 'dark' || (userTheme === 'system' && systemTheme === 'dark');
2022-05-11 19:50:53 +00:00
const pepeEnabled = soapboxConfig.getIn(['extensions', 'pepe', 'enabled']) === true;
2022-05-04 13:08:40 +00:00
2022-04-21 16:47:28 +00:00
const themeCss = generateThemeCss(soapboxConfig);
2020-06-05 02:22:58 +00:00
2022-04-21 16:47:28 +00:00
// Load the user's locale
useEffect(() => {
MESSAGES[locale]().then(messages => {
setMessages(messages);
setLocaleLoading(false);
}).catch(() => { });
2022-04-21 16:47:28 +00:00
}, [locale]);
2022-04-19 23:15:42 +00:00
2022-04-21 16:47:28 +00:00
// Load initial data from the API
useEffect(() => {
dispatch(loadInitial()).then(() => {
setIsLoaded(true);
2022-04-19 23:15:42 +00:00
}).catch(() => {
setIsLoaded(true);
2022-04-19 23:15:42 +00:00
});
2022-04-21 22:47:11 +00:00
}, []);
2020-06-05 02:22:58 +00:00
// @ts-ignore: I don't actually know what these should be, lol
const shouldUpdateScroll = (prevRouterProps, { location }) => {
return !(location.state?.soapboxModalKey && location.state?.soapboxModalKey !== prevRouterProps?.location?.state?.soapboxModalKey);
};
/** Whether to display a loading indicator. */
const showLoading = [
me === null,
me && !account,
!isLoaded,
localeLoading,
swUpdating,
].some(Boolean);
2022-05-09 12:27:26 +00:00
const bodyClass = classNames('bg-white dark:bg-slate-900 text-base h-full', {
2022-04-21 17:19:33 +00:00
'no-reduce-motion': !settings.get('reduceMotion'),
'underline-links': settings.get('underlineLinks'),
'dyslexic': settings.get('dyslexicFont'),
'demetricator': settings.get('demetricator'),
});
2020-04-21 20:05:49 +00:00
const helmet = (
<Helmet>
<html lang={locale} className={classNames('h-full', { dark: darkMode })} />
<body className={bodyClass} />
{themeCss && <style id='theme' type='text/css'>{`:root{${themeCss}}`}</style>}
{darkMode && <style type='text/css'>{':root { color-scheme: dark; }'}</style>}
<meta name='theme-color' content={soapboxConfig.brandColor} />
</Helmet>
);
/** Render the onboarding flow. */
const renderOnboarding = () => (
<BundleContainer fetchComponent={OnboardingWizard} loading={LoadingScreen}>
{(Component) => <Component />}
</BundleContainer>
);
/** Render the auth layout or UI. */
const renderSwitch = () => (
<Switch>
<Redirect from='/v1/verify_email/:token' to='/verify/email/:token' />
{/* Redirect signup route depending on Pepe enablement. */}
{/* We should prefer using /signup in components. */}
{pepeEnabled ? (
<Redirect from='/signup' to='/verify' />
) : (
<Redirect from='/verify' to='/signup' />
)}
{waitlisted && (
2022-05-20 18:29:17 +00:00
<Route render={(props) => (
<BundleContainer fetchComponent={WaitlistPage} loading={LoadingScreen}>
{(Component) => <Component {...props} account={account} />}
</BundleContainer>
)}
/>
)}
{!me && (singleUserMode
? <Redirect exact from='/' to={`/${singleUserMode}`} />
: <Route exact path='/' component={PublicLayout} />)}
{!me && (
<Route exact path='/' component={PublicLayout} />
)}
<Route exact path='/about/:slug?' component={PublicLayout} />
<Route exact path='/mobile/:slug?' component={PublicLayout} />
<Route path='/login' component={AuthLayout} />
{(features.accountCreation && instance.registrations) && (
<Route exact path='/signup' component={AuthLayout} />
)}
{pepeEnabled && (
<Route path='/verify' component={AuthLayout} />
)}
<Route path='/reset-password' component={AuthLayout} />
<Route path='/edit-password' component={AuthLayout} />
<Route path='/invite/:token' component={AuthLayout} />
<Route path='/' component={UI} />
</Switch>
);
/** Render the onboarding flow or UI. */
const renderBody = () => {
if (showOnboarding) {
return renderOnboarding();
} else {
return renderSwitch();
}
};
// intl is part of loading.
// It's important nothing in here depends on intl.
if (showLoading) {
return (
<>
{helmet}
<LoadingScreen />
</>
);
2020-03-27 20:59:38 +00:00
}
2022-04-21 16:47:28 +00:00
return (
<IntlProvider locale={locale} messages={messages}>
{helmet}
2022-04-21 16:47:28 +00:00
<ErrorBoundary>
2022-04-21 17:19:33 +00:00
<BrowserRouter basename={BuildConfig.FE_SUBDIRECTORY}>
<ScrollContext shouldUpdateScroll={shouldUpdateScroll}>
<>
{renderBody()}
<BundleContainer fetchComponent={NotificationsContainer}>
{(Component) => <Component />}
</BundleContainer>
<BundleContainer fetchComponent={ModalContainer}>
{Component => <Component />}
</BundleContainer>
</>
</ScrollContext>
2022-04-21 16:47:28 +00:00
</BrowserRouter>
</ErrorBoundary>
</IntlProvider>
);
};
2020-03-27 20:59:38 +00:00
2022-05-20 17:30:29 +00:00
/** The root React node of the application. */
const Soapbox = () => {
return (
<Provider store={store}>
<SoapboxMount />
</Provider>
);
};
2020-03-27 20:59:38 +00:00
export default Soapbox;