File size: 9,751 Bytes
f6a1fd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use client'

import { useState, useRef, useEffect } from 'react'
import { useChat } from 'ai/react'

type MessageWithLoading = {
  content: string;
  role: string;
  isStreaming?: boolean;
}

const EXAMPLE_PROMPTS = [
  {
    title: "Personalized Learning",
    description: "Get tailored explanations",
    prompt: "Can you help me understand photosynthesis in a simple way?"
  },
  {
    title: "24/7 Availability",
    description: "Learn at your own pace",
    prompt: "I need help solving this quadratic equation: x² + 5x + 6 = 0"
  },
  {
    title: "Multiple Subjects",
    description: "From math to literature",
    prompt: "What are the main themes in Shakespeare's Macbeth?"
  }
]

export default function LandingPageChatBot() {
  const { messages: rawMessages, input, handleInputChange, handleSubmit, isLoading, append } = useChat({
    api: '/api/landing_page_chat',
    streamProtocol: 'data',
    onError: (error) => {
      console.error('Chat error:', error);
    },
    onFinish: (message) => {
      console.log('Chat finished:', message);
      setMessages(prev => prev.map(msg => ({...msg, isStreaming: false})))
    },
    keepLastMessageOnError: true,
  })

  const chatContainerRef = useRef<HTMLDivElement>(null)
  const [hasInteracted, setHasInteracted] = useState(false)
  const [messages, setMessages] = useState<MessageWithLoading[]>([])

  useEffect(() => {
    setMessages(rawMessages.map((msg, index) => ({
      ...msg,
      isStreaming: isLoading && index === rawMessages.length - 1 && msg.role === 'assistant'
    })))
  }, [rawMessages, isLoading])

  const scrollToBottom = () => {
    if (chatContainerRef.current) {
      const { scrollHeight, clientHeight } = chatContainerRef.current
      chatContainerRef.current.scrollTop = scrollHeight - clientHeight
    }
  }

  useEffect(() => {
    scrollToBottom()
  }, [messages])

  const sendMessage = async (text: string) => {
    setHasInteracted(true)
    await append({
      content: text,
      role: 'user',
    })
  }

  const onSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
    setHasInteracted(true)
    handleSubmit(e)
  }

  return (
    <section className="w-full h-[800px] bg-gradient-to-b from-background-secondary to-background-primary flex items-center justify-center px-4">
      <div className="w-full max-w-5xl mx-auto h-full flex items-center">
        {!hasInteracted && messages.length === 0 ? (
          <div className="text-center space-y-8 w-full">
            <div className="space-y-4">
              <h2 className="text-5xl font-bold bg-gradient-to-r from-primary to-secondary bg-clip-text text-transparent">
                AI Learning Assistant
              </h2>
              <p className="text-text-secondary text-xl max-w-2xl mx-auto leading-relaxed">
                Explore new topics, get homework help, or discover learning resources
              </p>
            </div>

            <form onSubmit={onSubmit} className="max-w-3xl mx-auto">
              <div className="relative flex items-center">
                <input
                  type="text"
                  value={input}
                  onChange={handleInputChange}
                  placeholder="Ask anything about learning..."
                  className="w-full p-6 pr-36 rounded-2xl border border-border/50 bg-background-primary/50 
                           backdrop-blur-sm shadow-lg focus:outline-none focus:ring-2 focus:ring-primary/50 
                           text-lg transition-all duration-200 placeholder:text-text-secondary/50"
                />
                <button
                  type="submit"
                  disabled={isLoading}
                  className="absolute right-2 p-3 bg-primary text-white rounded-full
                           hover:bg-primary/90 transition-all duration-200
                           shadow-md hover:shadow-lg active:scale-95 disabled:opacity-50
                           disabled:hover:bg-primary disabled:cursor-not-allowed"
                >
                  <svg 
                    xmlns="http://www.w3.org/2000/svg" 
                    fill="none" 
                    viewBox="0 0 24 24" 
                    strokeWidth="1.5" 
                    stroke="currentColor" 
                    className={`size-6 ${isLoading ? 'animate-pulse' : ''}`}
                  >
                    <path 
                      strokeLinecap="round" 
                      strokeLinejoin="round" 
                      d="M6 12 3.269 3.125A59.769 59.769 0 0 1 21.485 12 59.768 59.768 0 0 1 3.27 20.875L5.999 12Zm0 0h7.5" 
                    />
                  </svg>
                </button>
              </div>
            </form>

            <div className="mt-12">
              <h3 className="text-xl font-semibold text-text-primary mb-6">Try these examples:</h3>
              <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                {EXAMPLE_PROMPTS.map((prompt, index) => (
                  <button
                    key={index}
                    onClick={() => sendMessage(prompt.prompt)}
                    className="group p-6 rounded-2xl border border-border/50 bg-background-primary/30 
                             hover:bg-background-primary/50 backdrop-blur-sm transition-all duration-200
                             hover:border-primary/50 hover:shadow-lg text-left"
                  >
                    <div className="flex items-center justify-between mb-2">
                      <h4 className="font-semibold text-text-primary">{prompt.title}</h4>
                      <svg
                        className="w-5 h-5 text-primary opacity-0 group-hover:opacity-100 transition-opacity duration-200" 
                        fill="none"
                        stroke="currentColor"
                        viewBox="0 0 24 24"
                      >
                        <path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M9 5l7 7-7 7" />
                      </svg>
                    </div>
                    <p className="text-text-secondary text-sm mb-2">{prompt.description}</p>
                    <p className="text-primary text-sm font-medium truncate">{prompt.prompt}</p>
                  </button>
                ))}
              </div>
            </div>
          </div>
        ) : (
          <div className="bg-background-primary/50 backdrop-blur-sm rounded-2xl shadow-lg border border-border/50 overflow-hidden w-full h-[700px] flex flex-col">
            <div className="p-6 border-b border-border/50 bg-background-secondary/30">
              <h2 className="text-xl font-semibold text-text-primary">AI Learning Assistant</h2>
            </div>

            <div
              ref={chatContainerRef}
              className="flex-1 overflow-y-auto p-6 space-y-6"
            >
              {messages.map((message, index) => (
                <div
                  key={index}
                  className={`flex ${message.role === 'user' ? 'justify-end' : 'justify-start'}`}
                >
                  <div
                    className={`max-w-[80%] p-4 rounded-2xl shadow-sm ${
                      message.role === 'user'
                        ? 'bg-primary text-white rounded-br-none'
                        : 'bg-background-secondary/50 text-text-primary rounded-bl-none'
                    }`}
                  >
                    {message.content}
                    {message.isStreaming && (
                      <span className="inline-block w-2.5 h-2.5 ml-1.5 bg-primary rounded-full animate-pulse" />
                    )}
                  </div>
                </div>
              ))}
            </div>

            <div className="p-6 border-t border-border/50 bg-background-secondary/30">
              <form onSubmit={onSubmit}>
                <div className="relative flex items-center">
                  <input
                    type="text"
                    value={input}
                    onChange={handleInputChange}
                    placeholder={isLoading ? "AI is thinking..." : "Type your message..."}
                    disabled={isLoading}
                    className="w-full p-4 pr-32 rounded-xl border border-border/50 bg-background-primary/50 
                             backdrop-blur-sm focus:outline-none focus:ring-2 focus:ring-primary/50
                             text-base transition-all duration-200 disabled:opacity-50"
                  />
                  <button
                    type="submit"
                    disabled={isLoading}
                    className="absolute right-2 p-2 bg-primary text-white rounded-full
                             hover:bg-primary/90 transition-all duration-200
                             shadow-md hover:shadow-lg active:scale-95 disabled:opacity-50
                             disabled:hover:bg-primary disabled:cursor-not-allowed"
                  >
                    <svg 
                      xmlns="http://www.w3.org/2000/svg" 
                      fill="none" 
                      viewBox="0 0 24 24" 
                      strokeWidth="1.5" 
                      stroke="currentColor" 
                      className={`size-5 ${isLoading ? 'animate-pulse' : ''}`}
                    >
                      <path 
                        strokeLinecap="round" 
                        strokeLinejoin="round" 
                        d="M6 12 3.269 3.125A59.769 59.769 0 0 1 21.485 12 59.768 59.768 0 0 1 3.27 20.875L5.999 12Zm0 0h7.5" 
                      />
                    </svg>
                  </button>
                </div>
              </form>
            </div>
          </div>
        )}
      </div>
    </section>
  )
}