11import { Card , CardContent , Grid , Typography } from '@material-ui/core'
22import MailIcon from '@material-ui/icons/Mail'
33import PropTypes from 'prop-types'
4- import React from 'react'
4+ import React , { useEffect , useState } from 'react'
55
66export const RecentMessageCard = ( { message, onClick, elevate = true } ) => {
77 const [ elevation , setElevation ] = React . useState ( 5 )
8+ const [ received , setReceived ] = useState ( )
9+
10+ useEffect ( ( ) => {
11+ const getDaysAgo = ( date ) => {
12+ const now = new Date ( )
13+ return Math . floor ( ( now - date ) / ( 1000 * 60 * 60 * 24 ) )
14+ }
15+
16+ const date = new Date ( message . timeCreated )
17+ const daysAgo = getDaysAgo ( date )
18+
19+ if ( daysAgo < 1 ) {
20+ setReceived ( 'Today' )
21+ } else if ( daysAgo >= 1 && daysAgo < 2 ) {
22+ setReceived ( 'Yesterday' )
23+ } else {
24+ setReceived ( `${ daysAgo } days ago` )
25+ }
26+ } , [ message ] )
827
928 const handleMouseOver = ( ) => elevate && setElevation ( 12 )
1029 const handleMouseOut = ( ) => elevate && setElevation ( 5 )
1130
12- const daysAgo = ( dateString ) => {
13- const now = new Date ( )
14- const date = new Date ( dateString )
15- return Math . floor ( ( now - date ) / ( 1000 * 60 * 60 * 24 ) )
16- }
17-
1831 return (
1932 < Card
2033 elevation = { elevation }
@@ -28,9 +41,7 @@ export const RecentMessageCard = ({ message, onClick, elevate = true }) => {
2841 < Typography style = { { marginRight : '12px' } } >
2942 { message . sender . alias }
3043 </ Typography >
31- < Typography variant = "caption" >
32- { daysAgo ( message . timeCreated ) } days ago
33- </ Typography >
44+ < Typography variant = "caption" > { received } </ Typography >
3445 </ Grid >
3546
3647 < Typography style = { { marginTop : '8px' } } > { message . message } </ Typography >
0 commit comments