An AI-powered search engine with a generative UI
TypeScript generative-aigenerative-uinextjsollamareactsearxngshadcn-uitailwindcsstavilytypescriptupstashvercel-ai-sdk
Fix chat refresh condition and format date in history item
Yoshiki Miurabb6199358657
diff --git a/components/chat.tsx b/components/chat.tsxindex bbb23ee9..fddcae2a 100644--- a/components/chat.tsx+++ b/components/chat.tsx@@ -23,11 +23,11 @@ export function Chat({ id }: ChatProps) {}, [id, path, messages])useEffect(() => {- const messagesLength = aiState.messages?.length- if (messagesLength === 2) {+ if (aiState.messages.length > 2) {+ console.log('refresh')router.refresh()}- }, [aiState.messages, router])+ }, [aiState, router])return (<div className="px-8 sm:px-12 pt-6 md:pt-8 pb-14 md:pb-24 max-w-3xl mx-auto flex flex-col space-y-3 md:space-y-4">diff --git a/components/history-item.tsx b/components/history-item.tsxindex e7c9795a..0aa391ad 100644--- a/components/history-item.tsx+++ b/components/history-item.tsx@@ -1,19 +1,58 @@import React from 'react'import Link from 'next/link'+import { Chat } from '@/lib/types'type HistoryItemProps = {- query: string- date: string+ chat: Chat}-const HistoryItem: React.FC<HistoryItemProps> = ({ query, date }) => {+const formatDateWithTime = (dateString: string) => {+ const date = new Date(dateString)+ const now = new Date()+ const yesterday = new Date()+ yesterday.setDate(yesterday.getDate() - 1)++ if (+ date.getDate() === now.getDate() &&+ date.getMonth() === now.getMonth() &&+ date.getFullYear() === now.getFullYear()+ ) {+ return `Today, ${date.getHours()}:${date+ .getMinutes()+ .toString()+ .padStart(2, '0')}`+ } else if (+ date.getDate() === yesterday.getDate() &&+ date.getMonth() === yesterday.getMonth() &&+ date.getFullYear() === yesterday.getFullYear()+ ) {+ return `Yesterday, ${date.getHours()}:${date+ .getMinutes()+ .toString()+ .padStart(2, '0')}`+ } else {+ return date.toLocaleString('en-US', {+ year: 'numeric',+ month: 'long',+ day: 'numeric',+ hour: 'numeric',+ minute: '2-digit'+ })+ }+}++const HistoryItem: React.FC<HistoryItemProps> = ({ chat }) => {return (<Link- href={`/search?q=${query}`}+ href={chat.path}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>+ <div className="text-xs font-medium truncate select-none">+ {chat.title}+ </div>+ <div className="text-xs text-muted-foreground">+ {formatDateWithTime(chat.createdAt as string)}+ </div></Link>)}diff --git a/components/history.tsx b/components/history.tsxindex f96d7a60..62cdf1e8 100644--- a/components/history.tsx+++ b/components/history.tsx@@ -1,7 +1,6 @@import {Sheet,SheetContent,- SheetDescription,SheetHeader,SheetTitle,SheetTrigger@@ -10,12 +9,15 @@ 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'type HistoryProps = {location: 'sidebar' | 'header'+ chats: Chat[]}-export function History({ location }: HistoryProps) {+export function History({ location, chats }: HistoryProps) {return (<Sheet><SheetTrigger asChild>@@ -29,15 +31,23 @@ export function History({ location }: HistoryProps) {{location === 'header' ? <Menu /> : <ChevronLeft size={16} />}</Button></SheetTrigger>- <SheetContent className="w-64 rounded-tl-lg rounded-bl-lg">+ <SheetContent className="w-64 rounded-tl-xl rounded-bl-xl"><SheetHeader>- <SheetTitle>Search history</SheetTitle>+ <SheetTitle className="flex items-center gap-1 text-base mb-2">+ <HistoryIcon size={16} />+ 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 className="pb-6 overflow-y-auto h-full">+ {!chats?.length ? (+ <div className="text-foreground/30 text-sm text-center py-4">+ No history yet+ </div>+ ) : (+ chats?.map((chat: Chat) => (+ <HistoryItem key={chat.id} chat={chat} />+ ))+ )}</div></SheetContent></Sheet>diff --git a/components/sidebar.tsx b/components/sidebar.tsxindex 2d49588b..19eb58d9 100644--- a/components/sidebar.tsx+++ b/components/sidebar.tsx@@ -1,10 +1,17 @@-import * as React from 'react'+import { cache } from 'react'import { History } from './history'+import { getChats } from '@/lib/actions/chat'-export function Sidebar() {+const loadChats = cache(async (userId?: string) => {+ return await getChats(userId)+})++export async function Sidebar() {+ const chats = await loadChats('anonymous')+ console.log('chats 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" />+ <History location="sidebar" chats={chats} /></div>)}diff --git a/components/ui/sheet.tsx b/components/ui/sheet.tsxindex 86915b83..82810327 100644--- a/components/ui/sheet.tsx+++ b/components/ui/sheet.tsx@@ -31,7 +31,7 @@ const SheetOverlay = React.forwardRef<SheetOverlay.displayName = SheetPrimitive.Overlay.displayNameconst 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',+ 'fixed z-50 gap-4 bg-background p-4 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: {diff --git a/lib/actions/chat.ts b/lib/actions/chat.tsindex 911d0fe8..14be8148 100644--- a/lib/actions/chat.ts+++ b/lib/actions/chat.ts@@ -1,5 +1,6 @@'use server'+import { redirect } from 'next/navigation'import { type Chat } from '@/lib/types'import { Redis } from '@upstash/redis'