File size: 5,526 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// @flow
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;
    }
  };

  /*
    Segments preserve scroll position when switched by default. We dont want
    this behavior - if you change the feed (eg threads => members) you should
    always end up at the top of the list. However, if the next active segment
    is chat, we want that scrolled to the bottom by default, since the behavior
    of chat is to scroll up for older messages
  */
  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();
      // If the user goes back, restore the scroll position
    } else if (stored && history.action === 'POP') {
      scrollTo(Number(stored));
    } else {
      scrollToTop();
    }
  }, [tab]);

  // Store the last scroll position on unmount
  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');

  // if the community being viewed changes, and the previous community had
  // a watercooler but the next one doesn't, select the posts tab on the new one
  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);