@@ -12,6 +12,7 @@ interface Comment {
12
12
image ?: string ; // Optional image field for comments with images
13
13
addedTime ?: any ; // Track when the comment was added
14
14
CommentId ? : number ;
15
+ tempId ?: string ; // Add temporary ID for tracking
15
16
}
16
17
17
18
interface RdsCompUserCommentsProps {
@@ -55,12 +56,18 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
55
56
} = props ;
56
57
57
58
const [ commentText , setCommentText ] = useState < string > ( '' ) ;
58
- const [ commentList , setCommentList ] = useState < Comment [ ] > ( comments ) ;
59
+ const [ commentList , setCommentList ] = useState < Comment [ ] > ( [ ] ) ;
59
60
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
61
62
62
63
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 ) ;
64
71
} , [ comments ] ) ;
65
72
66
73
useEffect ( ( ) => {
@@ -84,7 +91,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
84
91
const handleClickOutside = ( event : MouseEvent ) => {
85
92
const commentContainer = document . querySelector ( '.comments-container' ) ;
86
93
if ( commentContainer && ! commentContainer . contains ( event . target as Node ) ) {
87
- setSelectedMessageIndex ( null ) ; // Reset selected message index
94
+ setSelectedMessageId ( null ) ; // Reset selected message index
88
95
}
89
96
} ;
90
97
@@ -105,6 +112,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
105
112
date : new Date ( ) . toLocaleDateString ( 'en-US' ) ,
106
113
comment : commentText , // Emoji and text will be added here
107
114
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
108
116
} ;
109
117
110
118
setCommentList ( [ ...commentList , newComment ] ) ;
@@ -127,6 +135,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
127
135
comment : '' , // No text for image-only comments
128
136
image : reader . result as string , // Base64 image data
129
137
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
130
139
} ;
131
140
132
141
setCommentList ( [ ...commentList , newComment ] ) ; // Add the new image comment
@@ -140,36 +149,34 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
140
149
setShowEmojiPicker ( false ) ; // Close the emoji picker
141
150
} ;
142
151
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 ;
149
154
150
- if ( commentId === undefined ) return ;
155
+ // Update the comments list
156
+ setCommentList ( ( prevList ) => prevList . filter ( c => c . tempId !== tempId ) ) ;
151
157
152
- // Update the comments list
153
- setCommentList ( ( prevList ) => prevList . filter ( ( _ , i ) => i !== index ) ) ;
158
+ // Reset selection
159
+ setSelectedMessageId ( null ) ;
154
160
155
- props . handleDeleteComment ?.( commentId ) ; // Trigger the callback with the comment ID
156
- } ;
161
+ if ( commentId !== undefined ) {
162
+ props . handleDeleteComment ?.( commentId ) ;
163
+ }
164
+ } ;
157
165
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 ) ) ;
160
169
} ;
161
170
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 ) ;
173
180
} ;
174
181
175
182
const formatDate = ( date : Date , dateFormat : string ) => {
@@ -186,13 +193,16 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
186
193
} ;
187
194
188
195
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 ) => {
191
198
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
193
200
194
201
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
+ } } >
196
206
< div className = { `d-flex ${ isCurrentUser ? 'flex-row-reverse' : '' } ` } >
197
207
< div className = "profile-initials" >
198
208
{ comment . profilePic && comment . profilePic . trim ( ) !== "" ? (
@@ -218,7 +228,7 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
218
228
{ comment . comment }
219
229
{ comment . image && < img src = { comment . image } alt = "uploaded" className = "comment-image" /> }
220
230
</ div >
221
- { selectedMessageIndex === index && (
231
+ { isSelected && allowDelete && (
222
232
< div className = "delete-option mt-2" >
223
233
< RdsIcon
224
234
name = "delete"
@@ -233,21 +243,6 @@ const RdsCompUserComments = (props: RdsCompUserCommentsProps) => {
233
243
</ div >
234
244
) }
235
245
</ 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
- ) }
251
246
</ div >
252
247
253
248
< div className = { `comment-footer d-flex ${ isCurrentUser ? 'justify-content-end' : 'justify-content-start' } ` } >
0 commit comments