// @flow import * as React from 'react'; import compose from 'recompose/compose'; import { Route, Switch, Redirect, withRouter, type Location, type History, } from 'react-router'; import { ThemeProvider } from 'styled-components'; import Loadable from 'react-loadable'; import { ErrorBoundary } from 'src/components/error'; import { CLIENT_URL } from './api/constants'; import generateMetaInfo from 'shared/generate-meta-info'; import GlobalStyles from './reset.css.js'; import { GlobalThreadAttachmentStyles } from 'src/components/message/threadAttachment/style'; import { theme } from 'shared/theme'; import AppViewWrapper from 'src/components/appViewWrapper'; import ScrollManager from 'src/components/scrollManager'; import Head from 'src/components/head'; import ModalRoot from 'src/components/modals/modalRoot'; import Gallery from 'src/components/gallery'; import Toasts from 'src/components/toasts'; import signedOutFallback from 'src/helpers/signed-out-fallback'; import ThreadSlider from 'src/views/threadSlider'; import AnnouncementBanner from 'src/components/announcementBanner'; import Navigation from 'src/views/navigation'; import Status from 'src/views/status'; import Login from 'src/views/login'; import DirectMessages from 'src/views/directMessages'; import { ThreadView } from 'src/views/thread'; import { withCurrentUser } from 'src/components/withCurrentUser'; import Maintenance from 'src/components/maintenance'; import type { GetUserType } from 'shared/graphql/queries/user/getUser'; import RedirectOldThreadRoute from './views/thread/redirect-old-route'; import NewUserOnboarding from './views/newUserOnboarding'; import QueryParamToastDispatcher from './views/queryParamToastDispatcher'; import { LoadingView } from 'src/views/viewHelpers'; import GlobalTitlebar from 'src/views/globalTitlebar'; import { NavigationContext } from 'src/helpers/navigation-context'; const Explore = Loadable({ loader: () => import('./views/explore' /* webpackChunkName: "Explore" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const UserView = Loadable({ loader: () => import('./views/user'/* webpackChunkName: "UserView" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const CommunityView = Loadable({ loader: () => import('./views/community'/* webpackChunkName: "CommunityView" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const ChannelView = Loadable({ loader: () => import('./views/channel'/* webpackChunkName: "ChannelView" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const UserSettings = Loadable({ loader: () => import('./views/userSettings'/* webpackChunkName: "UserSettings" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const CommunitySettings = Loadable({ loader: () => import('./views/communitySettings'/* webpackChunkName: "communitySettings" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const ChannelSettings = Loadable({ loader: () => import('./views/channelSettings'/* webpackChunkName: "channelSettings" */), loading: ({ isLoading }) => isLoading && , }); /* prettier-ignore */ const Pages = Loadable({ loader: () => import('./views/pages'/* webpackChunkName: "Splash" */), loading: ({ isLoading }) => isLoading && null, }); /* prettier-ignore */ const ErrorFallback = Loadable({ loader: () => import('./components/error'/* webpackChunkName: "Error" */), loading: ({ isLoading }) => isLoading && }); const LoginFallback = signedOutFallback(() => , Login); const MessagesFallback = signedOutFallback(DirectMessages, () => ( )); const UserSettingsFallback = signedOutFallback(UserSettings, () => ( )); const CommunitySettingsFallback = signedOutFallback(CommunitySettings, () => ( )); const ChannelSettingsFallback = signedOutFallback(ChannelSettings, () => ( )); export const RouteModalContext = React.createContext({ isModal: false, }); type Props = { currentUser: ?GetUserType, isLoadingCurrentUser: boolean, maintenanceMode?: boolean, location: Location, history: History, }; type State = { navigationIsOpen: boolean, }; class Routes extends React.Component { previousLocation = this.props.location; state = { navigationIsOpen: false }; setNavigationIsOpen = (val: boolean) => this.setState({ navigationIsOpen: val }); render() { const { currentUser, isLoadingCurrentUser } = this.props; const { navigationIsOpen } = this.state; const { title, description } = generateMetaInfo(); if (this.props.maintenanceMode) { return ( ); } const { location } = this.props; const isModal = false; /* !!( location.state && location.state.modal && this.previousLocation !== location ); // not initial render */ // allows any UI in the tree to open or close the side navigation on mobile const navigationContext = { navigationIsOpen, setNavigationIsOpen: this.setNavigationIsOpen, }; // allows any UI in the tree to know if it is existing within a modal or not // commonly used for background views to know that they are backgrounded const routeModalContext = { isModal }; return ( {/* default meta tags, get overridden by anything further down the tree */} {/* dont let non-critical pieces of UI crash the whole app */} {isModal && ( id-123-id, easy start that works // - /some-custom-slug~id-123-id => id-123-id, custom slug also works // - /~id-123-id => id-123-id => id-123-id, empty custom slug also works // - /some~custom~slug~id-123-id => id-123-id, custom slug with delimiter char in it (~) also works! :tada: path="/:communitySlug/:channelSlug/(.*~)?:threadId" component={props => ( )} /> )} {/* this context provider allows children views to determine how they should behave if a modal is open. For example, you could tell a community view to not paginate the thread feed if a thread modal is open. */} {/* we tell the app view wrapper any time the modal state changes so that we can restore the scroll position to where it was before the modal was opened */}
{/* switch only renders the first match. Subrouting happens downstream https://reacttraining.com/react-router/web/api/Switch */} } /> {/* Public Business Pages */} } /> } /> } /> } /> } /> } /> } /> {/* App Pages */} } /> } /> currentUser && currentUser.username ? ( ) : currentUser && !currentUser.username ? ( ) : isLoadingCurrentUser ? null : ( ) } /> currentUser && currentUser.username ? ( ) : currentUser && !currentUser.username ? ( ) : isLoadingCurrentUser ? null : ( ) } /> {/* We check communitySlug last to ensure none of the above routes pass. We handle null communitySlug values downstream by either redirecting to home or showing a 404 */} } /> id-123-id, easy start that works // - /some-custom-slug~id-123-id => id-123-id, custom slug also works // - /~id-123-id => id-123-id => id-123-id, empty custom slug also works // - /some~custom~slug~id-123-id => id-123-id, custom slug with delimiter char in it (~) also works! :tada: path="/:communitySlug/:channelSlug/(.*~)?:threadId" component={ThreadView} />
{isModal && ( )}
); } } export default compose( withCurrentUser, withRouter )(Routes);