Skip to content

An AI-powered search engine with a generative UI

Add follow-up feature to action.tsx and Chat component

Yoshiki Miurae2911989be5d
diff --git a/app/action.tsx b/app/action.tsx
index 6723a442..c5c03175 100644
--- a/app/action.tsx
+++ b/app/action.tsx
@@ -197,6 +197,12 @@ async function submit(formData?: FormData, skip?: boolean) {
role: 'assistant',
content: JSON.stringify(relatedQueries),
type: 'related'
+ },
+ {
+ id: groupeId,
+ role: 'assistant',
+ content: 'followup',
+ type: 'followup'
}
]
})
@@ -337,21 +343,37 @@ export const getUIStateFromAIState = (aiState: Chat) => {
</Section>
)
}
- }
- case 'tool':
- const toolOutput = JSON.parse(content)
- const isCollapsed = createStreamableValue()
- isCollapsed.done(true)
- const searchResults = createStreamableValue()
- searchResults.done(JSON.stringify(toolOutput))
- switch (name) {
- case 'search':
+ case 'followup':
return {
id,
- component: <SearchSection result={searchResults.value} />,
- isCollapsed: isCollapsed.value
+ component: (
+ <Section title="Follow-up" className="pb-8">
+ <FollowupPanel />
+ </Section>
+ )
}
}
+ case 'tool':
+ try {
+ const toolOutput = JSON.parse(content)
+ const isCollapsed = createStreamableValue()
+ isCollapsed.done(true)
+ const searchResults = createStreamableValue()
+ searchResults.done(JSON.stringify(toolOutput))
+ switch (name) {
+ case 'search':
+ return {
+ id,
+ component: <SearchSection result={searchResults.value} />,
+ isCollapsed: isCollapsed.value
+ }
+ }
+ } catch (error) {
+ return {
+ id,
+ component: null
+ }
+ }
default:
return {
id,
diff --git a/components/chat.tsx b/components/chat.tsx
index fddcae2a..fe55d7c3 100644
--- a/components/chat.tsx
+++ b/components/chat.tsx
@@ -27,7 +27,7 @@ export function Chat({ id }: ChatProps) {
console.log('refresh')
router.refresh()
}
- }, [aiState, router])
+ }, [aiState, messages, router])
return (
<div className="px-8 sm:px-12 pt-6 md:pt-8 pb-14 md:pb-24 max-w-3xl mx-auto flex flex-col space-y-3 md:space-y-4">
diff --git a/components/history-item.tsx b/components/history-item.tsx
index 0aa391ad..81296b92 100644
--- a/components/history-item.tsx
+++ b/components/history-item.tsx
@@ -1,37 +1,41 @@
+'use client'
+
import React from 'react'
import Link from 'next/link'
+import { usePathname } from 'next/navigation'
import { Chat } from '@/lib/types'
+import { cn } from '@/lib/utils'
type HistoryItemProps = {
chat: Chat
}
-const formatDateWithTime = (dateString: string) => {
- const date = new Date(dateString)
+const formatDateWithTime = (date: Date | string) => {
+ const parsedDate = new Date(date)
const now = new Date()
const yesterday = new Date()
yesterday.setDate(yesterday.getDate() - 1)
if (
- date.getDate() === now.getDate() &&
- date.getMonth() === now.getMonth() &&
- date.getFullYear() === now.getFullYear()
+ parsedDate.getDate() === now.getDate() &&
+ parsedDate.getMonth() === now.getMonth() &&
+ parsedDate.getFullYear() === now.getFullYear()
) {
- return `Today, ${date.getHours()}:${date
+ return `Today, ${parsedDate.getHours()}:${parsedDate
.getMinutes()
.toString()
.padStart(2, '0')}`
} else if (
- date.getDate() === yesterday.getDate() &&
- date.getMonth() === yesterday.getMonth() &&
- date.getFullYear() === yesterday.getFullYear()
+ parsedDate.getDate() === yesterday.getDate() &&
+ parsedDate.getMonth() === yesterday.getMonth() &&
+ parsedDate.getFullYear() === yesterday.getFullYear()
) {
- return `Yesterday, ${date.getHours()}:${date
+ return `Yesterday, ${parsedDate.getHours()}:${parsedDate
.getMinutes()
.toString()
.padStart(2, '0')}`
} else {
- return date.toLocaleString('en-US', {
+ return parsedDate.toLocaleString('en-US', {
year: 'numeric',
month: 'long',
day: 'numeric',
@@ -42,16 +46,22 @@ const formatDateWithTime = (dateString: string) => {
}
const HistoryItem: React.FC<HistoryItemProps> = ({ chat }) => {
+ const pathname = usePathname()
+ const isActive = pathname === chat.path
+
return (
<Link
href={chat.path}
- className="flex flex-col hover:bg-muted cursor-pointer p-2 rounded"
+ className={cn(
+ 'flex flex-col hover:bg-muted cursor-pointer p-2 rounded',
+ isActive ? 'bg-muted/50' : ''
+ )}
>
<div className="text-xs font-medium truncate select-none">
{chat.title}
</div>
<div className="text-xs text-muted-foreground">
- {formatDateWithTime(chat.createdAt as string)}
+ {formatDateWithTime(chat.createdAt)}
</div>
</Link>
)
diff --git a/lib/types/index.ts b/lib/types/index.ts
index 969b3f87..850d3c31 100644
--- a/lib/types/index.ts
+++ b/lib/types/index.ts
@@ -33,4 +33,5 @@ export type AIMessage = {
| 'input'
| 'input_related'
| 'tool'
+ | 'followup'
}