|
1 | | -import { FC } from 'react'; |
| 1 | +import { FC, useEffect, useRef, useState } from 'react'; |
2 | 2 | import { Send } from 'lucide-react'; |
| 3 | +import { toast } from 'sonner'; |
| 4 | +import { usePost } from '@/core/react-query'; |
| 5 | +import { BasicBody, BasicMessage, BasicResponse } from './basic.types'; |
| 6 | +import { API } from '@/enums/api.enums'; |
| 7 | +import { BeatLoader } from 'react-spinners'; |
3 | 8 |
|
4 | 9 | const Basic: FC = () => { |
| 10 | + const { mutateAsync } = usePost<BasicResponse, BasicBody>(API.Basic, 'basic'); |
| 11 | + |
| 12 | + const [minToken, setMinToken] = useState('100'); |
| 13 | + const [maxToken, setMaxToken] = useState('500'); |
| 14 | + const [service, setService] = useState('terraform'); |
| 15 | + const [input, setInput] = useState(''); |
| 16 | + const [messages, setMessages] = useState<BasicMessage[]>([]); |
| 17 | + |
| 18 | + const messagesRef = useRef<HTMLDivElement>(null); |
| 19 | + |
| 20 | + useEffect(() => { |
| 21 | + if (messagesRef.current) { |
| 22 | + messagesRef.current.scrollTo({ |
| 23 | + top: messagesRef.current.scrollHeight, |
| 24 | + behavior: 'smooth', |
| 25 | + }); |
| 26 | + } |
| 27 | + }, [messages]); |
| 28 | + |
| 29 | + const handleSendMessage = async () => { |
| 30 | + try { |
| 31 | + setMessages((prev) => [ |
| 32 | + ...prev, |
| 33 | + { role: 'user', content: input }, |
| 34 | + { role: 'assistant', content: '', loading: true }, |
| 35 | + ]); |
| 36 | + |
| 37 | + const body: BasicBody = { |
| 38 | + max_tokens: parseInt(maxToken), |
| 39 | + min_tokens: parseInt(minToken), |
| 40 | + service, |
| 41 | + input, |
| 42 | + }; |
| 43 | + const { |
| 44 | + data: { output }, |
| 45 | + } = await mutateAsync(body); |
| 46 | + setInput(''); |
| 47 | + setMessages((prev) => |
| 48 | + prev.map((message, index) => |
| 49 | + index === prev.length - 1 |
| 50 | + ? { ...message, content: output, loading: false } |
| 51 | + : message, |
| 52 | + ), |
| 53 | + ); |
| 54 | + } catch (error) { |
| 55 | + console.log(error); |
| 56 | + setMessages((prev) => prev.slice(0, -1)); |
| 57 | + toast.error('Something went wrong'); |
| 58 | + } |
| 59 | + }; |
| 60 | + |
5 | 61 | return ( |
6 | | - <div className="flex items-center justify-center w-full h-full"> |
| 62 | + <div className="flex h-full w-full items-center justify-center"> |
7 | 63 | <div className="w-full max-w-[768px]"> |
8 | | - <div className="w-full p-2 rounded-md"> |
9 | | - <div className="flex items-center justify-center w-full h-full gap-3"> |
10 | | - <div className="flex flex-col w-full"> |
| 64 | + <div className="w-full rounded-md p-2"> |
| 65 | + <div className="flex h-full w-full items-center justify-center gap-3"> |
| 66 | + <div className="flex w-full flex-col"> |
11 | 67 | <label htmlFor="min_token" className="mb-2"> |
12 | 68 | Min Token |
13 | 69 | </label> |
14 | 70 | <input |
15 | 71 | id="min_token" |
16 | | - className="w-full p-3 rounded-md outline-none" |
| 72 | + type="number" |
| 73 | + value={minToken} |
| 74 | + onChange={(e) => setMinToken(e.target.value)} |
| 75 | + className="w-full rounded-md p-3 outline-none" |
17 | 76 | /> |
18 | 77 | </div> |
19 | | - <div className="flex flex-col w-full"> |
| 78 | + <div className="flex w-full flex-col"> |
20 | 79 | <label htmlFor="min_token" className="mb-2"> |
21 | 80 | Max Token |
22 | 81 | </label> |
23 | 82 | <input |
24 | 83 | id="min_token" |
25 | | - className="w-full p-3 rounded-md outline-none" |
| 84 | + type="number" |
| 85 | + value={maxToken} |
| 86 | + onChange={(e) => setMaxToken(e.target.value)} |
| 87 | + className="w-full rounded-md p-3 outline-none" |
26 | 88 | /> |
27 | 89 | </div> |
28 | | - <div className="flex flex-col w-full"> |
| 90 | + <div className="flex w-full flex-col"> |
29 | 91 | <label htmlFor="min_token" className="mb-2"> |
30 | 92 | Service |
31 | 93 | </label> |
32 | 94 | <input |
33 | 95 | id="min_token" |
34 | | - className="w-full p-3 rounded-md outline-none" |
| 96 | + type="text" |
| 97 | + value={service} |
| 98 | + onChange={(e) => setService(e.target.value)} |
| 99 | + className="w-full rounded-md p-3 outline-none" |
35 | 100 | /> |
36 | 101 | </div> |
37 | 102 | </div> |
38 | 103 | <div className="mt-4"> |
39 | | - <div className="w-full p-3 overflow-y-auto rounded-md scrollbar-corner-transparent scrollbar-thin scrollbar-track-transparent h-96 bg-slate-900"> |
40 | | - <div className="max-w-full chat chat-end"> |
41 | | - <div className="text-white bg-gray-600 chat-bubble"> |
42 | | - You underestimate my power! |
43 | | - </div> |
44 | | - </div> |
45 | | - <div className="max-w-full chat chat-start"> |
46 | | - <div className="text-white chat-bubble"> |
47 | | - You underestimate my power! |
48 | | - </div> |
49 | | - </div> |
50 | | - <div className="max-w-full chat chat-end"> |
51 | | - <div className="text-white bg-gray-600 chat-bubble"> |
52 | | - You underestimate my power! |
53 | | - </div> |
54 | | - </div> |
55 | | - <div className="max-w-full chat chat-start"> |
56 | | - <div className="text-white chat-bubble"> |
57 | | - You underestimate my power! |
58 | | - </div> |
59 | | - </div> |
60 | | - <div className="max-w-full chat chat-end"> |
61 | | - <div className="text-white bg-gray-600 chat-bubble"> |
62 | | - You underestimate my power! |
63 | | - </div> |
64 | | - </div> |
65 | | - <div className="max-w-full chat chat-start"> |
66 | | - <div className="text-white chat-bubble"> |
67 | | - You underestimate my power! |
68 | | - </div> |
69 | | - </div> |
70 | | - <div className="max-w-full chat chat-end"> |
71 | | - <div className="text-white bg-gray-600 chat-bubble"> |
72 | | - You underestimate my power! |
73 | | - </div> |
74 | | - </div> |
75 | | - <div className="max-w-full chat chat-start"> |
76 | | - <div className="text-white chat-bubble"> |
77 | | - You underestimate my power! |
78 | | - </div> |
79 | | - </div> |
| 104 | + <div |
| 105 | + ref={messagesRef} |
| 106 | + className="h-96 w-full overflow-y-auto rounded-md bg-slate-900 p-3 scrollbar-thin scrollbar-track-transparent scrollbar-corner-transparent" |
| 107 | + > |
| 108 | + {messages.map((message) => |
| 109 | + message.role === 'user' ? ( |
| 110 | + <div className="chat chat-end max-w-full"> |
| 111 | + <div className="chat-bubble bg-gray-600 text-white"> |
| 112 | + {message.content} |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <div className="chat chat-start max-w-full"> |
| 117 | + <div className="chat-bubble text-white"> |
| 118 | + {message.loading ? ( |
| 119 | + <BeatLoader color="#e3e3e3" size={10} /> |
| 120 | + ) : ( |
| 121 | + message.content |
| 122 | + )} |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + ), |
| 126 | + )} |
80 | 127 | </div> |
81 | 128 | </div> |
82 | 129 | <div className="relative mt-4"> |
83 | 130 | <textarea |
84 | | - className="w-full p-4 pr-16 rounded-md outline-none resize-none" |
| 131 | + value={input} |
| 132 | + onChange={(e) => setInput(e.target.value)} |
85 | 133 | rows={2} |
| 134 | + className="w-full resize-none rounded-md p-4 pr-16 outline-none" |
86 | 135 | /> |
87 | | - <button className="absolute flex items-center justify-center p-2 bg-white rounded-full right-3 top-5"> |
| 136 | + <button |
| 137 | + disabled={!input} |
| 138 | + onClick={handleSendMessage} |
| 139 | + className="absolute right-3 top-5 flex items-center justify-center rounded-full bg-white p-2 transition-all disabled:opacity-50" |
| 140 | + > |
88 | 141 | <Send className="size-6 stroke-[#121212]" /> |
89 | 142 | </button> |
90 | 143 | </div> |
|
0 commit comments