Skip to content

Commit 398bb07

Browse files
committed
チャット削除を実装
1 parent 4907897 commit 398bb07

5 files changed

Lines changed: 49 additions & 11 deletions

File tree

app/(docs)/@chat/chat/[chatId]/chatArea.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"use client";
22

3+
import { useChatHistoryContext } from "@/(docs)/@docs/[lang]/[pageId]/chatHistory";
34
import { ChatAreaStateUpdater } from "@/(docs)/chatAreaState";
4-
import { getChatOne } from "@/lib/chatHistory";
5+
import { deleteChatAction } from "@/actions/deleteChat";
6+
import { ChatWithMessages } from "@/lib/chatHistory";
57
import { LanguageEntry, MarkdownSection, PageEntry } from "@/lib/docs";
68
import { Heading } from "@/markdown/heading";
79
import { StyledMarkdown } from "@/markdown/markdown";
@@ -10,7 +12,7 @@ import Link from "next/link";
1012

1113
interface Props {
1214
chatId: string;
13-
chatData: Awaited<ReturnType<typeof getChatOne>>;
15+
chatData: ChatWithMessages;
1416
targetLang: LanguageEntry | undefined;
1517
targetPage: PageEntry | undefined;
1618
targetSection: MarkdownSection | undefined;
@@ -26,6 +28,10 @@ export function ChatAreaContent(props: Props) {
2628
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
2729
);
2830

31+
// useChatHistoryContext must be used within a ChatHistoryProvider
32+
// const { deleteChat } = useChatHistoryContext();
33+
34+
2935
return (
3036
<aside
3137
className={clsx(
@@ -89,7 +95,12 @@ export function ChatAreaContent(props: Props) {
8995
</div>
9096
<button
9197
className="btn btn-error btn-soft btn-sm"
92-
onClick={() => alert("TODO: チャットの削除機能")}
98+
onClick={async () => {
99+
if (confirm("このチャットを削除してもよろしいですか?")) {
100+
await deleteChatAction(chatId);
101+
// deleteChat(chatId);
102+
}
103+
}}
93104
>
94105
{/*<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->*/}
95106
<svg

app/(docs)/@chat/chat/[chatId]/page.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { getChatOne, initContext } from "@/lib/chatHistory";
22
import { getMarkdownSections, getPagesList } from "@/lib/docs";
33
import { ChatAreaContent } from "./chatArea";
4+
import { notFound } from "next/navigation";
45

56
export default async function ChatPage({
67
params,
@@ -12,6 +13,10 @@ export default async function ChatPage({
1213
const ctx = await initContext();
1314
const chatData = await getChatOne(chatId, ctx);
1415

16+
if (!chatData) {
17+
notFound();
18+
}
19+
1520
const pagesList = await getPagesList();
1621
const targetLang = pagesList.find(
1722
(lang) => lang.id === chatData.section.pagePath.split("/")[0]

app/(docs)/@docs/[lang]/[pageId]/chatHistory.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { PagePath } from "@/lib/docs";
66
import {
77
createContext,
88
ReactNode,
9+
useCallback,
910
useContext,
1011
useEffect,
1112
useState,
@@ -15,6 +16,7 @@ import useSWR from "swr";
1516
export interface IChatHistoryContext {
1617
chatHistories: ChatWithMessages[];
1718
addChat: (chat: ChatWithMessages) => void;
19+
deleteChat: (chatId: string) => void;
1820
// updateChat: (sectionId: string, chatId: string, message: ChatMessage) => void;
1921
}
2022
const ChatHistoryContext = createContext<IChatHistoryContext | null>(null);
@@ -61,12 +63,15 @@ export function ChatHistoryProvider({
6163
}, [fetchedChatHistories]);
6264

6365
// チャットを更新した際にはクライアントサイドでchatHistoryに反映する
64-
const addChat = (chat: ChatWithMessages) => {
66+
const addChat = useCallback((chat: ChatWithMessages) => {
6567
setChatHistories([...chatHistories, chat]);
66-
};
68+
}, []);
69+
const deleteChat = useCallback((chatId: string) => {
70+
setChatHistories(chatHistories.filter((chat) => chat.chatId !== chatId));
71+
}, []);
6772

6873
return (
69-
<ChatHistoryContext.Provider value={{ chatHistories, addChat }}>
74+
<ChatHistoryContext.Provider value={{ chatHistories, addChat, deleteChat }}>
7075
{children}
7176
</ChatHistoryContext.Provider>
7277
);

app/actions/deleteChat.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"use server";
2+
3+
import { initContext } from "@/lib/chatHistory";
4+
import { chat, diff, message } from "@/schema/chat";
5+
import { and, eq } from "drizzle-orm";
6+
7+
export async function deleteChatAction(chatId: string) {
8+
const ctx = await initContext();
9+
if (!ctx.userId) {
10+
throw new Error("Not authenticated");
11+
}
12+
const deletedUser = await ctx.drizzle
13+
.delete(chat)
14+
.where(and(eq(chat.chatId, chatId), eq(chat.userId, ctx.userId)))
15+
.returning();
16+
if (deletedUser.length === 0) {
17+
throw new Error("Chat not found or not authorized");
18+
}
19+
await ctx.drizzle.delete(message).where(eq(message.chatId, chatId));
20+
await ctx.drizzle.delete(diff).where(eq(diff.chatId, chatId));
21+
}

app/lib/chatHistory.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,7 @@ export async function getChatOne(chatId: string, context: Context) {
191191
orderBy: [asc(diff.createdAt)],
192192
},
193193
},
194-
})) as typeof chat.$inferSelect & {
195-
section: typeof section.$inferSelect;
196-
messages: (typeof message.$inferSelect)[];
197-
diff: (typeof diff.$inferSelect)[];
198-
};
194+
})) as ChatWithMessages | undefined;
199195
}
200196

201197
export async function migrateChatUser(oldUserId: string, newUserId: string) {

0 commit comments

Comments
 (0)