Skip to content
Snippets Groups Projects
index.tsx 4.65 KiB
Newer Older
  • Learn to ignore specific revisions
  • /* eslint-disable @typescript-eslint/no-explicit-any */
    
    /* eslint-disable @typescript-eslint/no-var-requires */
    
    declare let __PIWIK_TRACKER_URL__: string
    declare let __PIWIK_SITEID__: number
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    declare let __SENTRY_DSN__: string
    
    declare let Piwik: any
    
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { ThemeProvider } from '@material-ui/core'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import * as Sentry from '@sentry/react'
    import { BrowserTracing } from '@sentry/tracing'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { theme } from 'components/theme'
    
    import CozyClient, { Client, CozyProvider } from 'cozy-client'
    
    import { isFlagshipApp } from 'cozy-device-helper'
    
    Yoan VALLET's avatar
    Yoan VALLET committed
    import { handleOAuthResponse } from 'cozy-harvest-lib/dist/helpers/oauth'
    
    import { WebviewIntentProvider } from 'cozy-intent'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { I18n, initTranslation } from 'cozy-ui/transpiled/react/I18n'
    
    import schema from 'doctypes'
    
    import { createHashHistory } from 'history'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { memoize } from 'lodash'
    import React from 'react'
    
    import { createRoot } from 'react-dom/client'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import { Provider } from 'react-redux'
    
    import { HashRouter } from 'react-router-dom'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import EnvironmentService from 'services/environment.service'
    
    import { setupStore } from 'store/store'
    
    import cozyBar from 'utils/cozyBar'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import logApp from 'utils/logger'
    
    import MatomoTracker from 'utils/matomoTracker'
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    import manifest from '../../../manifest.webapp'
    import '../../styles/index.scss'
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
    const setupApp = memoize(() => {
    
      const history = createHashHistory()
      const container: any = document.querySelector('[role=application]')
      const data = JSON.parse(container.dataset.cozy)
    
      const protocol = window.location.protocol
      const cozyUrl = `${protocol}//${data.domain}`
    
      const locale = 'fr'
    
      const polyglot = initTranslation(locale, lang => require(`locales/${lang}`))
    
    
      const client: Client = new CozyClient({
        uri: cozyUrl,
        token: data.token,
        appMetadata: {
          slug: manifest.name,
          version: manifest.version,
        },
        schema,
      })
    
    
      const store = setupStore(client)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      const envService = new EnvironmentService()
      const isLocal = envService.isLocal()
      const development = envService.isDev()
    
      cozyBar.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,
    
        isInvertedTheme: isFlagshipApp(),
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
      let tracker: undefined | MatomoTracker
    
      if (window.Piwik) {
        Piwik.getTracker()
        tracker = new MatomoTracker({
          cozyUrl: cozyUrl,
          url: __PIWIK_TRACKER_URL__,
          siteId: __PIWIK_SITEID__,
          history: history,
          phpFilename: 'matomo.php',
        })
      }
    
      !isLocal &&
        Sentry.init({
          dsn: __SENTRY_DSN__,
          integrations: [new BrowserTracing()],
          // Set tracesSampleRate to 1.0 to capture 100%
          // of transactions for performance monitoring.
          // We recommend adjusting this value in production
          // Set to 0 for local development
          tracesSampleRate: 1.0,
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    
          // Custom settings below
          release: client.appMetadata.version,
          environment: development ? 'development' : 'production',
          // cast because init is somehow missing dsn property
        } as Sentry.BrowserOptions)
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
    
    
      return { container, store, client, locale, polyglot, history, tracker }
    
      const { container, store, client, locale, polyglot, history, tracker } =
        setupApp()
      const root = createRoot(container)
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
    
    
    Yoan VALLET's avatar
    Yoan VALLET committed
      if (handleOAuthResponse()) return
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      const App = require('components/App').default
    
        <WebviewIntentProvider setBarContext={cozyBar.setWebviewContext}>
          <Provider store={store}>
            <CozyProvider client={client}>
              <I18n lang={locale} polyglot={polyglot}>
                <HashRouter {...history}>
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
                  <ThemeProvider theme={theme}>
                    <App tracker={tracker} />
                  </ThemeProvider>
    
                </HashRouter>
              </I18n>
            </CozyProvider>
          </Provider>
    
    Hugo NOUTS's avatar
    Hugo NOUTS committed
      )
    }
    
    
    // initial rendering of the application
    document.addEventListener('DOMContentLoaded', () => init())
    
    // 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) {
    
      window.addEventListener('load', function () {
    
        navigator.serviceWorker
          .register('/serviceWorker.js')
          .then(reg => console.log('service worker registered', reg.scope))
    
    Bastien DUMONT's avatar
    Bastien DUMONT committed
          .catch(error => {
            const errorMessage = `service worker not registered: ${JSON.stringify(
              error
            )}`
            logApp.error(errorMessage)
            Sentry.captureException(errorMessage)
          })
    
    if (module.hot) {
      init()
      module.hot.accept()