-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchat-bottombar.tsx
192 lines (178 loc) · 6.85 KB
/
chat-bottombar.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
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
'use client';
import React, { useEffect } from 'react';
import { ChatProps } from './chat';
import Link from 'next/link';
import { cn } from '@/lib/utils';
import { Button, buttonVariants } from '../ui/button';
import TextareaAutosize from 'react-textarea-autosize';
import { motion, AnimatePresence } from 'framer-motion';
import {
Cross2Icon,
ImageIcon,
PaperPlaneIcon,
StopIcon,
} from '@radix-ui/react-icons';
import { Mic, SendHorizonal } from 'lucide-react';
import useSpeechToText from '@/app/hooks/useSpeechRecognition';
import MultiImagePicker from '../image-embedder';
import useChatStore from '@/app/hooks/useChatStore';
import Image from 'next/image';
export default function ChatBottombar({
messages,
input,
handleInputChange,
handleSubmit,
stop,
formRef,
setInput,
}: ChatProps) {
const [message, setMessage] = React.useState(input);
const [isMobile, setIsMobile] = React.useState(false);
const inputRef = React.useRef<HTMLTextAreaElement>(null);
const base64Images = useChatStore((state) => state.base64Images);
const setBase64Images = useChatStore((state) => state.setBase64Images);
const env = process.env.NODE_ENV;
React.useEffect(() => {
const checkScreenWidth = () => {
setIsMobile(window.innerWidth <= 768);
};
// Initial check
checkScreenWidth();
// Event listener for screen width changes
window.addEventListener('resize', checkScreenWidth);
// Cleanup the event listener on component unmount
return () => {
window.removeEventListener('resize', checkScreenWidth);
};
}, []);
const handleKeyPress = (e: React.KeyboardEvent<HTMLTextAreaElement>) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSubmit(e as unknown as React.FormEvent<HTMLFormElement>);
}
};
const { isListening, transcript, startListening, stopListening } =
useSpeechToText({ continuous: true });
const listen = () => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
isListening ? stopVoiceInput() : startListening();
};
const stopVoiceInput = () => {
// eslint-disable-next-line @typescript-eslint/no-unused-expressions
setInput && setInput(transcript.length ? transcript : '');
stopListening();
};
const handleListenClick = () => {
listen();
};
useEffect(() => {
if (inputRef.current) {
inputRef.current.focus();
}
}, []);
return (
<div className="p-4 pb-7 flex justify-between w-full items-center gap-2">
<AnimatePresence initial={false}>
<div className="w-full items-center flex relative gap-2">
<div className="flex flex-col relative w-full bg-accent dark:bg-card rounded-lg">
<div className="flex w-full">
<form
onSubmit={handleSubmit}
className="w-full items-center flex relative gap-2"
>
<div className="absolute flex left-3 z-10">
<MultiImagePicker
disabled={env === 'production'}
onImagesPick={setBase64Images}
/>
</div>
<TextareaAutosize
autoComplete="off"
value={
isListening ? (transcript.length ? transcript : '') : input
}
ref={inputRef}
onKeyDown={handleKeyPress}
onChange={handleInputChange}
name="message"
placeholder={
!isListening ? 'Enter your prompt here' : 'Listening'
}
className=" max-h-24 px-14 bg-accent py-[22px] rounded-lg text-sm placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 w-full flex items-center h-16 resize-none overflow-hidden dark:bg-card"
/>
<div className="flex absolute right-3 items-center">
{isListening ? (
<div className="flex">
<Button
className="shrink-0 relative rounded-full bg-blue-500/30 hover:bg-blue-400/30 "
variant="ghost"
size="icon"
type="button"
onClick={handleListenClick}
>
<Mic className="w-5 h-5 " />
<span className="animate-pulse absolute h-[120%] w-[120%] rounded-full bg-blue-500/30" />
</Button>
</div>
) : (
<Button
className="shrink-0 rounded-full"
variant="ghost"
size="icon"
type="button"
onClick={handleListenClick}
>
<Mic className="w-5 h-5 " />
</Button>
)}
<Button
className="shrink-0 rounded-full"
variant="ghost"
size="icon"
type="submit"
disabled={!input.trim() || isListening}
>
<SendHorizonal className="w-5 h-5 " />
</Button>
</div>
</form>
</div>
{base64Images && (
<div className="flex px-2 pb-2 gap-2 ">
{base64Images.map((image, index) => {
return (
<div
key={index}
className="relative bg-muted-foreground/20 flex w-fit flex-col gap-2 p-1 border-t border-x rounded-md"
>
<div className="flex text-sm">
<Image
src={image}
width={20}
height={20}
className="h-auto rounded-md w-auto max-w-[100px] max-h-[100px]"
alt={''}
/>
</div>
<Button
onClick={() => {
const updatedImages = (prevImages: string[]) =>
prevImages.filter((_, i) => i !== index);
setBase64Images(updatedImages(base64Images));
}}
size="icon"
className="absolute -top-1.5 -right-1.5 text-white cursor-pointer bg-red-500 hover:bg-red-600 w-4 h-4 rounded-full flex items-center justify-center"
>
<Cross2Icon className="w-3 h-3" />
</Button>
</div>
);
})}
</div>
)}
</div>
</div>
</AnimatePresence>
</div>
);
}