index.tsx 3.21 KB
Newer Older
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
/* eslint-disable @typescript-eslint/no-var-requires */
Hugo NOUTS's avatar
Hugo NOUTS committed
3
/* global cozy */
Yoan VALLET's avatar
Yoan VALLET committed
4
declare let cozy: any
Yoan VALLET's avatar
Yoan VALLET committed
5
6
7
8
declare let __PIWIK_TRACKER_URL__: string
declare let __PIWIK_SITEID__: number
declare let Piwik: any

Hugo NOUTS's avatar
Hugo NOUTS committed
9
10
11
import '../../styles/index.scss'

import React from 'react'
Yoan VALLET's avatar
Yoan VALLET committed
12
import { render } from 'react-dom'
Yoan VALLET's avatar
Yoan VALLET committed
13
import CozyClient, { Client, CozyProvider } from 'cozy-client'
14
15
import { Provider } from 'react-redux'
import configureStore from 'store'
Yoan VALLET's avatar
Yoan VALLET committed
16
import { I18n, initTranslation } from 'cozy-ui/transpiled/react/I18n'
Yoan VALLET's avatar
Yoan VALLET committed
17
import { handleOAuthResponse } from 'cozy-harvest-lib/dist/helpers/oauth'
18
import { memoize } from 'lodash'
Yoan VALLET's avatar
Yoan VALLET committed
19
20
import manifest from '../../../manifest.webapp'
import schema from 'doctypes'
Yoan VALLET's avatar
Yoan VALLET committed
21
import { createBrowserHistory, History } from 'history'
Yoan VALLET's avatar
Yoan VALLET committed
22
import { HashRouter } from 'react-router-dom'
Yoan VALLET's avatar
Yoan VALLET committed
23
import MatomoTracker from 'utils/matomoTracker'
Hugo NOUTS's avatar
Hugo NOUTS committed
24

25
const setupApp = memoize(() => {
Yoan VALLET's avatar
Yoan VALLET committed
26
  const history: History = createBrowserHistory()
Yoan VALLET's avatar
Yoan VALLET committed
27
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
28
  const root: any = document.querySelector('[role=application]')
Yoan VALLET's avatar
Yoan VALLET committed
29
30
31
32
33
34
  const data = JSON.parse(root.dataset.cozy)
  const protocol = window.location.protocol
  const cozyUrl = `${protocol}//${data.domain}`

  const locale = 'fr'
  const polyglot: any = initTranslation(locale, lang =>
Yoan VALLET's avatar
Yoan VALLET committed
35
36
    require(`locales/${lang}`)
  )
Yoan VALLET's avatar
Yoan VALLET committed
37
38
39
40
41
42
43
44
45
46
47

  const client: Client = new CozyClient({
    uri: cozyUrl,
    token: data.token,
    appMetadata: {
      slug: manifest.name,
      version: manifest.version,
    },
    schema,
  })

Yoan VALLET's avatar
Yoan VALLET committed
48
49
  const persistedState: any = {}
  const store: any = configureStore(client, persistedState)
50

Yoan VALLET's avatar
Yoan VALLET committed
51
52
53
54
55
56
57
58
59
60
  cozy.bar.init({
    appName: data.app.name,
    appEditor: data.app.editor,
    cozyClient: client,
    iconPath: data.app.icon,
    lang: data.locale,
    replaceTitleOnMobile: false,
    appSlug: data.app.slug,
    appNamePrefix: data.app.prefix,
  })
61

Yoan VALLET's avatar
Yoan VALLET committed
62
63
  Piwik.getTracker()
  const tracker = new MatomoTracker({
64
    cozyUrl: cozyUrl,
Yoan VALLET's avatar
Yoan VALLET committed
65
66
67
68
69
    url: __PIWIK_TRACKER_URL__,
    siteId: __PIWIK_SITEID__,
    history: history,
    phpFilename: 'matomo.php',
  })
Yoan VALLET's avatar
Yoan VALLET committed
70

Yoan VALLET's avatar
Yoan VALLET committed
71
  return { root, store, client, locale, polyglot, history, tracker }
72
73
74
})

const init = () => {
Yoan VALLET's avatar
Yoan VALLET committed
75
  const { root, store, client, locale, polyglot, history, tracker } = setupApp()
Hugo NOUTS's avatar
Hugo NOUTS committed
76

Yoan VALLET's avatar
Yoan VALLET committed
77
  if (handleOAuthResponse()) return
Hugo NOUTS's avatar
Hugo NOUTS committed
78
79
  const App = require('components/App').default
  render(
Yoan VALLET's avatar
Yoan VALLET committed
80
    <Provider store={store}>
81
      <CozyProvider client={client}>
Yoan VALLET's avatar
Yoan VALLET committed
82
        <I18n lang={locale} polyglot={polyglot}>
Yoan VALLET's avatar
Yoan VALLET committed
83
          <HashRouter {...history}>
Yoan VALLET's avatar
Yoan VALLET committed
84
            <App tracker={tracker} />
Yoan VALLET's avatar
Yoan VALLET committed
85
          </HashRouter>
86
87
88
89
        </I18n>
      </CozyProvider>
    </Provider>,
    root
Hugo NOUTS's avatar
Hugo NOUTS committed
90
91
92
  )
}

93
94
// initial rendering of the application
document.addEventListener('DOMContentLoaded', () => init())
95
96
97
// excludes Chrome, Edge, and all Android browsers that include the Safari name in their user agent
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent)
if (!isSafari && 'serviceWorker' in navigator) {
98
99
100
101
102
103
104
105
  window.addEventListener('load', function() {
    navigator.serviceWorker
      .register('/serviceWorker.js')
      .then(reg => console.log('service worker registered', reg.scope))
      .catch(err => console.log('service worker not registered', err))
  })
}

106
107
108
if (module.hot) {
  init()
  module.hot.accept()
109
}