// @flow import * as React from 'react'; import { connect } from 'react-redux'; import { Route, Link, withRouter, type Location } from 'react-router-dom'; import compose from 'recompose/compose'; import type { Dispatch } from 'redux'; import { ErrorBoundary } from 'src/components/error'; import Icon from 'src/components/icon'; import { Loading } from 'src/components/loading'; import viewNetworkHandler from 'src/components/viewNetworkHandler'; import getCommunityChannels from 'shared/graphql/queries/community/getCommunityChannelConnection'; import type { GetCommunityChannelConnectionType } from 'shared/graphql/queries/community/getCommunityChannelConnection'; import { withCurrentUser } from 'src/components/withCurrentUser'; import Tooltip from 'src/components/tooltip'; import { ChannelListItem } from 'src/components/entities'; import { WhiteIconButton } from 'src/components/button'; import { SidebarSectionHeader, SidebarSectionHeading, List } from '../style'; import { Row, Content, Label, Actions, } from 'src/components/entities/listItems/style'; type Props = { data: { community: GetCommunityChannelConnectionType, }, isLoading: boolean, dispatch: Dispatch, currentUser: Object, communitySlug: string, location: Location, }; class Component extends React.Component { sortChannels = (array: Array): Array => { if (!array || array.length === 0) return []; const generalChannel = array.find(channel => channel.slug === 'general'); const withoutGeneral = array.filter(channel => channel.slug !== 'general'); const sortedWithoutGeneral = withoutGeneral.sort((a, b) => { if (a.slug < b.slug) return -1; if (a.slug > b.slug) return 1; return 0; }); if (generalChannel) { sortedWithoutGeneral.unshift(generalChannel); return sortedWithoutGeneral; } else { return sortedWithoutGeneral; } }; render() { const { isLoading, currentUser, data: { community }, location, } = this.props; if (isLoading) { return ( Channels ); } if (community && community.channelConnection) { const { isOwner } = community.communityPermissions; const channels = community.channelConnection.edges .map(channel => channel && channel.node) .filter(channel => { if (!channel) return null; if (channel.isArchived) return null; if (channel.isPrivate && !channel.channelPermissions.isMember) return null; return channel; }) .filter(channel => channel && !channel.channelPermissions.isBlocked); const sortedChannels = this.sortChannels(channels); return ( Channels {isOwner && ( )} {({ match }) => ( -1 } > )} {sortedChannels.map(channel => { if (!channel) return null; return ( {({ match }) => ( )} ); })} ); } return null; } } export const ChannelsList = compose( getCommunityChannels, viewNetworkHandler, withCurrentUser, withRouter, connect() )(Component);