2020-03-27 13:59:38 -07:00
|
|
|
'use strict';
|
|
|
|
|
2022-01-10 14:17:52 -08:00
|
|
|
import classNames from 'classnames';
|
2022-04-21 09:47:28 -07:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { IntlProvider } from 'react-intl';
|
2022-04-21 10:19:33 -07:00
|
|
|
import { Provider } from 'react-redux';
|
2022-02-25 07:08:05 -08:00
|
|
|
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';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
2021-10-20 11:18:55 -07:00
|
|
|
import { loadInstance } from 'soapbox/actions/instance';
|
2020-05-28 15:52:07 -07:00
|
|
|
import { fetchMe } from 'soapbox/actions/me';
|
2022-01-10 14:17:52 -08:00
|
|
|
import { loadSoapboxConfig } 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';
|
2022-01-10 14:17:52 -08:00
|
|
|
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';
|
2022-01-10 14:17:52 -08:00
|
|
|
import PublicLayout from 'soapbox/features/public_layout';
|
2022-04-12 06:52:04 -07:00
|
|
|
import NotificationsContainer from 'soapbox/features/ui/containers/notifications_container';
|
2022-03-21 11:09:01 -07:00
|
|
|
import WaitlistPage from 'soapbox/features/verification/waitlist_page';
|
2021-11-02 09:47:26 -07:00
|
|
|
import { createGlobals } from 'soapbox/globals';
|
2022-04-30 10:02:30 -07:00
|
|
|
import { useAppSelector, useAppDispatch, useOwnAccount, useFeatures, useSoapboxConfig, useSettings } from 'soapbox/hooks';
|
2022-04-21 09:47:28 -07:00
|
|
|
import MESSAGES from 'soapbox/locales/messages';
|
2022-04-19 16:15:42 -07:00
|
|
|
import { getFeatures } from 'soapbox/utils/features';
|
2022-03-23 13:31:19 -07:00
|
|
|
import { generateThemeCss } from 'soapbox/utils/theme';
|
2022-01-10 14:25:06 -08:00
|
|
|
|
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';
|
2022-01-10 14:17:52 -08:00
|
|
|
import ErrorBoundary from '../components/error_boundary';
|
2022-01-10 14:01:24 -08:00
|
|
|
import UI from '../features/ui';
|
2022-03-14 16:01:09 -07:00
|
|
|
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
|
|
|
|
2021-11-02 09:47:26 -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);
|
2021-09-03 05:42:27 -07:00
|
|
|
|
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());
|
2022-04-19 16:33:13 -07:00
|
|
|
// Await for feature detection
|
|
|
|
await dispatch(loadInstance());
|
2022-04-19 16:15:42 -07:00
|
|
|
|
2022-04-19 16:33:13 -07:00
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
promises.push(dispatch(loadSoapboxConfig()));
|
2022-04-19 16:15:42 -07:00
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
const features = getFeatures(state.instance);
|
2022-03-21 11:09:01 -07:00
|
|
|
|
2022-04-19 16:15:42 -07:00
|
|
|
if (features.pepe && !state.me) {
|
2022-04-19 16:33:13 -07:00
|
|
|
promises.push(dispatch(fetchVerificationConfig()));
|
2022-03-21 11:09:01 -07:00
|
|
|
}
|
2022-04-19 16:33:13 -07:00
|
|
|
|
|
|
|
await Promise.all(promises);
|
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 = () => {
|
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-04 06:08:40 -07:00
|
|
|
const colorSchemeQueryList = window.matchMedia('(prefers-color-scheme: dark)');
|
|
|
|
const [isSystemDarkMode, setSystemDarkMode] = useState(colorSchemeQueryList.matches);
|
|
|
|
const userTheme = settings.get('themeMode');
|
|
|
|
const darkMode = userTheme === 'dark' || (userTheme === 'system' && isSystemDarkMode);
|
|
|
|
|
2022-04-21 09:47:28 -07:00
|
|
|
const themeCss = generateThemeCss(soapboxConfig);
|
2020-06-04 19:22:58 -07:00
|
|
|
|
2022-05-04 06:08:40 -07:00
|
|
|
const handleSystemModeChange = (event: MediaQueryListEvent) => {
|
|
|
|
setSystemDarkMode(event.matches);
|
|
|
|
};
|
|
|
|
|
2022-04-21 09:47:28 -07:00
|
|
|
// Load the user's locale
|
|
|
|
useEffect(() => {
|
|
|
|
MESSAGES[locale]().then(messages => {
|
|
|
|
setMessages(messages);
|
|
|
|
setLocaleLoading(false);
|
|
|
|
}).catch(() => {});
|
|
|
|
}, [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(() => {
|
2022-04-26 13:07:13 -07:00
|
|
|
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-05-04 06:08:40 -07:00
|
|
|
useEffect(() => {
|
|
|
|
colorSchemeQueryList.addEventListener('change', handleSystemModeChange);
|
|
|
|
|
|
|
|
return () => colorSchemeQueryList.removeEventListener('change', handleSystemModeChange);
|
|
|
|
}, []);
|
|
|
|
|
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 }) => {
|
2022-01-06 08:45:10 -08:00
|
|
|
return !(location.state?.soapboxModalKey && location.state?.soapboxModalKey !== prevRouterProps?.location?.state?.soapboxModalKey);
|
2022-04-21 09:47:28 -07:00
|
|
|
};
|
2021-08-28 06:17: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);
|
|
|
|
|
|
|
|
const bodyClass = classNames('bg-white dark:bg-slate-900 text-base', {
|
|
|
|
'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}>
|
2022-03-24 06:17:32 -07:00
|
|
|
<Helmet>
|
2022-05-04 06:08:40 -07:00
|
|
|
<html lang={locale} className={classNames({ dark: darkMode })} />
|
2022-03-24 06:17:32 -07:00
|
|
|
<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} />
|
2022-03-24 06:17:32 -07:00
|
|
|
</Helmet>
|
|
|
|
|
2021-04-22 11:57:47 -07:00
|
|
|
<ErrorBoundary>
|
2022-04-21 10:19:33 -07:00
|
|
|
<BrowserRouter basename={BuildConfig.FE_SUBDIRECTORY}>
|
2022-04-21 09:47:28 -07:00
|
|
|
<OnboardingWizard />
|
|
|
|
<NotificationsContainer />
|
2020-04-28 13:25:10 -07:00
|
|
|
</BrowserRouter>
|
2021-04-22 11:57:47 -07:00
|
|
|
</ErrorBoundary>
|
2020-04-28 13:25:10 -07:00
|
|
|
</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-04 06:08:40 -07:00
|
|
|
<html lang={locale} className={classNames({ 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
|
|
|
|
|
|
|
{waitlisted && <Route render={(props) => <WaitlistPage {...props} account={account} />} />}
|
|
|
|
|
|
|
|
{!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='/beta/:slug?' component={PublicLayout} />
|
|
|
|
<Route exact path='/mobile/:slug?' component={PublicLayout} />
|
|
|
|
<Route exact path='/login' component={AuthLayout} />
|
2022-04-30 10:02:30 -07:00
|
|
|
{(features.accountCreation && instance.registrations) && (
|
|
|
|
<Route exact path='/signup' component={AuthLayout} />
|
|
|
|
)}
|
2022-04-30 09:28:18 -07:00
|
|
|
<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} />
|
|
|
|
|
|
|
|
<Route path='/' component={UI} />
|
|
|
|
</Switch>
|
|
|
|
</ScrollContext>
|
|
|
|
</>
|
|
|
|
</BrowserRouter>
|
|
|
|
</ErrorBoundary>
|
|
|
|
</IntlProvider>
|
|
|
|
);
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-04-21 09:21:09 -07:00
|
|
|
const Soapbox = () => {
|
|
|
|
return (
|
|
|
|
<Provider store={store}>
|
|
|
|
<SoapboxMount />
|
|
|
|
</Provider>
|
|
|
|
);
|
|
|
|
};
|
2020-03-27 13:59:38 -07:00
|
|
|
|
2022-04-21 09:21:09 -07:00
|
|
|
export default Soapbox;
|