<commit_before>import React from 'react'
import PropTypes from 'prop-types'
import { flowRight } from 'lodash'
import { withRouter } from 'next/router'
import { translate } from 'react-i18next'

import withSession from '../../hoc/with-session'

import Container from '../../container'
import Link from '../../link'
import LanguageSelection from './language-selection'

import { PUBLIC_URL, PUBLICATION_BASE_URL } from '@env'

class Header extends React.Component {
  static propTypes = {
    router: PropTypes.shape({
      pathname: PropTypes.string.isRequired
    }).isRequired,

    session: PropTypes.shape({
      user: PropTypes.shape({
        id: PropTypes.string.isRequired,
        first_name: PropTypes.string.isRequired,
        last_name: PropTypes.string.isRequired,
        avatar_thumbnail: PropTypes.string.isRequired
      }),
      clear: PropTypes.func.isRequired
    }),

    i18n: PropTypes.shape({
      language: PropTypes.string.isRequired
    }).isRequired,
    t: PropTypes.func.isRequired
  }

  clearSession = e => {
    const { session } = this.props

    session.clear()
  }

  render() {
    const { router, session, t, i18n: { language } } = this.props

    const publicUrl = `${PUBLIC_URL}/${language}`

    const loginRedirect = `${publicUrl}/publication`
    const isPublication = router.pathname.startsWith('/publication')
    const logoutRedirect = isPublication ? `${PUBLIC_URL}/${language}` : PUBLIC_URL + router.asPath

    const logInUrl = `${PUBLICATION_BASE_URL}/login?redirect=${encodeURIComponent(loginRedirect)}`
    const logoutUrl = `${PUBLICATION_BASE_URL}/logout?redirect=${encodeURIComponent(logoutRedirect)}`

    return (
      <nav>
        <Container fluid>
          <div>
            <Link href='/' prefetch>
              <a className='logo'>
                <img src='/static/images/logo.svg' alt='Logo de la République française (1999)' />
              </a>
            </Link>

            <ul>
              <li>
                {session && session.user ? (
                  <Link href='/publication'>
                    <a>
                      <img alt='' className='avatar' src={session.user.avatar_thumbnail || '/static/images/avatar.png'} />
                      {session.user.first_name} {session.user.last_name}
                    </a>
                  </Link>
                ) : (
                  <a href={logInUrl} onClick={session && this.clearSession}>
                    {t('header.publish')}
                  </a>
                )}
              </li>
              <li>
                <LanguageSelection />
              </li>
              {session && session.user && (
                <li>
                  <a href={logoutUrl} onClick={this.clearSession}>
                    <span>{t('header.logout')}</span>
                  </a>
                </li>
              )}
            </ul>
          </div>
        </Container>

        <style jsx>{`
          @import 'colors';<commit_msg>Prefix publication home<commit_after>import React from 'react'
import PropTypes from 'prop-types'
import { flowRight } from 'lodash'
import { withRouter } from 'next/router'
import { translate } from 'react-i18next'

import withSession from '../../hoc/with-session'

import Container from '../../container'
import Link from '../../link'
import LanguageSelection from './language-selection'

import { PUBLIC_URL, PUBLICATION_BASE_URL } from '@env'

class Header extends React.Component {
  static propTypes = {
    router: PropTypes.shape({
      pathname: PropTypes.string.isRequired
    }).isRequired,

    session: PropTypes.shape({
      user: PropTypes.shape({
        id: PropTypes.string.isRequired,
        first_name: PropTypes.string.isRequired,
        last_name: PropTypes.string.isRequired,
        avatar_thumbnail: PropTypes.string.isRequired
      }),
      clear: PropTypes.func.isRequired
    }),

    i18n: PropTypes.shape({
      language: PropTypes.string.isRequired
    }).isRequired,
    t: PropTypes.func.isRequired
  }

  clearSession = e => {
    const { session } = this.props

    session.clear()
  }

  render() {
    const { router, session, t, i18n: { language } } = this.props

    const publicUrl = `${PUBLIC_URL}/${language}`

    const loginRedirect = `${publicUrl}/publication`
    const isPublication = router.pathname.startsWith('/publication')
    const logoutRedirect = isPublication ? `${PUBLIC_URL}/${language}` : PUBLIC_URL + router.asPath

    const logInUrl = `${PUBLICATION_BASE_URL}/login?redirect=${encodeURIComponent(loginRedirect)}`
    const logoutUrl = `${PUBLICATION_BASE_URL}/logout?redirect=${encodeURIComponent(logoutRedirect)}`

    return (
      <nav>
        <Container fluid>
          <div>
            <Link href='/' prefetch>
              <a className='logo'>
                <img src='/static/images/logo.svg' alt='Logo de la République française (1999)' />
              </a>
            </Link>

            <ul>
              <li>
                {session && session.user ? (
                  <Link href='/publication' prefetch>
                    <a>
                      <img alt='' className='avatar' src={session.user.avatar_thumbnail || '/static/images/avatar.png'} />
                      {session.user.first_name} {session.user.last_name}
                    </a>
                  </Link>
                ) : (
                  <a href={logInUrl} onClick={session && this.clearSession}>
                    {t('header.publish')}
                  </a>
                )}
              </li>
              <li>
                <LanguageSelection />
              </li>
              {session && session.user && (
                <li>
                  <a href={logoutUrl} onClick={this.clearSession}>
                    <span>{t('header.logout')}</span>
                  </a>
                </li>
              )}
            </ul>
          </div>
        </Container>

        <style jsx>{`
          @import 'colors';
{'fraction_non_alphanumeric': 0.13295473525972942, 'fraction_numerical': 0.0033405712376816435, 'mean_word_length': 1.873320537428023, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, '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': '1055057', 'n_tokens_mistral': 1698, 'n_tokens_neox': 1655, 'n_words': 342}