|
| 1 | +"use client"; |
| 2 | +import React, { useState } from "react"; |
| 3 | +import api from "./lib/api"; |
| 4 | +import { useWallet } from "@suiet/wallet-kit"; |
| 5 | +import JSONFormatter from "./utils/JSONFormatter"; |
| 6 | +import SampleQuestionsCarousel from "./components/sections/SampleQuestionsCarousel"; |
| 7 | +export default function Home() { |
| 8 | + const [messages, setMessages] = useState< |
| 9 | + { text: string; sender: "user" | "llm"; isHTML?: boolean }[] |
| 10 | + >([]); |
| 11 | + const [inputValue, setInputValue] = useState(""); |
| 12 | + const [isThinking, setIsThinking] = useState(false); |
| 13 | + const { address } = useWallet(); |
| 14 | + const sampleQuestions = [ |
| 15 | + "What is The price of SUI?", |
| 16 | + "What is the price of bitcoin?", |
| 17 | + "Top 5 pools by TVL?", |
| 18 | + "Compare the prices between btc and sui?", |
| 19 | + ]; |
| 20 | + const handleSend = async (message?: string) => { |
| 21 | + const userMessage = message || inputValue.trim(); |
| 22 | + |
| 23 | + if (userMessage) { |
| 24 | + setMessages((prev) => [...prev, { text: userMessage, sender: "user" }]); |
| 25 | + setIsThinking(true); |
| 26 | + setInputValue(""); |
| 27 | + |
| 28 | + try { |
| 29 | + let modifiedMessage = userMessage; |
| 30 | + |
| 31 | + const keywords = ["transaction", "transfer", "send", "funds", "wallet"]; |
| 32 | + const containsKeywords = keywords.some((keyword) => |
| 33 | + userMessage.toLowerCase().includes(keyword) |
| 34 | + ); |
| 35 | + |
| 36 | + if (containsKeywords) { |
| 37 | + modifiedMessage = `${userMessage}. My wallet address is ${address}.`; |
| 38 | + } |
| 39 | + |
| 40 | + console.log(modifiedMessage, "modified"); |
| 41 | + const response = await api.post("/query", { prompt: modifiedMessage }); |
| 42 | + const res = response.data[0]; |
| 43 | + console.log(res); |
| 44 | + let llmResponse = ""; |
| 45 | + |
| 46 | + if (typeof res.response === "string") { |
| 47 | + llmResponse = res.response; |
| 48 | + } else { |
| 49 | + llmResponse = JSONFormatter.format(res.response); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + setMessages((prev) => [ |
| 54 | + ...prev, |
| 55 | + { text: llmResponse, sender: "llm", isHTML: true }, |
| 56 | + ]); |
| 57 | + } catch (error) { |
| 58 | + console.error("Error querying the LLM:", error); |
| 59 | + setMessages((prev) => [ |
| 60 | + ...prev, |
| 61 | + { |
| 62 | + text: "Sorry, there was an error. Please try again.", |
| 63 | + sender: "llm", |
| 64 | + isHTML: false, |
| 65 | + }, |
| 66 | + ]); |
| 67 | + } finally { |
| 68 | + setIsThinking(false); |
| 69 | + } |
| 70 | + } |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="h-[90dvh] w-[90dvw] flex justify-center relative items-center flex-col bg-gray-100"> |
| 75 | + {/* Chat messages */} |
| 76 | + <div className="flex-grow overflow-y-auto p-4 w-[82dvw] rounded mt-3 bg-gray-100 relative"> |
| 77 | + <div |
| 78 | + className="absolute inset-0 bg-cover bg-center opacity-10" |
| 79 | + style={{ |
| 80 | + backgroundImage: "url('/atomaLogo.svg')", |
| 81 | + backgroundRepeat: "no-repeat", |
| 82 | + backgroundSize: "300px 200px", |
| 83 | + }} |
| 84 | + ></div> |
| 85 | + |
| 86 | + {messages.map((message, index) => ( |
| 87 | + <div |
| 88 | + key={index} |
| 89 | + className={`relative mb-3 p-3 rounded-md w-fit md:max-w-[40%] break-words opacity-100 ${ |
| 90 | + message.sender === "user" |
| 91 | + ? "bg-blue-500 text-white self-end ml-auto text-right" |
| 92 | + : "bg-gray-300 text-black self-start mr-auto text-left" |
| 93 | + }`} |
| 94 | + > |
| 95 | + {message.isHTML ? ( |
| 96 | + <div dangerouslySetInnerHTML={{ __html: message.text }} /> |
| 97 | + ) : ( |
| 98 | + <div>{message.text}</div> |
| 99 | + )} |
| 100 | + </div> |
| 101 | + ))} |
| 102 | + |
| 103 | + {/* Loading indicator for LLM thinking */} |
| 104 | + {isThinking && ( |
| 105 | + <div className="relative mb-3 p-3 rounded-md max-w-[70%] bg-gray-300 text-black self-start mr-auto text-left"> |
| 106 | + Please wait... |
| 107 | + </div> |
| 108 | + )} |
| 109 | + </div> |
| 110 | + {/* Input area */} |
| 111 | + |
| 112 | + |
| 113 | + {/* Input area */} |
| 114 | + <div className="w-[90%] max-w-2xl"> |
| 115 | + <div className="flex items-center mt-2"> |
| 116 | + <input |
| 117 | + type="text" |
| 118 | + value={inputValue} |
| 119 | + onChange={(e) => setInputValue(e.target.value)} |
| 120 | + onKeyDown={(e) => e.key === "Enter" && handleSend()} |
| 121 | + placeholder="Chat with CoinSage..." |
| 122 | + className="flex-grow border-gray-500 border rounded-md px-4 py-3 focus:outline-none focus:ring-2 focus:ring-blue-500" |
| 123 | + /> |
| 124 | + <button |
| 125 | + onClick={() => handleSend()} |
| 126 | + className="ml-2 bg-blue-500 text-white px-4 py-2 rounded-md hover:bg-blue-600" |
| 127 | + > |
| 128 | + <svg |
| 129 | + xmlns="http://www.w3.org/2000/svg" |
| 130 | + fill="none" |
| 131 | + viewBox="0 0 24 24" |
| 132 | + strokeWidth={1.5} |
| 133 | + stroke="currentColor" |
| 134 | + className="w-6 h-6" |
| 135 | + > |
| 136 | + <path |
| 137 | + strokeLinecap="round" |
| 138 | + strokeLinejoin="round" |
| 139 | + d="M6 12L3.269 3.126A59.768 59.768 0 0121.485 12 59.77 59.77 0 013.27 20.876L5.999 12zm0 0h7.5" |
| 140 | + /> |
| 141 | + </svg> |
| 142 | + </button> |
| 143 | + </div> |
| 144 | + |
| 145 | + {/* Sample Questions */} |
| 146 | + <div className="mt-4"> |
| 147 | + {/* Desktop layout */} |
| 148 | + <div className="hidden sm:flex flex-wrap justify-center gap-2"> |
| 149 | + {sampleQuestions.map((question, index) => ( |
| 150 | + <button |
| 151 | + key={index} |
| 152 | + onClick={() => handleSend(question)} |
| 153 | + className="bg-gray-200 text-black px-4 py-2 rounded-md hover:bg-gray-300" |
| 154 | + > |
| 155 | + {question} |
| 156 | + </button> |
| 157 | + ))} |
| 158 | + </div> |
| 159 | + |
| 160 | + {/* Mobile carousel */} |
| 161 | + <div className="sm:hidden flex justify-center"> |
| 162 | + <SampleQuestionsCarousel |
| 163 | + questions={sampleQuestions} |
| 164 | + onQuestionClick={handleSend} |
| 165 | + /> |
| 166 | + </div> |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + |
| 170 | + </div> |
| 171 | + ); |
| 172 | +} |
0 commit comments