Skip to content

Commit

Permalink
fix: editing returning an html element (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dun-sin authored Sep 25, 2023
1 parent c2d7cfc commit 60dfacf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 25 deletions.
48 changes: 23 additions & 25 deletions client/src/components/Chat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import MarkdownIt from 'markdown-it';

import { useChat } from 'src/context/ChatContext';
import { useAuth } from 'src/context/AuthContext';
import { useApp } from 'src/context/AppContext';

import useChatUtils from 'src/lib/chat';
import MessageStatus from './MessageStatus';
import listOfBadWordsNotAllowed from 'src/lib/badWords';
import { useNotification } from 'src/lib/notification';
import { useApp } from 'src/context/AppContext';

let senderId;
const Chat = () => {
Expand All @@ -46,21 +47,7 @@ const Chat = () => {
const { sendMessage, deleteMessage, editMessage } = useChatUtils(socket);
const inputRef = useRef('');

const listOfBadWordsNotAllowed = [
'sex',
'porn',
'fuck',
'cock',
'titties',
'boner',
'muff',
'pussy',
'asshole',
'cunt',
'ass',
'cockfoam',
'nigger',
];

senderId = authState.email ?? authState.loginId;

const getMessage = (id) => {
Expand All @@ -75,7 +62,11 @@ const Chat = () => {
return Boolean(getMessage(id));
};

const md = new MarkdownIt();
const md = new MarkdownIt({
html: false,
linkify: true,
typographer: true
});

function logOut() {
dispatchAuth({
Expand Down Expand Up @@ -247,6 +238,7 @@ const Chat = () => {
};

const warningMessage = (sender, message) => {
// TODO: Instrad of replacing the message we should add some kind of increment for the users to decide to see the message or not
if (message.includes('Warning Message')) {
if (senderId === sender) {
return (
Expand All @@ -272,12 +264,11 @@ const Chat = () => {
socket.emit('typing', { chatId: app.currentChatId, isTyping: false });
const d = new Date();
let message = inputRef.current.value;

if (message === '' || senderId === undefined || senderId === '123456') {
return;
}

message = md.render(message);

const splitMessage = message.split(' ');
for (const word of splitMessage) {
// We need a better way to implement this
Expand Down Expand Up @@ -334,13 +325,16 @@ const Chat = () => {

const handleEdit = (id) => {
inputRef.current.focus();

const { message } = getMessage(id);
console.log('first', message)


if (message.includes('Warning Message')) {
cancelEdit();
return;
}
inputRef.current.value = message;

setEditing({ isediting: true, messageID: id });
};

Expand Down Expand Up @@ -399,6 +393,8 @@ const Chat = () => {
!(resultOfWarningMessage === undefined) &&
(message = resultOfWarningMessage);



return (
<div
key={id}
Expand All @@ -415,11 +411,12 @@ const Chat = () => {
'justify-between'
}`}
>
<p
dangerouslySetInnerHTML={{
__html: message,
}}
></p>

<span
dangerouslySetInnerHTML={{ __html: md.render(message) }}
/>


{sender.toString() ===
senderId.toString() &&
status !== 'pending' && (
Expand Down Expand Up @@ -480,6 +477,7 @@ const Chat = () => {
);
}
)}

</ScrollToBottom>
</div>
<form
Expand Down
17 changes: 17 additions & 0 deletions client/src/lib/badWords.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const listOfBadWordsNotAllowed = [
'sex',
'porn',
'fuck',
'cock',
'titties',
'boner',
'muff',
'pussy',
'asshole',
'cunt',
'ass',
'cockfoam',
'nigger',
];

export default listOfBadWordsNotAllowed;

1 comment on commit 60dfacf

@vercel
Copy link

@vercel vercel bot commented on 60dfacf Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.