@@ -33,19 +33,57 @@ type ChatMessagesProps = BaseMessageProps & {
33
33
} ;
34
34
35
35
export type ChatMessageHeaderProps = IUser & {
36
- timestamp : string ;
36
+ timestamp : number ;
37
37
rawTime ?: boolean ;
38
38
deleted ?: boolean ;
39
39
edited ?: boolean ;
40
40
sx ?: SxProps < Theme > ;
41
41
} ;
42
42
43
43
export function ChatMessageHeader ( props : ChatMessageHeaderProps ) : JSX . Element {
44
+ const [ datetime , setDatetime ] = useState < Record < number , string > > ( { } ) ;
44
45
const sharedStyles : SxProps < Theme > = {
45
46
height : '24px' ,
46
47
width : '24px'
47
48
} ;
48
49
50
+ /**
51
+ * Effect: update cached datetime strings upon receiving a new message.
52
+ */
53
+ useEffect ( ( ) => {
54
+ if ( ! datetime [ props . timestamp ] ) {
55
+ const newDatetime : Record < number , string > = { } ;
56
+ let datetime : string ;
57
+ const currentDate = new Date ( ) ;
58
+ const sameDay = ( date : Date ) =>
59
+ date . getFullYear ( ) === currentDate . getFullYear ( ) &&
60
+ date . getMonth ( ) === currentDate . getMonth ( ) &&
61
+ date . getDate ( ) === currentDate . getDate ( ) ;
62
+
63
+ const msgDate = new Date ( props . timestamp * 1000 ) ; // Convert message time to milliseconds
64
+
65
+ // Display only the time if the day of the message is the current one.
66
+ if ( sameDay ( msgDate ) ) {
67
+ // Use the browser's default locale
68
+ datetime = msgDate . toLocaleTimeString ( [ ] , {
69
+ hour : 'numeric' ,
70
+ minute : '2-digit'
71
+ } ) ;
72
+ } else {
73
+ // Use the browser's default locale
74
+ datetime = msgDate . toLocaleString ( [ ] , {
75
+ day : 'numeric' ,
76
+ month : 'numeric' ,
77
+ year : 'numeric' ,
78
+ hour : 'numeric' ,
79
+ minute : '2-digit'
80
+ } ) ;
81
+ }
82
+ newDatetime [ props . timestamp ] = datetime ;
83
+ setDatetime ( newDatetime ) ;
84
+ }
85
+ } ) ;
86
+
49
87
const bgcolor = props . color ;
50
88
const avatar = props . avatar_url ? (
51
89
< Avatar
@@ -125,7 +163,7 @@ export function ChatMessageHeader(props: ChatMessageHeaderProps): JSX.Element {
125
163
} }
126
164
title = { props . rawTime ? 'Unverified time' : '' }
127
165
>
128
- { `${ props . timestamp } ${ props . rawTime ? '*' : '' } ` }
166
+ { `${ datetime [ props . timestamp ] } ${ props . rawTime ? '*' : '' } ` }
129
167
</ Typography >
130
168
</ Box >
131
169
</ Box >
@@ -136,51 +174,6 @@ export function ChatMessageHeader(props: ChatMessageHeaderProps): JSX.Element {
136
174
* The messages list UI.
137
175
*/
138
176
export function ChatMessages ( props : ChatMessagesProps ) : JSX . Element {
139
- const [ timestamps , setTimestamps ] = useState < Record < string , string > > ( { } ) ;
140
-
141
- /**
142
- * Effect: update cached timestamp strings upon receiving a new message.
143
- */
144
- useEffect ( ( ) => {
145
- const newTimestamps : Record < string , string > = { ...timestamps } ;
146
- let timestampAdded = false ;
147
-
148
- const currentDate = new Date ( ) ;
149
- const sameDay = ( date : Date ) =>
150
- date . getFullYear ( ) === currentDate . getFullYear ( ) &&
151
- date . getMonth ( ) === currentDate . getMonth ( ) &&
152
- date . getDate ( ) === currentDate . getDate ( ) ;
153
-
154
- for ( const message of props . messages ) {
155
- if ( ! ( message . id in newTimestamps ) ) {
156
- const date = new Date ( message . time * 1000 ) ; // Convert message time to milliseconds
157
-
158
- // Display only the time if the day of the message is the current one.
159
- if ( sameDay ( date ) ) {
160
- // Use the browser's default locale
161
- newTimestamps [ message . id ] = date . toLocaleTimeString ( [ ] , {
162
- hour : 'numeric' ,
163
- minute : '2-digit'
164
- } ) ;
165
- } else {
166
- // Use the browser's default locale
167
- newTimestamps [ message . id ] = date . toLocaleString ( [ ] , {
168
- day : 'numeric' ,
169
- month : 'numeric' ,
170
- year : 'numeric' ,
171
- hour : 'numeric' ,
172
- minute : '2-digit'
173
- } ) ;
174
- }
175
-
176
- timestampAdded = true ;
177
- }
178
- }
179
- if ( timestampAdded ) {
180
- setTimestamps ( newTimestamps ) ;
181
- }
182
- } , [ props . messages ] ) ;
183
-
184
177
return (
185
178
< Box
186
179
sx = { {
@@ -206,7 +199,7 @@ export function ChatMessages(props: ChatMessagesProps): JSX.Element {
206
199
>
207
200
< ChatMessageHeader
208
201
{ ...sender }
209
- timestamp = { timestamps [ message . id ] }
202
+ timestamp = { message . time }
210
203
rawTime = { message . raw_time }
211
204
deleted = { message . deleted }
212
205
edited = { message . edited }
@@ -241,14 +234,12 @@ export function ChatMessage(props: ChatMessageProps): JSX.Element {
241
234
}
242
235
}
243
236
const [ edit , setEdit ] = useState < boolean > ( false ) ;
244
- const [ input , setInput ] = useState ( message . body ) ;
245
237
246
238
const cancelEdition = ( ) : void => {
247
- setInput ( message . body ) ;
248
239
setEdit ( false ) ;
249
240
} ;
250
241
251
- const updateMessage = ( id : string ) : void => {
242
+ const updateMessage = ( id : string , input : string ) : void => {
252
243
if ( ! canEdit ) {
253
244
return ;
254
245
}
@@ -274,9 +265,8 @@ export function ChatMessage(props: ChatMessageProps): JSX.Element {
274
265
< div >
275
266
{ edit && canEdit ? (
276
267
< ChatInput
277
- value = { input }
278
- onChange = { setInput }
279
- onSend = { ( ) => updateMessage ( message . id ) }
268
+ value = { message . body }
269
+ onSend = { ( input : string ) => updateMessage ( message . id , input ) }
280
270
onCancel = { ( ) => cancelEdition ( ) }
281
271
sendWithShiftEnter = { model . config . sendWithShiftEnter ?? false }
282
272
/>
0 commit comments