An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor: propagate chat status to tool components instead of using multiple useChat instances
Yoshiki Miura699ed140c9a5
diff --git a/components/related-questions.tsx b/components/related-questions.tsxindex 3d72dcdd..6dcdf687 100644--- a/components/related-questions.tsx+++ b/components/related-questions.tsx@@ -1,7 +1,6 @@'use client'-import { CHAT_ID } from '@/lib/constants'-import { useChat } from '@ai-sdk/react'+import { UseChatHelpers } from '@ai-sdk/react'import { JSONValue } from 'ai'import { ArrowRight } from 'lucide-react'import React from 'react'@@ -15,6 +14,7 @@ export interface RelatedQuestionsProps {onQuerySelect: (query: string) => voidisOpen: booleanonOpenChange: (open: boolean) => void+ status?: UseChatHelpers['status']}interface RelatedQuestionsAnnotation extends Record<string, JSONValue> {@@ -28,11 +28,9 @@ export const RelatedQuestions: React.FC<RelatedQuestionsProps> = ({annotations,onQuerySelect,isOpen,- onOpenChange+ onOpenChange,+ status}) => {- const { status } = useChat({- id: CHAT_ID- })const isLoading = status === 'submitted' || status === 'streaming'if (!annotations) {diff --git a/components/render-message.tsx b/components/render-message.tsxindex 46fc8bf6..25214994 100644--- a/components/render-message.tsx+++ b/components/render-message.tsx@@ -57,6 +57,7 @@ export function RenderMessage({onOpenChange(part.toolInvocation.toolCallId, open)}addToolResult={addToolResult}+ status={status}/>)case 'text':diff --git a/components/retrieve-section.tsx b/components/retrieve-section.tsxindex c3c54488..79030349 100644--- a/components/retrieve-section.tsx+++ b/components/retrieve-section.tsx@@ -4,6 +4,7 @@ import { useArtifact } from '@/components/artifact/artifact-context'import { SearchResults } from '@/components/search-results'import { Section, ToolArgsSection } from '@/components/section'import { SearchResults as SearchResultsType } from '@/lib/types'+import { UseChatHelpers } from '@ai-sdk/react'import { ToolInvocation } from 'ai'import { CollapsibleMessage } from './collapsible-message'import { DefaultSkeleton } from './default-skeleton'@@ -12,14 +13,19 @@ interface RetrieveSectionProps {tool: ToolInvocationisOpen: booleanonOpenChange: (open: boolean) => void+ status?: UseChatHelpers['status']}export function RetrieveSection({tool,isOpen,- onOpenChange+ onOpenChange,+ status}: RetrieveSectionProps) {- const isLoading = tool.state === 'call'+ const isToolLoading = tool.state === 'call'+ const isChatLoading = status === 'submitted' || status === 'streaming'+ const isLoading = isToolLoading || isChatLoading+const data: SearchResultsType =tool.state === 'result' ? tool.result : undefinedconst url = tool.args.url as string | undefineddiff --git a/components/search-section.tsx b/components/search-section.tsxindex b4aac733..abfe8636 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -1,9 +1,8 @@'use client'import { useArtifact } from '@/components/artifact/artifact-context'-import { CHAT_ID } from '@/lib/constants'import type { SearchResults as TypeSearchResults } from '@/lib/types'-import { useChat } from '@ai-sdk/react'+import { UseChatHelpers } from '@ai-sdk/react'import { ToolInvocation } from 'ai'import { CollapsibleMessage } from './collapsible-message'import { SearchSkeleton } from './default-skeleton'@@ -15,16 +14,15 @@ interface SearchSectionProps {tool: ToolInvocationisOpen: booleanonOpenChange: (open: boolean) => void+ status?: UseChatHelpers['status']}export function SearchSection({tool,isOpen,- onOpenChange+ onOpenChange,+ status}: SearchSectionProps) {- const { status } = useChat({- id: CHAT_ID- })const isLoading = status === 'submitted' || status === 'streaming'const isToolLoading = tool.state === 'call'diff --git a/components/tool-section.tsx b/components/tool-section.tsxindex 771b4c28..423d3874 100644--- a/components/tool-section.tsx+++ b/components/tool-section.tsx@@ -1,5 +1,6 @@'use client'+import { UseChatHelpers } from '@ai-sdk/react'import { ToolInvocation } from 'ai'import { QuestionConfirmation } from './question-confirmation'import RetrieveSection from './retrieve-section'@@ -10,6 +11,7 @@ interface ToolSectionProps {tool: ToolInvocationisOpen: booleanonOpenChange: (open: boolean) => void+ status?: UseChatHelpers['status']addToolResult?: (params: { toolCallId: string; result: any }) => void}@@ -17,6 +19,7 @@ export function ToolSection({tool,isOpen,onOpenChange,+ status,addToolResult}: ToolSectionProps) {// Special handling for ask_question tool@@ -61,6 +64,7 @@ export function ToolSection({tool={tool}isOpen={isOpen}onOpenChange={onOpenChange}+ status={status}/>)case 'videoSearch':@@ -69,6 +73,7 @@ export function ToolSection({tool={tool}isOpen={isOpen}onOpenChange={onOpenChange}+ status={status}/>)case 'retrieve':@@ -77,6 +82,7 @@ export function ToolSection({tool={tool}isOpen={isOpen}onOpenChange={onOpenChange}+ status={status}/>)default:diff --git a/components/video-search-section.tsx b/components/video-search-section.tsxindex c016fb1d..95d15254 100644--- a/components/video-search-section.tsx+++ b/components/video-search-section.tsx@@ -1,9 +1,8 @@'use client'import { useArtifact } from '@/components/artifact/artifact-context'-import { CHAT_ID } from '@/lib/constants'import type { SerperSearchResults } from '@/lib/types'-import { useChat } from '@ai-sdk/react'+import { UseChatHelpers } from '@ai-sdk/react'import { ToolInvocation } from 'ai'import { CollapsibleMessage } from './collapsible-message'import { DefaultSkeleton } from './default-skeleton'@@ -14,16 +13,15 @@ interface VideoSearchSectionProps {tool: ToolInvocationisOpen: booleanonOpenChange: (open: boolean) => void+ status?: UseChatHelpers['status']}export function VideoSearchSection({tool,isOpen,- onOpenChange+ onOpenChange,+ status}: VideoSearchSectionProps) {- const { status } = useChat({- id: CHAT_ID- })const isLoading = status === 'submitted' || status === 'streaming'const isToolLoading = tool.state === 'call'