Skip to content

Commit 842caa5

Browse files
committed
chatHistoryをcontextで管理するのをやめる & 削除時のcacheクリア
1 parent 398bb07 commit 842caa5

8 files changed

Lines changed: 135 additions & 168 deletions

File tree

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

Lines changed: 6 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
"use client";
22

3-
import { useChatHistoryContext } from "@/(docs)/@docs/[lang]/[pageId]/chatHistory";
4-
import { ChatAreaStateUpdater } from "@/(docs)/chatAreaState";
53
import { deleteChatAction } from "@/actions/deleteChat";
64
import { ChatWithMessages } from "@/lib/chatHistory";
75
import { LanguageEntry, MarkdownSection, PageEntry } from "@/lib/docs";
86
import { Heading } from "@/markdown/heading";
97
import { StyledMarkdown } from "@/markdown/markdown";
108
import clsx from "clsx";
119
import Link from "next/link";
10+
import { useRouter } from "next/navigation";
1211

1312
interface Props {
1413
chatId: string;
@@ -28,45 +27,10 @@ export function ChatAreaContent(props: Props) {
2827
(a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()
2928
);
3029

31-
// useChatHistoryContext must be used within a ChatHistoryProvider
32-
// const { deleteChat } = useChatHistoryContext();
33-
30+
const router = useRouter();
3431

3532
return (
36-
<aside
37-
className={clsx(
38-
// モバイルでは全画面表示する
39-
"fixed inset-0 pt-20 bg-base-100",
40-
// PCではスクロールで動かない右サイドバー
41-
"lg:sticky lg:top-0 lg:pt-4 lg:w-1/3 lg:h-screen lg:shadow-md lg:bg-base-200 ",
42-
"p-4",
43-
"flex flex-col",
44-
"overflow-y-auto"
45-
)}
46-
>
47-
<ChatAreaStateUpdater chatId={chatId} />
48-
<div className="flex flex-row items-center">
49-
<span className="flex-1 text-base font-bold opacity-40">
50-
AIへの質問
51-
</span>
52-
<Link className="btn btn-ghost" href="/chat" scroll={false}>
53-
<svg
54-
className="w-8 h-8 -scale-x-100"
55-
viewBox="0 0 24 24"
56-
fill="none"
57-
xmlns="http://www.w3.org/2000/svg"
58-
>
59-
<path
60-
d="M18 17L13 12L18 7M11 17L6 12L11 7"
61-
stroke="currentColor"
62-
strokeWidth="1.5"
63-
strokeLinecap="round"
64-
strokeLinejoin="round"
65-
/>
66-
</svg>
67-
<span className="text-lg">閉じる</span>
68-
</Link>
69-
</div>
33+
<>
7034
<Heading level={2} className="mt-2!">
7135
{chatData.title}
7236
</Heading>
@@ -98,7 +62,8 @@ export function ChatAreaContent(props: Props) {
9862
onClick={async () => {
9963
if (confirm("このチャットを削除してもよろしいですか?")) {
10064
await deleteChatAction(chatId);
101-
// deleteChat(chatId);
65+
router.push("/chat", { scroll: false });
66+
router.refresh();
10267
}
10368
}}
10469
>
@@ -191,6 +156,6 @@ export function ChatAreaContent(props: Props) {
191156
</div>
192157
)
193158
)}
194-
</aside>
159+
</>
195160
);
196161
}

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
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";
4+
import { ChatAreaStateUpdater } from "@/(docs)/chatAreaState";
5+
import { ReactNode } from "react";
6+
import clsx from "clsx";
7+
import Link from "next/link";
58

