-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextFieldInChatOverview.tsx
113 lines (103 loc) · 3.04 KB
/
TextFieldInChatOverview.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use client";
import { useCreateChatRoom } from "@/chat/hooks/useCreateChatRoom";
import ArrowUpIcon from "@/shared/assets/icons/arrow-up-default.svg";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { css } from "styled-components";
import { SendChatMessageRequest } from "../apis/sendChatMessage";
import TextareaAutoSize from "./TextareaAutoSize";
export default function TextFieldInChatOverview() {
const [message, setMessage] = useState("");
const { mutate: createChatRoom } = useCreateChatRoom();
const router = useRouter();
const [isMessageSent, setIsMessageSent] = useState(false);
const [isComposing, setIsComposing] = useState(false);
const handleCompositionStart = () => {
setIsComposing(true);
};
const handleCompositionEnd = () => {
setIsComposing(false);
};
const handleChange = (e: React.ChangeEvent<HTMLTextAreaElement>) => {
const value = e.target.value;
if (value.length <= maxMessageLength) {
setMessage(value);
}
};
const submit = () => {
setMessage("");
setIsMessageSent(true);
createChatRoom(undefined, {
onSuccess: (data) => {
const messageRequest: SendChatMessageRequest = {
roomId: data.roomId,
message: message,
intent: "NORMAL",
};
router.push(`/chats/${data.roomId}?message=${JSON.stringify(messageRequest)}`);
},
});
};
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
submit();
};
const maxMessageLength = 300;
const disabled = isMessageSent;
const isOnlyWhiteSpace = message.trim().length === 0;
return (
<form
onSubmit={handleSubmit}
css={css`
position: relative;
border-radius: 8px;
`}
>
<TextareaAutoSize
value={message}
onChange={handleChange}
onKeyDown={(e) => {
if (e.key === "Enter" && e.shiftKey) return;
if (e.key === "Enter" && !isComposing) {
e.preventDefault();
submit();
}
}}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
disabled={disabled}
placeholder="오늘의 운세는 어떨까?"
minRows={1}
maxRows={8}
maxLength={maxMessageLength}
autoFocus
/>
<button
type="submit"
disabled={disabled || isOnlyWhiteSpace}
css={css`
position: absolute;
right: 12px;
bottom: 12px;
padding: 4px;
border-radius: 4px;
background-color: ${({ theme }) => theme.colors.primary03};
&:disabled {
background-color: ${({ theme }) => theme.colors.grey20};
pointer-events: none;
}
`}
aria-label="메세지 전송"
>
<ArrowUpIcon
width={24}
height={24}
css={css`
display: block;
color: ${({ theme }) => theme.colors.grey00};
`}
/>
</button>
</form>
);
}