xonecas.io
5414
Sa

sat / files

sat / src / components / app-sidebar.tsx

5.3 KB
1"use client";
2
3import { MessageSquarePlus, Trash2 } from "lucide-react";
4import Link from "next/link";
5import { usePathname, useRouter } from "next/navigation";
6import { useEffect, useState, useTransition } from "react";
7import { toast } from "sonner";
8import {
9  deleteConversation,
10  getConversations,
11} from "@/app/actions/conversations";
12import { Brand } from "@/components/brand";
13import { ModeToggle } from "@/components/mode-toggle";
14import {
15  AlertDialog,
16  AlertDialogAction,
17  AlertDialogCancel,
18  AlertDialogContent,
19  AlertDialogDescription,
20  AlertDialogFooter,
21  AlertDialogHeader,
22  AlertDialogTitle,
23  AlertDialogTrigger,
24} from "@/components/ui/alert-dialog";
25import { Button } from "@/components/ui/button";
26import {
27  Sidebar,
28  SidebarContent,
29  SidebarFooter,
30  SidebarGroup,
31  SidebarGroupContent,
32  SidebarGroupLabel,
33  SidebarHeader,
34  SidebarMenu,
35  SidebarMenuAction,
36  SidebarMenuButton,
37  SidebarMenuItem,
38} from "@/components/ui/sidebar";
39import type { Conversation } from "@/db/schema";
40
41export function AppSidebar() {
42  const pathname = usePathname();
43  const router = useRouter();
44  const [conversations, setConversations] = useState<Conversation[]>([]);
45  const [isPending, startTransition] = useTransition();
46
47  // Load conversations on mount and when pathname changes (to catch new conversations)
48  useEffect(() => {
49    async function loadConversations() {
50      try {
51        const data = await getConversations();
52        setConversations(data);
53      } catch (error) {
54        console.error("Failed to load conversations:", error);
55      }
56    }
57
58    loadConversations();
59  }, [pathname]);
60
61  const handleDelete = async (id: string) => {
62    startTransition(async () => {
63      try {
64        await deleteConversation(id);
65        setConversations((prev) => prev.filter((c) => c.id !== id));
66        toast.success("Conversation deleted");
67
68        // If we're currently viewing the deleted conversation, redirect to home
69        if (pathname === `/conversation/${id}`) {
70          router.push("/");
71        }
72      } catch (error) {
73        console.error("Failed to delete conversation:", error);
74        toast.error("Failed to delete conversation");
75      }
76    });
77  };
78
79  // Extract conversation ID from pathname
80  const currentConversationId = pathname.startsWith("/conversation/")
81    ? pathname.split("/")[2]
82    : null;
83
84  return (
85    <Sidebar>
86      <SidebarHeader className="flex items-center justify-center">
87        <Brand />
88      </SidebarHeader>
89
90      <SidebarContent>
91        <SidebarGroup>
92          <SidebarGroupContent>
93            <Button asChild variant="outline" className="w-full justify-start">
94              <Link href="/">
95                <MessageSquarePlus className="size-4" />
96                New Conversation
97              </Link>
98            </Button>
99          </SidebarGroupContent>
100        </SidebarGroup>
101
102        {conversations.length > 0 && (
103          <SidebarGroup>
104            <SidebarGroupLabel>Conversations</SidebarGroupLabel>
105            <SidebarGroupContent>
106              <SidebarMenu>
107                {conversations.map((conversation) => (
108                  <SidebarMenuItem key={conversation.id}>
109                    <SidebarMenuButton
110                      asChild
111                      isActive={currentConversationId === conversation.id}
112                    >
113                      <Link href={`/conversation/${conversation.id}`}>
114                        <span className="truncate">
115                          {conversation.title || "Untitled"}
116                        </span>
117                      </Link>
118                    </SidebarMenuButton>
119
120                    <AlertDialog>
121                      <AlertDialogTrigger asChild>
122                        <SidebarMenuAction showOnHover>
123                          <Trash2 className="size-4 cursor-pointer" />
124                          <span className="sr-only">Delete conversation</span>
125                        </SidebarMenuAction>
126                      </AlertDialogTrigger>
127                      <AlertDialogContent size="sm">
128                        <AlertDialogHeader>
129                          <AlertDialogTitle>
130                            Delete conversation?
131                          </AlertDialogTitle>
132                          <AlertDialogDescription>
133                            This will permanently delete this conversation and
134                            all its messages. This action cannot be undone.
135                          </AlertDialogDescription>
136                        </AlertDialogHeader>
137                        <AlertDialogFooter>
138                          <AlertDialogCancel>Cancel</AlertDialogCancel>
139                          <AlertDialogAction
140                            variant="destructive"
141                            onClick={() => handleDelete(conversation.id)}
142                            disabled={isPending}
143                          >
144                            Delete
145                          </AlertDialogAction>
146                        </AlertDialogFooter>
147                      </AlertDialogContent>
148                    </AlertDialog>
149                  </SidebarMenuItem>
150                ))}
151              </SidebarMenu>
152            </SidebarGroupContent>
153          </SidebarGroup>
154        )}
155      </SidebarContent>
156
157      <SidebarFooter>
158        <ModeToggle />
159      </SidebarFooter>
160    </Sidebar>
161  );
162}