-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFrequentWords.tsx
More file actions
181 lines (165 loc) · 5.59 KB
/
FrequentWords.tsx
File metadata and controls
181 lines (165 loc) · 5.59 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
import { useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
import {
Button,
Chip,
Dialog,
DialogActions,
DialogContent,
DialogTitle,
Stack,
TextField,
Typography,
} from '@mui/material';
import type { AppAction } from '@graasp/sdk';
import type { CommentData } from '@/config/appData';
import {
ADD_CUSTOM_WORD_INPUT_ID,
buildCheckWholeMemberChatButtonId,
buildKeywordChipId,
} from '@/config/selectors';
import { ConversationForUser } from '../comment/ConversationForUser';
import KeywordChip from '../common/KeywordChip';
import TextWithHighlightedKeywords from '../common/TextWithHighlightedKeywords';
import { createRegexFromString, getTopFrequentWords } from './utils';
type Props = {
commentsByUserSide: AppAction<CommentData>[];
allWords: { [key: string]: number };
};
function FrequentWords({
commentsByUserSide,
allWords,
}: Readonly<Props>): JSX.Element {
const { t } = useTranslation();
const mostFrequentWordsWithCount = getTopFrequentWords(allWords, 5);
const mostFrequentWords = Object.keys(mostFrequentWordsWithCount);
const [selectedFrequentWords, setSelectedFrequentWords] =
useState<string[]>(mostFrequentWords);
const [selectedCustomWords, setSelectedCustomWords] = useState<string[]>([]);
const [customWord, setCustomWord] = useState('');
const [selectedConversation, setSelectedConversation] = useState<null | {
accountId: string;
conversationId?: string;
}>(null);
const closeConversation = () => {
setSelectedConversation(null);
};
const isAllSelected = mostFrequentWords.every(
(ele) => -1 < selectedFrequentWords.indexOf(ele),
);
const commentsMatchSelectedWords = useMemo(
() =>
0 < commentsByUserSide.length
? commentsByUserSide.filter(({ data: { content } }) =>
[...selectedFrequentWords, ...selectedCustomWords].some((ele) =>
new RegExp(createRegexFromString(ele)).test(content),
),
)
: [],
[selectedFrequentWords, commentsByUserSide, selectedCustomWords],
);
const deleteCustomWord = (word: string): void => {
setSelectedCustomWords(selectedCustomWords.filter((w) => w !== word));
};
const selectFrequentChip = (text: string): void => {
if (-1 < selectedFrequentWords.indexOf(text)) {
setSelectedFrequentWords(
selectedFrequentWords.filter((ele) => ele !== text),
);
} else {
setSelectedFrequentWords([...new Set([...selectedFrequentWords, text])]);
}
};
return (
<Stack spacing={2} mt={2}>
<Typography variant="h6">{t('MOST_FREQUENT_WORDS_TITLE')}</Typography>
<Typography variant="body1">{t('FILTER_BY_COMMON_KEYWORDS')}</Typography>
<Stack spacing={1} direction="row">
{Object.entries(mostFrequentWordsWithCount).map(([text, count]) => (
<KeywordChip
key={text}
text={text}
count={count}
isSelected={-1 < selectedFrequentWords.indexOf(text)}
onClick={() => selectFrequentChip(text)}
/>
))}
<Button
onClick={() => setSelectedFrequentWords(mostFrequentWords)}
variant={isAllSelected ? 'contained' : 'outlined'}
>
{t('ALL')}
</Button>
</Stack>
<Stack spacing={1} sx={{ maxWidth: 300 }}>
<Typography variant="body1">{t('SEARCH_BY_OTHER_KEYWORDS')}</Typography>
<TextField
size="small"
onKeyDown={(event) => {
if ('Enter' === event.key) {
setSelectedCustomWords([
...new Set([...selectedCustomWords, customWord]),
]);
setCustomWord('');
}
}}
value={customWord}
onChange={(e) => {
setCustomWord(e.target.value);
}}
id={ADD_CUSTOM_WORD_INPUT_ID}
placeholder={t('SEARCH_COMMON_WORDS_PLACEHOLDER')}
/>
<Stack spacing={1} direction="row">
{selectedCustomWords.map((text) => (
<Chip
key={text}
label={text}
variant="outlined"
onDelete={() => deleteCustomWord(text)}
id={buildKeywordChipId(text)}
/>
))}
</Stack>
</Stack>
<Stack spacing={2} p={1}>
{commentsMatchSelectedWords.map((ele) => (
<TextWithHighlightedKeywords
key={ele.id}
sentence={ele.data.content}
memberName={ele.account.name}
words={[...selectedFrequentWords, ...selectedCustomWords]}
onClick={() =>
setSelectedConversation({
accountId: ele.account.id,
conversationId: ele.data.conversationId,
})
}
buttonId={buildCheckWholeMemberChatButtonId(ele.account.id)}
/>
))}
{
// oxlint-disable-next-line eslint/yoda
commentsMatchSelectedWords.length === 0 && (
<Typography mt={2}>{t('NO_RESULTS_MATCH_WORDS')}</Typography>
)
}
</Stack>
{selectedConversation && (
<Dialog open onClose={closeConversation}>
<DialogTitle>{t('ANALYTICS_CONVERSATION_MEMBER')}</DialogTitle>
<DialogContent>
<ConversationForUser
accountId={selectedConversation.accountId}
conversationId={selectedConversation.conversationId}
/>
</DialogContent>
<DialogActions>
<Button onClick={closeConversation}>{t('CLOSE')}</Button>
</DialogActions>
</Dialog>
)}
</Stack>
);
}
export default FrequentWords;