<commit_before>/* 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: <PASSWORD>,
    appMetadata,
    schema
  })
  client.registerPlugin(RealtimePlugin)

  configureReporter()
  setCozyUrl(cozyUrl)
  // we still need cozy-client-js for opening a folder
  cozy.client.init({
    cozyURL: cozyUrl,
    token: <PASSWORD>
  })

  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}>
          <SharingProvider>
            {isFile ? (
              <PublicLayout>
                <LightFileViewer files={[data]} isFile={true} />
              </PublicLayout>
            ) : (
              <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>
            )}<commit_msg>feat: Make url names consistent on public view<commit_after>/* 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}>
          <SharingProvider>
            {isFile ? (
              <PublicLayout>
                <LightFileViewer files={[data]} isFile={true} />
              </PublicLayout>
            ) : (
              <Router history={hashHistory}>
                <Route component={PublicLayout}>
                  <Redirect from="/files/:folderId" to="/folder/:folderId" />
                  <Route path="folder(/:folderId)" component={LightFolderView}>
                    <Route
                      path="file/:fileId/revision"
                      component={FileHistory}
                    />
                  </Route>
                </Route>
                <Route path="external/:fileId" component={ExternalRedirect} />
                <Redirect from="/*" to={`files/${sharedDocumentId}`} />
              </Router>
            )}
{'fraction_non_alphanumeric': 0.12147239263803682, 'fraction_numerical': 0.00196319018404908, 'mean_word_length': 2.9319826338639654, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4021863', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2416, 'n_words': 526}