shellphone.app/app/features/messages/components/new-message-area.tsx

89 lines
2.4 KiB
TypeScript
Raw Normal View History

2021-08-05 17:07:15 +00:00
import type { FunctionComponent } from "react";
import { IoSend } from "react-icons/io5";
2022-05-14 10:22:06 +00:00
import { type Message, Direction, MessageStatus } from "@prisma/client";
import useSession from "~/features/core/hooks/use-session";
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 }) => {
2022-05-14 10:22:06 +00:00
const { currentOrganization, /*hasOngoingSubscription*/ } = useSession();
// const phoneNumber = useCurrentPhoneNumber();
// const sendMessageMutation = useMutation(sendMessage)[0];
const onSubmit = async () => {
/*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: hasOngoingSubscription
? content
: content + "\n\nSent from Shellphone (https://www.shellphone.app)",
2021-07-31 14:33:18 +00:00
direction: Direction.Outbound,
status: MessageStatus.Queued,
sentAt: new Date(),
2022-05-14 10:22:06 +00:00
};*/
2021-07-31 14:33:18 +00:00
2022-05-14 10:22:06 +00:00
/*await setConversationsQueryData(
2021-07-31 14:33:18 +00:00
(conversations) => {
const nextConversations = { ...conversations };
2021-07-31 14:33:18 +00:00
if (!nextConversations[recipient]) {
nextConversations[recipient] = {
recipient,
formattedPhoneNumber: recipient,
messages: [],
};
2021-07-31 14:33:18 +00:00
}
nextConversations[recipient]!.messages = [...nextConversations[recipient]!.messages, message];
2021-08-05 17:07:15 +00:00
return Object.fromEntries(
Object.entries(nextConversations).sort(
([, a], [, b]) =>
b.messages[b.messages.length - 1]!.sentAt.getTime() -
a.messages[a.messages.length - 1]!.sentAt.getTime(),
2021-08-05 17:07:15 +00:00
),
);
2021-07-31 14:33:18 +00:00
},
2021-08-01 12:04:04 +00:00
{ refetch: false },
2022-05-14 10:22:06 +00:00
);*/
// setValue("content", "");
// onSend?.();
};
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
2022-05-14 10:22:06 +00:00
name="content"
2021-07-31 14:33:18 +00:00
className="resize-none flex-1"
autoCapitalize="sentences"
autoCorrect="on"
placeholder="Text message"
rows={1}
spellCheck
2022-05-14 10:22:06 +00:00
required
2021-07-31 14:33:18 +00:00
/>
<button type="submit">
<IoSend className="h-8 w-8 pl-1 pr-2" />
2021-07-31 14:33:18 +00:00
</button>
</form>
);
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,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
2021-07-31 14:33:18 +00:00
}