-
Notifications
You must be signed in to change notification settings - Fork 1
/
DialogueItem.jsx
44 lines (37 loc) · 1.17 KB
/
DialogueItem.jsx
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
import { useState } from 'react';
const DialogueItem = ({ idx, message, handleEditMessage, style }) => {
const messageClass = message.sender === "user" ? "user-dialogue" : "ai-dialogue";
const [isEditing, setIsEditing] = useState(false);
const [editedMessage, setEditedMessage] = useState(message.text);
const handleEditClick = () => {
setIsEditing(!isEditing);
};
const handleSubmit = (event) => {
event.preventDefault();
handleEditMessage(idx, editedMessage);
setIsEditing(!isEditing);
};
/*let newLine = ""
const poemLine = message.text.match(/\[(.*?)\]/);
if (poemLine) {
newLine = poemLine[1];
} else {
newLine = message.text
}*/
return (
<>
{isEditing ? (
<form onSubmit={handleSubmit} className="edit-form">
<textarea
value={editedMessage}
onChange={(event) => setEditedMessage(event.target.value)}
/>
<button type="submit" disabled={!editedMessage.trim()}> Save </button>
</form>
) : (
<div className={`${style} ${messageClass}`} onClick={handleEditClick}>{message.text} <br/> </div>
)}
</>
);
};
export default DialogueItem;