'use strict'; import classNames from 'classnames'; import React, { useState, useEffect } from 'react'; import { IntlProvider } from 'react-intl'; 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'; import { fetchMe } from 'soapbox/actions/me'; import { loadSoapboxConfig } from 'soapbox/actions/soapbox'; import { fetchVerificationConfig } from 'soapbox/actions/verification'; import * as BuildConfig from 'soapbox/build_config'; import Helmet from 'soapbox/components/helmet'; import AuthLayout from 'soapbox/features/auth_layout'; import OnboardingWizard from 'soapbox/features/onboarding/onboarding-wizard'; import PublicLayout from 'soapbox/features/public_layout'; import NotificationsContainer from 'soapbox/features/ui/containers/notifications_container'; import WaitlistPage from 'soapbox/features/verification/waitlist_page'; import { createGlobals } from 'soapbox/globals'; import { useAppSelector, useAppDispatch, useOwnAccount, useSoapboxConfig, useSettings } from 'soapbox/hooks'; import MESSAGES from 'soapbox/locales/messages'; import { getFeatures } from 'soapbox/utils/features'; import { generateThemeCss } from 'soapbox/utils/theme'; import { ONBOARDING_VERSION } from '../actions/onboarding'; import { preload } from '../actions/preload'; import ErrorBoundary from '../components/error_boundary'; import UI from '../features/ui'; import { store } from '../store'; /** Ensure the given locale exists in our codebase */ const validLocale = (locale: string): boolean => Object.keys(MESSAGES).includes(locale); // Configure global functions for developers createGlobals(store); // Preload happens synchronously store.dispatch(preload() as any); /** Load initial data from the backend */ const loadInitial = () => { // @ts-ignore return async(dispatch, getState) => { // Await for authenticated fetch await dispatch(fetchMe()); // Await for feature detection await dispatch(loadInstance()); const promises = []; promises.push(dispatch(loadSoapboxConfig())); const state = getState(); const features = getFeatures(state.instance); if (features.pepe && !state.me) { promises.push(dispatch(fetchVerificationConfig())); } await Promise.all(promises); }; }; const SoapboxMount = () => { const dispatch = useAppDispatch(); const me = useAppSelector(state => state.me); const account = useOwnAccount(); const settings = useSettings(); const soapboxConfig = useSoapboxConfig(); const locale = validLocale(settings.get('locale')) ? settings.get('locale') : 'en'; const needsOnboarding = settings.get('onboardingVersion') < ONBOARDING_VERSION; const singleUserMode = soapboxConfig.singleUserMode && soapboxConfig.singleUserModeProfile; const [messages, setMessages] = useState>({}); const [localeLoading, setLocaleLoading] = useState(true); const [isLoaded, setIsLoaded] = useState(false); const themeCss = generateThemeCss(soapboxConfig); // Load the user's locale useEffect(() => { MESSAGES[locale]().then(messages => { setMessages(messages); setLocaleLoading(false); }).catch(() => {}); }, [locale]); // Load initial data from the API useEffect(() => { dispatch(loadInitial()).then(() => { setIsLoaded(true); }).catch(() => { setIsLoaded(false); }); }); // @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); }; if (me === null) return null; if (me && !account) return null; if (!isLoaded) return null; if (localeLoading) return null; 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'), }); if (account && !waitlisted && needsOnboarding) { return ( {themeCss && } ); } return ( {themeCss && } {soapboxConfig.appleAppId && ( )} <> {waitlisted && } />} {!me && (singleUserMode ? : )} {!me && } ); }; const Soapbox = () => { return ( ); }; export default Soapbox;