File size: 2,068 Bytes
ae34ac2
9f076f8
054d282
 
 
9f076f8
 
 
054d282
 
9f076f8
 
d92a0cd
f6091f7
d92a0cd
9f076f8
 
 
 
 
 
f6091f7
9f076f8
d92a0cd
f6091f7
9f076f8
d92a0cd
29df9bc
 
 
 
 
fce0d00
f6091f7
fce0d00
9f076f8
 
d92a0cd
29df9bc
 
 
9f076f8
054d282
9f076f8
 
054d282
 
 
 
 
 
 
 
 
 
 
 
 
 
9f076f8
054d282
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
"use client"
import { useEffect, useState } from "react";
import WebSearchResults from "@/components/WebSearchResults";
import Link from "next/link";

export default function WebSearchPage({ searchParams }) {
  const [results, setResults] = useState(null);
  const [aiResponse, setAiResponse] = useState(null);
  const startIndex = searchParams.start || "1";

  useEffect(() => {
    async function fetchData() {
      // Fetch Google search results via server-side route
      /*
      const response = await fetch(`/api/search?searchTerm=${searchParams.searchTerm}&start=${startIndex}`);
      if (!response.ok) {
        console.log(response);
        throw new Error("Something went wrong");
      }
      const data = await response.json();
      setResults(data.items);
      */

      // Prepare AI prompt
      // const aiPrompt = `You're creating a search engine experience. You got the following search results for the term "${searchParams.searchTerm}": ${JSON.stringify(data.items)}. How can you present these results in a helpful way?`;

      // Fetch AI response via server-side route
      const openaiRes = new EventSource('/api/llm', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          'question': searchParams.searchTerm
        })
      });

      // Listen for AI responses and append to state
      openaiRes.onmessage = function(event) {
        setAiResponse(aiResponse => aiResponse + event.data);
      };
    }

    fetchData();
  }, [searchParams, startIndex]);

  if (!results) {
    return (
      <div className="flex flex-col justify-center items-center pt-10">
        <h1 className="text-3xl mb-4">No results found</h1>
        <p className="text-lg">
          Try searching for something else or go back to the homepage{" "}
          <Link href="/" className="text-blue-500">
            Home
          </Link>
        </p>
      </div>
    );
  }
  return <>{results && <WebSearchResults results={results} aiResponse={aiResponse} />}</>;
}