Skip to content

Commit 78fc0da

Browse files
authored
Merge branch 'development' into users/dipak/bug-1946-Design-System-Elements-Radio-Button-Radio-Button-is-not-displayed-properly-in-dark-theme
2 parents 90771ec + 583cadf commit 78fc0da

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed

raaghu-components/src/rds-comp-chat/rds-comp-chat.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
.comments-container .btn-icon.btn-md {
1111
width: 35px !important;
1212
height: 35px !important;
13+
min-width: 35px !important;
1314
}
1415

1516
.comment-box {

raaghu-components/src/rds-comp-chat/rds-comp-chat.stories.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ const meta: Meta<typeof RdsCompUserComments> = {
1111
},
1212
tags: ['autodocs'],
1313
argTypes: {
14-
allowDelete: { control: "boolean" },
14+
allowDelete: {
15+
control: "boolean",
16+
description: "When true, allows deletion of any message (both sent and received)"
17+
},
1518
isEmojiPicker: { control: "boolean" },
1619
isFilepload: { control: "boolean" },
1720
dateFormat: {

raaghu-components/src/rds-comp-chat/rds-comp-chat.tsx

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ interface Comment {
1212
image?: string; // Optional image field for comments with images
1313
addedTime?: any; // Track when the comment was added
1414
CommentId? : number;
15+
tempId?: string; // Add temporary ID for tracking
1516
}
1617

1718
interface RdsCompUserCommentsProps {
@@ -55,12 +56,18 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
5556
} = props;
5657

5758
const [commentText, setCommentText] = useState<string>('');
58-
const [commentList, setCommentList] = useState<Comment[]>(comments);
59+
const [commentList, setCommentList] = useState<Comment[]>([]);
5960
const [showEmojiPicker, setShowEmojiPicker] = useState(false); // Toggle emoji picker
60-
const [selectedMessageIndex, setSelectedMessageIndex] = useState<number | null>(null); // Track selected message for deletion
61+
const [selectedMessageId, setSelectedMessageId] = useState<string | null>(null); // Track by ID instead of index
6162

6263
useEffect(() => {
63-
setCommentList(comments); // Set initial comments from props
64+
const commentsWithIds = comments.map(comment => {
65+
if (!comment.tempId) {
66+
return { ...comment, tempId: `temp-${Date.now()}-${Math.random().toString(36).substring(2, 9)}` };
67+
}
68+
return comment;
69+
});
70+
setCommentList(commentsWithIds);
6471
}, [comments]);
6572

6673
useEffect(() => {
@@ -84,7 +91,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
8491
const handleClickOutside = (event: MouseEvent) => {
8592
const commentContainer = document.querySelector('.comments-container');
8693
if (commentContainer && !commentContainer.contains(event.target as Node)) {
87-
setSelectedMessageIndex(null); // Reset selected message index
94+
setSelectedMessageId(null); // Reset selected message index
8895
}
8996
};
9097

@@ -105,6 +112,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
105112
date: new Date().toLocaleDateString('en-US'),
106113
comment: commentText, // Emoji and text will be added here
107114
addedTime: Date.now(), // Store the time when the comment was added
115+
tempId: `temp-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`, // Add unique temporary ID
108116
};
109117

110118
setCommentList([...commentList, newComment]);
@@ -127,6 +135,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
127135
comment: '', // No text for image-only comments
128136
image: reader.result as string, // Base64 image data
129137
addedTime: Date.now(), // Store the time when the comment was added
138+
tempId: `temp-${Date.now()}-${Math.random().toString(36).substring(2, 9)}`, // Add unique temporary ID
130139
};
131140

132141
setCommentList([...commentList, newComment]); // Add the new image comment
@@ -140,36 +149,34 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
140149
setShowEmojiPicker(false); // Close the emoji picker
141150
};
142151

143-
const handleDeleteComment = (index: number) => {
144-
if (!allowDelete) return;
145-
146-
// Get the comment to be deleted
147-
const commentToDelete = commentList[index];
148-
const commentId = commentToDelete?.CommentId;
152+
const handleDeleteComment = (commentId: number, tempId: string) => {
153+
if (!allowDelete) return;
149154

150-
if (commentId === undefined) return;
155+
// Update the comments list
156+
setCommentList((prevList) => prevList.filter(c => c.tempId !== tempId));
151157

152-
// Update the comments list
153-
setCommentList((prevList) => prevList.filter((_, i) => i !== index));
158+
// Reset selection
159+
setSelectedMessageId(null);
154160

155-
props.handleDeleteComment?.(commentId); // Trigger the callback with the comment ID
156-
};
161+
if (commentId !== undefined) {
162+
props.handleDeleteComment?.(commentId);
163+
}
164+
};
157165

158-
const handleDeleteOptionClick = (index: number) => {
159-
setSelectedMessageIndex((prevIndex) => (prevIndex === index ? null : index)); // Toggle selection
166+
const handleDeleteOptionClick = (tempId: string, event: React.MouseEvent) => {
167+
event.stopPropagation(); // Prevent event bubbling
168+
setSelectedMessageId(prevId => (prevId === tempId ? null : tempId));
160169
};
161170

162-
const confirmDeleteMessage = () => {
163-
if (selectedMessageIndex === null) return;
164-
165-
const commentToDelete = commentList[selectedMessageIndex];
166-
const commentId = commentToDelete?.CommentId;
167-
168-
// Update the comments list
169-
setCommentList((prevList) => prevList.filter((_, i) => i !== selectedMessageIndex));
170-
171-
commentId && props.handleDeleteComment?.(commentId);
172-
setSelectedMessageIndex(null); // Reset the selected message index
171+
const confirmDeleteMessage = (event: React.MouseEvent) => {
172+
event.stopPropagation(); // Prevent event bubbling
173+
174+
if (!selectedMessageId) return;
175+
176+
const commentToDelete = commentList.find(c => c.tempId === selectedMessageId);
177+
if (!commentToDelete) return;
178+
179+
handleDeleteComment(commentToDelete.CommentId!, selectedMessageId);
173180
};
174181

175182
const formatDate = (date: Date, dateFormat: string) => {
@@ -186,13 +193,16 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
186193
};
187194

188195
return (
189-
<div className={`comments-container ${width}`}>
190-
{commentList.map((comment, index) => {
196+
<div className={`comments-container ${width}`} onClick={() => setSelectedMessageId(null)}>
197+
{commentList.map((comment) => {
191198
const isCurrentUser = comment.firstName === currentUser.firstName && comment.lastName === currentUser.lastName;
192-
const showDeleteIcon = allowDelete && (Date.now() - (comment.addedTime || 0) < deleteIconTimeout); // Show delete icon based on timeout
199+
const isSelected = selectedMessageId === comment.tempId; // Check if the message is selected
193200

194201
return (
195-
<div key={index} className={`comment-box ${isCurrentUser ? 'current-user' : 'other-user'}`} onClick={() => handleDeleteOptionClick(index)}>
202+
<div key={comment.tempId} className={`comment-box ${isCurrentUser ? 'current-user' : 'other-user'}`} onClick={(e) => {
203+
e.stopPropagation();
204+
handleDeleteOptionClick(comment.tempId!, e);
205+
}}>
196206
<div className={`d-flex ${isCurrentUser ? 'flex-row-reverse' : ''}`}>
197207
<div className="profile-initials">
198208
{comment.profilePic && comment.profilePic.trim() !== "" ? (
@@ -218,7 +228,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
218228
{comment.comment}
219229
{comment.image && <img src={comment.image} alt="uploaded" className="comment-image" />}
220230
</div>
221-
{selectedMessageIndex === index && (
231+
{isSelected && allowDelete && (
222232
<div className="delete-option mt-2">
223233
<RdsIcon
224234
name="delete"
@@ -233,21 +243,6 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
233243
</div>
234244
)}
235245
</div>
236-
237-
{showDeleteIcon && isCurrentUser && (
238-
<span className="d-flex align-items-top me-2 mt-2 d-none">
239-
<RdsIcon
240-
name="delete"
241-
fill={false}
242-
stroke={true}
243-
colorVariant="danger"
244-
isCursorPointer={true}
245-
width="18px"
246-
height="18px"
247-
onClick={() => handleDeleteComment(index)}
248-
/>
249-
</span>
250-
)}
251246
</div>
252247

253248
<div className={`comment-footer d-flex ${isCurrentUser ? 'justify-content-end' : 'justify-content-start'}`}>

0 commit comments

Comments
 (0)