2021-07-31 15:57:43 +00:00
|
|
|
import { Suspense, useEffect, useRef } from "react";
|
|
|
|
import { useRouter } from "blitz";
|
|
|
|
import clsx from "clsx";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import { Direction } from "../../../db";
|
|
|
|
import useConversation from "../hooks/use-conversation";
|
|
|
|
import NewMessageArea from "./new-message-area";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
export default function Conversation() {
|
2021-07-31 15:57:43 +00:00
|
|
|
const router = useRouter();
|
2021-08-01 07:40:18 +00:00
|
|
|
const recipient = decodeURIComponent(router.params.recipient);
|
|
|
|
const conversation = useConversation(recipient)[0];
|
2021-08-28 21:07:51 +00:00
|
|
|
const messages = conversation?.messages ?? [];
|
2021-07-31 15:57:43 +00:00
|
|
|
const messagesListRef = useRef<HTMLUListElement>(null);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2021-07-31 15:57:43 +00:00
|
|
|
messagesListRef.current?.querySelector("li:last-child")?.scrollIntoView();
|
2021-08-28 21:07:51 +00:00
|
|
|
}, [messages, messagesListRef]);
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="flex flex-col space-y-6 p-6 pt-12 pb-16">
|
|
|
|
<ul ref={messagesListRef}>
|
2021-08-28 21:07:51 +00:00
|
|
|
{messages.length === 0 ? "empty state" : null}
|
|
|
|
{messages.map((message, index) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const isOutbound = message.direction === Direction.Outbound;
|
2021-08-28 21:07:51 +00:00
|
|
|
const nextMessage = messages![index + 1];
|
|
|
|
const previousMessage = messages![index - 1];
|
2021-07-31 15:57:43 +00:00
|
|
|
const isSameNext = message.from === nextMessage?.from;
|
|
|
|
const isSamePrevious = message.from === previousMessage?.from;
|
2021-07-31 14:33:18 +00:00
|
|
|
const differenceInMinutes = previousMessage
|
2021-08-01 14:03:49 +00:00
|
|
|
? (new Date(message.sentAt).getTime() - new Date(previousMessage.sentAt).getTime()) /
|
2021-07-31 14:33:18 +00:00
|
|
|
1000 /
|
|
|
|
60
|
2021-07-31 15:57:43 +00:00
|
|
|
: 0;
|
|
|
|
const isTooLate = differenceInMinutes > 15;
|
2021-07-31 14:33:18 +00:00
|
|
|
return (
|
|
|
|
<li key={message.id}>
|
|
|
|
{(!isSamePrevious || isTooLate) && (
|
|
|
|
<div className="flex py-2 space-x-1 text-xs justify-center">
|
|
|
|
<strong>
|
|
|
|
{new Date(message.sentAt).toLocaleDateString("fr-FR", {
|
|
|
|
weekday: "long",
|
|
|
|
day: "2-digit",
|
|
|
|
month: "short",
|
|
|
|
})}
|
|
|
|
</strong>
|
|
|
|
<span>
|
|
|
|
{new Date(message.sentAt).toLocaleTimeString("fr-FR", {
|
|
|
|
hour: "2-digit",
|
|
|
|
minute: "2-digit",
|
|
|
|
})}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div
|
|
|
|
className={clsx(
|
|
|
|
isSameNext ? "pb-1" : "pb-2",
|
2021-08-01 12:04:04 +00:00
|
|
|
isOutbound ? "text-right" : "text-left",
|
2021-07-31 14:33:18 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
className={clsx(
|
|
|
|
"inline-block text-left w-[fit-content] p-2 rounded-lg text-white",
|
2021-08-01 14:03:49 +00:00
|
|
|
isOutbound ? "bg-[#3194ff] rounded-br-none" : "bg-black rounded-bl-none",
|
2021-07-31 14:33:18 +00:00
|
|
|
)}
|
|
|
|
>
|
|
|
|
{message.content}
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
</li>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
<Suspense fallback={null}>
|
2021-08-01 07:40:18 +00:00
|
|
|
<NewMessageArea recipient={recipient} />
|
2021-07-31 14:33:18 +00:00
|
|
|
</Suspense>
|
|
|
|
</>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|