2021-08-01 03:05:40 +00:00
|
|
|
import { Link, useQuery, Routes } from "blitz";
|
2021-09-07 20:59:38 +00:00
|
|
|
import { IoChevronForward } from "react-icons/io5";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import getConversationsQuery from "../queries/get-conversations";
|
2021-08-30 15:19:33 +00:00
|
|
|
import { formatRelativeDate } from "../../core/helpers/date-formatter";
|
2021-09-24 23:07:40 +00:00
|
|
|
import { useEffect } from "react";
|
|
|
|
import PhoneInitLoader from "../../core/components/phone-init-loader";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
export default function ConversationsList() {
|
2021-09-24 23:07:40 +00:00
|
|
|
const [conversations, query] = useQuery(getConversationsQuery, {});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!conversations) {
|
|
|
|
const pollInterval = setInterval(() => query.refetch(), 1500);
|
|
|
|
return () => clearInterval(pollInterval);
|
|
|
|
}
|
|
|
|
}, [conversations, query]);
|
|
|
|
|
|
|
|
if (!conversations) {
|
|
|
|
return <PhoneInitLoader />;
|
|
|
|
}
|
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">
|
2021-08-28 21:07:51 +00:00
|
|
|
{Object.values(conversations).map(({ recipient, formattedPhoneNumber, messages }) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const lastMessage = messages[messages.length - 1]!;
|
2021-07-31 14:33:18 +00:00
|
|
|
return (
|
2021-08-30 20:13:40 +00:00
|
|
|
<li key={`sms-conversation-${recipient}`} className="py-2 px-4">
|
2021-08-28 21:07:51 +00:00
|
|
|
<Link href={Routes.ConversationPage({ recipient })}>
|
2021-07-31 14:33:18 +00:00
|
|
|
<a className="flex flex-col">
|
|
|
|
<div className="flex flex-row justify-between">
|
2021-08-28 21:07:51 +00:00
|
|
|
<strong>{formattedPhoneNumber}</strong>
|
2021-08-26 20:12:11 +00:00
|
|
|
<div className="text-gray-700 flex flex-row gap-x-1">
|
2021-08-30 15:19:33 +00:00
|
|
|
{formatRelativeDate(lastMessage.sentAt)}
|
2021-09-07 20:59:38 +00:00
|
|
|
<IoChevronForward className="w-4 h-4 my-auto" />
|
2021-08-26 20:12:11 +00:00
|
|
|
</div>
|
2021-07-31 14:33:18 +00:00
|
|
|
</div>
|
2021-08-26 20:12:11 +00:00
|
|
|
<div className="line-clamp-2 text-gray-700">{lastMessage.content}</div>
|
2021-07-31 14:33:18 +00:00
|
|
|
</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
|
|
|
}
|