|
|
|
|
|
import React, { useEffect, useLayoutEffect } from 'react'; |
|
|
import compose from 'recompose/compose'; |
|
|
import { withRouter, type History, type Location } from 'react-router-dom'; |
|
|
import querystring from 'query-string'; |
|
|
import type { UserInfoType } from 'shared/graphql/fragments/user/userInfo'; |
|
|
import type { CommunityInfoType } from 'shared/graphql/fragments/community/communityInfo'; |
|
|
import MembersList from './membersList'; |
|
|
import { TeamMembersList } from './teamMembersList'; |
|
|
import { MobileCommunityInfoActions } from './mobileCommunityInfoActions'; |
|
|
import { ChannelsList } from './channelsList'; |
|
|
import { CommunityMeta } from 'src/components/entities/profileCards/components/communityMeta'; |
|
|
import MessagesSubscriber from 'src/views/thread/components/messagesSubscriber'; |
|
|
import { PostsFeeds } from './postsFeeds'; |
|
|
import { SegmentedControl, Segment } from 'src/components/segmentedControl'; |
|
|
import { useAppScroller } from 'src/hooks/useAppScroller'; |
|
|
import usePrevious from 'src/hooks/usePrevious'; |
|
|
import { withCurrentUser } from 'src/components/withCurrentUser'; |
|
|
import { FeedsContainer, SidebarSection, InfoContainer } from '../style'; |
|
|
|
|
|
type Props = { |
|
|
community: CommunityInfoType, |
|
|
location: Location, |
|
|
history: History, |
|
|
currentUser: UserInfoType, |
|
|
}; |
|
|
|
|
|
const Feeds = (props: Props) => { |
|
|
const { community, location, history } = props; |
|
|
const { search } = location; |
|
|
const { tab } = querystring.parse(search); |
|
|
|
|
|
const changeTab = (tab: string) => { |
|
|
return history.replace({ |
|
|
...location, |
|
|
search: querystring.stringify({ tab }), |
|
|
}); |
|
|
}; |
|
|
|
|
|
const handleTabRedirect = () => { |
|
|
const { search } = location; |
|
|
const { tab } = querystring.parse(search); |
|
|
|
|
|
if (!tab) { |
|
|
changeTab('posts'); |
|
|
} |
|
|
|
|
|
if (tab === 'chat' && !community.watercoolerId) { |
|
|
changeTab('posts'); |
|
|
} |
|
|
}; |
|
|
|
|
|
useEffect(() => { |
|
|
handleTabRedirect(); |
|
|
}, [tab]); |
|
|
|
|
|
const renderFeed = () => { |
|
|
switch (tab) { |
|
|
case 'chat': { |
|
|
if (!community.watercoolerId) return null; |
|
|
return ( |
|
|
<React.Fragment> |
|
|
<MessagesSubscriber isWatercooler id={community.watercoolerId} /> |
|
|
</React.Fragment> |
|
|
); |
|
|
} |
|
|
case 'posts': { |
|
|
return <PostsFeeds community={community} />; |
|
|
} |
|
|
case 'members': { |
|
|
return ( |
|
|
<MembersList |
|
|
id={community.id} |
|
|
filter={{ isMember: true, isBlocked: false }} |
|
|
/> |
|
|
); |
|
|
} |
|
|
case 'info': { |
|
|
return ( |
|
|
<InfoContainer> |
|
|
<SidebarSection style={{ paddingBottom: '16px' }}> |
|
|
<CommunityMeta community={community} /> |
|
|
</SidebarSection> |
|
|
|
|
|
<SidebarSection> |
|
|
<TeamMembersList |
|
|
community={community} |
|
|
id={community.id} |
|
|
first={100} |
|
|
filter={{ isModerator: true, isOwner: true }} |
|
|
/> |
|
|
</SidebarSection> |
|
|
|
|
|
<SidebarSection> |
|
|
<ChannelsList id={community.id} communitySlug={community.slug} /> |
|
|
</SidebarSection> |
|
|
|
|
|
<SidebarSection> |
|
|
<MobileCommunityInfoActions community={community} /> |
|
|
</SidebarSection> |
|
|
</InfoContainer> |
|
|
); |
|
|
} |
|
|
default: |
|
|
return null; |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { scrollToBottom, scrollToTop, scrollTo, ref } = useAppScroller(); |
|
|
const lastTab = usePrevious(tab); |
|
|
const lastScroll = ref ? ref.scrollTop : null; |
|
|
useLayoutEffect(() => { |
|
|
if (lastTab && lastTab !== tab && lastScroll) { |
|
|
sessionStorage.setItem(`last-scroll-${lastTab}`, lastScroll.toString()); |
|
|
} |
|
|
const stored = |
|
|
sessionStorage && sessionStorage.getItem(`last-scroll-${tab}`); |
|
|
if (tab === 'chat') { |
|
|
scrollToBottom(); |
|
|
|
|
|
} else if (stored && history.action === 'POP') { |
|
|
scrollTo(Number(stored)); |
|
|
} else { |
|
|
scrollToTop(); |
|
|
} |
|
|
}, [tab]); |
|
|
|
|
|
|
|
|
useLayoutEffect(() => { |
|
|
return () => { |
|
|
const elem = document.getElementById('main'); |
|
|
if (!elem) return; |
|
|
sessionStorage.setItem(`last-scroll-${tab}`, elem.scrollTop.toString()); |
|
|
}; |
|
|
}, []); |
|
|
|
|
|
const segments = ['posts', 'members', 'info']; |
|
|
if (community.watercoolerId) segments.splice(1, 0, 'chat'); |
|
|
|
|
|
|
|
|
|
|
|
useEffect(() => { |
|
|
handleTabRedirect(); |
|
|
}, [community.slug]); |
|
|
|
|
|
return ( |
|
|
<FeedsContainer data-cy="community-view-content"> |
|
|
<SegmentedControl> |
|
|
{segments.map(segment => { |
|
|
return ( |
|
|
<Segment |
|
|
key={segment} |
|
|
hideOnDesktop |
|
|
isActive={segment === tab} |
|
|
onClick={() => changeTab(segment)} |
|
|
> |
|
|
{segment[0].toUpperCase() + segment.substr(1)} |
|
|
</Segment> |
|
|
); |
|
|
})} |
|
|
</SegmentedControl> |
|
|
{renderFeed()} |
|
|
</FeedsContainer> |
|
|
); |
|
|
}; |
|
|
|
|
|
export const CommunityFeeds = compose( |
|
|
withRouter, |
|
|
withCurrentUser |
|
|
)(Feeds); |
|
|
|