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