-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.jsx
138 lines (122 loc) · 4.11 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
/* global cozy */
import 'whatwg-fetch'
import React from 'react'
import { render } from 'react-dom'
import { Router, Route, Redirect, hashHistory } from 'react-router'
import CozyClient, { models } from 'cozy-client'
import { RealtimePlugin } from 'cozy-realtime'
import { I18n, initTranslation } from 'cozy-ui/transpiled/react/I18n'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import SharingProvider from 'cozy-sharing'
import { getQueryParameter } from 'react-cozy-helpers'
import { schema } from 'drive/lib/doctypes'
import configureStore from 'drive/store/configureStore'
import PublicLayout from 'drive/web/modules/public/PublicLayout'
import LightFolderView from 'drive/web/modules/public/LightFolderView'
import LightFileViewer from 'drive/web/modules/public/LightFileViewer'
import FileHistory from 'components/FileHistory'
import ErrorShare from 'components/Error/ErrorShare'
import { configureReporter, setCozyUrl } from 'drive/lib/reporter'
import getSharedDocument from 'cozy-sharing/dist/getSharedDocument'
import appMetadata from 'drive/appMetadata'
import logger from 'lib/logger'
import App from 'components/App/App'
import ExternalRedirect from 'drive/web/modules/navigation/ExternalRedirect'
const initCozyBar = (data, client) => {
if (
data.cozyAppName &&
data.cozyAppEditor &&
data.cozyIconPath &&
data.cozyLocale
) {
cozy.bar.init({
appName: data.cozyAppName,
appEditor: data.cozyAppEditor,
cozyClient: client,
iconPath: data.cozyIconPath,
lang: data.cozyLocale,
replaceTitleOnMobile: true,
isPublic: true
})
}
}
const renderError = (lang, root) =>
render(
<I18n lang={lang} dictRequire={lang => require(`drive/locales/${lang}`)}>
<ErrorShare errorType={`public_unshared`} />
</I18n>,
root
)
const init = async () => {
const lang = document.documentElement.getAttribute('lang') || 'en'
const root = document.querySelector('[role=application]')
const dataset = root.dataset
const { sharecode } = getQueryParameter()
const protocol = window.location ? window.location.protocol : 'https:'
const cozyUrl = `${protocol}//${dataset.cozyDomain}`
const client = new CozyClient({
uri: cozyUrl,
token: sharecode,
appMetadata,
schema
})
client.registerPlugin(RealtimePlugin)
configureReporter()
setCozyUrl(cozyUrl)
// we still need cozy-client-js for opening a folder
cozy.client.init({
cozyURL: cozyUrl,
token: sharecode
})
const polyglot = initTranslation(dataset.cozyLocale, lang =>
require(`drive/locales/${lang}`)
)
const store = configureStore(client, polyglot.t.bind(polyglot))
try {
const sharedDocumentId = await getSharedDocument(client)
const { data } = await client
.collection('io.cozy.files')
.get(sharedDocumentId)
const isFile = data && data.type === 'file'
const isNote = models.file.isNote(data)
if (isNote) {
try {
window.location.href = await models.note.fetchURL(client, data)
} catch (e) {
Alerter.error('alert.offline')
}
} else {
initCozyBar(dataset)
render(
<App lang={lang} polyglot={polyglot} client={client} store={store}>
{isFile ? (
<PublicLayout>
<LightFileViewer files={[data]} isFile={true} />
</PublicLayout>
) : (
<SharingProvider>
<Router history={hashHistory}>
<Route component={PublicLayout}>
<Route path="files(/:folderId)" component={LightFolderView}>
<Route
path="file/:fileId/revision"
component={FileHistory}
/>
</Route>
</Route>
<Route path="external/:fileId" component={ExternalRedirect} />
<Redirect from="/*" to={`files/${sharedDocumentId}`} />
</Router>
</SharingProvider>
)}
</App>,
root
)
}
} catch (e) {
logger.warn(e)
initCozyBar(dataset, client)
renderError(lang, root)
}
}
document.addEventListener('DOMContentLoaded', init)