|
| 1 | +"use client"; |
| 2 | +import { ChatMessage } from "@/components/ChatMessage"; |
| 3 | +import { ConversationButton } from "@/components/ConversationButton"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import { useEffect, useState } from "react"; |
| 6 | + |
| 7 | +export interface GPTMessage { |
| 8 | + role: string; |
| 9 | + message: string; |
| 10 | +} |
| 11 | + |
| 12 | +export default function AppPage() { |
| 13 | + const router = useRouter(); |
| 14 | + const [model, setModel] = useState<string>("gpt-4o"); |
| 15 | + const [message, setMessage] = useState<string>(""); |
| 16 | + const [conversationId, setConversationId] = useState<number>(0); |
| 17 | + const [conversationList, setConversationList] = useState<number[]>([]); |
| 18 | + const [messages, setMessages] = useState<GPTMessage[]>([]); |
| 19 | + const [loading, setLoading] = useState<boolean>(false); |
| 20 | + |
| 21 | + function newChat() { |
| 22 | + setConversationId(0); |
| 23 | + setMessages([]); |
| 24 | + } |
| 25 | + |
| 26 | + async function handleSubmit(e: React.FormEvent<HTMLFormElement>) { |
| 27 | + e.preventDefault(); |
| 28 | + |
| 29 | + if (conversationId === 0) { |
| 30 | + const res = await fetch(`/api/chat/create`, { |
| 31 | + method: "POST", |
| 32 | + headers: { |
| 33 | + "content-type": "application/json", |
| 34 | + }, |
| 35 | + body: JSON.stringify({ |
| 36 | + prompt: message, |
| 37 | + model: model, |
| 38 | + }), |
| 39 | + }); |
| 40 | + |
| 41 | + const data = await res.json(); |
| 42 | + |
| 43 | + const new_conversation_list = conversationList; |
| 44 | + new_conversation_list.unshift(data.conversation_id); |
| 45 | + console.log(data.conversation_id); |
| 46 | + setConversationId(data.conversation_id); |
| 47 | + setConversationList(new_conversation_list); |
| 48 | + } |
| 49 | + |
| 50 | + setMessages((prev) => { |
| 51 | + return [...prev, { message: message, role: "user" }]; |
| 52 | + }); |
| 53 | + setMessage(""); |
| 54 | + setLoading(true); |
| 55 | + |
| 56 | + const conversation_res = await fetch( |
| 57 | + `/api/chat/conversations/${conversationId}`, |
| 58 | + { |
| 59 | + method: "POST", |
| 60 | + headers: { |
| 61 | + "content-type": "application/json", |
| 62 | + }, |
| 63 | + body: JSON.stringify({ |
| 64 | + prompt: message, |
| 65 | + model: model, |
| 66 | + }), |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + const promptReqData = await conversation_res.json(); |
| 71 | + |
| 72 | + setMessages((prev) => { |
| 73 | + return [...prev, { message: promptReqData.response, role: "system" }]; |
| 74 | + }); |
| 75 | + setLoading(false); |
| 76 | + } |
| 77 | + |
| 78 | + useEffect(() => { |
| 79 | + const fetchData = async () => { |
| 80 | + const res = await fetch("/api/chat/conversations"); |
| 81 | + if (res.ok) { |
| 82 | + const data = await res.json(); |
| 83 | + setConversationList(data); |
| 84 | + } else { |
| 85 | + router.replace("/login"); |
| 86 | + } |
| 87 | + }; |
| 88 | + |
| 89 | + fetchData(); |
| 90 | + }, []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <main className="grid grid-cols-6 grid-rows-1 w-full h-full min-h-screen"> |
| 94 | + <div |
| 95 | + id="sidebar" |
| 96 | + className="col-span-1 row-span-1 border-r border-[#333]" |
| 97 | + > |
| 98 | + <h1>Shuttle</h1> |
| 99 | + <div className="p-4"> |
| 100 | + <button |
| 101 | + className="px-4 py-1 w-full text-left rounded-md bg-gradient-to-r from-orange-700 to-yellow-400" |
| 102 | + onClick={() => newChat()} |
| 103 | + > |
| 104 | + New chat |
| 105 | + </button> |
| 106 | + <h2 className="text-center font-bold mt-4">Conversations</h2> |
| 107 | + <div className="py-4 flex flex-col items-start gap-2"> |
| 108 | + {conversationList.map((x) => ( |
| 109 | + <ConversationButton |
| 110 | + id={x} |
| 111 | + key={x} |
| 112 | + setMessages={setMessages} |
| 113 | + setConversationId={setConversationId} |
| 114 | + active={conversationId === x} |
| 115 | + /> |
| 116 | + ))} |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + <div className="col-span-5 row-span-1 flex flex-col p-4 w-full gap-4 max-h-screen overflow-auto"> |
| 121 | + <select |
| 122 | + className="font-bold text-slate-300 w-[15%] bg-gray-800 px-4 py-2" |
| 123 | + onChange={(e) => setModel((e.target as HTMLSelectElement).value)} |
| 124 | + > |
| 125 | + <option className="text-black" value="gpt-4o"> |
| 126 | + gpt-4o |
| 127 | + </option> |
| 128 | + <option className="text-black" value="gpt-4o-mini"> |
| 129 | + gpt-4o-mini |
| 130 | + </option> |
| 131 | + <option className="text-black" value="gpt-4o-preview"> |
| 132 | + gpt-4o-preview |
| 133 | + </option> |
| 134 | + </select> |
| 135 | + <div |
| 136 | + id="chatbox" |
| 137 | + className="flex flex-col gap-4 w-full h-full min-h-[90%-2px] max-h-[90%-2px] overflow-y-scroll" |
| 138 | + > |
| 139 | + {messages.map((x, idx) => ( |
| 140 | + <ChatMessage |
| 141 | + message={x.message} |
| 142 | + role={x.role} |
| 143 | + key={`message=${idx}`} |
| 144 | + /> |
| 145 | + ))} |
| 146 | + {loading ? <p> Waiting for response... </p> : null} |
| 147 | + </div> |
| 148 | + <form |
| 149 | + className="w-full flex flex-row gap-2 self-end " |
| 150 | + onSubmit={(e) => handleSubmit(e)} |
| 151 | + > |
| 152 | + <input |
| 153 | + className="w-full px-4 py-2 text-black" |
| 154 | + name="message" |
| 155 | + type="text" |
| 156 | + value={message} |
| 157 | + onInput={(e) => setMessage((e.target as HTMLInputElement).value)} |
| 158 | + required |
| 159 | + ></input> |
| 160 | + <button type="submit" className="bg-slate-300 text-black px-4 py-2"> |
| 161 | + Send |
| 162 | + </button> |
| 163 | + </form> |
| 164 | + </div> |
| 165 | + </main> |
| 166 | + ); |
| 167 | +} |
0 commit comments