An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Refactor: Extract useMediaQuery hook
Yoshiki Miurac9e5e6b9d314
diff --git a/components/artifact/chat-artifact-container.tsx b/components/artifact/chat-artifact-container.tsxindex 366bf0b6..8996a0ef 100644--- a/components/artifact/chat-artifact-container.tsx+++ b/components/artifact/chat-artifact-container.tsx@@ -5,6 +5,7 @@ import {ResizablePanel,ResizablePanelGroup} from '@/components/ui/resizable'+import { useMediaQuery } from '@/lib/hooks/use-media-query'import React from 'react'import { useArtifact } from './artifact-context'import { ArtifactDrawer } from './artifact-drawer'@@ -16,31 +17,39 @@ export function ChatArtifactContainer({children: React.ReactNode}) {const { state } = useArtifact()+ const isMobile = useMediaQuery('(max-width: 767px)') // Below md breakpointreturn (<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>+ {/* Desktop: Resizable panels (Do not render on mobile) */}+ {!isMobile && (+ <ResizablePanelGroup+ direction="horizontal"+ className="flex flex-1 min-w-0 h-full" // Responsive classes removed+ >+ <ResizablePanel className="min-w-0">{children}</ResizablePanel>- {state.isOpen && (- <>- <ResizableHandle withHandle />- <ResizablePanel className="min-w-0 w-96 border-l">- <ArtifactPanel />- </ResizablePanel>- </>- )}- </ResizablePanelGroup>+ {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>+ {/* Mobile: full-width chat + drawer (Do not render on desktop) */}+ {isMobile && (+ <div className="flex-1 h-full">+ {' '}+ {/* Responsive classes removed */}+ {children}+ {/* ArtifactDrawer checks isMobile internally, no double check needed */}+ <ArtifactDrawer />+ </div>+ )}</div>)}diff --git a/lib/hooks/use-media-query.ts b/lib/hooks/use-media-query.tsnew file mode 100644index 00000000..99d6882f--- /dev/null+++ b/lib/hooks/use-media-query.ts@@ -0,0 +1,32 @@+'use client'++import { useEffect, useState } from 'react'++/**+ * Custom hook to track media query matches.+ * @param query - The media query string (e.g., '(max-width: 767px)').+ * @returns boolean - True if the media query matches, false otherwise.+ */+export function useMediaQuery(query: string): boolean {+ const [matches, setMatches] = useState(false)++ useEffect(() => {+ // Ensure window is available (client-side only)+ if (typeof window === 'undefined') {+ return+ }+ const mql = window.matchMedia(query)+ const handler = (e: MediaQueryListEvent) => setMatches(e.matches)++ // Set initial state+ setMatches(mql.matches)++ // Add listener+ mql.addEventListener('change', handler)++ // Cleanup listener on unmount+ return () => mql.removeEventListener('change', handler)+ }, [query])++ return matches+}