Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/(chat)/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getTitleModel } from "@/lib/ai/providers";
import {
deleteMessagesByChatIdAfterTimestamp,
getMessageById,
updateChatTitleById,
updateChatVisibilityById,
} from "@/lib/db/queries";
import { getTextFromMessage } from "@/lib/utils";
Expand Down Expand Up @@ -49,3 +50,13 @@ export async function updateChatVisibility({
}) {
await updateChatVisibilityById({ chatId, visibility });
}

export async function updateChatTitle({
chatId,
title,
}: {
chatId: string;
title: string;
}) {
await updateChatTitleById({ chatId, title });
}
14 changes: 14 additions & 0 deletions components/sidebar-history-item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
GlobeIcon,
LockIcon,
MoreHorizontalIcon,
PencilEditIcon,
ShareIcon,
TrashIcon,
} from "./icons";
Expand All @@ -30,11 +31,13 @@ const PureChatItem = ({
chat,
isActive,
onDelete,
onRename,
setOpenMobile,
}: {
chat: Chat;
isActive: boolean;
onDelete: (chatId: string) => void;
onRename: (chatId: string, currentTitle: string) => void;
setOpenMobile: (open: boolean) => void;
}) => {
const { visibilityType, setVisibilityType } = useChatVisibility({
Expand Down Expand Up @@ -62,6 +65,14 @@ const PureChatItem = ({
</DropdownMenuTrigger>

<DropdownMenuContent align="end" side="bottom">
<DropdownMenuItem
className="cursor-pointer"
onSelect={() => onRename(chat.id, chat.title)}
>
<PencilEditIcon />
<span>Rename</span>
</DropdownMenuItem>

<DropdownMenuSub>
<DropdownMenuSubTrigger className="cursor-pointer">
<ShareIcon />
Expand Down Expand Up @@ -116,5 +127,8 @@ export const ChatItem = memo(PureChatItem, (prevProps, nextProps) => {
if (prevProps.isActive !== nextProps.isActive) {
return false;
}
if (prevProps.chat.title !== nextProps.chat.title) {
return false;
}
return true;
});
99 changes: 99 additions & 0 deletions components/sidebar-history.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { User } from "next-auth";
import { useState } from "react";
import { toast } from "sonner";
import useSWRInfinite from "swr/infinite";
import { updateChatTitle } from "@/app/(chat)/actions";
import {
AlertDialog,
AlertDialogAction,
Expand All @@ -17,6 +18,16 @@ import {
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import {
SidebarGroup,
SidebarGroupContent,
Expand Down Expand Up @@ -116,6 +127,10 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
const [deleteId, setDeleteId] = useState<string | null>(null);
const [showDeleteDialog, setShowDeleteDialog] = useState(false);

const [renameId, setRenameId] = useState<string | null>(null);
const [showRenameDialog, setShowRenameDialog] = useState(false);
const [newTitle, setNewTitle] = useState("");

const hasReachedEnd = paginatedChatHistories
? paginatedChatHistories.some((page) => page.hasMore === false)
: false;
Expand Down Expand Up @@ -159,6 +174,32 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
});
};

const handleRename = () => {
if (!renameId || !newTitle.trim()) {
setShowRenameDialog(false);
return;
}

const trimmedTitle = newTitle.trim();
setShowRenameDialog(false);

mutate(
(chatHistories) => {
if (chatHistories) {
return chatHistories.map((chatHistory) => ({
...chatHistory,
chats: chatHistory.chats.map((chat) =>
chat.id === renameId ? { ...chat, title: trimmedTitle } : chat
),
}));
}
},
{ revalidate: false }
);

updateChatTitle({ chatId: renameId, title: trimmedTitle });
};

if (!user) {
return (
<SidebarGroup>
Expand Down Expand Up @@ -241,6 +282,11 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
setDeleteId(chatId);
setShowDeleteDialog(true);
}}
onRename={(chatId, currentTitle) => {
setRenameId(chatId);
setNewTitle(currentTitle);
setShowRenameDialog(true);
}}
setOpenMobile={setOpenMobile}
/>
))}
Expand All @@ -261,6 +307,11 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
setDeleteId(chatId);
setShowDeleteDialog(true);
}}
onRename={(chatId, currentTitle) => {
setRenameId(chatId);
setNewTitle(currentTitle);
setShowRenameDialog(true);
}}
setOpenMobile={setOpenMobile}
/>
))}
Expand All @@ -281,6 +332,11 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
setDeleteId(chatId);
setShowDeleteDialog(true);
}}
onRename={(chatId, currentTitle) => {
setRenameId(chatId);
setNewTitle(currentTitle);
setShowRenameDialog(true);
}}
setOpenMobile={setOpenMobile}
/>
))}
Expand All @@ -301,6 +357,11 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
setDeleteId(chatId);
setShowDeleteDialog(true);
}}
onRename={(chatId, currentTitle) => {
setRenameId(chatId);
setNewTitle(currentTitle);
setShowRenameDialog(true);
}}
setOpenMobile={setOpenMobile}
/>
))}
Expand All @@ -321,6 +382,11 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
setDeleteId(chatId);
setShowDeleteDialog(true);
}}
onRename={(chatId, currentTitle) => {
setRenameId(chatId);
setNewTitle(currentTitle);
setShowRenameDialog(true);
}}
setOpenMobile={setOpenMobile}
/>
))}
Expand Down Expand Up @@ -371,6 +437,39 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>

<Dialog onOpenChange={setShowRenameDialog} open={showRenameDialog}>
<DialogContent>
<DialogHeader>
<DialogTitle>Rename chat</DialogTitle>
<DialogDescription>
Enter a new name for this chat.
</DialogDescription>
</DialogHeader>
<Input
value={newTitle}
onChange={(e) => setNewTitle(e.target.value)}
placeholder="Chat title"
onKeyDown={(e) => {
if (e.key === "Enter") {
handleRename();
}
}}
/>
<DialogFooter>
<Button
type="button"
variant="outline"
onClick={() => setShowRenameDialog(false)}
>
Cancel
</Button>
<Button type="button" onClick={handleRename}>
Save
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
</>
);
}