<commit_before>import React from 'react'
import cx from 'classnames'
import { nextPath } from '../util/navigation'
import { merge } from 'lodash'
import { flow, get, map, sortBy } from 'lodash/fp'
import { IndexA, A } from './A'
import Dropdown from './Dropdown'
import LazyLoader from './LazyLoader'
import { assetUrl } from '../util/assets'
import { navigate } from '../actions'
const { object, func, bool } = React.PropTypes

const getMenuItems = (currentUser, firstItem) =>
  [firstItem].concat(flow(
    get('memberships'),
    sortBy(m => -Date.parse(m.last_viewed_at || '2001-01-01')),
    map('community')
  )(currentUser))

export const allCommunities = () => ({
  id: null,
  avatar_url: assetUrl('/img/hylo-merkaba-300x300.png'),
  name: 'All Communities'
})

const CommunityMenu = ({ network, community }, { isMobile, dispatch, currentUser, location }) => {
  // don't show All Communities if the user is in only one
  const onlyOneCommunity = get('memberships.length', currentUser) === 1
  if (onlyOneCommunity && !community.id) {
    community = currentUser.memberships[0].community
  }
  const firstItem = network ? merge(network, {isNetwork: true}) : community
  const menuItems = getMenuItems(currentUser, firstItem)
  const currentItem = menuItems[0]
  const { isNetwork } = currentItem

  const url = community => {
    const { query, pathname } = location
    return nextPath(pathname, community, false, query)
  }

  const conversationsUrl = firstItem.id
    ? `/${network ? 'n' : 'c'}/${firstItem.slug}`
    : '/app'

  const toggle = isMobile
    ? <img src={currentItem.avatar_url}/>
    : <span className='caret'/>

  const visitCommunity = () => dispatch(navigate(`/c/${community.slug}`))

  return <div id='community-menu'>
    <IndexA to={conversationsUrl}>
      <img src={currentItem.avatar_url}/>
      <span className={cx('name', {network: isNetwork})}>
        {currentItem.name}
      </span>
    </IndexA>
    <Dropdown backdrop triangle toggleChildren={toggle} rivalrous='nav'
      insteadOfOpening={onlyOneCommunity && visitCommunity}>
      <li>
        <ul className='inner-list dropdown-menu'>
          {menuItems.length > 1 && <li>
            <A to={url()}>
              <img src={allCommunities().avatar_url}/> All Communities
            </A>
          </li>}
          {menuItems.slice(1).map(community => <li key={community.id}>
            <A to={url(community)} title={community.name}>
              <LazyLoader>
                <img src={community.avatar_url}/>
              </LazyLoader>
              {community.name}
            </A>
          </li>)}
        </ul>
      </li>
      <li className='start'>
        <div>
          <A to='/create'>Start your own community</A>
        </div>
      </li>
    </Dropdown>
  </div><commit_msg>fix dropdown when in one community & not mobile<commit_after>import React from 'react'
import cx from 'classnames'
import { nextPath } from '../util/navigation'
import { merge } from 'lodash'
import { flow, get, map, sortBy } from 'lodash/fp'
import { IndexA, A } from './A'
import Dropdown from './Dropdown'
import LazyLoader from './LazyLoader'
import { assetUrl } from '../util/assets'
import { navigate } from '../actions'
const { object, func, bool } = React.PropTypes

const getMenuItems = (currentUser, firstItem) =>
  [firstItem].concat(flow(
    get('memberships'),
    sortBy(m => -Date.parse(m.last_viewed_at || '2001-01-01')),
    map('community')
  )(currentUser))

export const allCommunities = () => ({
  id: null,
  avatar_url: assetUrl('/img/hylo-merkaba-300x300.png'),
  name: 'All Communities'
})

const CommunityMenu = ({ network, community }, { isMobile, dispatch, currentUser, location }) => {
  // don't show All Communities if the user is in only one
  const onlyOneCommunity = get('memberships.length', currentUser) === 1
  if (onlyOneCommunity && !community.id) {
    community = currentUser.memberships[0].community
  }
  const firstItem = network ? merge(network, {isNetwork: true}) : community
  const menuItems = getMenuItems(currentUser, firstItem)
  const currentItem = menuItems[0]
  const { isNetwork } = currentItem

  const url = community => {
    const { query, pathname } = location
    return nextPath(pathname, community, false, query)
  }

  const conversationsUrl = firstItem.id
    ? `/${network ? 'n' : 'c'}/${firstItem.slug}`
    : '/app'

  const toggle = isMobile
    ? <img src={currentItem.avatar_url}/>
    : <span className='caret'/>

  const visitCommunity = () => dispatch(navigate(`/c/${community.slug}`))

  return <div id='community-menu'>
    <IndexA to={conversationsUrl}>
      <img src={currentItem.avatar_url}/>
      <span className={cx('name', {network: isNetwork})}>
        {currentItem.name}
      </span>
    </IndexA>
    <Dropdown backdrop triangle toggleChildren={toggle} rivalrous='nav'
      insteadOfOpening={onlyOneCommunity && isMobile && visitCommunity}>
      <li>
        <ul className='inner-list dropdown-menu'>
          {menuItems.length > 2 && <li>
            <A to={url()}>
              <img src={allCommunities().avatar_url}/> All Communities
            </A>
          </li>}
          {menuItems.slice(1).map(community => <li key={community.id}>
            <A to={url(community)} title={community.name}>
              <LazyLoader>
                <img src={community.avatar_url}/>
              </LazyLoader>
              {community.name}
            </A>
          </li>)}
        </ul>
      </li>
      <li className='start'>
        <div>
          <A to='/create'>Start your own community</A>
        </div>
      </li>
    </Dropdown>
  </div>
{'fraction_non_alphanumeric': 0.15418736692689852, 'fraction_numerical': 0.00674237047551455, 'mean_word_length': 2.9118667591950036, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 85, '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': '1662766', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1724, 'n_words': 423}