aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/index.js
blob: aa39e81076558cd9601a4915a2bc7f666c35ceb4 (plain) (blame)
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
import 'core-js/modules/es.array.map'
import 'core-js/modules/es.map'
import 'core-js/modules/es.object.create'
import 'core-js/modules/es.object.define-property'
import 'core-js/modules/es.object.set-prototype-of'
import 'core-js/modules/es.promise'
import 'core-js/modules/es.set'
import 'core-js/modules/es.symbol'
import 'core-js/modules/web.dom-collections.iterator'
import 'url-polyfill'
import { StrictMode, lazy } from 'react'
import { createRoot, hydrateRoot } from 'react-dom/client'
import { BrowserRouter } from 'react-router-dom'
import { HelmetProvider } from 'react-helmet-async'

import { VisitorProvider } from './ui/VisitorContext'
import { me } from './api'

const Juick = lazy(() => import('./App'))
const props = window.__PROPS__ ? JSON.parse(decodeURIComponent(escape(atob(window.__PROPS__)))) : {}

function ready(fn) {
    if (document.readyState != 'loading') {
        fn()
    } else {
        document.addEventListener('DOMContentLoaded', fn)
    }
}

ready(async () => {
    let visitor
    try {
        visitor = await me()
    } catch (e) {
        visitor = undefined
    }
    const JuickApp = () => (
        <StrictMode>
            <HelmetProvider>
                <VisitorProvider auth={visitor}>
                    <BrowserRouter>
                        <Juick {...props} />
                    </BrowserRouter>
                </VisitorProvider>
            </HelmetProvider>
        </StrictMode>
    )
    let root = document.getElementById('app')
    if (window.__PROPS__) {
        hydrateRoot(root, <JuickApp />)
    } else {
        createRoot(root).render(<JuickApp />)
    }
})