2021-07-31 15:57:43 +00:00
|
|
|
import { Link, useQuery } from "blitz";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import getConversationsQuery from "../queries/get-conversations";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
export default function ConversationsList() {
|
2021-07-31 15:57:43 +00:00
|
|
|
const conversations = useQuery(getConversationsQuery, {})[0];
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
if (Object.keys(conversations).length === 0) {
|
2021-07-31 15:57:43 +00:00
|
|
|
return <div>empty state</div>;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<ul className="divide-y">
|
|
|
|
{Object.entries(conversations).map(([recipient, messages]) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const lastMessage = messages[messages.length - 1]!;
|
2021-07-31 14:33:18 +00:00
|
|
|
return (
|
|
|
|
<li key={recipient} className="py-2">
|
|
|
|
<Link href={`/messages/${encodeURIComponent(recipient)}`}>
|
|
|
|
<a className="flex flex-col">
|
|
|
|
<div className="flex flex-row justify-between">
|
|
|
|
<strong>{recipient}</strong>
|
|
|
|
<div>
|
|
|
|
{new Date(lastMessage.sentAt).toLocaleString("fr-FR")}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div>{lastMessage.content}</div>
|
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
</li>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
})}
|
|
|
|
</ul>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|