bigbuffet-rw/app/soapbox/containers/soapbox.tsx

229 lines
8.2 KiB
TypeScript
Raw Normal View History

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