# Chat history

> Past chats grouped by day, searchable, with titles that write themselves, rename in place and delete with Undo.

The sidebar people come back to every day. Chats group into Pinned, Today, Yesterday, Previous 7 days and older. A new chat shimmers "New chat" until its title is generated, then the title types itself in. Search filters as you type and marks the match, and the rows that stay glide up to close the gaps. Hover a chat to pin, rename or delete it: pinning glides the row into Pinned while the pin trades places with unpin, renaming turns the row into a field in place, and deleting lets the title give way to "Chat deleted · Undo", only deleting (and folding the row away) once the undo window has passed.

## Install

```bash
npx shadcn@latest add https://hairlineui.com/r/chat-history.json
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): none
- Docs and live preview: https://hairlineui.com/ui/chat-history
- Registry item (source): https://hairlineui.com/r/chat-history.json

## Usage

```tsx
import { ChatHistory } from "@/components/chat-history";

<ChatHistory chats={chats} activeId={chatId} onSelect={openChat} onNew={newChat} onRename={rename} onDelete={remove} onPin={pin} />
```

## With the AI SDK

```tsx
// Server: name a new chat from its first message
import { generateText } from "ai";

export async function POST(req: Request) {
  const { chatId, firstMessage } = await req.json();
  const { text } = await generateText({
    model: "openai/gpt-5-mini",
    system: "Write a 2 to 5 word title for this chat. No quotes or punctuation at the end.",
    prompt: firstMessage,
  });
  await db.chats.update(chatId, { title: text.trim(), generating: false });
  return Response.json({ title: text.trim() });
}

// Client: create the chat with generating: true and send the first message;
// when the title comes back, update that chat and ChatHistory types it in.
const { sendMessage } = useChat({ id: chatId });
setChats((c) => [{ id: chatId, updatedAt: Date.now(), generating: true }, ...c]);
sendMessage({ text });
const { title } = await fetch("/api/title", { method: "POST", body: JSON.stringify({ chatId, firstMessage: text }) }).then((r) => r.json());
setChats((c) => c.map((x) => (x.id === chatId ? { ...x, title, generating: false } : x)));
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `chats` | `{ id; title?; updatedAt; pinned?; generating? }[]` | – | Grouped and sorted for you. Leave title empty (or set generating) while it’s being named. |
| `activeId / onSelect` | `string / (id) => void` | – | The open chat, marked with aria-current. |
| `onNew` | `() => void` | – | Shows the new-chat button beside search. |
| `onRename / onPin` | `(id, title) => void / (id, pinned) => void` | – | Show Rename and Pin on hover or focus. |
| `onDelete / undoWindow` | `(id) => void / number` | `— / 5000` | Called once the undo window has passed, after the row has folded away. |
| `searchable / defaultQuery` | `boolean / string` | `true / ""` | The search field and its starting text. |

## States

- **grouped**: Newest first under Pinned, Today, Yesterday, Previous 7 days, Previous 30 days, then month names. It regroups at midnight.
- **new**: "New chat" shimmers while generating is true; the title then types in once.
- **search**: Rows filter as you type and the ones that stay glide to their new places (420ms); the match is highlighted, Escape clears, and an empty search says so.
- **pin**: The row glides from its group into Pinned (and back) instead of jumping; the pin icon blurs into unpin.
- **rename**: The same row becomes a field, its outline fading in, with the title selected; Enter or leaving saves, Escape cancels.
- **deleted**: The title blurs out as "Chat deleted · Undo" fades in for undoWindow, then the row folds away and onDelete is called.

## What it handles

Every AI product needs a list of past chats, and every team rebuilds the same details: grouping by day, naming new chats, search, rename, pin and delete. This is that list, finished.

- **Grouped by day**: Pinned, Today, Yesterday, Previous 7 days, Previous 30 days, then month names. It regroups at midnight.
- **New chats name themselves**: A new chat shimmers “New chat” until its title is generated, then the title types itself in.
- **Search as you type**: Rows filter as you type, the match is highlighted and the rows that stay glide up to close the gaps.
- **Pin, rename and delete in the row**: The actions appear on hover or focus, and always on touch screens. Rename turns the row into a field.
- **Delete with Undo**: The title gives way to “Chat deleted · Undo”. Nothing is deleted until the undo window has passed.
- **Keyboard and landmarks**: Up and Down move between chats, the list is a navigation landmark and the open chat is marked as current.

## Replaces

date grouping code, a sidebar list, inline rename, a delete confirmation, search highlighting

## Where it fits

- **AI chat apps**: The sidebar of past conversations, grouped by day like the ones people already know.
- **In-product assistants**: Past conversations with the help or copilot inside a SaaS product.
- **Agent and coding tools**: Past sessions of an agent, searchable by what they were about.
- **Internal knowledge assistants**: Staff chats with company documents, kept and findable.

## Notes

- A labelled navigation landmark with a heading per group; the open chat has aria-current="page".
- Up and Down move between chats from anywhere in the list, including the search field.
- Row actions appear on hover and keyboard focus, always on touch screens, and are labelled with the chat ("Delete Pistachio supplier prices").
- Nothing is deleted until Undo has had its chance, so a slip of the finger never costs a conversation.
- With reduced motion, rows move and change state in place.

License: MIT (https://hairlineui.com/license#components)
