Skip to content

An AI-powered search engine with a generative UI

feat: close artifact when sidebar opens

Yoshiki Miura1be9576475a7
diff --git a/components/artifact/artifact-context.tsx b/components/artifact/artifact-context.tsx
index 7ab0917d..d24b1d9b 100644
--- a/components/artifact/artifact-context.tsx
+++ b/components/artifact/artifact-context.tsx
@@ -1,7 +1,14 @@
'use client'
import type { ToolInvocation } from 'ai'
-import { createContext, ReactNode, useContext, useReducer } from 'react'
+import {
+ createContext,
+ ReactNode,
+ useCallback,
+ useContext,
+ useEffect,
+ useReducer
+} from 'react'
import { useSidebar } from '../ui/sidebar'
// Part types as seen in render-message.tsx
@@ -60,16 +67,24 @@ const ArtifactContext = createContext<ArtifactContextValue | undefined>(
export function ArtifactProvider({ children }: { children: ReactNode }) {
const [state, dispatch] = useReducer(artifactReducer, initialState)
- const { setOpen } = useSidebar()
+ const { setOpen, open: sidebarOpen } = useSidebar()
+
+ const close = useCallback(() => {
+ dispatch({ type: 'CLOSE' })
+ }, [])
+
+ // Close artifact when sidebar opens
+ useEffect(() => {
+ if (sidebarOpen && state.isOpen) {
+ close()
+ }
+ }, [sidebarOpen, state.isOpen, close])
+
const open = (part: Part) => {
dispatch({ type: 'OPEN', payload: part })
setOpen(false)
}
- const close = () => {
- dispatch({ type: 'CLOSE' })
- }
-
return (
<ArtifactContext.Provider value={{ state, open, close }}>
{children}