An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
refactor(ui): update FetchSection styling to match other tool sections
Yoshiki Miurac26049a47134
diff --git a/components/__tests__/research-process-section.test.tsx b/components/__tests__/research-process-section.test.tsxindex 3a912217..34109ca2 100644--- a/components/__tests__/research-process-section.test.tsx+++ b/components/__tests__/research-process-section.test.tsx@@ -1,9 +1,10 @@-import { render, screen, fireEvent } from '@testing-library/react'-import { describe, expect, test, vi, beforeEach, Mock } from 'vitest'-import type { ReasoningPart } from '@ai-sdk/provider-utils'import React from 'react'-import type { UIMessage, ToolPart } from '@/lib/types/ai'+import type { ReasoningPart } from '@ai-sdk/provider-utils'+import { fireEvent, render, screen } from '@testing-library/react'+import { beforeEach, describe, expect, Mock, test, vi } from 'vitest'++import type { ToolPart, UIMessage } from '@/lib/types/ai'import { ResearchProcessSection } from '../research-process-section'@@ -47,7 +48,7 @@ describe('ResearchProcessSection', () => {type: 'reasoning',text: 'Test reasoning'}-+const message: UIMessage = {role: 'assistant',content: '',@@ -74,7 +75,7 @@ describe('ResearchProcessSection', () => {input: {},state: 'output-available'}-+const message: UIMessage = {role: 'assistant',content: '',@@ -99,12 +100,12 @@ describe('ResearchProcessSection', () => {type: 'reasoning',text: ''}-+const validReasoningPart: ReasoningPart = {type: 'reasoning',text: 'Valid reasoning'}-+const message: UIMessage = {role: 'assistant',content: '',@@ -131,11 +132,16 @@ describe('ResearchProcessSection', () => {test('splits parts by text correctly', () => {const parts = [{ type: 'reasoning', text: 'First reasoning' } as ReasoningPart,- { type: 'tool-search', toolCallId: 'tool-1', input: {}, state: 'output-available' } as ToolPart,+ {+ type: 'tool-search',+ toolCallId: 'tool-1',+ input: {},+ state: 'output-available'+ } as ToolPart,{ type: 'text', text: 'Text separator' },{ type: 'reasoning', text: 'Second reasoning' } as ReasoningPart]-+const message: UIMessage = {role: 'assistant',content: '',@@ -162,11 +168,26 @@ describe('ResearchProcessSection', () => {test('groups consecutive tool parts of same type', () => {const parts = [- { type: 'tool-search', toolCallId: 'tool-1', input: {}, state: 'output-available' } as ToolPart,- { type: 'tool-search', toolCallId: 'tool-2', input: {}, state: 'output-available' } as ToolPart,- { type: 'tool-fetch', toolCallId: 'tool-3', input: {}, state: 'output-available' } as ToolPart+ {+ type: 'tool-search',+ toolCallId: 'tool-1',+ input: {},+ state: 'output-available'+ } as ToolPart,+ {+ type: 'tool-search',+ toolCallId: 'tool-2',+ input: {},+ state: 'output-available'+ } as ToolPart,+ {+ type: 'tool-fetch',+ toolCallId: 'tool-3',+ input: {},+ state: 'output-available'+ } as ToolPart]-+const message: UIMessage = {role: 'assistant',content: '',@@ -194,7 +215,7 @@ describe('ResearchProcessSection', () => {{ type: 'reasoning', text: 'First' } as ReasoningPart,{ type: 'reasoning', text: 'Second' } as ReasoningPart]-+const message: UIMessage = {role: 'assistant',content: '',@@ -212,16 +233,16 @@ describe('ResearchProcessSection', () => {)const buttons = screen.getAllByRole('button')-+// Click first button to openfireEvent.click(buttons[0])-+// Should call onOpenChangeexpect(mockOnOpenChange).toHaveBeenCalled()-+// Update mock to return true for the clicked item- mockGetIsOpen.mockImplementation((id) => id.includes('reasoning-0-0-0'))-+ mockGetIsOpen.mockImplementation(id => id.includes('reasoning-0-0-0'))+rerender(<ResearchProcessSectionmessage={message}@@ -237,7 +258,7 @@ describe('ResearchProcessSection', () => {const singlePart = [{ type: 'reasoning', text: 'Single reasoning' } as ReasoningPart]-+const message: UIMessage = {role: 'assistant',content: '',@@ -272,7 +293,7 @@ describe('ResearchProcessSection', () => {{ type: 'text', text: 'Text' },{ type: 'reasoning', text: 'Second' } as ReasoningPart]-+const message: UIMessage = {role: 'assistant',content: '',@@ -323,9 +344,7 @@ describe('ResearchProcessSection', () => {const message: UIMessage = {role: 'assistant',content: '',- parts: [- { type: 'reasoning', text: 'Original' } as ReasoningPart- ]+ parts: [{ type: 'reasoning', text: 'Original' } as ReasoningPart]}const overrideParts = [@@ -353,10 +372,8 @@ describe('ResearchProcessSection', () => {})test('handles data parts correctly', () => {- const parts = [- { type: 'data-test', data: 'test' }- ]-+ const parts = [{ type: 'data-test', data: 'test' }]+const message: UIMessage = {role: 'assistant',content: '',@@ -374,7 +391,9 @@ describe('ResearchProcessSection', () => {)// Data parts should not render anything- expect(container.firstChild?.firstChild?.firstChild?.firstChild).toBeNull()+ expect(+ container.firstChild?.firstChild?.firstChild?.firstChild+ ).toBeNull()})})@@ -386,7 +405,7 @@ describe('ResearchProcessSection', () => {input: {},state: 'output-available'}-+const message: UIMessage = {role: 'assistant',content: '',@@ -414,7 +433,7 @@ describe('ResearchProcessSection', () => {input: {},state: 'output-available'}-+const message: UIMessage = {role: 'assistant',content: '',@@ -435,4 +454,4 @@ describe('ResearchProcessSection', () => {expect(screen.getByTestId('tool-section')).toBeInTheDocument()})})-})\ No newline at end of file+})diff --git a/components/fetch-preview.tsx b/components/fetch-preview.tsxdeleted file mode 100644index 71d1ab27..00000000--- a/components/fetch-preview.tsx+++ /dev/null@@ -1,117 +0,0 @@-import { ExternalLink, Globe } from 'lucide-react'--interface FetchPreviewProps {- url: string- title?: string- contentLength?: number- status: 'fetching' | 'success' | 'error'- error?: string-}--export function FetchPreview({- url,- title,- contentLength,- status,- error-}: FetchPreviewProps) {- const getPageTitle = () => {- if (title) return title- try {- const domain = new URL(url).hostname- return domain.replace('www.', '')- } catch {- return url- }- }-- const getFavicon = () => {- try {- const domain = new URL(url).hostname- return `https://www.google.com/s2/favicons?domain=${domain}&sz=16`- } catch {- return null- }- }-- if (status === 'fetching') {- return (- <div className="p-3 bg-card border border-border rounded-lg">- <div className="flex items-center justify-between gap-2 w-full">- <div className="flex items-center gap-2 min-w-0 flex-1">- <Globe className="w-4 h-4 text-muted-foreground animate-pulse shrink-0" />- <span- className="text-sm text-foreground font-medium block truncate min-w-0"- title={getPageTitle()}- >- {getPageTitle()}- </span>- </div>- <span className="text-xs text-muted-foreground animate-pulse whitespace-nowrap">- Retrieving...- </span>- </div>- </div>- )- }-- if (status === 'error') {- return (- <div className="p-3 bg-card border border-border rounded-lg">- <div className="flex items-center gap-2 w-full">- <Globe className="w-4 h-4 text-muted-foreground shrink-0" />- <span className="text-sm text-destructive block flex-1 truncate min-w-0">- {error}- </span>- </div>- </div>- )- }-- const favicon = getFavicon()-- return (- <div- className="p-3 bg-card border border-border rounded-lg cursor-pointer hover:border-border/80 transition-colors"- onClick={() => window.open(url, '_blank', 'noopener,noreferrer')}- >- <div className="flex items-center justify-between gap-2 w-full">- <div className="flex items-center gap-2 min-w-0 flex-1">- {favicon && (- // eslint-disable-next-line @next/next/no-img-element- <img- src={favicon}- alt="Favicon"- className="w-4 h-4 shrink-0"- onError={e => {- e.currentTarget.style.display = 'none'- e.currentTarget.nextElementSibling?.classList.remove('hidden')- }}- />- )}- <Globe- className={`w-4 h-4 text-muted-foreground shrink-0 ${- favicon ? 'hidden' : ''- }`}- />- <span- className="text-sm text-foreground font-medium block truncate min-w-0"- title={getPageTitle()}- >- {getPageTitle()}- </span>- </div>- <div className="flex items-center gap-2 shrink-0 ml-2">- {contentLength && contentLength > 0 && (- <span className="text-xs text-muted-foreground whitespace-nowrap">- {contentLength > 1000- ? `${Math.round(contentLength / 1000)}k chars`- : `${contentLength} chars`}- </span>- )}- <ExternalLink className="w-4 h-4 text-muted-foreground shrink-0" />- </div>- </div>- </div>- )-}diff --git a/components/fetch-section.tsx b/components/fetch-section.tsxindex 4181d228..0ee1d832 100644--- a/components/fetch-section.tsx+++ b/components/fetch-section.tsx@@ -1,11 +1,13 @@'use client'import { UseChatHelpers } from '@ai-sdk/react'+import { AlertCircle, Check, ExternalLink, Globe } from 'lucide-react'import { SearchResults as SearchResultsType } from '@/lib/types'import type { ToolPart, UIDataTypes, UIMessage, UITools } from '@/lib/types/ai'+import { cn } from '@/lib/utils'-import { FetchPreview } from './fetch-preview'+import ProcessHeader from './process-header'interface FetchSectionProps {tool: ToolPart<'fetch'>@@ -19,14 +21,17 @@ interface FetchSectionProps {export function FetchSection({tool,- isOpen,- onOpenChange,+ isOpen, // Required by ToolSection interface+ onOpenChange, // Required by ToolSection interfacestatus,borderless = false,isFirst = false,isLast = false}: FetchSectionProps) {const url = tool.input?.url+ const isLoading = status === 'submitted' || status === 'streaming'+ const isToolLoading =+ tool.state === 'input-streaming' || tool.state === 'input-available'// Determine the status based on tool output availabilitylet displayStatus: 'fetching' | 'success' | 'error' = 'fetching'@@ -34,13 +39,11 @@ export function FetchSection({let title: string | undefinedlet contentLength: number | undefined- // Check tool state first+ // Check tool stateif (tool.state === 'output-error') {- // Error statedisplayStatus = 'error'error = tool.errorText || 'Failed to retrieve content'} else if (!tool.output) {- // Still fetchingdisplayStatus = 'fetching'} else {// Success state - we have output@@ -55,8 +58,64 @@ export function FetchSection({}}+ // Get page title for display+ const getPageTitle = () => {+ if (title) return title+ if (!url) return 'Unknown URL'+ try {+ const domain = new URL(url).hostname+ return domain.replace('www.', '')+ } catch {+ return url+ }+ }++ // Handle click to open URL+ const handleClick = () => {+ if (url && displayStatus === 'success') {+ window.open(url, '_blank', 'noopener,noreferrer')+ }+ }++ const header = (+ <ProcessHeader+ onInspect={handleClick}+ isLoading={isLoading && isToolLoading}+ label={+ <div className="flex items-center gap-2 min-w-0 overflow-hidden">+ <Globe className="h-4 w-4 shrink-0 text-muted-foreground" />+ <span className="truncate block min-w-0 max-w-full">+ {getPageTitle()}+ </span>+ </div>+ }+ meta={+ displayStatus === 'success' && contentLength ? (+ <>+ <Check size={16} className="text-green-500" />+ <span>+ {contentLength > 1000+ ? `${Math.round(contentLength / 1000)}k chars`+ : `${contentLength} chars`}+ </span>+ </>+ ) : displayStatus === 'error' ? (+ <>+ <AlertCircle size={16} className="text-destructive" />+ <span>{error}</span>+ </>+ ) : isToolLoading ? (+ <span className="animate-pulse">Retrieving...</span>+ ) : undefined+ }+ className={cn(+ displayStatus === 'success' && 'hover:text-foreground cursor-pointer'+ )}+ />+ )+return (- <div className="relative w-full">+ <div className="relative">{/* Rails for header - show based on position */}{borderless && (<>@@ -68,13 +127,27 @@ export function FetchSection({)}</>)}- <FetchPreview- url={url || ''}- title={title}- contentLength={contentLength}- status={displayStatus}- error={error}- />+ {/* Header only section - no collapsible body */}+ <div+ className={cn(+ 'rounded-lg',+ !borderless && 'bg-card border border-border'+ )}+ >+ <div className="flex items-center gap-2 p-3">+ <div className="flex-1 min-w-0">{header}</div>+ {displayStatus === 'success' && (+ <button+ type="button"+ onClick={handleClick}+ className="shrink-0 p-1 hover:bg-accent rounded transition-colors"+ aria-label="Open in new tab"+ >+ <ExternalLink className="h-4 w-4 text-muted-foreground" />+ </button>+ )}+ </div>+ </div></div>)}diff --git a/components/research-process-section.tsx b/components/research-process-section.tsxindex db7870c4..281fe717 100644--- a/components/research-process-section.tsx+++ b/components/research-process-section.tsx@@ -309,10 +309,7 @@ export function ResearchProcessSection({return (<div key={`${messageId}-seg-${sidx}`} className={containerClass}>{groups.map((grp, gidx) => (- <div- key={`${messageId}-grp-${sidx}-${gidx}`}- className={cn('space-y-1')}- >+ <div key={`${messageId}-grp-${sidx}-${gidx}`}>{grp.map((part, pidx) => {const partId = isToolPart(part)? part.toolCallId