An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add Artifact panel with desktop/mobile views
Yoshiki Miurac9236facdfae
diff --git a/app/layout.tsx b/app/layout.tsxindex bf1970dd..bac1d673 100644--- a/app/layout.tsx+++ b/app/layout.tsx@@ -1,3 +1,4 @@+import ArtifactRoot from '@/components/artifact/artifact-root'import Footer from '@/components/footer'import Header from '@/components/header'import { ThemeProvider } from '@/components/theme-provider'@@ -60,7 +61,9 @@ export default function RootLayout({><div className="flex flex-col flex-1"><Header />- <main className="flex flex-col flex-1 min-h-0">{children}</main>+ <main className="flex flex-1 min-h-0">+ <ArtifactRoot>{children}</ArtifactRoot>+ </main><Footer /></div><Toaster />diff --git a/components/artifact/artifact-content.tsx b/components/artifact/artifact-content.tsxnew file mode 100644index 00000000..3ed3eeb6--- /dev/null+++ b/components/artifact/artifact-content.tsx@@ -0,0 +1,49 @@+'use client'++import { SearchResults } from '@/components/search-results'+import { SearchResultsImageSection } from '@/components/search-results-image'+import { Section } from '@/components/section'+import type { SearchResults as TypeSearchResults } from '@/lib/types'+import type { ToolInvocation } from 'ai'++export function ArtifactContent({+ artifact+}: {+ artifact: ToolInvocation | null+}) {+ if (!artifact) return null++ switch (artifact.toolName) {+ case 'search':+ return <SearchArtifactContent tool={artifact} />+ default:+ return <div className="p-4">Details for this tool are not available</div>+ }+}++function SearchArtifactContent({ tool }: { tool: ToolInvocation }) {+ const searchResults: TypeSearchResults =+ tool.state === 'result' ? tool.result : undefined+ const query = tool.args?.query as string | undefined++ if (!searchResults?.results) {+ return <div className="p-4">No search results</div>+ }++ return (+ <div className="p-4 space-y-6">+ {searchResults.images && searchResults.images.length > 0 && (+ <Section>+ <SearchResultsImageSection+ images={searchResults.images}+ query={query}+ />+ </Section>+ )}++ <Section title="Sources">+ <SearchResults results={searchResults.results} />+ </Section>+ </div>+ )+}diff --git a/components/artifact/artifact-context.tsx b/components/artifact/artifact-context.tsxnew file mode 100644index 00000000..a9edf693--- /dev/null+++ b/components/artifact/artifact-context.tsx@@ -0,0 +1,68 @@+'use client'++import type { ToolInvocation } from 'ai'+import { createContext, ReactNode, useContext, useReducer } from 'react'++interface ArtifactState {+ artifact: ToolInvocation | null+ isOpen: boolean+}++type ArtifactAction =+ | { type: 'OPEN'; payload: ToolInvocation }+ | { type: 'CLOSE' }++const initialState: ArtifactState = {+ artifact: null,+ isOpen: false+}++function artifactReducer(+ state: ArtifactState,+ action: ArtifactAction+): ArtifactState {+ switch (action.type) {+ case 'OPEN':+ return { artifact: action.payload, isOpen: true }+ case 'CLOSE':+ return { ...state, isOpen: false }+ default:+ return state+ }+}++interface ArtifactContextValue {+ state: ArtifactState+ open: (artifact: ToolInvocation) => void+ close: () => void+}++const ArtifactContext = createContext<ArtifactContextValue | undefined>(+ undefined+)++export function ArtifactProvider({ children }: { children: ReactNode }) {+ const [state, dispatch] = useReducer(artifactReducer, initialState)++ const open = (artifact: ToolInvocation) => {+ dispatch({ type: 'OPEN', payload: artifact })+ }++ const close = () => {+ dispatch({ type: 'CLOSE' })+ }++ return (+ <ArtifactContext.Provider value={{ state, open, close }}>+ {children}+ </ArtifactContext.Provider>+ )+}++export function useArtifact() {+ const context = useContext(ArtifactContext)+ if (context === undefined) {+ throw new Error('useArtifact must be used within an ArtifactProvider')+ }+ return context+}diff --git a/components/artifact/artifact-drawer.tsx b/components/artifact/artifact-drawer.tsxnew file mode 100644index 00000000..374ba576--- /dev/null+++ b/components/artifact/artifact-drawer.tsx@@ -0,0 +1,38 @@+'use client'++import { Drawer, DrawerContent } from '@/components/ui/drawer'+import { useEffect, useState } from 'react'+import { useArtifact } from './artifact-context'+import { ArtifactPanel } from './artifact-panel'++// Hook to detect mobile screen width+function useMediaQuery(query: string): boolean {+ const [matches, setMatches] = useState(false)+ useEffect(() => {+ const mql = window.matchMedia(query)+ const handler = (e: MediaQueryListEvent) => setMatches(e.matches)+ setMatches(mql.matches)+ mql.addEventListener('change', handler)+ return () => mql.removeEventListener('change', handler)+ }, [query])+ return matches+}++export function ArtifactDrawer() {+ const { state, close } = useArtifact()+ const isMobile = useMediaQuery('(max-width: 767px)')+ if (!isMobile) return null+ return (+ <Drawer+ open={state.isOpen}+ onOpenChange={open => {+ if (!open) close()+ }}+ modal={false}+ >+ <DrawerContent className="p-0 max-h-[90vh] md:hidden">+ <ArtifactPanel />+ </DrawerContent>+ </Drawer>+ )+}diff --git a/components/artifact/artifact-panel.tsx b/components/artifact/artifact-panel.tsxnew file mode 100644index 00000000..96a7f2d3--- /dev/null+++ b/components/artifact/artifact-panel.tsx@@ -0,0 +1,35 @@+'use client'++import { Button } from '@/components/ui/button'+import { X } from 'lucide-react'+import { ArtifactContent } from './artifact-content'+import { useArtifact } from './artifact-context'++export function ArtifactPanel() {+ const { state, close } = useArtifact()+ const artifact = state.artifact+ if (!artifact) return null++ return (+ <div className="h-full flex flex-col overflow-hidden bg-background">+ <div className="flex items-center justify-between p-4 border-b">+ <h3 className="font-medium capitalize">+ {artifact.toolName === 'search'+ ? 'Search Results'+ : artifact.toolName}+ </h3>+ <Button+ variant="ghost"+ size="icon"+ onClick={close}+ aria-label="Close panel"+ >+ <X className="h-4 w-4" />+ </Button>+ </div>+ <div className="flex-1 overflow-y-auto">+ <ArtifactContent artifact={artifact} />+ </div>+ </div>+ )+}diff --git a/components/artifact/artifact-root.tsx b/components/artifact/artifact-root.tsxnew file mode 100644index 00000000..b79464c9--- /dev/null+++ b/components/artifact/artifact-root.tsx@@ -0,0 +1,13 @@+'use client'++import { ReactNode } from 'react'+import { ArtifactProvider } from './artifact-context'+import { ChatArtifactContainer } from './chat-artifact-container'++export default function ArtifactRoot({ children }: { children: ReactNode }) {+ return (+ <ArtifactProvider>+ <ChatArtifactContainer>{children}</ChatArtifactContainer>+ </ArtifactProvider>+ )+}diff --git a/components/artifact/chat-artifact-container.tsx b/components/artifact/chat-artifact-container.tsxnew file mode 100644index 00000000..366bf0b6--- /dev/null+++ b/components/artifact/chat-artifact-container.tsx@@ -0,0 +1,46 @@+'use client'++import {+ ResizableHandle,+ ResizablePanel,+ ResizablePanelGroup+} from '@/components/ui/resizable'+import React from 'react'+import { useArtifact } from './artifact-context'+import { ArtifactDrawer } from './artifact-drawer'+import { ArtifactPanel } from './artifact-panel'++export function ChatArtifactContainer({+ children+}: {+ children: React.ReactNode+}) {+ const { state } = useArtifact()++ return (+ <div className="flex-1 min-h-0 flex">+ {/* Desktop: Resizable panels */}+ <ResizablePanelGroup+ direction="horizontal"+ className="hidden md:flex flex-1 min-w-0 h-full"+ >+ <ResizablePanel className="min-w-0">{children}</ResizablePanel>++ {state.isOpen && (+ <>+ <ResizableHandle withHandle />+ <ResizablePanel className="min-w-0 w-96 border-l">+ <ArtifactPanel />+ </ResizablePanel>+ </>+ )}+ </ResizablePanelGroup>++ {/* Mobile: full-width chat + drawer */}+ <div className="flex-1 md:hidden h-full">+ {children}+ <ArtifactDrawer />+ </div>+ </div>+ )+}diff --git a/components/search-section.tsx b/components/search-section.tsxindex caf9ddfc..2898b7a1 100644--- a/components/search-section.tsx+++ b/components/search-section.tsx@@ -1,9 +1,11 @@'use client'+import { useArtifact } from '@/components/artifact/artifact-context'import { CHAT_ID } from '@/lib/constants'import type { SearchResults as TypeSearchResults } from '@/lib/types'import { useChat } from '@ai-sdk/react'import { ToolInvocation } from 'ai'+import { Maximize2 } from 'lucide-react'import { CollapsibleMessage } from './collapsible-message'import { SearchSkeleton } from './default-skeleton'import { SearchResults } from './search-results'@@ -35,11 +37,21 @@ export function SearchSection({? ` [${includeDomains.join(', ')}]`: ''+ const { open } = useArtifact()const header = (- <ToolArgsSection- tool="search"- number={searchResults?.results?.length}- >{`${query}${includeDomainsString}`}</ToolArgsSection>+ <div className="flex items-center justify-between w-full">+ <ToolArgsSection+ tool="search"+ number={searchResults?.results?.length}+ >{`${query}${includeDomainsString}`}</ToolArgsSection>+ <button+ onClick={() => open(tool)}+ className="ml-2 rounded-md p-1 hover:bg-accent"+ title="Open details"+ >+ <Maximize2 size={14} />+ </button>+ </div>)return (diff --git a/components/ui/drawer.tsx b/components/ui/drawer.tsxnew file mode 100644index 00000000..45eb821e--- /dev/null+++ b/components/ui/drawer.tsx@@ -0,0 +1,118 @@+"use client"++import * as React from "react"+import { Drawer as DrawerPrimitive } from "vaul"++import { cn } from "@/lib/utils/index"++const Drawer = ({+ shouldScaleBackground = true,+ ...props+}: React.ComponentProps<typeof DrawerPrimitive.Root>) => (+ <DrawerPrimitive.Root+ shouldScaleBackground={shouldScaleBackground}+ {...props}+ />+)+Drawer.displayName = "Drawer"++const DrawerTrigger = DrawerPrimitive.Trigger++const DrawerPortal = DrawerPrimitive.Portal++const DrawerClose = DrawerPrimitive.Close++const DrawerOverlay = React.forwardRef<+ React.ElementRef<typeof DrawerPrimitive.Overlay>,+ React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Overlay>+>(({ className, ...props }, ref) => (+ <DrawerPrimitive.Overlay+ ref={ref}+ className={cn("fixed inset-0 z-50 bg-black/80", className)}+ {...props}+ />+))+DrawerOverlay.displayName = DrawerPrimitive.Overlay.displayName++const DrawerContent = React.forwardRef<+ React.ElementRef<typeof DrawerPrimitive.Content>,+ React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Content>+>(({ className, children, ...props }, ref) => (+ <DrawerPortal>+ <DrawerOverlay />+ <DrawerPrimitive.Content+ ref={ref}+ className={cn(+ "fixed inset-x-0 bottom-0 z-50 mt-24 flex h-auto flex-col rounded-t-[10px] border bg-background",+ className+ )}+ {...props}+ >+ <div className="mx-auto mt-4 h-2 w-[100px] rounded-full bg-muted" />+ {children}+ </DrawerPrimitive.Content>+ </DrawerPortal>+))+DrawerContent.displayName = "DrawerContent"++const DrawerHeader = ({+ className,+ ...props+}: React.HTMLAttributes<HTMLDivElement>) => (+ <div+ className={cn("grid gap-1.5 p-4 text-center sm:text-left", className)}+ {...props}+ />+)+DrawerHeader.displayName = "DrawerHeader"++const DrawerFooter = ({+ className,+ ...props+}: React.HTMLAttributes<HTMLDivElement>) => (+ <div+ className={cn("mt-auto flex flex-col gap-2 p-4", className)}+ {...props}+ />+)+DrawerFooter.displayName = "DrawerFooter"++const DrawerTitle = React.forwardRef<+ React.ElementRef<typeof DrawerPrimitive.Title>,+ React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Title>+>(({ className, ...props }, ref) => (+ <DrawerPrimitive.Title+ ref={ref}+ className={cn(+ "text-lg font-semibold leading-none tracking-tight",+ className+ )}+ {...props}+ />+))+DrawerTitle.displayName = DrawerPrimitive.Title.displayName++const DrawerDescription = React.forwardRef<+ React.ElementRef<typeof DrawerPrimitive.Description>,+ React.ComponentPropsWithoutRef<typeof DrawerPrimitive.Description>+>(({ className, ...props }, ref) => (+ <DrawerPrimitive.Description+ ref={ref}+ className={cn("text-sm text-muted-foreground", className)}+ {...props}+ />+))+DrawerDescription.displayName = DrawerPrimitive.Description.displayName++export {+ Drawer,+ DrawerPortal,+ DrawerOverlay,+ DrawerTrigger,+ DrawerClose,+ DrawerContent,+ DrawerHeader,+ DrawerFooter,+ DrawerTitle,+ DrawerDescription,+}diff --git a/components/ui/resizable.tsx b/components/ui/resizable.tsxnew file mode 100644index 00000000..eef5e91e--- /dev/null+++ b/components/ui/resizable.tsx@@ -0,0 +1,45 @@+"use client"++import { GripVertical } from "lucide-react"+import * as ResizablePrimitive from "react-resizable-panels"++import { cn } from "@/lib/utils/index"++const ResizablePanelGroup = ({+ className,+ ...props+}: React.ComponentProps<typeof ResizablePrimitive.PanelGroup>) => (+ <ResizablePrimitive.PanelGroup+ className={cn(+ "flex h-full w-full data-[panel-group-direction=vertical]:flex-col",+ className+ )}+ {...props}+ />+)++const ResizablePanel = ResizablePrimitive.Panel++const ResizableHandle = ({+ withHandle,+ className,+ ...props+}: React.ComponentProps<typeof ResizablePrimitive.PanelResizeHandle> & {+ withHandle?: boolean+}) => (+ <ResizablePrimitive.PanelResizeHandle+ className={cn(+ "relative flex w-px items-center justify-center bg-border after:absolute after:inset-y-0 after:left-1/2 after:w-1 after:-translate-x-1/2 focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring focus-visible:ring-offset-1 data-[panel-group-direction=vertical]:h-px data-[panel-group-direction=vertical]:w-full data-[panel-group-direction=vertical]:after:left-0 data-[panel-group-direction=vertical]:after:h-1 data-[panel-group-direction=vertical]:after:w-full data-[panel-group-direction=vertical]:after:-translate-y-1/2 data-[panel-group-direction=vertical]:after:translate-x-0 [&[data-panel-group-direction=vertical]>div]:rotate-90",+ className+ )}+ {...props}+ >+ {withHandle && (+ <div className="z-10 flex h-4 w-3 items-center justify-center rounded-sm border bg-border">+ <GripVertical className="h-2.5 w-2.5" />+ </div>+ )}+ </ResizablePrimitive.PanelResizeHandle>+)++export { ResizablePanelGroup, ResizablePanel, ResizableHandle }