2021-07-20 16:28:56 +00:00
|
|
|
import { useEffect } from "react";
|
2021-07-18 15:32:45 +00:00
|
|
|
import type { NextPage } from "next";
|
|
|
|
import { useRouter } from "next/router";
|
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faChevronLeft } from "@fortawesome/pro-regular-svg-icons";
|
|
|
|
import clsx from "clsx";
|
2021-07-20 16:28:56 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2021-07-18 15:32:45 +00:00
|
|
|
|
|
|
|
import { withPageOnboardingRequired } from "../../../lib/session-helpers";
|
2021-07-21 16:48:49 +00:00
|
|
|
import type { Message } from "../../database/message";
|
|
|
|
import { findConversation } from "../../database/message";
|
2021-07-20 16:28:56 +00:00
|
|
|
import supabase from "../../supabase/client";
|
|
|
|
import useUser from "../../hooks/use-user";
|
|
|
|
import useConversation from "../../hooks/use-conversation";
|
|
|
|
import Layout from "../../components/layout";
|
2021-07-18 15:32:45 +00:00
|
|
|
|
|
|
|
type Props = {
|
|
|
|
recipient: string;
|
2021-07-21 16:48:49 +00:00
|
|
|
conversation: Message[];
|
2021-07-20 16:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Form = {
|
|
|
|
content: string;
|
|
|
|
}
|
2021-07-18 15:32:45 +00:00
|
|
|
|
2021-07-20 16:28:56 +00:00
|
|
|
const Messages: NextPage<Props> = (props) => {
|
2021-07-18 15:32:45 +00:00
|
|
|
const { userProfile } = useUser();
|
|
|
|
const router = useRouter();
|
2021-07-20 16:28:56 +00:00
|
|
|
const recipient = router.query.recipient as string;
|
|
|
|
const { conversation, error, refetch, sendMessage } = useConversation({
|
|
|
|
initialData: props.conversation,
|
|
|
|
recipient,
|
|
|
|
});
|
|
|
|
const pageTitle = `Messages with ${recipient}`;
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
setValue,
|
|
|
|
formState: {
|
|
|
|
isSubmitting,
|
|
|
|
},
|
|
|
|
} = useForm<Form>();
|
|
|
|
|
|
|
|
const onSubmit = handleSubmit(async ({ content }) => {
|
|
|
|
if (isSubmitting) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sendMessage.mutate({
|
|
|
|
to: recipient,
|
|
|
|
content,
|
|
|
|
});
|
|
|
|
setValue("content", "");
|
|
|
|
});
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (!userProfile) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-18 15:32:45 +00:00
|
|
|
|
2021-07-20 16:28:56 +00:00
|
|
|
const subscription = supabase
|
2021-07-21 16:48:49 +00:00
|
|
|
.from<Message>(`sms:customerId=eq.${userProfile.id}`)
|
2021-07-20 16:28:56 +00:00
|
|
|
.on("INSERT", (payload) => {
|
|
|
|
const message = payload.new;
|
|
|
|
if ([message.from, message.to].includes(recipient)) {
|
|
|
|
refetch();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.subscribe();
|
|
|
|
|
|
|
|
return () => void subscription.unsubscribe();
|
|
|
|
}, [userProfile, recipient, refetch]);
|
2021-07-18 15:32:45 +00:00
|
|
|
|
|
|
|
if (!userProfile) {
|
2021-07-20 16:28:56 +00:00
|
|
|
return (
|
|
|
|
<Layout title={pageTitle}>
|
|
|
|
Loading...
|
|
|
|
</Layout>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
console.error("error", error);
|
|
|
|
return (
|
|
|
|
<Layout title={pageTitle}>
|
|
|
|
Oops, something unexpected happened. Please try reloading the page.
|
|
|
|
</Layout>
|
|
|
|
);
|
2021-07-18 15:32:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Layout title={pageTitle}>
|
|
|
|
<header className="flex">
|
|
|
|
<span className="flex items-center cursor-pointer" onClick={router.back}>
|
|
|
|
<FontAwesomeIcon className="h-8 w-8" icon={faChevronLeft} /> Back
|
|
|
|
</span>
|
|
|
|
</header>
|
|
|
|
<div className="flex flex-col space-y-6 p-6">
|
|
|
|
<ul>
|
2021-07-21 16:48:49 +00:00
|
|
|
{conversation!.map((message, index) => {
|
|
|
|
const isOutbound = message.direction === "outbound";
|
|
|
|
const isSameSender = message.from === conversation![index + 1]?.from;
|
|
|
|
const isLast = index === conversation!.length;
|
2021-07-18 15:32:45 +00:00
|
|
|
return (
|
2021-07-21 16:48:49 +00:00
|
|
|
<li
|
|
|
|
key={message.id}
|
|
|
|
className={clsx(
|
|
|
|
isSameSender || isLast ? "pb-3" : "pb-4",
|
|
|
|
isOutbound ? "text-right" : "text-left",
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
<span
|
|
|
|
className={clsx(
|
|
|
|
"p-2 rounded-lg text-white",
|
|
|
|
isOutbound ? "bg-[#3194ff] rounded-br-none" : "bg-black rounded-bl-none",
|
|
|
|
)}
|
|
|
|
>
|
|
|
|
{message.content}
|
|
|
|
</span>
|
2021-07-18 15:32:45 +00:00
|
|
|
</li>
|
2021-07-21 16:48:49 +00:00
|
|
|
);
|
2021-07-18 15:32:45 +00:00
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
2021-07-20 16:28:56 +00:00
|
|
|
<form onSubmit={onSubmit}>
|
|
|
|
<textarea{...register("content")} />
|
|
|
|
<button type="submit">Send</button>
|
|
|
|
</form>
|
2021-07-18 15:32:45 +00:00
|
|
|
</Layout>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getServerSideProps = withPageOnboardingRequired<Props>(
|
|
|
|
async (context, user) => {
|
|
|
|
const recipient = context.params?.recipient;
|
|
|
|
if (!recipient || Array.isArray(recipient)) {
|
|
|
|
return {
|
|
|
|
redirect: {
|
|
|
|
destination: "/messages",
|
|
|
|
permanent: false,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const conversation = await findConversation(user.id, recipient);
|
|
|
|
|
|
|
|
return {
|
|
|
|
props: {
|
|
|
|
recipient,
|
2021-07-20 16:28:56 +00:00
|
|
|
conversation,
|
2021-07-18 15:32:45 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
export default Messages;
|