An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Refactor history component and add history list
Yoshiki Miurabb9166bd7ff5
diff --git a/components/history-container.tsx b/components/history-container.tsxindex 1789fb99..0bf37705 100644--- a/components/history-container.tsx+++ b/components/history-container.tsx@@ -1,22 +1,16 @@import React from 'react'import { History } from './history'-import { getChats } from '@/lib/actions/chat'-import { cache } from 'react'type HistoryContainerProps = {location: 'sidebar' | 'header'}-const loadChats = cache(async (userId?: string) => {- return await getChats(userId)-})const HistoryContainer: React.FC<HistoryContainerProps> = async ({location}) => {- const chats = await loadChats('anonymous')return (<div className="sm:hidden block">- <History location={location} chats={chats} />+ <History location={location} /></div>)}diff --git a/components/history-list.tsx b/components/history-list.tsxnew file mode 100644index 00000000..807e0403--- /dev/null+++ b/components/history-list.tsx@@ -0,0 +1,37 @@+import React, { cache } from 'react'+import HistoryItem from './history-item'+import { Chat } from '@/lib/types'+import { getChats } from '@/lib/actions/chat'+import { ClearHistory } from './clear-history'++type HistoryListProps = {+ userId?: string+}++const loadChats = cache(async (userId?: string) => {+ return await getChats(userId)+})++// Start of Selection+export async function HistoryList({ userId }: HistoryListProps) {+ const chats = await loadChats(userId)++ return (+ <div className="flex flex-col flex-1 space-y-3 h-full">+ <div className="flex flex-col gap-2 flex-1 overflow-y-auto">+ {!chats?.length ? (+ <div className="text-foreground/30 text-sm text-center py-4">+ No search history+ </div>+ ) : (+ chats?.map(+ (chat: Chat) => chat && <HistoryItem key={chat.id} chat={chat} />+ )+ )}+ </div>+ <div className="mt-auto">+ <ClearHistory empty={!chats?.length} />+ </div>+ </div>+ )+}diff --git a/components/history-skelton.tsx b/components/history-skelton.tsxnew file mode 100644index 00000000..c0e257e5--- /dev/null+++ b/components/history-skelton.tsx@@ -0,0 +1,12 @@+import React from 'react'+import { Skeleton } from './ui/skeleton'++export function HistorySkeleton() {+ return (+ <div className="flex flex-col flex-1 space-y-4 overflow-auto">+ {Array.from({ length: 3 }).map((_, i) => (+ <Skeleton key={i} className="w-full h-12 rounded" />+ ))}+ </div>+ )+}diff --git a/components/history.tsx b/components/history.tsxindex 61650471..82cadb4c 100644--- a/components/history.tsx+++ b/components/history.tsx@@ -1,7 +1,6 @@import {Sheet,SheetContent,- SheetFooter,SheetHeader,SheetTitle,SheetTrigger@@ -9,17 +8,16 @@ import {import { Button } from '@/components/ui/button'import { ChevronLeft, Menu } from 'lucide-react'import { cn } from '@/lib/utils'-import HistoryItem from './history-item'-import { Chat } from '@/lib/types'import { History as HistoryIcon } from 'lucide-react'-import { ClearHistory } from './clear-history'+import { HistoryList } from './history-list'+import { Suspense } from 'react'+import { HistorySkeleton } from './history-skelton'type HistoryProps = {location: 'sidebar' | 'header'- chats: Chat[]}-export function History({ location, chats }: HistoryProps) {+export function History({ location }: HistoryProps) {return (<Sheet><SheetTrigger asChild>@@ -40,20 +38,11 @@ export function History({ location, chats }: HistoryProps) {History</SheetTitle></SheetHeader>- <div className="my-2 overflow-y-auto h-[calc(100vh-7.5rem)]">- {!chats?.length ? (- <div className="text-foreground/30 text-sm text-center py-4">- No search history- </div>- ) : (- chats?.map((chat: Chat) => (- chat && <HistoryItem key={chat.id} chat={chat} />- ))- )}+ <div className="my-2 h-[calc(100vh-4.5rem)]">+ <Suspense fallback={<HistorySkeleton />}>+ <HistoryList userId="anonymous" />+ </Suspense></div>- <SheetFooter>- <ClearHistory empty={!chats?.length} />- </SheetFooter></SheetContent></Sheet>)diff --git a/components/sidebar.tsx b/components/sidebar.tsxindex 7b04764a..8843f0dc 100644--- a/components/sidebar.tsx+++ b/components/sidebar.tsx@@ -1,17 +1,9 @@-import { cache } from 'react'import { History } from './history'-import { getChats } from '@/lib/actions/chat'--const loadChats = cache(async (userId?: string) => {- return await getChats(userId)-})export async function Sidebar() {- const chats = await loadChats('anonymous')- console.log('history loaded')return (<div className="h-screen p-2 fixed top-0 right-0 flex-col justify-center pb-24 hidden sm:flex">- <History location="sidebar" chats={chats} />+ <History location="sidebar" /></div>)}