useConvos
Overview
useConvos holds the list of conversations for a chat app's sidebar: the conversations, which one is open, and creating, renaming, pinning and removing them.
It holds no messages: each conversation's chat is a useChat of its own, and your
app loads and saves each one's messages. Give that chat the open conversation's id as its React
key, so switching conversations starts it afresh and stops a reply still streaming in the one you
left.
Import
import { useConvos } from '@allxsmith/bestax-ai';
Usage
A Sidebar
create adds a conversation at the top of the list and opens it, and returns it. rename, pin
and remove take its id. Removing the open conversation leaves none open.
function Sidebar() { const { conversations, activeId, setActiveId, create, rename, pin, remove } = useConvos({ defaultConversations: [ { id: 'trip', title: 'Trip to Lisbon', createdAt: 1 }, { id: 'menu', title: 'Dinner menu', createdAt: 2 }, ], defaultActiveId: 'trip', }); return ( <Box> <Button color="primary" onClick={() => { const made = create(); rename(made.id, `Chat ${conversations.length + 1}`); }} > New chat </Button> <Block mt="4"> {conversations.map(conversation => ( <Buttons key={conversation.id} hasAddons> <Button aria-current={conversation.id === activeId ? 'page' : undefined} color={conversation.id === activeId ? 'link' : undefined} onClick={() => setActiveId(conversation.id)} > {conversation.pinned ? '📌 ' : ''} {conversation.title} </Button> <Button aria-label={`${conversation.pinned ? 'Unpin' : 'Pin'} ${conversation.title}`} onClick={() => pin(conversation.id, !conversation.pinned)} > {conversation.pinned ? 'Unpin' : 'Pin'} </Button> <Button aria-label={`Delete ${conversation.title}`} onClick={() => remove(conversation.id)} > Delete </Button> </Buttons> ))} </Block> <Paragraph> Open: {conversations.find(c => c.id === activeId)?.title ?? 'none'} </Paragraph> </Box> ); }
create and rename in one handler both see the new conversation, as above: the hook reads the
newest list, not the one the render had.
With a Chat
Render the open conversation's chat with its id as the key, and save its messages as they change.
function App() {
const convos = useConvos({ conversations, onConversationsChange: save });
return (
<>
<Sidebar {...convos} />
{convos.activeId && (
<Conversation key={convos.activeId} id={convos.activeId} />
)}
</>
);
}
function Conversation({ id }) {
const chat = useChat({
adapter: anthropicAdapter(),
defaultMessages: loadMessages(id),
onMessagesChange: messages => saveMessages(id, messages),
});
// …
}
update changes a conversation's fields and sets its updatedAt, for sorting by recent use:
call it from the chat's onFinish, say. The list stays in the order it's held, with a new
conversation first, so sort or group it (pinned first, then by date) where you render it.
Holding the List
Without conversations, the hook holds the list, starting from defaultConversations, and
setConversations replaces it. Pass conversations and onConversationsChange to hold it
yourself. The open id works the same way, with activeId, defaultActiveId and
onActiveIdChange.
Held by you, the list is yours: each call builds on the one you pass, so a change you don't pass back
is dropped, and two changes in one handler, a create then a rename, need the first passed back
before the second. Held by the hook, both calls see each other, as in the sidebar above.
API
function useConvos<TMetadata = unknown>(
options?: UseConvosOptions<TMetadata>
): UseConvosResult<TMetadata>;
interface ChatConversation<TMetadata = unknown> {
id: string;
title: string;
createdAt: number;
updatedAt?: number;
pinned?: boolean;
metadata?: TMetadata;
}
Options
| Option | Type | Default | Description |
|---|---|---|---|
conversations | ChatConversation[] | — | The conversations, when you hold the list. |
defaultConversations | ChatConversation[] | [] | The conversations to start with, when the hook holds the list. |
onConversationsChange | (conversations: ChatConversation[]) => void | — | Called with the new list each time it changes. |
activeId | string | null | — | The open conversation's id, or null, when you hold it. |
defaultActiveId | string | null | null | The conversation open to start with, when the hook holds it. |
onActiveIdChange | (activeId: string | null) => void | — | Called with the open conversation's id each time it changes. |
generateId | () => string | — | Makes the id of each new conversation. |
Returns
| Value | Type | Description |
|---|---|---|
conversations | ChatConversation[] | The conversations, in the order they're held. |
activeId | string | null | The open conversation's id. |
active | ChatConversation | undefined | The open conversation, when it's in the list. |
setActiveId | (activeId: string | null) => void | Opens a conversation, or none. |
create | (init?: { title?, metadata?, activate? }) => ChatConversation | Adds a conversation at the start of the list, opens it unless activate is false, and returns it. |
rename | (id: string, title: string) => void | Gives a conversation a new title. |
pin | (id: string, pinned?: boolean) => void | Pins a conversation, or unpins it with false. |
remove | (id: string) => void | Removes a conversation. Removing the open one leaves none open. |
update | (id: string, changes) => void | Changes a conversation's fields, and sets its updatedAt. |
setConversations | (next: ChatConversation[] | (previous) => ChatConversation[]) => void | Replaces the list. A function gets the newest list, and React may run it again, so keep it pure. |
Accessibility
- Make each conversation a real button or link, and mark the open one with
aria-current. - Name the buttons that act on a conversation with its title ("Delete Trip to Lisbon"), so each one says which conversation it acts on.
- Ask before removing a conversation, or offer an undo: removing one can't be taken back.
Related Components
useChat: One conversation's messages and its streamed replies.- AI kit: What the kit holds, and what's planned.