Skip to content

An AI-powered search engine with a generative UI

Add AppStateContext to manage and share isGenerating state

Yoshiki Miura79e613fe91f0
diff --git a/app/layout.tsx b/app/layout.tsx
index d1b3caf1..29438228 100644
--- a/app/layout.tsx
+++ b/app/layout.tsx
@@ -7,6 +7,7 @@ import Header from '@/components/header'
import Footer from '@/components/footer'
import { Sidebar } from '@/components/sidebar'
import { Toaster } from '@/components/ui/sonner'
+import { AppStateProvider } from '@/lib/utils/app-state'
const fontSans = FontSans({
subsets: ['latin'],
@@ -54,11 +55,13 @@ export default function RootLayout({
enableSystem
disableTransitionOnChange
>
- <Header />
- {children}
- <Sidebar />
- <Footer />
- <Toaster />
+ <AppStateProvider>
+ <Header />
+ {children}
+ <Sidebar />
+ <Footer />
+ <Toaster />
+ </AppStateProvider>
</ThemeProvider>
</body>
</html>
diff --git a/components/chat-panel.tsx b/components/chat-panel.tsx
index 7048a35d..2c24735c 100644
--- a/components/chat-panel.tsx
+++ b/components/chat-panel.tsx
@@ -3,7 +3,7 @@
import { useEffect, useState, useRef } from 'react'
import { useRouter } from 'next/navigation'
import type { AI, UIState } from '@/app/actions'
-import { useUIState, useActions } from 'ai/rsc'
+import { useUIState, useActions, useAIState } from 'ai/rsc'
import { cn } from '@/lib/utils'
import { UserMessage } from './user-message'
import { Button } from './ui/button'
@@ -11,6 +11,7 @@ import { ArrowRight, Plus } from 'lucide-react'
import { EmptyScreen } from './empty-screen'
import Textarea from 'react-textarea-autosize'
import { nanoid } from 'ai'
+import { useAppState } from '@/lib/utils/app-state'
interface ChatPanelProps {
messages: UIState
@@ -21,6 +22,8 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
const [input, setInput] = useState('')
const [showEmptyScreen, setShowEmptyScreen] = useState(false)
const [, setMessages] = useUIState<typeof AI>()
+ const [aiMessage] = useAIState<typeof AI>()
+ const { isGenerating, setIsGenerating } = useAppState()
const { submit } = useActions()
const router = useRouter()
const inputRef = useRef<HTMLTextAreaElement>(null)
@@ -28,6 +31,7 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
async function handleQuerySubmit(query: string, formData?: FormData) {
setInput(query)
+ setIsGenerating(true)
// Add user message to UI state
setMessages(currentMessages => [
@@ -62,8 +66,16 @@ export function ChatPanel({ messages, query }: ChatPanelProps) {
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [query])
+ useEffect(() => {
+ const lastMessage = aiMessage.messages.slice(-1)[0]
+ if (lastMessage?.type === 'followup') {
+ setIsGenerating(false)
+ }
+ }, [aiMessage, setIsGenerating])
+
// Clear messages
const handleClear = () => {
+ setIsGenerating(false)
router.push('/')
}
diff --git a/components/history.tsx b/components/history.tsx
index a7e99672..a1e92a37 100644
--- a/components/history.tsx
+++ b/components/history.tsx
@@ -15,6 +15,7 @@ import { cn } from '@/lib/utils'
import { History as HistoryIcon } from 'lucide-react'
import { Suspense } from 'react'
import { HistorySkeleton } from './history-skelton'
+import { useAppState } from '@/lib/utils/app-state'
type HistoryProps = {
location: 'sidebar' | 'header'
@@ -24,6 +25,7 @@ type HistoryProps = {
export function History({ location, children }: HistoryProps) {
const router = useRouter()
const [isPending, startTransition] = useTransition()
+ const { isGenerating, setIsGenerating } = useAppState()
const onOpenChange = (open: boolean) => {
if (open) {
@@ -42,6 +44,7 @@ export function History({ location, children }: HistoryProps) {
className={cn({
'rounded-full text-foreground/30': location === 'sidebar'
})}
+ disabled={isGenerating}
>
{location === 'header' ? <Menu /> : <ChevronLeft size={16} />}
</Button>
diff --git a/lib/utils/app-state.tsx b/lib/utils/app-state.tsx
new file mode 100644
index 00000000..24a376ad
--- /dev/null
+++ b/lib/utils/app-state.tsx
@@ -0,0 +1,29 @@
+'use client'
+
+import { createContext, useState, ReactNode, useContext } from 'react'
+
+const AppStateContext = createContext<
+ | {
+ isGenerating: boolean
+ setIsGenerating: (value: boolean) => void
+ }
+ | undefined
+>(undefined)
+
+export const AppStateProvider = ({ children }: { children: ReactNode }) => {
+ const [isGenerating, setIsGenerating] = useState(false)
+
+ return (
+ <AppStateContext.Provider value={{ isGenerating, setIsGenerating }}>
+ {children}
+ </AppStateContext.Provider>
+ )
+}
+
+export const useAppState = () => {
+ const context = useContext(AppStateContext)
+ if (!context) {
+ throw new Error('useAppState must be used within an AppStateProvider')
+ }
+ return context
+}