Skip to content

An AI-powered search engine with a generative UI

refactor: Update Chat component to include query parameter

Yoshiki Miura062816d2b683
diff --git a/app/search/page.tsx b/app/search/page.tsx
index a48bcb59..9cef30d9 100644
--- a/app/search/page.tsx
+++ b/app/search/page.tsx
@@ -13,6 +13,5 @@ export default async function SearchPage(props: {
}
const id = generateId()
- return <Chat id={id} />
- // return <Chat initialInput={searchParams.q} />
+ return <Chat id={id} query={q} />
}
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index cd935ece..33d98261 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -1,6 +1,6 @@
'use client'
-import { useState, useRef } from 'react'
+import { useState, useRef, useEffect } from 'react'
import { useRouter } from 'next/navigation'
import { cn } from '@/lib/utils'
import { Button } from './ui/button'
@@ -19,6 +19,7 @@ interface ChatPanelProps {
setMessages: (messages: Message[]) => void
query?: string
stop: () => void
+ append: (message: any) => void
}
export function ChatPanel({
@@ -29,12 +30,13 @@ export function ChatPanel({
messages,
setMessages,
query,
- stop
+ stop,
+ append
}: ChatPanelProps) {
const [showEmptyScreen, setShowEmptyScreen] = useState(false)
const router = useRouter()
const inputRef = useRef<HTMLTextAreaElement>(null)
-
+ const isFirstRender = useRef(true)
const [isComposing, setIsComposing] = useState(false) // Composition state
const [enterDisabled, setEnterDisabled] = useState(false) // Disable Enter after composition ends
@@ -53,9 +55,17 @@ export function ChatPanel({
router.push('/')
}
- if (query && query.trim().length > 0) {
- return null
- }
+ // if query is not empty, submit the query
+ useEffect(() => {
+ if (isFirstRender.current && query && query.trim().length > 0) {
+ append({
+ role: 'user',
+ content: query
+ })
+ isFirstRender.current = false
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [query])
return (
<div
diff --git a/components/chat.tsx b/components/chat.tsx
index bece7eaf..0768b8e5 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -6,10 +6,12 @@ import { ChatPanel } from './chat-panel'
export function Chat({
id,
- savedMessages = []
+ savedMessages = [],
+ query
}: {
id: string
savedMessages?: Message[]
+ query?: string
}) {
const {
messages,
@@ -52,6 +54,8 @@ export function Chat({
messages={messages}
setMessages={setMessages}
stop={stop}
+ query={query}
+ append={append}
/>
</div>
)