diff --git a/src/app/(root)/(routes)/results/[searchId]/page.tsx b/src/app/(root)/(routes)/results/[searchId]/page.tsx index f0b1abd..bd75818 100644 --- a/src/app/(root)/(routes)/results/[searchId]/page.tsx +++ b/src/app/(root)/(routes)/results/[searchId]/page.tsx @@ -42,20 +42,6 @@ const resultsPage = async ({ params: { searchId } }: Props) => { }, }); - const chatInitiated = async (searchResult: SearchResult) => { - const dbMessages = await prismadb.message.findMany({ - where: { - searchResultId: searchResult.id, - }, - }); - - if (dbMessages.length === 0) { - return false; - } else { - return true; - } - }; - return (
{searchResults.map((result) => ( @@ -64,7 +50,6 @@ const resultsPage = async ({ params: { searchId } }: Props) => { key={result.id} data={result} query={search_metadata[0].query} - chatInitiated={() => chatInitiated(result)} />
))} diff --git a/src/app/(root)/(routes)/results/components/chat-component.tsx b/src/app/(root)/(routes)/results/components/chat-component.tsx index 310a2b1..9b84694 100644 --- a/src/app/(root)/(routes)/results/components/chat-component.tsx +++ b/src/app/(root)/(routes)/results/components/chat-component.tsx @@ -22,45 +22,55 @@ interface ChatComponentProps { } const ChatComponent: React.FC = ({ data, query }) => { - // const [dbMessages, setDbMessages] = useState([]); - // const [chatArgs, setChatArgs] = useState({}); + const [dbMessages, setDbMessages] = useState([]); + const [chatArgs, setChatArgs] = useState({}); - // useEffect(() => { - // const fetchDbMessages = async () => { - // try { - // const response = await axios.post(`/api/get-messages`, { - // searchResultId: data.id, - // }); - // setDbMessages(response.data); - // } catch (error) { - // console.error("Error fetching messages:", error); - // } - // }; + useEffect(() => { + const fetchDbMessages = async () => { + try { + const response = await axios.post(`/api/get-messages`, { + searchResultId: data.id, + }); + setDbMessages(response.data); + } catch (error) { + console.error("Error fetching messages:", error); + } + }; - // fetchDbMessages(); - // }, [data.id]); + fetchDbMessages(); + }, [data.id]); - // useEffect(() => { - // if (dbMessages.length === 0) { - // console.log("no messages. Adding initial input."); - // setChatArgs({ - // initialInput: - // "Please first summarise this case for me and then explain why this case is relevant to my situation as follow: " + - // query, - // }); - // console.log(chatArgs); - // } else { - // console.log("Have messages. Adding initial messages."); - // const simplifiedMessages = dbMessages.map(({ role, content }) => ({ - // role, - // content, - // })); - // setChatArgs({ initialMessages: simplifiedMessages }); - // console.log(chatArgs); - // } - // }, [dbMessages, query]); + useEffect(() => { + if (dbMessages.length === 0) { + console.log("no messages. Adding initial input."); + setChatArgs({ + initialInput: + "Please first summarise this case for me and then explain why this case is relevant to my situation as follow: " + + query, + }); + console.log(chatArgs); + } else { + console.log("Have messages. Adding initial messages."); + const simplifiedMessages = dbMessages.map(({ role, content }) => ({ + role, + content, + })); + setChatArgs({ initialMessages: simplifiedMessages }); + console.log(chatArgs); + } + }, [dbMessages, query]); + + useEffect(() => { + const mockEvent = { + preventDefault: () => {}, + // Add other minimal properties and methods if needed + } as unknown as React.FormEvent; + handleSubmit(mockEvent); + console.log("haha"); + }, [query]); + + console.log(chatArgs); - // console.log(chatArgs); const { messages, input, handleInputChange, handleSubmit } = useChat({ body: { filter: data.caseNeutralCit }, initialInput: @@ -68,6 +78,8 @@ const ChatComponent: React.FC = ({ data, query }) => { query, }); + if (Object.keys(chatArgs).length == 0) return
Loading...
; + return (