-
Notifications
You must be signed in to change notification settings - Fork 355
/
IgnoreUserSettingsContainer.tsx
183 lines (170 loc) · 5.54 KB
/
IgnoreUserSettingsContainer.tsx
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
import { Localized } from "@fluent/react/compat";
import cn from "classnames";
import React, { FunctionComponent, useCallback, useState } from "react";
import { graphql } from "react-relay";
import { useViewerEvent } from "coral-framework/lib/events";
import { useMutation, withFragmentContainer } from "coral-framework/lib/relay";
import CLASSES from "coral-stream/classes";
import { ShowIgnoreUserdDialogEvent } from "coral-stream/events";
import { Flex, HorizontalGutter, Icon } from "coral-ui/components/v2";
import { Button } from "coral-ui/components/v3";
import { IgnoreUserSettingsContainer_viewer as ViewerData } from "coral-stream/__generated__/IgnoreUserSettingsContainer_viewer.graphql";
import RemoveUserIgnoreMutation from "./RemoveUserIgnoreMutation";
import styles from "./IgnoreUserSettingsContainer.css";
interface Props {
viewer: ViewerData;
}
interface User {
id: string;
name: string | null;
removed: boolean;
}
const renderName = (name: string | null) => {
return (
<span className={cn(styles.username, CLASSES.ignoredCommenters.username)}>
{name}
</span>
);
};
const IgnoreUserSettingsContainer: FunctionComponent<Props> = ({ viewer }) => {
const emitShow = useViewerEvent(ShowIgnoreUserdDialogEvent);
const removeUserIgnore = useMutation(RemoveUserIgnoreMutation);
const [showManage, setShowManage] = useState(false);
const [removed, setRemoved] = useState(new Array<User>());
const toggleManage = useCallback(() => {
if (!showManage) {
emitShow();
} else {
setRemoved([]);
}
setShowManage(!showManage);
}, [showManage, setShowManage, setRemoved]);
const remove = useCallback(
async (user: any) => {
await removeUserIgnore({ userID: user.id });
setRemoved([...removed, { id: user.id, name: user.name, removed: true }]);
},
[removeUserIgnore, removed, setRemoved]
);
const merged = [
...viewer.ignoredUsers.map(user => {
return { id: user.id, name: user.username, removed: false };
}),
...removed,
];
const userList = merged.sort((a, b) => {
if (!a.name) {
return -1;
}
if (!b.name) {
return 1;
}
return a.name.localeCompare(b.name);
});
return (
<div
data-testid="profile-account-ignoredCommenters"
className={cn(styles.root, CLASSES.ignoredCommenters.$root)}
>
<Localized id="profile-account-ignoredCommenters">
<div className={styles.title}>Ignored Commenters</div>
</Localized>
<div className={styles.description}>
You can ignore other commenters by clicking on their username and
selecting Ignore. When you ignore someone, all of their comments are
hidden from you. Commenters you Ignore will still be able to see your
comments.
</div>
{showManage && (
<Localized id="profile-account-ignoredCommenters-close">
<Button
variant="filled"
color="secondary"
upperCase
onClick={toggleManage}
className={CLASSES.ignoredCommenters.manageButton}
>
Close
</Button>
</Localized>
)}
{!showManage && (
<Localized id="profile-account-ignoredCommenters-manage">
<Button
variant="outlined"
color="secondary"
upperCase
onClick={toggleManage}
className={cn(
styles.manageButton,
CLASSES.ignoredCommenters.manageButton
)}
>
Manage
</Button>
</Localized>
)}
{showManage && (
<HorizontalGutter
spacing={1}
className={cn(styles.list, CLASSES.ignoredCommenters.list)}
>
{userList.map(user =>
user.removed ? (
<div className={styles.removed} key={user.id}>
<Localized id="profile-account-ignoredCommenters-youAreNoLonger">
<span>{"You are no longer ignoring "}</span>
</Localized>
{renderName(user.name)}
</div>
) : (
<Flex
key={user.id}
justifyContent="space-between"
alignItems="center"
>
{renderName(user.name)}
<Button
variant="none"
color="none"
onClick={() => remove(user)}
className={cn(
styles.stopIgnoringButton,
CLASSES.ignoredCommenters.stopIgnoreButton
)}
>
<Flex justifyContent="center" alignItems="center">
<Icon size="sm" className={styles.icon}>
close
</Icon>
<Localized id="profile-account-ignoredCommenters-stopIgnoring">
<span>Stop ignoring</span>
</Localized>
</Flex>
</Button>
</Flex>
)
)}
{userList.length === 0 && (
<Localized id="profile-account-ignoredCommenters-empty">
<div className={styles.empty}>
You are not currently ignoring anyone
</div>
</Localized>
)}
</HorizontalGutter>
)}
</div>
);
};
const enhanced = withFragmentContainer<Props>({
viewer: graphql`
fragment IgnoreUserSettingsContainer_viewer on User {
ignoredUsers {
id
username
}
}
`,
})(IgnoreUserSettingsContainer);
export default enhanced;