Skip to content

An AI-powered search engine with a generative UI

refactor(ui): extract TodoListContent component and enhance todo displays

Yoshiki Miura72d07dc3e759
diff --git a/components/artifact/todo-invocation-content.tsx b/components/artifact/todo-invocation-content.tsx
index b7078da5..0ab40fdf 100644
--- a/components/artifact/todo-invocation-content.tsx
+++ b/components/artifact/todo-invocation-content.tsx
@@ -1,9 +1,8 @@
'use client'
-import { AlertCircle, Check } from 'lucide-react'
+import type { ToolPart } from '@/lib/types/ai'
-import { TodoItem, ToolPart } from '@/lib/types/ai'
-import { cn } from '@/lib/utils'
+import TodoListContent from '../todo-list-content'
interface TodoInvocationContentProps {
part: ToolPart<'todoWrite'> | ToolPart<'todoRead'>
@@ -11,83 +10,30 @@ interface TodoInvocationContentProps {
export function TodoInvocationContent({ part }: TodoInvocationContentProps) {
const todos = part.output?.todos || part.input?.todos || []
- const completedCount =
- part.output?.completedCount ??
- todos.filter(t => t.status === 'completed').length
- const totalCount = part.output?.totalCount ?? todos.length
-
- const getStatusIcon = (status: TodoItem['status']) => {
- switch (status) {
- case 'completed':
- return <Check className="h-4 w-4 text-green-600 flex-shrink-0" />
- case 'in_progress':
- return (
- <div className="relative h-4 w-4 flex items-center justify-center flex-shrink-0">
- <div className="h-2 w-2 bg-blue-600 rounded-full animate-ping absolute" />
- <div className="h-2 w-2 bg-blue-600 rounded-full" />
- </div>
- )
- default:
- return (
- <div className="h-4 w-4 flex items-center justify-center flex-shrink-0">
- <div className="h-2 w-2 bg-muted-foreground rounded-full" />
- </div>
- )
- }
- }
+ const completedCount = part.output?.completedCount
+ const totalCount = part.output?.totalCount
if (part.state === 'output-error') {
return (
- <div className="flex items-center gap-2 p-4 text-red-600">
- <AlertCircle className="size-4" />
- <span>Error: {part.errorText || 'Failed to process todos'}</span>
- </div>
+ <TodoListContent
+ errorText={part.errorText || 'Failed to process todos'}
+ />
)
}
- return (
- <div className="space-y-4">
- {/* Summary */}
- <div className="flex items-center justify-between text-sm text-muted-foreground">
- <span>
- {part.output && 'message' in part.output
- ? part.output.message
- : part.output && 'summary' in part.output
- ? part.output.summary
- : 'Todo List'}
- </span>
- <span className="font-medium whitespace-nowrap">
- ({completedCount}/{totalCount})
- </span>
- </div>
+ const summaryOrMessage =
+ (part.output && 'message' in part.output && part.output.message) ||
+ (part.output && 'summary' in part.output && part.output.summary) ||
+ undefined
- {/* Todo List */}
- <div className="space-y-2">
- {todos.map((todo, index) => (
- <div
- key={todo.id || index}
- className="flex items-center justify-between gap-2 p-3 rounded-lg border bg-card"
- >
- <div className="flex items-center gap-2">
- {getStatusIcon(todo.status)}
- <p
- className={cn(
- 'text-sm',
- todo.status === 'completed' && 'text-muted-foreground'
- )}
- >
- {todo.content}
- </p>
- </div>
- </div>
- ))}
- </div>
-
- {todos.length === 0 && (
- <div className="text-center py-8 text-muted-foreground">
- No todos available
- </div>
- )}
- </div>
+ return (
+ <TodoListContent
+ todos={todos}
+ message={summaryOrMessage}
+ completedCount={completedCount}
+ totalCount={totalCount}
+ showSummary={true}
+ itemVariant="plain"
+ />
)
}
diff --git a/components/process-header.tsx b/components/process-header.tsx
index d7a076f1..bf0e76f5 100644
--- a/components/process-header.tsx
+++ b/components/process-header.tsx
@@ -27,7 +27,7 @@ export function ProcessHeader({
onClick={onInspect}
aria-expanded={ariaExpanded}
className={cn(
- 'flex items-center justify-between w-full min-w-0 text-left text-sm text-muted-foreground hover:text-foreground transition-colors cursor-pointer',
+ 'flex items-center justify-between w-full min-w-0 text-left text-xs text-muted-foreground hover:text-foreground transition-colors cursor-pointer',
isLoading && 'animate-pulse',
className
)}
diff --git a/components/todo-list-content.tsx b/components/todo-list-content.tsx
new file mode 100644
index 00000000..36dbbbb4
--- /dev/null
+++ b/components/todo-list-content.tsx
@@ -0,0 +1,109 @@
+'use client'
+
+import { AlertCircle, Check } from 'lucide-react'
+
+import type { TodoItem } from '@/lib/types/ai'
+import { cn } from '@/lib/utils'
+
+export type TodoListContentProps = {
+ todos?: TodoItem[]
+ message?: string
+ summary?: string
+ completedCount?: number
+ totalCount?: number
+ errorText?: string
+ showSummary?: boolean
+ itemVariant?: 'bordered' | 'plain'
+ className?: string
+}
+
+export function TodoListContent({
+ todos = [],
+ message,
+ summary,
+ completedCount,
+ totalCount,
+ errorText,
+ showSummary = true,
+ itemVariant = 'bordered',
+ className
+}: TodoListContentProps) {
+ const completed =
+ completedCount ?? todos.filter(t => t.status === 'completed').length
+ const total = totalCount ?? todos.length
+
+ const getStatusIcon = (status: TodoItem['status']) => {
+ switch (status) {
+ case 'completed':
+ return <Check className="h-4 w-4 text-green-600 flex-shrink-0" />
+ case 'in_progress':
+ return (
+ <div className="relative h-4 w-4 flex items-center justify-center flex-shrink-0">
+ <div className="h-2 w-2 bg-blue-600 rounded-full animate-ping absolute" />
+ <div className="h-2 w-2 bg-blue-600 rounded-full" />
+ </div>
+ )
+ default:
+ return (
+ <div className="h-4 w-4 flex items-center justify-center flex-shrink-0">
+ <div className="h-2 w-2 bg-muted-foreground rounded-full" />
+ </div>
+ )
+ }
+ }
+
+ if (errorText) {
+ return (
+ <div className="flex items-center gap-2 p-4 text-red-600">
+ <AlertCircle className="size-4" />
+ <span>Error: {errorText}</span>
+ </div>
+ )
+ }
+
+ return (
+ <div className={cn('space-y-3', className)}>
+ {showSummary && (
+ <div className="flex items-center justify-between text-sm text-muted-foreground">
+ <span>{message || summary || 'Todo List'}</span>
+ <span className="font-medium whitespace-nowrap">
+ ({completed}/{total})
+ </span>
+ </div>
+ )}
+
+ <ul className="space-y-2">
+ {todos.map((todo, index) => (
+ <li key={todo.id || index}>
+ <div
+ className={cn(
+ 'flex items-center gap-2',
+ itemVariant === 'bordered'
+ ? 'justify-between p-3 rounded-lg border bg-card'
+ : 'py-1'
+ )}
+ >
+ {getStatusIcon(todo.status)}
+ <p
+ className={cn(
+ 'text-sm',
+ todo.status === 'completed' && 'text-muted-foreground'
+ )}
+ >
+ {todo.content}
+ </p>
+ </div>
+ </li>
+ ))}
+ </ul>
+
+ {todos.length === 0 && (
+ <div className="text-center py-8 text-muted-foreground">
+ No todos available
+ </div>
+ )}
+ </div>
+ )
+}
+
+export default TodoListContent
diff --git a/components/tool-section.tsx b/components/tool-section.tsx
index ba619538..948d33bd 100644
--- a/components/tool-section.tsx
+++ b/components/tool-section.tsx
@@ -92,6 +92,8 @@ export function ToolSection({
output={tool.output}
errorText={tool.errorText}
toolCallId={tool.toolCallId}
+ isOpen={isOpen}
+ onOpenChange={onOpenChange}
/>
)
case 'tool-todoRead':
@@ -103,6 +105,8 @@ export function ToolSection({
output={tool.output}
errorText={tool.errorText}
toolCallId={tool.toolCallId}
+ isOpen={isOpen}
+ onOpenChange={onOpenChange}
/>
)
default:
diff --git a/components/tool-todo-display.tsx b/components/tool-todo-display.tsx
index c5bf0ece..007e6fa8 100644
--- a/components/tool-todo-display.tsx
+++ b/components/tool-todo-display.tsx
@@ -3,6 +3,9 @@ import { Check, ListTodo } from 'lucide-react'
import { Part, TodoItem } from '@/lib/types/ai'
import { useArtifact } from './artifact/artifact-context'
+import { CollapsibleMessage } from './collapsible-message'
+import ProcessHeader from './process-header'
+import TodoListContent from './todo-list-content'
interface ToolTodoDisplayProps {
tool: 'todoWrite' | 'todoRead'
@@ -22,13 +25,17 @@ interface ToolTodoDisplayProps {
}
errorText?: string
toolCallId: string
+ isOpen?: boolean
+ onOpenChange?: (open: boolean) => void
}
export function ToolTodoDisplay({
tool,
state,
output,
- toolCallId
+ toolCallId,
+ isOpen = false,
+ onOpenChange
}: ToolTodoDisplayProps) {
const { open: openArtifact } = useArtifact()
// Calculate counts for display
@@ -42,47 +49,84 @@ export function ToolTodoDisplay({
const isLoading = state === 'input-streaming' || state === 'input-available'
- const handleClick = () => {
- if (state === 'output-available' && output) {
- const partType = tool === 'todoWrite' ? 'tool-todoWrite' : 'tool-todoRead'
- const part: Part = {
- type: partType as 'tool-todoWrite' | 'tool-todoRead',
- toolCallId: toolCallId,
- state: state,
- input: { todos: output.todos || [] },
- output: output
- }
- openArtifact(part)
+ const openInspector = () => {
+ if (!(state === 'output-available' && output)) return
+ const partType = tool === 'todoWrite' ? 'tool-todoWrite' : 'tool-todoRead'
+ const part: Part = {
+ type: partType as 'tool-todoWrite' | 'tool-todoRead',
+ toolCallId,
+ state,
+ input: { todos: output.todos || [] },
+ output
}
+ openArtifact(part)
}
- return (
- <div
- className={`rounded-lg border bg-card ${isLoading ? 'animate-pulse' : ''} ${
- state === 'output-available' && output ? 'cursor-pointer' : ''
- }`}
- onClick={handleClick}
- >
- <div className="flex items-center justify-between gap-2 px-3 py-2 text-sm">
- <div className="flex items-center gap-2">
- <ListTodo className="size-4 text-muted-foreground" />
- <span className="text-muted-foreground">
+ const header = (
+ <ProcessHeader
+ onInspect={openInspector}
+ isLoading={isLoading}
+ label={
+ <span className="inline-flex items-center gap-2 min-w-0 overflow-hidden">
+ <ListTodo className="size-4 text-muted-foreground shrink-0" />
+ <span className="truncate">
{state === 'output-available'
- ? output?.message || 'Todo list updated'
+ ? tool === 'todoRead'
+ ? (output as any)?.summary || output?.message || 'Todo list'
+ : output?.message || 'Updated tasks'
: tool === 'todoWrite'
? 'Updating tasks...'
: 'Reading tasks...'}
</span>
- </div>
- {state === 'output-available' && totalCount > 0 && (
- <span className="flex items-center gap-1 text-xs text-muted-foreground">
- {completedCount === totalCount && (
+ </span>
+ }
+ meta={
+ state === 'output-available' && totalCount > 0 ? (
+ <span className="flex items-center gap-1 text-muted-foreground">
+ {completedCount === totalCount ? (
<Check className="size-4 text-green-500" />
- )}
- ({completedCount}/{totalCount})
+ ) : null}
+ <span className="text-xs">
+ ({completedCount}/{totalCount})
+ </span>
</span>
- )}
- </div>
- </div>
+ ) : undefined
+ }
+ ariaExpanded={isOpen}
+ />
+ )
+
+ return (
+ <CollapsibleMessage
+ role="assistant"
+ isCollapsible={true}
+ header={header}
+ isOpen={isOpen}
+ onOpenChange={onOpenChange}
+ showBorder={true}
+ showIcon={false}
+ variant="default"
+ showSeparator={false}
+ >
+ {state === 'output-available' ? (
+ <TodoListContent
+ todos={output?.todos}
+ message={
+ tool === 'todoRead'
+ ? (output as any)?.summary || output?.message
+ : output?.message
+ }
+ completedCount={completedCount}
+ totalCount={totalCount}
+ showSummary={false}
+ itemVariant="plain"
+ className="pb-1"
+ />
+ ) : state === 'output-error' ? (
+ <div className="px-3 pb-3 text-xs text-destructive">{`Todo tool failed${
+ (output as any)?.message ? `: ${(output as any).message}` : ''
+ }`}</div>
+ ) : null}
+ </CollapsibleMessage>
)
}