69
export default async function ChatPage({
710
params,
@@ -14,7 +17,12 @@ export default async function ChatPage({
1417
const chatData = await getChatOne(chatId, ctx);
1518

1619
if (!chatData) {
17-
notFound();
20+
// notFound(); だとページ全体が404になってしまう
21+
return (
22+
<ChatAreaContainer chatId={chatId}>
23+
<p>指定されたチャットのデータが見つかりません。</p>
24+
</ChatAreaContainer>
25+
);
1826
}
1927

2028
const pagesList = await getPagesList();
@@ -28,12 +36,55 @@ export default async function ChatPage({
2836
const targetSection = sections.find((sec) => sec.id === chatData.sectionId);
2937

3038
return (
31-
<ChatAreaContent
32-
chatId={chatId}
33-
chatData={chatData}
34-
targetLang={targetLang}
35-
targetPage={targetPage}
36-
targetSection={targetSection}
37-
/>
39+
<ChatAreaContainer chatId={chatId}>
40+
<ChatAreaContent
41+
chatId={chatId}
42+
chatData={chatData}
43+
targetLang={targetLang}
44+
targetPage={targetPage}
45+
targetSection={targetSection}
46+
/>
47+
</ChatAreaContainer>
48+
);
49+
}
50+
51+
function ChatAreaContainer(props: { chatId: string; children: ReactNode }) {
52+
return (
53+
<aside
54+
className={clsx(
55+
// モバイルでは全画面表示する
56+
"fixed inset-0 pt-20 bg-base-100",
57+
// PCではスクロールで動かない右サイドバー
58+
"lg:sticky lg:top-0 lg:pt-4 lg:w-1/3 lg:h-screen lg:shadow-md lg:bg-base-200 ",
59+
"p-4",
60+
"flex flex-col",
61+
"overflow-y-auto"
62+
)}
63+
>
64+
<ChatAreaStateUpdater chatId={props.chatId} />
65+
<div className="flex flex-row items-center">
66+
<span className="flex-1 text-base font-bold opacity-40">
67+
AIへの質問
68+
</span>
69+
<Link className="btn btn-ghost" href="/chat" scroll={false}>
70+
<svg
71+
className="w-8 h-8 -scale-x-100"
72+
viewBox="0 0 24 24"
73+
fill="none"
74+
xmlns="http://www.w3.org/2000/svg"
75+
>
76+
<path
77+
d="M18 17L13 12L18 7M11 17L6 12L11 7"
78+
stroke="currentColor"
79+
strokeWidth="1.5"
80+
strokeLinecap="round"
81+
strokeLinejoin="round"
82+
/>
83+
</svg>
84+
<span className="text-lg">閉じる</span>
85+
</Link>
86+
</div>
87+
{props.children}
88+
</aside>
3889
);
3990
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { useState, FormEvent, useEffect } from "react";
99
// import { getLanguageName } from "../pagesList";
1010
import { DynamicMarkdownSection } from "./pageContent";
1111
import { useEmbedContext } from "@/terminal/embedContext";
12-
import { useChatHistoryContext } from "./chatHistory";
1312
import { askAI } from "@/actions/chatActions";
1413
import { PagePath } from "@/lib/docs";
1514
import { useRouter } from "next/navigation";
@@ -26,8 +25,6 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
2625
const [isLoading, setIsLoading] = useState(false);
2726
const [errorMessage, setErrorMessage] = useState<string | null>(null);
2827

29-
const { addChat } = useChatHistoryContext();
30-
3128
// const lang = getLanguageName(docs_id);
3229

3330
const { files, replOutputs, execResults } = useEmbedContext();
@@ -90,11 +87,11 @@ export function ChatForm({ path, sectionContent, close }: ChatFormProps) {
9087
setErrorMessage(result.error);
9188
console.log(result.error);
9289
} else {
93-
addChat(result.chat);
9490
document.getElementById(result.chat.sectionId)?.scrollIntoView({
9591
behavior: "smooth",
9692
});
9793
router.push(`/chat/${result.chat.chatId}`, { scroll: false });
94+
router.refresh();
9895
setInputValue("");
9996
close();
10097
}

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

Lines changed: 0 additions & 78 deletions
This file was deleted.

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import { Metadata } from "next";
22
import { notFound } from "next/navigation";
33
import { PageContent } from "./pageContent";
4-
import { ChatHistoryProvider } from "./chatHistory";
54
import {
65
cacheKeyForPage,
76
ChatWithMessages,
87
getAllChat,
9-
getChatFromCache,
108
initContext,
119
} from "@/lib/chatHistory";
1210
import {
@@ -60,22 +58,18 @@ export default async function Page({
6058
const sections = await getMarkdownSections(lang, pageId);
6159

6260
const context = await initContext();
63-
const initialChatHistories = await getChatFromCache(path, context.userId);
61+
const chatHistories = await getChatFromCache(path, context.userId);
6462

6563
return (
66-
<ChatHistoryProvider
67-
initialChatHistories={initialChatHistories}
64+
<PageContent
65+
chatHistories={chatHistories}
66+
splitMdContent={sections}
67+
langEntry={langEntry}
68+
pageEntry={pageEntry}
69+
prevPage={prevPage}
70+
nextPage={nextPage}
6871
path={path}
69-
>
70-
<PageContent
71-
splitMdContent={sections}
72-
langEntry={langEntry}
73-
pageEntry={pageEntry}
74-
prevPage={prevPage}
75-
nextPage={nextPage}
76-
path={path}
77-
/>
78-
</ChatHistoryProvider>
72+
/>
7973
);
8074
}
8175

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { ReplacedRange } from "@/markdown/multiHighlight";
1818
import { Heading } from "@/markdown/heading";
1919
import Link from "next/link";
2020
import { useChatId } from "@/(docs)/chatAreaState";
21+
import { ChatWithMessages } from "@/lib/chatHistory";
2122

2223
/**
2324
* MarkdownSectionに追加で、動的な情報を持たせる
@@ -41,12 +42,11 @@ interface PageContentProps {
4142
prevPage?: PageEntry;
4243
nextPage?: PageEntry;
4344
path: PagePath;
45+
chatHistories: ChatWithMessages[];
4446
}
4547
export function PageContent(props: PageContentProps) {
4648
const { setSidebarMdContent } = useSidebarMdContext();
47-
const { splitMdContent, pageEntry, path } = props;
48-
49-
const { chatHistories } = useChatHistoryContext();
49+
const { splitMdContent, pageEntry, path, chatHistories } = props;
5050

5151
const initDynamicMdContent = useCallback(() => {
5252
const newContent: DynamicMarkdownSection[] = splitMdContent.map(
@@ -192,6 +192,7 @@ export function PageContent(props: PageContentProps) {
192192
<ChatListForSection
193193
sectionId={section.id}
194194
dynamicMdContent={dynamicMdContent}
195+
chatHistories={chatHistories}
195196
/>
196197
</div>
197198
</Fragment>
@@ -227,9 +228,9 @@ export function PageContent(props: PageContentProps) {
227228
function ChatListForSection(props: {
228229
dynamicMdContent: DynamicMarkdownSection[];
229230
sectionId: SectionId;
231+
chatHistories: ChatWithMessages[];
230232
}) {
231-
const { chatHistories } = useChatHistoryContext();
232-
const { dynamicMdContent, sectionId } = props;
233+
const { dynamicMdContent, sectionId, chatHistories } = props;
233234
const filteredChatHistories = chatHistories.filter(
234235
(c) =>
235236
c.sectionId === sectionId ||

app/actions/deleteChat.ts

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,8 @@
11
"use server";
22

3-
import { initContext } from "@/lib/chatHistory";
4-
import { chat, diff, message } from "@/schema/chat";
5-
import { and, eq } from "drizzle-orm";
3+
import { deleteChat, initContext } from "@/lib/chatHistory";
64

75
export async function deleteChatAction(chatId: string) {
86
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));
7+
await deleteChat(chatId, ctx);
218
}

0 commit comments

Comments
 (0)