File size: 15,446 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
// @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 && <LoadingView />,
});

/* prettier-ignore */
const UserView = Loadable({
  loader: () => import('./views/user'/* webpackChunkName: "UserView" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* prettier-ignore */
const CommunityView = Loadable({
  loader: () => import('./views/community'/* webpackChunkName: "CommunityView" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* prettier-ignore */
const ChannelView = Loadable({
  loader: () => import('./views/channel'/* webpackChunkName: "ChannelView" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* prettier-ignore */
const UserSettings = Loadable({
  loader: () => import('./views/userSettings'/* webpackChunkName: "UserSettings" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* prettier-ignore */
const CommunitySettings = Loadable({
  loader: () => import('./views/communitySettings'/* webpackChunkName: "communitySettings" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* prettier-ignore */
const ChannelSettings = Loadable({
  loader: () => import('./views/channelSettings'/* webpackChunkName: "channelSettings" */),
  loading: ({ isLoading }) => isLoading && <LoadingView />,
});

/* 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 && <LoadingView />
});

const LoginFallback = signedOutFallback(() => <Redirect to="/" />, Login);
const MessagesFallback = signedOutFallback(DirectMessages, () => (
  <Login redirectPath={`${CLIENT_URL}/messages`} />
));
const UserSettingsFallback = signedOutFallback(UserSettings, () => (
  <Login redirectPath={`${CLIENT_URL}/me/settings`} />
));
const CommunitySettingsFallback = signedOutFallback(CommunitySettings, () => (
  <Login />
));
const ChannelSettingsFallback = signedOutFallback(ChannelSettings, () => (
  <Login />
));

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<Props, State> {
  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 (
        <ThemeProvider theme={theme}>
          <ScrollManager>
            <GlobalStyles />
            <Head
              title="Ongoing Maintenance - Spectrum"
              description="Spectrum is currently undergoing scheduled maintenance downtime. Please check https://twitter.com/withspectrum for ongoing updates."
            />
            <Maintenance />
          </ScrollManager>
        </ThemeProvider>
      );
    }

    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 (
      <ErrorBoundary fallbackComponent={ErrorFallback}>
        <ThemeProvider theme={theme}>
          <NavigationContext.Provider value={navigationContext}>
            {/* default meta tags, get overridden by anything further down the tree */}
            <Head title={title} description={description} />
            <GlobalStyles />
            <GlobalThreadAttachmentStyles />

            {/* dont let non-critical pieces of UI crash the whole app */}
            <ErrorBoundary>
              <Status />
            </ErrorBoundary>
            <ErrorBoundary>
              <Toasts />
            </ErrorBoundary>
            <ErrorBoundary>
              <Gallery />
            </ErrorBoundary>
            <ErrorBoundary>
              <ModalRoot />
            </ErrorBoundary>
            <ErrorBoundary>
              <QueryParamToastDispatcher />
            </ErrorBoundary>

            {isModal && (
              <Route
                // NOTE(@mxstbr): This custom path regexp matches threadId correctly in all cases, no matter if we prepend it with a custom slug or not.
                // Imagine our threadId is "id-123-id" (similar in shape to an actual UUID)
                // - /id-123-id => 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 => (
                  <ThreadSlider
                    previousLocation={this.previousLocation}
                    {...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.
            */}
            <RouteModalContext.Provider value={routeModalContext}>
              {/*
                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
              */}
              <AppViewWrapper {...routeModalContext}>
                <Route component={Navigation} />
                <Route component={GlobalTitlebar} />

                <div css={isModal ? { overflow: 'hidden' } : {}}>
                  <ErrorBoundary>
                    <AnnouncementBanner />
                  </ErrorBoundary>

                  {/*
                    switch only renders the first match. Subrouting happens downstream
                    https://reacttraining.com/react-router/web/api/Switch
                  */}
                  <Switch location={isModal ? this.previousLocation : location}>
                    <Route
                      exact
                      path="/"
                      render={() => <Redirect to="/explore" />}
                    />

                    {/* Public Business Pages */}
                    <Route
                      path="/home"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/about"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/contact"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/support"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/faq"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/features"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      path="/new"
                      exact
                      render={() => <Redirect to="/explore" />}
                    />

                    <Route path="/terms" component={Pages} />
                    <Route path="/privacy" component={Pages} />
                    <Route path="/terms.html" component={Pages} />
                    <Route path="/privacy.html" component={Pages} />
                    <Route path="/code-of-conduct" component={Pages} />

                    {/* App Pages */}
                    <Route path="/login" component={LoginFallback} />
                    <Route path="/explore" component={Explore} />
                    <Route
                      path="/messages/:threadId"
                      component={MessagesFallback}
                    />
                    <Route path="/messages" component={MessagesFallback} />
                    <Route
                      path="/thread/:threadId"
                      component={RedirectOldThreadRoute}
                    />
                    <Route path="/thread" render={() => <Redirect to="/" />} />
                    <Route
                      exact
                      path="/users"
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route exact path="/users/:username" component={UserView} />
                    <Route
                      exact
                      path="/users/:username/settings"
                      component={UserSettingsFallback}
                    />

                    <Route
                      path="/me/settings"
                      render={() =>
                        currentUser && currentUser.username ? (
                          <Redirect
                            to={`/users/${currentUser.username}/settings`}
                          />
                        ) : currentUser && !currentUser.username ? (
                          <NewUserOnboarding />
                        ) : isLoadingCurrentUser ? null : (
                          <Login redirectPath={`${CLIENT_URL}/me/settings`} />
                        )
                      }
                    />
                    <Route
                      path="/me"
                      render={() =>
                        currentUser && currentUser.username ? (
                          <Redirect to={`/users/${currentUser.username}`} />
                        ) : currentUser && !currentUser.username ? (
                          <NewUserOnboarding />
                        ) : isLoadingCurrentUser ? null : (
                          <Login redirectPath={`${CLIENT_URL}/me`} />
                        )
                      }
                    />

                    {/*
                        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
                      */}
                    <Route
                      path="/:communitySlug/:channelSlug/settings"
                      component={ChannelSettingsFallback}
                    />
                    <Route
                      path="/:communitySlug/settings"
                      component={CommunitySettingsFallback}
                    />
                    <Route
                      path="/:communitySlug/login"
                      render={() => <Redirect to="/explore" />}
                    />
                    <Route
                      // NOTE(@mxstbr): This custom path regexp matches threadId correctly in all cases, no matter if we prepend it with a custom slug or not.
                      // Imagine our threadId is "id-123-id" (similar in shape to an actual UUID)
                      // - /id-123-id => 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}
                    />
                    <Route
                      path="/:communitySlug/:channelSlug"
                      component={ChannelView}
                    />
                    <Route path="/:communitySlug" component={CommunityView} />
                  </Switch>
                </div>

                {isModal && (
                  <Route
                    path="/thread/:threadId"
                    component={RedirectOldThreadRoute}
                  />
                )}
              </AppViewWrapper>
            </RouteModalContext.Provider>
          </NavigationContext.Provider>
        </ThemeProvider>
      </ErrorBoundary>
    );
  }
}

export default compose(
  withCurrentUser,
  withRouter
)(Routes);