2021-08-05 17:07:15 +00:00
|
|
|
import type { FunctionComponent } from "react";
|
|
|
|
import { useMutation, useQuery } from "blitz";
|
2021-07-31 15:57:43 +00:00
|
|
|
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
|
|
|
|
import { faPaperPlane } from "@fortawesome/pro-regular-svg-icons";
|
|
|
|
import { useForm } from "react-hook-form";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
import sendMessage from "../mutations/send-message";
|
|
|
|
import { Direction, Message, MessageStatus } from "../../../db";
|
|
|
|
import getConversationsQuery from "../queries/get-conversations";
|
2021-08-05 17:07:15 +00:00
|
|
|
import useCurrentUser from "../../core/hooks/use-current-user";
|
|
|
|
import useCurrentPhoneNumber from "../../core/hooks/use-current-phone-number";
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
type Form = {
|
2021-07-31 15:57:43 +00:00
|
|
|
content: string;
|
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
2021-08-01 07:40:18 +00:00
|
|
|
type Props = {
|
|
|
|
recipient: string;
|
|
|
|
onSend?: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
const NewMessageArea: FunctionComponent<Props> = ({ recipient, onSend }) => {
|
2021-08-05 17:07:15 +00:00
|
|
|
const { organization } = useCurrentUser();
|
|
|
|
const phoneNumber = useCurrentPhoneNumber();
|
2021-07-31 15:57:43 +00:00
|
|
|
const sendMessageMutation = useMutation(sendMessage)[0];
|
2021-07-31 14:33:18 +00:00
|
|
|
const { setQueryData: setConversationsQueryData, refetch: refetchConversations } = useQuery(
|
|
|
|
getConversationsQuery,
|
2021-08-01 12:04:04 +00:00
|
|
|
{},
|
2021-07-31 15:57:43 +00:00
|
|
|
)[1];
|
2021-07-31 14:33:18 +00:00
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
setValue,
|
|
|
|
formState: { isSubmitting },
|
2021-07-31 15:57:43 +00:00
|
|
|
} = useForm<Form>();
|
2021-07-31 14:33:18 +00:00
|
|
|
const onSubmit = handleSubmit(async ({ content }) => {
|
2021-08-01 07:40:18 +00:00
|
|
|
if (!recipient) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-07-31 14:33:18 +00:00
|
|
|
if (isSubmitting) {
|
2021-07-31 15:57:43 +00:00
|
|
|
return;
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
const id = uuidv4();
|
2021-07-31 14:33:18 +00:00
|
|
|
const message: Message = {
|
|
|
|
id,
|
2021-08-05 17:07:15 +00:00
|
|
|
organizationId: organization!.id,
|
|
|
|
phoneNumberId: phoneNumber!.id,
|
|
|
|
from: phoneNumber!.number,
|
2021-07-31 14:33:18 +00:00
|
|
|
to: recipient,
|
|
|
|
content: content,
|
|
|
|
direction: Direction.Outbound,
|
|
|
|
status: MessageStatus.Queued,
|
|
|
|
sentAt: new Date(),
|
2021-07-31 15:57:43 +00:00
|
|
|
};
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
await setConversationsQueryData(
|
|
|
|
(conversations) => {
|
2021-07-31 15:57:43 +00:00
|
|
|
const nextConversations = { ...conversations };
|
2021-07-31 14:33:18 +00:00
|
|
|
if (!nextConversations[recipient]) {
|
2021-07-31 15:57:43 +00:00
|
|
|
nextConversations[recipient] = [];
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 15:57:43 +00:00
|
|
|
nextConversations[recipient] = [...nextConversations[recipient]!, message];
|
2021-08-05 17:07:15 +00:00
|
|
|
|
|
|
|
return Object.fromEntries(
|
|
|
|
Object.entries(nextConversations).sort(
|
|
|
|
([, a], [, b]) => b[b.length - 1]!.sentAt.getTime() - a[a.length - 1]!.sentAt.getTime(),
|
|
|
|
),
|
|
|
|
);
|
2021-07-31 14:33:18 +00:00
|
|
|
},
|
2021-08-01 12:04:04 +00:00
|
|
|
{ refetch: false },
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
|
|
|
setValue("content", "");
|
2021-08-01 07:40:18 +00:00
|
|
|
onSend?.();
|
2021-07-31 15:57:43 +00:00
|
|
|
await sendMessageMutation({ to: recipient, content });
|
|
|
|
await refetchConversations({ cancelRefetch: true });
|
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={onSubmit}
|
|
|
|
className="absolute bottom-0 w-screen backdrop-filter backdrop-blur-xl bg-white bg-opacity-75 border-t flex flex-row h-16 p-2 pr-0"
|
|
|
|
>
|
|
|
|
<textarea
|
|
|
|
className="resize-none flex-1"
|
|
|
|
autoCapitalize="sentences"
|
|
|
|
autoCorrect="on"
|
|
|
|
placeholder="Text message"
|
|
|
|
rows={1}
|
|
|
|
spellCheck
|
|
|
|
{...register("content", { required: true })}
|
|
|
|
/>
|
|
|
|
<button type="submit">
|
|
|
|
<FontAwesomeIcon size="2x" className="h-8 w-8 pl-1 pr-2" icon={faPaperPlane} />
|
|
|
|
</button>
|
|
|
|
</form>
|
2021-07-31 15:57:43 +00:00
|
|
|
);
|
2021-08-01 07:40:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default NewMessageArea;
|
2021-07-31 14:33:18 +00:00
|
|
|
|
|
|
|
function uuidv4() {
|
|
|
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
|
|
|
|
const r = (Math.random() * 16) | 0,
|
2021-07-31 15:57:43 +00:00
|
|
|
v = c == "x" ? r : (r & 0x3) | 0x8;
|
|
|
|
return v.toString(16);
|
|
|
|
});
|
2021-07-31 14:33:18 +00:00
|
|
|
}
|