-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathChatInput.jsx
More file actions
181 lines (174 loc) · 4.49 KB
/
Copy pathChatInput.jsx
File metadata and controls
181 lines (174 loc) · 4.49 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import React, { useState } from "react";
import { BsEmojiSmileFill } from "react-icons/bs";
import { IoMdSend } from "react-icons/io";
import styled from "styled-components";
import Picker from "emoji-picker-react";
import axios from "axios";
import { sendMessageRoute,host } from "../utils/APIRoutes";
import { AiOutlinePaperClip } from "react-icons/ai";
export default function ChatInput({ handleSendMsg }) {
const [msg, setMsg] = useState("");
const [showEmojiPicker, setShowEmojiPicker] = useState(false);
const [file, setFile] = useState(null);
const handleEmojiPickerhideShow = () => {
setShowEmojiPicker(!showEmojiPicker);
};
const handleEmojiClick = (event, emojiObject) => {
let message = msg;
message += emojiObject.emoji;
setMsg(message);
};
const handleFileChange = (e) => {
setFile(e.target.files[0]);
};
// Handle file upload and send
const sendChat = async (event) => {
event.preventDefault();
if (file) {
const formData = new FormData();
formData.append("file", file);
const response = await axios.post(`${host}/api/messages/upload`, formData, {
headers: { "Content-Type": "multipart/form-data" },
});
handleSendMsg(response.data.fileUrl); // send file URL as message
setFile(null);
} else if (msg.length > 0) {
handleSendMsg(msg);
setMsg("");
}
};
return (
<Container>
<div className="button-container">
<div className="emoji">
<BsEmojiSmileFill onClick={handleEmojiPickerhideShow} />
{showEmojiPicker && <Picker onEmojiClick={handleEmojiClick} />}
</div>
</div>
<form className="input-container" onSubmit={sendChat}>
<input
type="text"
placeholder="type your message here"
onChange={(e) => setMsg(e.target.value)}
value={msg}
/>
{/* WhatsApp-style paperclip icon for file upload */}
<label htmlFor="file-upload" className="file-attach">
<AiOutlinePaperClip size={24} />
</label>
<input
id="file-upload"
type="file"
style={{ display: "none" }}
onChange={handleFileChange}
/>
<button type="submit">
<IoMdSend />
</button>
</form>
</Container>
);
}
const Container = styled.div`
display: grid;
align-items: center;
grid-template-columns: 5% 95%;
background-color: #080420;
padding: 0 2rem;
@media screen and (min-width: 720px) and (max-width: 1080px) {
padding: 0 1rem;
gap: 1rem;
}
.button-container {
display: flex;
align-items: center;
color: white;
gap: 1rem;
.emoji {
position: relative;
svg {
font-size: 1.5rem;
color: #ffff00c8;
cursor: pointer;
}
.emoji-picker-react {
position: absolute;
top: -350px;
background-color: #080420;
box-shadow: 0 5px 10px #9a86f3;
border-color: #9a86f3;
.emoji-scroll-wrapper::-webkit-scrollbar {
background-color: #080420;
width: 5px;
&-thumb {
background-color: #9a86f3;
}
}
.emoji-categories {
button {
filter: contrast(0);
}
}
.emoji-search {
background-color: transparent;
border-color: #9a86f3;
}
.emoji-group:before {
background-color: #080420;
}
}
}
}
.input-container {
width: 100%;
border-radius: 2rem;
display: flex;
align-items: center;
gap: 1rem;
background-color: #ffffff34;
input[type="text"] {
width: 80%;
height: 60%;
background-color: transparent;
color: white;
border: none;
padding-left: 1rem;
font-size: 1.2rem;
&::selection {
background-color: #9a86f3;
}
&:focus {
outline: none;
}
}
.file-attach {
cursor: pointer;
display: flex;
align-items: center;
color: #9a86f3;
margin-right: 0.5rem;
svg {
font-size: 1.7rem;
}
}
button {
padding: 0.3rem 2rem;
border-radius: 2rem;
display: flex;
justify-content: center;
align-items: center;
background-color: #9a86f3;
border: none;
@media screen and (min-width: 720px) and (max-width: 1080px) {
padding: 0.3rem 1rem;
svg {
font-size: 1rem;
}
}
svg {
font-size: 2rem;
color: white;
}
}
}
`;