File size: 2,805 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
// @flow
import React from 'react';
import compose from 'recompose/compose';
import { Route, type History } from 'react-router-dom';
import Tooltip from 'src/components/tooltip';
import { MIN_WIDTH_TO_EXPAND_NAVIGATION } from 'src/components/layout';
import viewNetworkHandler from 'src/components/viewNetworkHandler';
import { ErrorBoundary } from 'src/components/error';
import {
  getCurrentUserCommunityConnection,
  type GetUserCommunityConnectionType,
} from 'shared/graphql/queries/user/getUserCommunityConnection';
import { getAccessibilityActiveState } from './accessibility';
import { AvatarGrid, AvatarLink, Avatar, Label } from './style';

type Props = {
  data: {
    user: GetUserCommunityConnectionType,
  },
  history: History,
  sidenavIsOpen: boolean,
  setNavigationIsOpen: Function,
};

const CommunityListItem = props => {
  const { isActive, community, sidenavIsOpen, onClick } = props;

  const isWideViewport =
    window && window.innerWidth > MIN_WIDTH_TO_EXPAND_NAVIGATION;

  return (
    <Tooltip
      content={community.name}
      placement={'left'}
      isEnabled={!isWideViewport}
    >
      <AvatarGrid isActive={isActive}>
        <AvatarLink
          to={`/${community.slug}?tab=posts`}
          onClick={onClick}
          {...getAccessibilityActiveState(isActive)}
        >
          <Avatar src={community.profilePhoto} size={sidenavIsOpen ? 32 : 36} />

          <Label>{community.name}</Label>
        </AvatarLink>
      </AvatarGrid>
    </Tooltip>
  );
};

const CommunityList = (props: Props) => {
  const { data, sidenavIsOpen, setNavigationIsOpen } = props;
  const { user } = data;

  if (!user) return null;

  const { communityConnection } = user;
  const { edges } = communityConnection;
  const communities = edges.map(edge => edge && edge.node);

  const sorted = communities.slice();

  return sorted.map((community, index) => {
    if (!community) return null;

    const { communityPermissions } = community;
    const { isMember, isBlocked } = communityPermissions;
    if (!isMember || isBlocked) return null;

    return (
      <ErrorBoundary key={community.id}>
        <Route path="/:communitySlug">
          {({ match }) => {
            const isActive =
              match &&
              match.params &&
              match.params.communitySlug === community.slug;

            return (
              <CommunityListItem
                isActive={isActive}
                community={community}
                index={index}
                sidenavIsOpen={sidenavIsOpen}
                onClick={() => setNavigationIsOpen(false)}
              />
            );
          }}
        </Route>
      </ErrorBoundary>
    );
  });
};
export default compose(
  getCurrentUserCommunityConnection,
  viewNetworkHandler
)(CommunityList);