File size: 2,681 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
// @flow
import 'css.escape';
import React from 'react';
import ReactDOM from 'react-dom';
import { ApolloProvider } from 'react-apollo';
import { Provider } from 'react-redux';
import { Router } from 'react-router';
import queryString from 'query-string';
import Loadable from 'react-loadable';
import * as OfflinePluginRuntime from 'offline-plugin/runtime';
import { HelmetProvider } from 'react-helmet-async';
import webPushManager from 'src/helpers/web-push-manager';
import { history } from 'src/helpers/history';
import { client } from 'shared/graphql';
import { initStore } from 'src/store';
import { wsLink } from 'shared/graphql';
import RedirectHandler from 'src/components/redirectHandler';
const params = queryString.parse(history.location.search);

// Redirect legacy ?thread=asdf & ?t=asdf URLs to the proper /<community>/<channel>/<thread>
// equivalents via the /thread/<id> shorthand
const threadParam = params.thread || params.t;
if (threadParam) {
  if (params.m) {
    history.replace(`/thread/${threadParam}?m=${params.m}`);
  } else {
    history.replace(`/thread/${threadParam}`);
  }
}
// If the server passes an initial redux state use that, otherwise construct our own
const store = initStore(window.__SERVER_STATE__ || {});

const App = () => {
  return (
    <Provider store={store}>
      <HelmetProvider>
        <ApolloProvider client={client}>
          <Router history={history}>
            <RedirectHandler
              maintenanceMode={
                process.env.REACT_APP_MAINTENANCE_MODE === 'enabled'
              }
            />
          </Router>
        </ApolloProvider>
      </HelmetProvider>
    </Provider>
  );
};

const renderMethod = window.__SERVER_STATE__
  ? // $FlowIssue
    ReactDOM.hydrate
  : ReactDOM.render;

function render() {
  return renderMethod(
    <App />,
    // $FlowIssue
    document.querySelector('#root')
  );
}

Loadable.preloadReady()
  .then(render)
  .catch(err => {
    console.error(err);
  });

OfflinePluginRuntime.install({
  // Apply new updates immediately
  onUpdateReady: () => OfflinePluginRuntime.applyUpdate(),
});

if ('serviceWorker' in navigator && 'PushManager' in window) {
  // $FlowIssue
  navigator.serviceWorker.ready.then(registration => {
    webPushManager.set(registration.pushManager);
  });
}

wsLink.subscriptionClient.on('disconnected', () =>
  store.dispatch({ type: 'WEBSOCKET_CONNECTION', value: 'disconnected' })
);
wsLink.subscriptionClient.on('connected', () =>
  store.dispatch({ type: 'WEBSOCKET_CONNECTION', value: 'connected' })
);
wsLink.subscriptionClient.on('reconnected', () =>
  store.dispatch({ type: 'WEBSOCKET_CONNECTION', value: 'reconnected' })
);