forked from withspectrum/spectrum
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchannel.js
More file actions
202 lines (183 loc) · 5.78 KB
/
channel.js
File metadata and controls
202 lines (183 loc) · 5.78 KB
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// @flow
import React, { useState } from 'react';
import compose from 'recompose/compose';
import { Link } from 'react-router-dom';
import type { ChannelInfoType } from 'shared/graphql/fragments/channel/channelInfo';
import { ErrorBoundary } from 'src/components/error';
import { withCurrentUser } from 'src/components/withCurrentUser';
import Icon from 'src/components/icon';
import JoinChannelWrapper from 'src/components/joinChannelWrapper';
import LeaveChannelWrapper from 'src/components/leaveChannelWrapper';
import ToggleChannelNotifications from 'src/components/toggleChannelNotifications';
import { OutlineButton, PrimaryOutlineButton } from 'src/components/button';
import Tooltip from 'src/components/tooltip';
import {
ChannelRow,
ChannelContent,
Label,
Description,
ChannelActions,
} from './style';
type Props = {
channel: ?ChannelInfoType,
id: string,
name?: string,
description?: ?string,
currentUser: ?Object,
children?: React$Node,
isActive?: boolean,
};
const Channel = (props: Props) => {
const {
channel,
name,
description,
children,
currentUser,
isActive = false,
} = props;
const [isHoveringNotifications, setIsHoveringNotifications] = useState(false);
if (!channel) return null;
const renderAction = () => {
const chevron = <Icon glyph="view-forward" size={24} />;
if (!currentUser) return chevron;
const { community, channelPermissions } = channel;
const { communityPermissions } = community;
const isCommunityMember = communityPermissions.isMember;
if (!isCommunityMember) return chevron;
if (
communityPermissions.isModerator ||
communityPermissions.isOwner ||
channelPermissions.isModerator ||
channelPermissions.isOwner
) {
const { isMember } = channelPermissions;
return (
<React.Fragment>
<Tooltip content="Go to settings">
<span style={{ marginLeft: '8px', display: 'flex' }}>
<OutlineButton
to={`/${community.slug}/${channel.slug}/settings`}
size={'small'}
style={{ padding: '4px' }}
>
<Icon
style={{ marginTop: '-1px' }}
glyph="settings"
size={24}
/>
</OutlineButton>
</span>
</Tooltip>
{!isMember && (
<React.Fragment>
<span style={{ padding: '4px' }} />
<JoinChannelWrapper
channel={channel}
render={({ isLoading }) => (
<PrimaryOutlineButton
size={'small'}
style={{ width: '100px' }}
>
{isLoading ? 'Joining...' : 'Join'}
</PrimaryOutlineButton>
)}
/>
</React.Fragment>
)}
</React.Fragment>
);
}
const { isMember } = channelPermissions;
if (isMember)
return (
<LeaveChannelWrapper
channel={channel}
render={({ isLoading, isHovering }) => (
<OutlineButton size={'small'} style={{ width: '100px' }}>
{isLoading ? 'Leaving...' : isHovering ? 'Leave' : 'Member'}
</OutlineButton>
)}
/>
);
return (
<JoinChannelWrapper
channel={channel}
render={({ isLoading }) => (
<PrimaryOutlineButton size={'small'} style={{ width: '100px' }}>
{isLoading ? 'Joining...' : 'Join'}
</PrimaryOutlineButton>
)}
/>
);
};
const renderNotificationPreferences = () => {
if (!currentUser) return null;
const { community, channelPermissions } = channel;
const { communityPermissions } = community;
const isCommunityMember = communityPermissions.isMember;
if (!isCommunityMember) return null;
const { receiveNotifications, isMember } = channelPermissions;
if (!isMember) return null;
const tipText = receiveNotifications
? 'Mute notifications'
: 'Enable channel notifications';
const glyph = receiveNotifications
? isHoveringNotifications
? 'mute'
: 'notification'
: isHoveringNotifications
? 'notification'
: 'mute';
return (
<ToggleChannelNotifications
channel={channel}
render={({ isLoading }) => (
<Tooltip content={tipText}>
<span style={{ marginLeft: '8px', display: 'flex' }}>
<OutlineButton
onMouseEnter={() => setIsHoveringNotifications(true)}
onMouseLeave={() => setIsHoveringNotifications(false)}
style={{ padding: '4px', opacity: isLoading ? 0.6 : 1 }}
size={'small'}
>
<Icon
style={{
marginTop: '-1px',
}}
glyph={glyph}
size={24}
/>
</OutlineButton>
</span>
</Tooltip>
)}
/>
);
};
return (
<ErrorBoundary>
<ChannelRow isActive={isActive}>
<Link to={`/${channel.community.slug}/${channel.slug}?tab=posts`}>
<ChannelContent>
{name && (
<Label title={name}>
{channel.isPrivate && (
<Icon glyph="private-outline" size={14} />
)}
# {name}
</Label>
)}
{description && <Description>{description}</Description>}
</ChannelContent>
</Link>
<ChannelActions>
{renderAction()}
{renderNotificationPreferences()}
{children}
</ChannelActions>
</ChannelRow>
</ErrorBoundary>
);
};
export const ChannelListItem = compose(withCurrentUser)(Channel);