-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.jsx
383 lines (355 loc) · 11.7 KB
/
index.jsx
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
/* global CONFIG */
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute, browserHistory, Redirect } from 'react-router';
import { Provider, useSelector } from 'react-redux';
import { syncHistoryWithStore } from 'react-router-redux';
import * as Sentry from '@sentry/react';
import 'autotrack'; // used by google-analytics in ../index.jade
import '../styles/common/common.scss';
import '../styles/helvetica/app.scss';
import '../styles/helvetica/dark-theme.scss';
import configureStore from './redux/configure-store';
import * as ActionCreators from './redux/action-creators';
import Layout from './components/layout';
import Home from './components/home';
import Discussions from './components/discussions';
import Summary from './components/summary';
import SinglePost from './components/single-post';
import User from './components/user';
import Subscribers from './components/subscribers';
import Subscriptions from './components/subscriptions';
import Groups from './components/groups';
import SearchFeed from './components/search-feed';
import PlainFeed from './components/plain-feed';
import ManageSubscribers from './components/manage-subscribers';
import Bookmarklet from './components/bookmarklet';
import SignupByInvitation from './components/signup-by-invitation';
import { settingsRoute } from './components/settings/routes';
Sentry.init({
dsn: CONFIG.sentry.publicDSN,
autoSessionTracking: false,
});
const store = configureStore();
//request main info for user
if (store.getState().authenticated) {
store.dispatch(ActionCreators.initialWhoAmI());
} else {
store.dispatch(ActionCreators.unauthenticated());
}
import { bindRouteActions } from './redux/route-actions';
import { initUnscroll, safeScrollTo } from './services/unscroll';
import { lazyRetry } from './utils/retry-promise';
import { HomeAux } from './components/home-aux';
import { NotFound } from './components/not-found';
// Set initial history state.
// Without this, there can be problems with third-party
// modules using history API (specifically, PhotoSwipe).
browserHistory.replace({
pathname: location.pathname,
search: location.search,
hash: location.hash,
});
const boundRouteActions = bindRouteActions(store.dispatch);
const history = syncHistoryWithStore(browserHistory, store);
const manageSubscribersActions = (next) => {
const { userName } = next.params;
store.dispatch(ActionCreators.getUserInfo(userName));
store.dispatch(ActionCreators.subscribers(userName));
};
const inviteActions = () => {
const { username } = store.getState().user;
store.dispatch(ActionCreators.subscriptions(username));
};
// needed to display mutual friends
const subscribersSubscriptionsActions = (next) => {
const { userName } = next.params;
store.dispatch(ActionCreators.subscribers(userName));
store.dispatch(ActionCreators.subscriptions(userName));
};
const enterStaticPage = (title) => () => {
store.dispatch(ActionCreators.staticPage(title));
};
history.listen(() => safeScrollTo(0, 0));
const generateRouteHooks = (callback) => ({
onEnter: callback,
onChange: (_, next) => callback(next),
});
// For some reason, the import() argument must have an explicit string type.
// See https://github.com/webpack/webpack/issues/4921#issuecomment-357147299
const lazyLoad = (path, importName = 'default') =>
lazyRetry(() => import(`${path}`).then((m) => ({ default: m[importName] })));
function InitialLayout({ children }) {
return (
<div className="container">
<div className="row header-row">
<div className="col-md-4">
<div className="header">
<h1 className="title">
<a href="/">{CONFIG.siteTitle}</a>
</h1>
<div className="jsonly">{children}</div>
</div>
</div>
</div>
</div>
);
}
initUnscroll();
function App() {
const initialized = useSelector((state) => state.initialized);
if (initialized.initial || initialized.loading) {
return (
<InitialLayout>
<p>Loading...</p>
</InitialLayout>
);
}
if (initialized.error) {
return (
<InitialLayout>
<div className="alert alert-danger" role="alert">
<p>Cannot load page: {initialized.errorText}</p>
<p>Try to reload this page later.</p>
</div>
</InitialLayout>
);
}
return (
<Router history={history}>
<Route name="bookmarklet" path="/bookmarklet" component={Bookmarklet} />
<Route path="/" component={Layout}>
<IndexRoute
name="home"
component={Home}
{...generateRouteHooks(boundRouteActions('home'))}
/>
<Route path="/list/:listId(/:listTitle)" name="homeAux" component={HomeAux} />
<Route path="about">
<IndexRoute
name="about"
component={lazyLoad('./components/about')}
onEnter={enterStaticPage('About')}
/>
<Route
path="terms"
component={lazyLoad('./components/terms')}
onEnter={enterStaticPage('Terms')}
/>
<Route
path="privacy"
component={lazyLoad('./components/privacy')}
onEnter={enterStaticPage('Privacy')}
/>
<Route
path="stats"
component={lazyLoad('./components/stats')}
onEnter={enterStaticPage('Stats')}
/>
<Route
path="donate"
component={lazyLoad('./components/donate')}
onEnter={enterStaticPage('Donate')}
/>
</Route>
<Route
path="dev"
component={lazyLoad('./components/dev')}
onEnter={enterStaticPage('Developers')}
/>
<Route
path="signin"
component={lazyLoad('./components/signin')}
onEnter={enterStaticPage('Sign in')}
/>
<Route
path="signup"
component={lazyLoad('./components/signup')}
onEnter={enterStaticPage('Sign up')}
/>
<Route path="restore" component={lazyLoad('./components/restore-password')} />
<Route path="reset" component={lazyLoad('./components/reset-password')} />
{settingsRoute('settings')}
<Route
path="settings/archive"
component={lazyLoad('./components/archive')}
onEnter={enterStaticPage('Restore from FriendFeed.com Archives')}
/>
<Route
name="groupSettings"
path="/:userName/settings"
component={lazyLoad('./components/group-settings')}
{...generateRouteHooks(boundRouteActions('getUserInfo'))}
/>
<Route
name="discussions"
path="filter/discussions"
component={Discussions}
{...generateRouteHooks(boundRouteActions('discussions'))}
/>
<Route
name="saves"
path="filter/saves"
component={Discussions}
{...generateRouteHooks(boundRouteActions('saves'))}
/>
<Route
name="summary"
path="/summary(/:days)"
component={Summary}
{...generateRouteHooks(boundRouteActions('summary'))}
/>
<Route
name="direct"
path="filter/direct"
component={Discussions}
{...generateRouteHooks(boundRouteActions('direct'))}
/>
<Route
name="search"
path="search"
component={SearchFeed}
{...generateRouteHooks(boundRouteActions('search'))}
/>
<Route
name="notifications"
path="filter/notifications"
component={lazyLoad('./components/notifications')}
{...generateRouteHooks(boundRouteActions('notifications'))}
/>
<Route
name="best_of"
path="filter/best_of"
component={PlainFeed}
{...generateRouteHooks(boundRouteActions('best_of'))}
/>
<Route
name="everything"
path="filter/everything"
component={PlainFeed}
{...generateRouteHooks(boundRouteActions('everything'))}
/>
<Route
name="groups"
path="/groups"
component={Groups}
onEnter={enterStaticPage('Groups')}
/>
<Route
name="all-groups"
path="/all-groups"
component={lazyLoad('./components/all-groups')}
/>
<Route
name="friends"
path="/friends"
component={lazyLoad('./components/friends-page', 'Friends')}
/>
<Route
name="groupCreate"
path="/groups/create"
component={lazyLoad('./components/group-create')}
onEnter={enterStaticPage('Create a group')}
/>
<Route
name="archivePost"
path="/archivePost"
component={lazyLoad('./components/archive-post')}
{...generateRouteHooks(boundRouteActions('archivePost'))}
/>
<Route
name="createInvitation"
path="/invite"
component={lazyLoad('./components/invitation-creation-form')}
onEnter={inviteActions}
/>
<Route
name="signupByInvitation"
path="/invited/:invitationId"
component={SignupByInvitation}
onEnter={boundRouteActions('signupByInvitation')}
/>
<Route
name="userFeed"
path="/:userName"
component={checkPath(User, isAccountPath)}
{...generateRouteHooks(boundRouteActions('userFeed'))}
/>
<Route
name="memories"
path="/memories/:from"
component={PlainFeed}
{...generateRouteHooks(boundRouteActions('memories'))}
/>
<Route
name="userMemories"
path="/:userName/memories/:from"
component={PlainFeed}
{...generateRouteHooks(boundRouteActions('userMemories'))}
/>
<Route
name="userSummary"
path="/:userName/summary(/:days)"
component={User}
{...generateRouteHooks(boundRouteActions('userSummary'))}
/>
<Route
name="subscribers"
path="/:userName/subscribers"
component={Subscribers}
onEnter={subscribersSubscriptionsActions}
/>
<Route
name="subscriptions"
path="/:userName/subscriptions"
component={Subscriptions}
onEnter={subscribersSubscriptionsActions}
/>
<Route
name="manage-subscribers"
path="/:userName/manage-subscribers"
component={ManageSubscribers}
onEnter={manageSubscribersActions}
/>
<Route
name="userComments"
path="/:userName/comments"
component={User}
{...generateRouteHooks(boundRouteActions('userComments'))}
/>
<Route
name="userLikes"
path="/:userName/likes"
component={User}
{...generateRouteHooks(boundRouteActions('userLikes'))}
/>
<Redirect from="/.well-known/change-password" to="/settings/sign-in" />
<Route
name="post"
path="/:userName/:postId"
component={checkPath(SinglePost, isPostPath)}
{...generateRouteHooks(boundRouteActions('post'))}
/>
</Route>
</Router>
);
}
ReactDOM.render(
<Provider store={store}>
<App />
</Provider>,
document.querySelector('#app'),
);
function checkPath(Component, checker) {
return (props) => {
return checker(props) ? <Component {...props} /> : <NotFound {...props} />;
};
}
function isPostPath({ params: { postId, userName } }) {
return (
/^[a-z\d-]{3,25}$/i.test(userName) &&
/^[a-f\d]{8}-[a-f\d]{4}-4[a-f\d]{3}-[89ab][a-f\d]{3}-[a-f\d]{12}$/i.test(postId)
);
}
function isAccountPath({ params: { userName } }) {
return /^[a-z\d-]{3,25}$/i.test(userName);
}