-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from ably-labs/refactor-MessageComponent-in-demo
Show createdAt and clientId in demo app + rename Message to MessageComponent
- Loading branch information
Showing
7 changed files
with
120 additions
and
50 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Message } from '@ably-labs/chat'; | ||
import React, { useCallback } from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
function twoDigits(input : number) : string { | ||
if (input === 0) { | ||
return "00"; | ||
} | ||
if (input < 10) { | ||
return "0"+input; | ||
} | ||
return ""+input; | ||
} | ||
|
||
interface MessageProps { | ||
id: string; | ||
self?: boolean; | ||
message: Message; | ||
onMessageClick?(id: string): void; | ||
} | ||
export const MessageComponent: React.FC<MessageProps> = ({ id, self = false, message, onMessageClick }) => { | ||
const handleMessageClick = useCallback(() => { | ||
onMessageClick?.(id); | ||
}, [id, onMessageClick]); | ||
|
||
let displayCreatedAt : string; | ||
if (new Date().getTime() - message.createdAt.getTime() < 1000 * 60 * 60 * 24) { | ||
// last 24h show the time | ||
displayCreatedAt = twoDigits(message.createdAt.getHours()) + ":" + twoDigits(message.createdAt.getMinutes()); | ||
} else { | ||
// older, show full date | ||
displayCreatedAt = message.createdAt.getDate()+"/"+message.createdAt.getMonth()+"/"+message.createdAt.getFullYear() + " " + twoDigits(message.createdAt.getHours()) + ":" + twoDigits(message.createdAt.getMinutes()); | ||
} | ||
|
||
return ( | ||
<div | ||
className="chat-message" | ||
onClick={handleMessageClick} | ||
> | ||
<div className={clsx('flex items-end', { ['justify-end']: self, ['justify-start']: !self })}> | ||
<div | ||
className={clsx('flex flex-col text max-w-xs mx-2', { | ||
['items-end order-1']: self, | ||
['items-start order-2']: !self, | ||
})} | ||
> | ||
<div className="text-xs"><span>{ message.clientId }</span> · <span className="sent-at-time"><span className="short">{ displayCreatedAt }</span><span className="long">{ message.createdAt.toLocaleString() }</span></span></div> | ||
<div | ||
className={clsx('px-4 py-2 rounded-lg inline-block', { | ||
['rounded-br bg-blue-600 text-white']: self, | ||
['rounded-bl justify-start bg-gray-300 text-gray-600']: !self, | ||
})} | ||
> | ||
{ message.content } | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { MessageComponent } from './MessageComponent.tsx'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters