Skip to content

An AI-powered search engine with a generative UI

refactor: standardize useChat hook ID usage

Yoshiki Miurac7a23adfe041
diff --git a/.vscode/settings.json b/.vscode/settings.json
index d292e786..fab9bb98 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -1,8 +1,8 @@
{
- "editor.formatOnSave": true,
- "editor.defaultFormatter": "esbenp.prettier-vscode",
- "cSpell.words": [
- "openai",
- "Tavily"
- ]
-}
\ No newline at end of file
+ "editor.formatOnSave": true,
+ "editor.defaultFormatter": "esbenp.prettier-vscode",
+ "cSpell.words": ["openai", "Tavily"],
+ "editor.codeActionsOnSave": {
+ "source.organizeImports": "always"
+ }
+}
diff --git a/components/chat-share.tsx b/components/chat-share.tsx
index d60f73f9..2eacd71f 100644
--- a/components/chat-share.tsx
+++ b/components/chat-share.tsx
@@ -1,23 +1,24 @@
'use client'
+import { shareChat } from '@/lib/actions/chat'
+import { CHAT_ID } from '@/lib/constants'
+import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
+import { cn } from '@/lib/utils'
+import { useChat } from 'ai/react'
+import { Share } from 'lucide-react'
import { useState, useTransition } from 'react'
+import { toast } from 'sonner'
import { Button } from './ui/button'
-import { Share } from 'lucide-react'
import {
Dialog,
DialogContent,
+ DialogDescription,
DialogFooter,
DialogHeader,
- DialogTrigger,
- DialogDescription,
- DialogTitle
+ DialogTitle,
+ DialogTrigger
} from './ui/dialog'
-import { shareChat } from '@/lib/actions/chat'
-import { toast } from 'sonner'
-import { useCopyToClipboard } from '@/lib/hooks/use-copy-to-clipboard'
import { Spinner } from './ui/spinner'
-import { useChat } from 'ai/react'
-import { cn } from '@/lib/utils'
interface ChatShareProps {
chatId: string
@@ -30,7 +31,7 @@ export function ChatShare({ chatId, className }: ChatShareProps) {
const { copyToClipboard } = useCopyToClipboard({ timeout: 1000 })
const [shareUrl, setShareUrl] = useState('')
const { isLoading } = useChat({
- id: 'chat'
+ id: CHAT_ID
})
const handleShare = async () => {
diff --git a/components/chat.tsx b/components/chat.tsx
index 7fcf43e3..25dd971a 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -1,9 +1,10 @@
'use client'
+import { CHAT_ID } from '@/lib/constants'
import { Message, useChat } from 'ai/react'
+import { toast } from 'sonner'
import { ChatMessages } from './chat-messages'
import { ChatPanel } from './chat-panel'
-import { toast } from 'sonner'
export function Chat({
id,
@@ -25,7 +26,7 @@ export function Chat({
append
} = useChat({
initialMessages: savedMessages,
- id: 'chat',
+ id: CHAT_ID,
body: {
id
},
@@ -37,16 +38,18 @@ export function Chat({
}
})
+ const onQuerySelect = (query: string) => {
+ append({
+ role: 'user',
+ content: query
+ })
+ }
+
return (
<div className="flex flex-col w-full max-w-3xl pt-10 pb-16 mx-auto stretch">
<ChatMessages
messages={messages}
- onQuerySelect={query => {
- append({
- role: 'user',
- content: query
- })
- }}
+ onQuerySelect={onQuerySelect}
isLoading={isLoading}
chatId={id}
/>
diff --git a/components/related-questions.tsx b/components/related-questions.tsx
index a74aafb9..d4adc48c 100644
--- a/components/related-questions.tsx
+++ b/components/related-questions.tsx
@@ -1,12 +1,13 @@
'use client'
+import { CHAT_ID } from '@/lib/constants'
+import { JSONValue } from 'ai'
+import { useChat } from 'ai/react'
+import { ArrowRight, Repeat2 } from 'lucide-react'
import React from 'react'
+import { CollapsibleMessage } from './collapsible-message'
import { Button } from './ui/button'
-import { ArrowRight, Repeat2 } from 'lucide-react'
import { Skeleton } from './ui/skeleton'
-import { JSONValue } from 'ai'
-import { CollapsibleMessage } from './collapsible-message'
-import { useChat } from 'ai/react'
export interface RelatedQuestionsProps {
annotations: JSONValue[]
@@ -29,7 +30,7 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({
onOpenChange
}) => {
const { isLoading } = useChat({
- id: 'chat'
+ id: CHAT_ID
})
if (!annotations) {
diff --git a/components/search-section.tsx b/components/search-section.tsx
index 2285baa7..cdd5d8c5 100644
--- a/components/search-section.tsx
+++ b/components/search-section.tsx
@@ -1,13 +1,14 @@
'use client'
-import { SearchResults } from './search-results'
-import { DefaultSkeleton } from './default-skeleton'
-import { SearchResultsImageSection } from './search-results-image'
-import { Section, ToolArgsSection } from './section'
+import { CHAT_ID } from '@/lib/constants'
import type { SearchResults as TypeSearchResults } from '@/lib/types'
import { ToolInvocation } from 'ai'
-import { CollapsibleMessage } from './collapsible-message'
import { useChat } from 'ai/react'
+import { CollapsibleMessage } from './collapsible-message'
+import { DefaultSkeleton } from './default-skeleton'
+import { SearchResults } from './search-results'
+import { SearchResultsImageSection } from './search-results-image'
+import { Section, ToolArgsSection } from './section'
interface SearchSectionProps {
tool: ToolInvocation
@@ -21,7 +22,7 @@ export function SearchSection({
onOpenChange
}: SearchSectionProps) {
const { isLoading } = useChat({
- id: 'chat'
+ id: CHAT_ID
})
const isToolLoading = tool.state === 'call'
const searchResults: TypeSearchResults =
diff --git a/lib/constants/index.ts b/lib/constants/index.ts
new file mode 100644
index 00000000..27f46d7f
--- /dev/null
+++ b/lib/constants/index.ts
@@ -0,0 +1 @@
+export const CHAT_ID = 'search' as const