Skip to content

Commit

Permalink
memoize history item
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyphilemon committed Dec 3, 2024
1 parent dd9fb88 commit 4bbd638
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions components/sidebar-history.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'use client';
"use client";

import { isToday, isYesterday, subMonths, subWeeks } from 'date-fns';
import Link from 'next/link';
import { useParams, usePathname, useRouter } from 'next/navigation';
import type { User } from 'next-auth';
import { useEffect, useState } from 'react';
import { toast } from 'sonner';
import useSWR from 'swr';
import { isToday, isYesterday, subMonths, subWeeks } from "date-fns";
import Link from "next/link";
import { useParams, usePathname, useRouter } from "next/navigation";
import type { User } from "next-auth";
import { memo, useEffect, useState } from "react";
import { toast } from "sonner";
import useSWR from "swr";

import { MoreHorizontalIcon, TrashIcon } from '@/components/icons';
import { MoreHorizontalIcon, TrashIcon } from "@/components/icons";
import {
AlertDialog,
AlertDialogAction,
Expand All @@ -18,13 +18,13 @@ import {
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
} from "@/components/ui/alert-dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
} from "@/components/ui/dropdown-menu";
import {
SidebarGroup,
SidebarGroupContent,
Expand All @@ -33,9 +33,10 @@ import {
SidebarMenuButton,
SidebarMenuItem,
useSidebar,
} from '@/components/ui/sidebar';
import type { Chat } from '@/lib/db/schema';
import { fetcher } from '@/lib/utils';
} from "@/components/ui/sidebar";
import type { Chat } from "@/lib/db/schema";
import { fetcher } from "@/lib/utils";
import equal from "fast-deep-equal";

type GroupedChats = {
today: Chat[];
Expand All @@ -45,7 +46,7 @@ type GroupedChats = {
older: Chat[];
};

const ChatItem = ({
const PureChatItem = ({
chat,
isActive,
onDelete,
Expand All @@ -62,6 +63,7 @@ const ChatItem = ({
<span>{chat.title}</span>
</Link>
</SidebarMenuButton>

<DropdownMenu modal={true}>
<DropdownMenuTrigger asChild>
<SidebarMenuAction
Expand All @@ -85,6 +87,11 @@ const ChatItem = ({
</SidebarMenuItem>
);

export const ChatItem = memo(PureChatItem, (prevProps, nextProps) => {
if (prevProps.isActive !== nextProps.isActive) return false;
return true;
});

export function SidebarHistory({ user }: { user: User | undefined }) {
const { setOpenMobile } = useSidebar();
const { id } = useParams();
Expand All @@ -93,7 +100,7 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
data: history,
isLoading,
mutate,
} = useSWR<Array<Chat>>(user ? '/api/history' : null, fetcher, {
} = useSWR<Array<Chat>>(user ? "/api/history" : null, fetcher, {
fallbackData: [],
});

Expand All @@ -106,26 +113,26 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
const router = useRouter();
const handleDelete = async () => {
const deletePromise = fetch(`/api/chat?id=${deleteId}`, {
method: 'DELETE',
method: "DELETE",
});

toast.promise(deletePromise, {
loading: 'Deleting chat...',
loading: "Deleting chat...",
success: () => {
mutate((history) => {
if (history) {
return history.filter((h) => h.id !== id);
}
});
return 'Chat deleted successfully';
return "Chat deleted successfully";
},
error: 'Failed to delete chat',
error: "Failed to delete chat",
});

setShowDeleteDialog(false);

if (deleteId === id) {
router.push('/');
router.push("/");
}
};

Expand Down Expand Up @@ -158,7 +165,7 @@ export function SidebarHistory({ user }: { user: User | undefined }) {
className="h-4 rounded-md flex-1 max-w-[--skeleton-width] bg-sidebar-accent-foreground/10"
style={
{
'--skeleton-width': `${item}%`,
"--skeleton-width": `${item}%`,
} as React.CSSProperties
}
/>
Expand Down

0 comments on commit 4bbd638

Please sign in to comment.