import { Suspense } from "react"; import { json, type LoaderFunction, type MetaFunction } from "@remix-run/node"; import { useLoaderData, useNavigate, useParams } from "@remix-run/react"; import { IoCall, IoChevronBack, IoInformationCircle } from "react-icons/io5"; import { type Message, Prisma } from "@prisma/client"; import Conversation from "~/features/messages/components/conversation"; import { getSeoMeta } from "~/utils/seo"; import db from "~/utils/db.server"; import { parsePhoneNumber } from "awesome-phonenumber"; import { requireLoggedIn } from "~/utils/auth.server"; export const meta: MetaFunction = ({ params }) => { const recipient = decodeURIComponent(params.recipient ?? ""); return { ...getSeoMeta({ title: `Messages with ${recipient}`, }), }; }; type ConversationType = { recipient: string; formattedPhoneNumber: string; messages: Message[]; }; export type ConversationLoaderData = { conversation: ConversationType; }; export const loader: LoaderFunction = async ({ request, params }) => { const { organizations } = await requireLoggedIn(request); const recipient = decodeURIComponent(params.recipient ?? ""); const conversation = await getConversation(recipient); return json({ conversation }); async function getConversation(recipient: string): Promise { /*if (!hasFilledTwilioCredentials) { return; }*/ const organizationId = organizations[0].id; const organization = await db.organization.findFirst({ where: { id: organizationId }, include: { phoneNumbers: true }, }); if (!organization || !organization.phoneNumbers[0]) { throw new Error("Not found"); } const phoneNumber = organization.phoneNumbers[0]; // TODO: use the active number, not the first one const phoneNumberId = phoneNumber.id; if (organization.phoneNumbers[0].isFetchingMessages) { throw new Error("Not found"); } const formattedPhoneNumber = parsePhoneNumber(recipient).getNumber("international"); const messages = await db.message.findMany({ where: { phoneNumberId, OR: [{ from: recipient }, { to: recipient }], }, orderBy: { sentAt: Prisma.SortOrder.desc }, }); return { recipient, formattedPhoneNumber, messages, }; } }; export default function ConversationPage() { const navigate = useNavigate(); const params = useParams<{ recipient: string }>(); const recipient = decodeURIComponent(params.recipient ?? ""); const { conversation } = useLoaderData(); return ( <>
navigate(-1)}> {conversation?.formattedPhoneNumber ?? recipient}
Loading messages with {recipient}}> ); }