An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Add sidebar component and history functionality
Yoshiki Miura0d8d8bc27310
diff --git a/app/layout.tsx b/app/layout.tsxindex ffdbe3b9..05260219 100644--- a/app/layout.tsx+++ b/app/layout.tsx@@ -6,6 +6,7 @@ import { cn } from '@/lib/utils'import { ThemeProvider } from '@/components/theme-provider'import Header from '@/components/header'import Footer from '@/components/footer'+import { Sidebar } from '@/components/sidebar'const fontSans = FontSans({subsets: ['latin'],@@ -56,6 +57,7 @@ export default function RootLayout({<Header /><AI>{children}</AI><Footer />+ <Sidebar /></ThemeProvider></body></html>diff --git a/bun.lockb b/bun.lockbindex 50d9a0e1..3bba42e9 100755Binary files a/bun.lockb and b/bun.lockb differdiff --git a/components/header.tsx b/components/header.tsxindex d9ec989c..39ab1df6 100644--- a/components/header.tsx+++ b/components/header.tsx@@ -1,21 +1,26 @@'use client'import React from 'react'-import Link from 'next/link'import { ModeToggle } from './mode-toggle'import { IconLogo } from './ui/icons'import { cn } from '@/lib/utils'+import { History } from './history'export const Header: React.FC = () => {return (- <header className="fixed w-full p-0 md:p-2 flex justify-between items-center z-10 backdrop-blur md:backdrop-blur-none bg-background/80 md:bg-transparent">- <div className="p-2">+ <header className="fixed w-full p-2 md:p-2 flex justify-between items-center z-10 backdrop-blur md:backdrop-blur-none bg-background/80 md:bg-transparent">+ <div><a href="/"><IconLogo className={cn('w-5 h-5')} /><span className="sr-only">Morphic</span></a></div>- <ModeToggle />+ <div className="flex gap-0.5">+ <ModeToggle />+ <div className="sm:hidden block">+ <History location="header" />+ </div>+ </div></header>)}diff --git a/components/history-item.tsx b/components/history-item.tsxnew file mode 100644index 00000000..94e9cf01--- /dev/null+++ b/components/history-item.tsx@@ -0,0 +1,22 @@+import React from 'react'+import Link from 'next/link'+import { buttonVariants } from '@/components/ui/button'++type HistoryItemProps = {+ query: string+ date: string+}++const HistoryItem: React.FC<HistoryItemProps> = ({ query, date }) => {+ return (+ <Link+ href={`/search?q=${query}`}+ className="flex flex-col hover:bg-muted cursor-pointer p-2 rounded"+ >+ <div className="text-xs font-medium truncate select-none">{query}</div>+ <div className="text-xs text-muted-foreground">{date}</div>+ </Link>+ )+}++export default HistoryItemdiff --git a/components/history.tsx b/components/history.tsxnew file mode 100644index 00000000..a74bb92f--- /dev/null+++ b/components/history.tsx@@ -0,0 +1,43 @@+import {+ Sheet,+ SheetContent,+ SheetDescription,+ SheetHeader,+ SheetTitle,+ SheetTrigger+} from '@/components/ui/sheet'+import { Button } from '@/components/ui/button'+import { ChevronLeft, Menu } from 'lucide-react'+import { cn } from '@/lib/utils'+import HistoryItem from './history-item'++type HistoryProps = {+ location: 'sidebar' | 'header'+}++export function History({ location }: HistoryProps) {+ return (+ <Sheet>+ <SheetTrigger asChild>+ <Button+ variant="ghost"+ size="icon"+ className={cn({ 'rounded-full': location === 'sidebar' })}+ >+ {location === 'header' ? <Menu /> : <ChevronLeft size={16} />}+ </Button>+ </SheetTrigger>+ <SheetContent className="w-64">+ <SheetHeader>+ <SheetTitle>Search history</SheetTitle>+ </SheetHeader>+ <div className="py-2 overflow-y-auto">+ <HistoryItem+ query="Why is NVIDIA growing rapidly? hoge fuga piyo"+ date="2024-01-01"+ />+ </div>+ </SheetContent>+ </Sheet>+ )+}diff --git a/components/sidebar.tsx b/components/sidebar.tsxnew file mode 100644index 00000000..2d49588b--- /dev/null+++ b/components/sidebar.tsx@@ -0,0 +1,10 @@+import * as React from 'react'+import { History } from './history'++export function Sidebar() {+ return (+ <div className="h-screen p-2 fixed top-0 right-0 flex-col justify-center pb-24 hidden sm:flex">+ <History location="sidebar" />+ </div>+ )+}diff --git a/components/ui/sheet.tsx b/components/ui/sheet.tsxnew file mode 100644index 00000000..1b78df0d--- /dev/null+++ b/components/ui/sheet.tsx@@ -0,0 +1,140 @@+'use client'++import * as React from 'react'+import * as SheetPrimitive from '@radix-ui/react-dialog'+import { cva, type VariantProps } from 'class-variance-authority'+import { X } from 'lucide-react'++import { cn } from '@/lib/utils'++const Sheet = SheetPrimitive.Root++const SheetTrigger = SheetPrimitive.Trigger++const SheetClose = SheetPrimitive.Close++const SheetPortal = SheetPrimitive.Portal++const SheetOverlay = React.forwardRef<+ React.ElementRef<typeof SheetPrimitive.Overlay>,+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Overlay>+>(({ className, ...props }, ref) => (+ <SheetPrimitive.Overlay+ className={cn(+ 'fixed inset-0 z-50 bg-black/20 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0',+ className+ )}+ {...props}+ ref={ref}+ />+))+SheetOverlay.displayName = SheetPrimitive.Overlay.displayName++const sheetVariants = cva(+ 'fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-300 data-[state=open]:duration-500',+ {+ variants: {+ side: {+ top: 'inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top',+ bottom:+ 'inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom',+ left: 'inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm',+ right:+ 'inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm'+ }+ },+ defaultVariants: {+ side: 'right'+ }+ }+)++interface SheetContentProps+ extends React.ComponentPropsWithoutRef<typeof SheetPrimitive.Content>,+ VariantProps<typeof sheetVariants> {}++const SheetContent = React.forwardRef<+ React.ElementRef<typeof SheetPrimitive.Content>,+ SheetContentProps+>(({ side = 'right', className, children, ...props }, ref) => (+ <SheetPortal>+ <SheetOverlay />+ <SheetPrimitive.Content+ ref={ref}+ className={cn(sheetVariants({ side }), className)}+ {...props}+ >+ {children}+ <SheetPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-secondary">+ <X className="h-4 w-4" />+ <span className="sr-only">Close</span>+ </SheetPrimitive.Close>+ </SheetPrimitive.Content>+ </SheetPortal>+))+SheetContent.displayName = SheetPrimitive.Content.displayName++const SheetHeader = ({+ className,+ ...props+}: React.HTMLAttributes<HTMLDivElement>) => (+ <div+ className={cn(+ 'flex flex-col space-y-2 text-center sm:text-left',+ className+ )}+ {...props}+ />+)+SheetHeader.displayName = 'SheetHeader'++const SheetFooter = ({+ className,+ ...props+}: React.HTMLAttributes<HTMLDivElement>) => (+ <div+ className={cn(+ 'flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2',+ className+ )}+ {...props}+ />+)+SheetFooter.displayName = 'SheetFooter'++const SheetTitle = React.forwardRef<+ React.ElementRef<typeof SheetPrimitive.Title>,+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Title>+>(({ className, ...props }, ref) => (+ <SheetPrimitive.Title+ ref={ref}+ className={cn('text-lg font-semibold text-foreground', className)}+ {...props}+ />+))+SheetTitle.displayName = SheetPrimitive.Title.displayName++const SheetDescription = React.forwardRef<+ React.ElementRef<typeof SheetPrimitive.Description>,+ React.ComponentPropsWithoutRef<typeof SheetPrimitive.Description>+>(({ className, ...props }, ref) => (+ <SheetPrimitive.Description+ ref={ref}+ className={cn('text-sm text-muted-foreground', className)}+ {...props}+ />+))+SheetDescription.displayName = SheetPrimitive.Description.displayName++export {+ Sheet,+ SheetPortal,+ SheetOverlay,+ SheetTrigger,+ SheetClose,+ SheetContent,+ SheetHeader,+ SheetFooter,+ SheetTitle,+ SheetDescription+}diff --git a/components/ui/tooltip.tsx b/components/ui/tooltip.tsxnew file mode 100644index 00000000..30fc44d9--- /dev/null+++ b/components/ui/tooltip.tsx@@ -0,0 +1,30 @@+"use client"++import * as React from "react"+import * as TooltipPrimitive from "@radix-ui/react-tooltip"++import { cn } from "@/lib/utils"++const TooltipProvider = TooltipPrimitive.Provider++const Tooltip = TooltipPrimitive.Root++const TooltipTrigger = TooltipPrimitive.Trigger++const TooltipContent = React.forwardRef<+ React.ElementRef<typeof TooltipPrimitive.Content>,+ React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>+>(({ className, sideOffset = 4, ...props }, ref) => (+ <TooltipPrimitive.Content+ ref={ref}+ sideOffset={sideOffset}+ className={cn(+ "z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md animate-in fade-in-0 zoom-in-95 data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=closed]:zoom-out-95 data-[side=bottom]:slide-in-from-top-2 data-[side=left]:slide-in-from-right-2 data-[side=right]:slide-in-from-left-2 data-[side=top]:slide-in-from-bottom-2",+ className+ )}+ {...props}+ />+))+TooltipContent.displayName = TooltipPrimitive.Content.displayName++export { Tooltip, TooltipTrigger, TooltipContent, TooltipProvider }diff --git a/package.json b/package.jsonindex f4043ab8..ed46964a 100644--- a/package.json+++ b/package.json@@ -22,6 +22,7 @@"@radix-ui/react-slider": "^1.1.2","@radix-ui/react-slot": "^1.0.2","@radix-ui/react-switch": "^1.0.3",+ "@radix-ui/react-tooltip": "^1.0.7","@tailwindcss/typography": "^0.5.12","ai": "^3.0.14","class-variance-authority": "^0.7.0",