Skip to content

Commit 4c62ab8

Browse files
committed
Fix badWords
1 parent dc7445c commit 4c62ab8

1 file changed

Lines changed: 52 additions & 23 deletions

File tree

  • services/app/apps/codebattle/assets/js/widgets/components

services/app/apps/codebattle/assets/js/widgets/components/ChatInput.jsx

Lines changed: 52 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import React, { useState, useEffect, useCallback } from 'react';
1+
import React, {
2+
useState, useEffect, useCallback, useRef,
3+
} from 'react';
24

35
import data from '@emoji-mart/data';
46
import BadWordsNext from 'bad-words-next';
5-
import en from 'bad-words-next/lib/en';
6-
import ru from 'bad-words-next/lib/ru';
7-
import rl from 'bad-words-next/lib/ru_lat';
87
import { SearchIndex, init } from 'emoji-mart';
98
import i18next from 'i18next';
109
import isEmpty from 'lodash/isEmpty';
@@ -36,12 +35,36 @@ export default function ChatInput({ inputRef, disabled = false }) {
3635
const [isMaxLengthExceeded, setMaxLengthExceeded] = useState(false);
3736
const [isTooltipVisible, setTooltipVisibility] = useState(false);
3837
const [text, setText] = useState('');
38+
const [badwordsReady, setBadwordsReady] = useState(false);
3939
const activeRoom = useSelector(selectors.activeRoomSelector);
40+
const badwordsRef = useRef(new BadWordsNext());
4041

41-
const badwords = new BadWordsNext();
42-
badwords.add(en);
43-
badwords.add(ru);
44-
badwords.add(rl);
42+
useEffect(() => {
43+
let mounted = true;
44+
async function loadBadwords() {
45+
try {
46+
// Import without extension to let webpack resolve the correct file
47+
const enData = await import('bad-words-next/lib/en');
48+
const ruData = await import('bad-words-next/lib/ru');
49+
const rlData = await import('bad-words-next/lib/ru_lat');
50+
51+
if (mounted) {
52+
badwordsRef.current.add(enData.default || enData);
53+
badwordsRef.current.add(ruData.default || ruData);
54+
badwordsRef.current.add(rlData.default || rlData);
55+
setBadwordsReady(true);
56+
}
57+
} catch (error) {
58+
console.error('Error loading bad words dictionaries:', error);
59+
}
60+
}
61+
62+
loadBadwords();
63+
64+
return () => {
65+
mounted = false;
66+
};
67+
}, []);
4568

4669
const isMessageBlank = !text.trim();
4770

@@ -57,24 +80,30 @@ export default function ChatInput({ inputRef, disabled = false }) {
5780

5881
const handleSubmit = e => {
5982
e.preventDefault();
60-
const filteredText = badwords.filter(text);
61-
const message = {
62-
filteredText,
63-
meta: {
64-
type: activeRoom.targetUserId ? messageTypes.private : messageTypes.general,
65-
targetUserId: activeRoom.targetUserId,
66-
},
67-
};
68-
if (isTooltipVisible) {
69-
return;
70-
}
71-
if (isMaxLengthExceeded) {
72-
return;
73-
}
74-
if (isMessageBlank) {
83+
84+
if (isTooltipVisible || isMaxLengthExceeded || isMessageBlank) {
7585
return;
7686
}
87+
7788
if (text) {
89+
let filteredText = text;
90+
91+
if (badwordsReady) {
92+
try {
93+
filteredText = badwordsRef.current.filter(text);
94+
} catch (error) {
95+
console.error('Error filtering text:', error);
96+
}
97+
}
98+
99+
const message = {
100+
text: filteredText,
101+
meta: {
102+
type: activeRoom.targetUserId ? messageTypes.private : messageTypes.general,
103+
targetUserId: activeRoom.targetUserId,
104+
},
105+
};
106+
78107
addMessage(message);
79108
setText('');
80109
}

0 commit comments

Comments
 (0)