File size: 5,671 Bytes
a5e87d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import React, { useState } from "react";
import { BOTTOMTEXT } from "../constants/constants";
import { useMessageFetching } from "../context/MessageFetch";
import { send } from "./Icons";

type InputProps = {
  askQuestion: (msg: string) => void;
  input: string;
  setInput: (value: string) => void;
  inputRef: React.RefObject<HTMLDivElement>;
  socket: any;
};

export default function Input({
  askQuestion,
  input,
  setInput,
  inputRef,
  socket,
}: InputProps) {
  const {
    messageFetching,
    setDisableinput,
    disableinput,
    stopFetching,
    setMessageFetching,
  } = useMessageFetching();

  function handleInputSubmit() {
    setDisableinput(true);
    setMessageFetching(true);
    if (input == "") return;
    askQuestion(input);
    setInput("");
    if (inputRef.current) {
      inputRef.current.innerText = "";
    }
  }

  const onKeyDown = (e: any) => {
    // if shift and enter are pressed together then add a new line if enter is pressed then submit the message
    if (e.key == "Enter" && !e.shiftKey && !messageFetching) {
      e.preventDefault();
      handleInputSubmit();
    }
  };

  return (
    <>
      <div
        className="input_wrapper"
        style={{
          display: "flex",
          flexDirection: "column",
          alignItems: "center",
          position: "fixed",
          width: "50%",
          paddingBottom: "10px",
          bottom: 0,
        }}
      >
        <div
          className="input_box"
          style={{
            width: "100%",
            backgroundColor: "#40414f",
            borderRadius: "7px",
            display: "flex",
            flexDirection: "row",
            alignItems: "center",
            paddingLeft: "5px",
            padding: "10px 5px",
            position: "relative",
          }}
        >
          <div
            ref={inputRef}
            style={{
              width: "97%",
              minHeight: 26,
              outline: "none",
              border: "none",
              backgroundColor: "#40414f",
              color: "white",
              paddingLeft: "8px",
              fontSize: "16px",
              bottom: 0,
              maxHeight: 150,
              overflowY: "auto",
              paddingRight: "30px",
            }}
            contentEditable={!disableinput}
            onInput={(e) => {
              setInput(e.currentTarget.innerText);
            }}
            onKeyDownCapture={(e) => {
              onKeyDown(e);
            }}
          />

          {disableinput ? (
            <div
              style={{
                position: "absolute",
                right: "10px",
                bottom: "5px",
                width: "35px",
                height: "35px",
                display: "flex",
                alignItems: "center",
                justifyContent: "center",
              }}
            >
              <div
                style={{
                  width: "20px",
                  height: "20px",
                  border: "2px solid #fff",
                  borderRadius: "50%",
                  borderTopColor: "transparent",
                  animation: "spin 1s linear infinite",
                }}
              ></div>
            </div>
          ) : (
            <>
              <button
                onClick={() => {
                  handleInputSubmit();
                }}
                style={{
                  marginRight: "20px",
                  backgroundColor: "transparent",
                  outline: "none",
                  border: "none",
                  color: "white",
                  cursor: "pointer",
                  padding: "5px",
                  position: "absolute",
                  bottom: "5px",
                  right: "0px",
                  borderRadius: "15%",
                  display: "flex",
                  alignItems: "center",
                  justifyContent: "center",
                  width: "35px",
                  height: "35px",
                }}
                onMouseOver={(e) => {
                  e.currentTarget.style.backgroundColor = "#000";
                }}
                onMouseLeave={(e) => {
                  e.currentTarget.style.backgroundColor = "transparent";
                }}
              >
                {send}
              </button>
            </>
          )}
        </div>
        <p
          style={{
            fontSize: "12.6px",
            textAlign: "center",
            marginTop: "19px",
            color: "rgb(185 185 185)",
            maxWidth: "85%",
          }}
        >
          {BOTTOMTEXT}
        </p>
      </div>

      {messageFetching && (
        <button
          onClick={() => {
            stopFetching(socket);
          }}
          style={{
            position: "absolute",
            bottom: "18vh",
            height: "35px",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            backgroundColor: "transparent",
            borderRadius: "5px",
            borderWidth: "2px",
            borderColor: "white",
            borderStyle: "solid",
            color: "white",
            cursor: "pointer",
            padding: "5px",
            left: "50%",
            transform: "translateX(-50%)",
          }}
          onMouseOver={(e) => {
            e.currentTarget.style.backgroundColor = "#000";
          }}
          onMouseLeave={(e) => {
            e.currentTarget.style.backgroundColor = "transparent";
            e.currentTarget.style.borderColor = "white";
          }}
        >
          Stop Responding
        </button>
      )}
    </>
  );
}