xonecas.io
53129
Pa

PAT / commit beca37c

feat: move the delete thread button

xonecas2025-12-18 beca37c0442a279c757c59506add19a806f153ed parent 210663a

10 files changed

app/api/threads/[id]/messages/[messageId]/route.ts+0−1
@@ -35,4 +35,3 @@ export async function DELETE(
3535 return NextResponse.json({ error: "Failed to delete message" }, { status: 500 });
3636 }
3737 }
38-
app/api/threads/[id]/route.ts+2−9
@@ -3,10 +3,7 @@ import { db } from "@/src/lib/db";
33 import { threads } from "@/src/lib/db/schema";
44 import { eq } from "drizzle-orm";
55
6-export async function DELETE(
7- request: Request,
8- { params }: { params: Promise<{ id: string }> }
9-) {
6+export async function DELETE(request: Request, { params }: { params: Promise<{ id: string }> }) {
107 try {
118 const { id } = await params;
129 const threadId = parseInt(id);
@@ -15,10 +12,7 @@ export async function DELETE(
1512 }
1613
1714 // Delete the thread (messages will be cascade deleted due to foreign key constraint)
18- const deletedThread = await db
19- .delete(threads)
20- .where(eq(threads.id, threadId))
21- .returning();
15+ const deletedThread = await db.delete(threads).where(eq(threads.id, threadId)).returning();
2216
2317 if (deletedThread.length === 0) {
2418 return NextResponse.json({ error: "Thread not found" }, { status: 404 });
@@ -30,4 +24,3 @@ export async function DELETE(
3024 return NextResponse.json({ error: "Failed to delete thread" }, { status: 500 });
3125 }
3226 }
33-
app/page.tsx+25−2
@@ -16,8 +16,15 @@ export default function Home() {
1616 const { themeMode, handleThemeChange } = useTheme();
1717 const { selectedModel, handleModelChange } = useModel();
1818 const { maxPromptLength, handleMaxPromptLengthChange } = useMaxPromptLength();
19- const { threads, totalThreadCount, hasMoreThreads, loadThreads, loadMoreThreads, createThread } =
20- useThreads();
19+ const {
20+ threads,
21+ totalThreadCount,
22+ hasMoreThreads,
23+ loadThreads,
24+ loadMoreThreads,
25+ createThread,
26+ deleteThread,
27+ } = useThreads();
2128 const { currentThreadId, selectThread, deselectThread, messagesContainerRef } =
2229 useThreadSelection();
2330 const {
@@ -45,6 +52,19 @@ export default function Home() {
4552 }, 0);
4653 };
4754
55+ const handleThreadDelete = async (threadId: number) => {
56+ try {
57+ await deleteThread(threadId);
58+ // If the deleted thread was the current thread, deselect it
59+ if (currentThreadId === threadId) {
60+ deselectThread();
61+ clearMessages();
62+ }
63+ } catch (error) {
64+ console.error("Failed to delete thread", error);
65+ }
66+ };
67+
4868 const handleSendMessage = async (message: string) => {
4969 await sendMessage(
5070 message,
@@ -96,6 +116,9 @@ export default function Home() {
96116 modelName={currentModelName}
97117 isStreaming={isLoading && streamingMessageId !== null}
98118 onStop={stopGeneration}
119+ currentThreadId={currentThreadId}
120+ currentThread={currentThreadId !== null ? threads.find((t) => t.id === currentThreadId) || null : null}
121+ onThreadDelete={handleThreadDelete}
99122 />
100123 </div>
101124 <Sidebar
src/components/DeleteConfirmationModal.tsx+62−0
@@ -0,0 +1,62 @@
1+"use client";
2+
3+import Modal from "@/src/components/Modal";
4+import SecondaryButton from "@/src/components/buttons/SecondaryButton";
5+import PrimaryButton from "@/src/components/buttons/PrimaryButton";
6+
7+type DeleteConfirmationModalProps = {
8+ isOpen: boolean;
9+ onClose: () => void;
10+ onConfirm: () => void | Promise<void>;
11+ title: string;
12+ message: string;
13+ isDeleting?: boolean;
14+ confirmLabel?: string;
15+ cancelLabel?: string;
16+};
17+
18+export default function DeleteConfirmationModal({
19+ isOpen,
20+ onClose,
21+ onConfirm,
22+ title,
23+ message,
24+ isDeleting = false,
25+ confirmLabel = "Delete",
26+ cancelLabel = "Cancel",
27+}: DeleteConfirmationModalProps) {
28+ const handleConfirm = async () => {
29+ await onConfirm();
30+ };
31+
32+ return (
33+ <Modal isOpen={isOpen} onClose={onClose}>
34+ <div className="flex flex-col gap-4">
35+ <h2 className="text-xl font-semibold">{title}</h2>
36+ <p className="text-neutral-600 dark:text-neutral-400">{message}</p>
37+ <div className="flex justify-end gap-2">
38+ <SecondaryButton
39+ onClick={(e) => {
40+ e.preventDefault();
41+ if (!isDeleting) {
42+ onClose();
43+ }
44+ }}
45+ >
46+ {cancelLabel}
47+ </SecondaryButton>
48+ <PrimaryButton
49+ onClick={(e) => {
50+ e.preventDefault();
51+ handleConfirm();
52+ }}
53+ disabled={isDeleting}
54+ color="red"
55+ >
56+ {isDeleting ? "Deleting..." : confirmLabel}
57+ </PrimaryButton>
58+ </div>
59+ </div>
60+ </Modal>
61+ );
62+}
src/components/DeleteThreadButton.tsx+56−0
@@ -0,0 +1,56 @@
1+"use client";
2+
3+import { useState } from "react";
4+import TrashIcon from "@/src/components/icons/TrashIcon";
5+import DeleteConfirmationModal from "@/src/components/DeleteConfirmationModal";
6+
7+type DeleteThreadButtonProps = {
8+ threadId: number;
9+ threadTitle: string;
10+ onDeleteThread: (threadId: number) => void;
11+};
12+
13+export default function DeleteThreadButton({
14+ threadId,
15+ threadTitle,
16+ onDeleteThread,
17+}: DeleteThreadButtonProps) {
18+ const [isModalOpen, setIsModalOpen] = useState(false);
19+ const [isDeleting, setIsDeleting] = useState(false);
20+
21+ const handleDelete = async () => {
22+ setIsDeleting(true);
23+ try {
24+ await onDeleteThread(threadId);
25+ setIsModalOpen(false);
26+ } catch (error) {
27+ console.error("Failed to delete thread", error);
28+ } finally {
29+ setIsDeleting(false);
30+ }
31+ };
32+
33+ return (
34+ <>
35+ <button
36+ onClick={(e) => {
37+ e.preventDefault();
38+ e.stopPropagation();
39+ setIsModalOpen(true);
40+ }}
41+ className="flex items-center justify-center rounded p-1 text-neutral-600 hover:bg-neutral-200 hover:text-neutral-800 dark:text-neutral-400 dark:hover:bg-neutral-800 dark:hover:text-neutral-200"
42+ aria-label="Delete thread"
43+ >
44+ <TrashIcon className="h-4 w-4" />
45+ </button>
46+ <DeleteConfirmationModal
47+ isOpen={isModalOpen}
48+ onClose={() => setIsModalOpen(false)}
49+ onConfirm={handleDelete}
50+ title="Delete Thread"
51+ message={`Are you sure you want to delete "${threadTitle || `Thread ${threadId}`}"? This action cannot be undone.`}
52+ isDeleting={isDeleting}
53+ />
54+ </>
55+ );
56+}
src/components/MessageInput.tsx+22−1
@@ -4,7 +4,9 @@ import { useState, useRef, useImperativeHandle, forwardRef, useEffect } from "re
44 import PaperPlaneIcon from "@/src/components/icons/PaperPlaneIcon";
55 import PrimaryButton from "@/src/components/buttons/PrimaryButton";
66 import ThinkingNotification from "@/src/components/ThinkingNotification";
7+import DeleteThreadButton from "@/src/components/DeleteThreadButton";
78 import type { Message } from "@/src/lib/db/schema";
9+import type { Thread } from "@/src/lib/db/schema";
810
911 type MessageInputProps = {
1012 isLoading: boolean;
@@ -14,6 +16,9 @@ type MessageInputProps = {
1416 modelName?: string;
1517 isStreaming?: boolean;
1618 onStop?: () => void;
19+ currentThreadId: number | null;
20+ currentThread: Thread | null;
21+ onThreadDelete?: (threadId: number) => void;
1722 };
1823
1924 export type MessageInputRef = {
@@ -30,6 +35,9 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
3035 modelName = "gpt-oss",
3136 isStreaming = false,
3237 onStop,
38+ currentThreadId,
39+ currentThread,
40+ onThreadDelete,
3341 },
3442 ref
3543 ) => {
@@ -129,7 +137,20 @@ const MessageInput = forwardRef<MessageInputRef, MessageInputProps>(
129137 return (
130138 <div className="pointer-events-none absolute bottom-0 left-0 right-0 z-10 flex justify-center p-1">
131139 <div className="pointer-events-auto m-4 w-full max-w-5xl">
132- {isStreaming && onStop && <ThinkingNotification onStop={onStop} />}
140+ <div className="mb-2 flex items-start justify-between gap-2">
141+ <div className="flex-1">
142+ {isStreaming && onStop && <ThinkingNotification onStop={onStop} />}
143+ </div>
144+ {currentThreadId !== null && currentThread && onThreadDelete && (
145+ <div className="flex-shrink-0">
146+ <DeleteThreadButton
147+ threadId={currentThreadId}
148+ threadTitle={currentThread.title || `Thread ${currentThreadId}`}
149+ onDeleteThread={onThreadDelete}
150+ />
151+ </div>
152+ )}
153+ </div>
133154 <form
134155 onSubmit={handleSubmit}
135156 className="relative rounded-lg bg-neutral-300 shadow-lg dark:bg-neutral-700"
src/components/MessageList/AssistantMessage.tsx+1−4
@@ -11,10 +11,7 @@ type AssistantMessageProps = {
1111 isStreaming?: boolean;
1212 };
1313
14-export default function AssistantMessage({
15- message,
16- isStreaming = false,
17-}: AssistantMessageProps) {
14+export default function AssistantMessage({ message, isStreaming = false }: AssistantMessageProps) {
1815 const formatTimestamp = (date: Date | string): string => {
1916 const d = typeof date === "string" ? new Date(date) : date;
2017 return d.toLocaleString();
src/components/MessageList/DeleteMessageButton.tsx+0−1
@@ -37,4 +37,3 @@ export default function DeleteMessageButton({
3737 </div>
3838 );
3939 }
40-
src/components/icons/TrashIcon.tsx+0−1
@@ -16,4 +16,3 @@ export default function TrashIcon({ className }: { className?: string }) {
1616 </svg>
1717 );
1818 }
19-
src/hooks/useThreads.ts+18−0
@@ -81,6 +81,23 @@ export function useThreads() {
8181 }
8282 };
8383
84+ const deleteThread = async (threadId: number): Promise<void> => {
85+ try {
86+ const res = await fetch(`/api/threads/${threadId}`, {
87+ method: "DELETE",
88+ });
89+ if (!res.ok) {
90+ throw new Error("Failed to delete thread");
91+ }
92+ // Remove the thread from the local state
93+ setThreads((prev) => prev.filter((t) => t.id !== threadId));
94+ setTotalThreadCount((prev) => Math.max(0, prev - 1));
95+ } catch (error) {
96+ console.error("Failed to delete thread", error);
97+ throw error;
98+ }
99+ };
100+
84101 useEffect(() => {
85102 loadThreads(8);
86103 }, []);
@@ -92,5 +109,6 @@ export function useThreads() {
92109 loadThreads,
93110 loadMoreThreads,
94111 createThread,
112+ deleteThread,
95113 };
96114 }