import React from 'react';
import { defineMessages, injectIntl, FormattedMessage } from 'react-intl';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import Column from 'mastodon/components/column';
import ColumnHeader from 'mastodon/components/column_header';
import { NavLink, Switch, Route } from 'react-router-dom';
import Links from './links';
import Tags from './tags';
import Statuses from './statuses';
import Suggestions from './suggestions';
import Search from 'mastodon/features/compose/containers/search_container';
import SearchResults from './results';
import { Helmet } from 'react-helmet';
import { showTrends } from 'mastodon/initial_state';

const messages = defineMessages({
  title: { id: 'explore.title', defaultMessage: 'Explore' },
  searchResults: { id: 'explore.search_results', defaultMessage: 'Search results' },
});

const mapStateToProps = state => ({
  layout: state.getIn(['meta', 'layout']),
  isSearching: state.getIn(['search', 'submitted']) || !showTrends,
});

export default @connect(mapStateToProps)
@injectIntl
class Explore extends React.PureComponent {

  static contextTypes = {
    router: PropTypes.object,
    identity: PropTypes.object,
  };

  static propTypes = {
    intl: PropTypes.object.isRequired,
    multiColumn: PropTypes.bool,
    isSearching: PropTypes.bool,
  };

  handleHeaderClick = () => {
    this.column.scrollTop();
  }

  setRef = c => {
    this.column = c;
  }

  render () {
    const { intl, multiColumn, isSearching } = this.props;
    const { signedIn } = this.context.identity;

    return (
      <Column bindToDocument={!multiColumn} ref={this.setRef} label={intl.formatMessage(messages.title)}>
        <ColumnHeader
          icon={isSearching ? 'search' : 'hashtag'}
          title={intl.formatMessage(isSearching ? messages.searchResults : messages.title)}
          onClick={this.handleHeaderClick}
          multiColumn={multiColumn}
        />

        <div className='explore__search-header'>
          <Search />
        </div>

        <div className='scrollable scrollable--flex'>
          {isSearching ? (
            <SearchResults />
          ) : (
            <React.Fragment>
              <div className='account__section-headline'>
                <NavLink exact to='/explore'><FormattedMessage id='explore.trending_statuses' defaultMessage='Posts' /></NavLink>
                <NavLink exact to='/explore/tags'><FormattedMessage id='explore.trending_tags' defaultMessage='Hashtags' /></NavLink>
                <NavLink exact to='/explore/links'><FormattedMessage id='explore.trending_links' defaultMessage='News' /></NavLink>
                {signedIn && <NavLink exact to='/explore/suggestions'><FormattedMessage id='explore.suggested_follows' defaultMessage='For you' /></NavLink>}
              </div>

              <Switch>
                <Route path='/explore/tags' component={Tags} />
                <Route path='/explore/links' component={Links} />
                <Route path='/explore/suggestions' component={Suggestions} />
                <Route exact path={['/explore', '/explore/posts', '/search']} component={Statuses} componentParams={{ multiColumn }} />
              </Switch>

              <Helmet>
                <title>{intl.formatMessage(messages.title)}</title>
                <meta name='robots' content={isSearching ? 'noindex' : 'all'} />
              </Helmet>
            </React.Fragment>
          )}
        </div>
      </Column>
    );
  }

}

@@ -1017,16 +1017,527 @@
 s,%0A%7D);%0A%0A
+// Fix strange bug on Safari where %3Cspan%3E (rendered by FormattedMessage) disappears%0A// after clicking around Explore top bar (issue #20885).%0A// Removing width=100%25 from %3Ca%3E also fixes it, as well as replacing %3Cspan%3E with %3Cdiv%3E%0A// We're choosing to wrap span with div to keep the changes local only to this tool bar.%0Aconst WrapFormattedMessage = (%7B children, ...props %7D) =%3E %3Cdiv%3E%3CFormattedMessage %7B...props%7D%3E%7Bchildren%7D%3C/FormattedMessage%3E%3C/div%3E;%0AWrapFormattedMessage.propTypes = %7B%0A  children: PropTypes.any,%0A%7D;%0A%0A%0A
 export d
@@ -1970,17 +1970,16 @@
   render
- 
 () %7B%0A   
@@ -2805,16 +2805,20 @@
 plore'%3E%3C
+Wrap
 Formatte
@@ -2940,24 +2940,28 @@
 lore/tags'%3E%3C
+Wrap
 FormattedMes
@@ -3079,24 +3079,28 @@
 ore/links'%3E%3C
+Wrap
 FormattedMes
@@ -3238,16 +3238,20 @@
 tions'%3E%3C
+Wrap
 Formatte

Fix safari explore disappearing tabs (#20917)