@@ -15,6 +15,33 @@ type MessageSeparatorProps = {
1515 use ?: any ;
1616} ;
1717
18+ const isSameDay = ( a : Date , b : Date ) =>
19+ a . getFullYear ( ) === b . getFullYear ( ) &&
20+ a . getMonth ( ) === b . getMonth ( ) &&
21+ a . getDate ( ) === b . getDate ( ) ;
22+
23+
24+ const getDateLabel = ( date : string , t : TFunction ) : string => {
25+ const messageDate = new Date ( date ) ;
26+ if ( isNaN ( messageDate . getTime ( ) ) ) {
27+ return '' ;
28+ }
29+ const today = new Date ( ) ;
30+ const yesterday = new Date ( ) ;
31+ yesterday . setDate ( today . getDate ( ) - 1 ) ;
32+
33+
34+ if ( isSameDay ( messageDate , today ) ) return t ( 'today' ) ;
35+ if ( isSameDay ( messageDate , yesterday ) ) return t ( 'yesterday' ) ;
36+
37+ return t ( 'message_separator_date' , {
38+ val : messageDate ,
39+ formatParams : {
40+ val : { month : 'short' , day : '2-digit' , year : 'numeric' } ,
41+ } ,
42+ } ) . toUpperCase ( ) ;
43+ } ;
44+
1845// TODO: find a better way to pass `use` and do not default to a string
1946// eslint-disable-next-line @typescript-eslint/naming-convention
2047const MessageSeparator = ( { date, unread, use : Element = 'div' , className, style = { } , t } : MessageSeparatorProps ) => (
@@ -33,13 +60,7 @@ const MessageSeparator = ({ date, unread, use: Element = 'div', className, style
3360 < hr className = { createClassName ( styles , 'separator__line' ) } />
3461 { ( date || unread ) && (
3562 < span className = { createClassName ( styles , 'separator__text' ) } >
36- { ( ! ! date &&
37- t ( 'message_separator_date' , {
38- val : new Date ( date ) ,
39- formatParams : {
40- val : { month : 'short' , day : '2-digit' , year : 'numeric' } ,
41- } ,
42- } ) . toUpperCase ( ) ) ||
63+ { ( ! ! date && getDateLabel ( date , t ) ) ||
4364 ( unread && t ( 'unread_messages' ) ) }
4465 </ span >
4566 ) }
0 commit comments