-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpage.tsx
More file actions
78 lines (69 loc) · 2.12 KB
/
page.tsx
File metadata and controls
78 lines (69 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import {
cacheKeyForChat,
ChatWithMessages,
getChatOne,
initContext,
} from "@/lib/chatHistory";
import { getMarkdownSections, getPagesList } from "@/lib/docs";
import { ChatAreaContainer, ChatAreaContent } from "./chatArea";
import { cacheLife, cacheTag } from "next/cache";
import { isCloudflare } from "@/lib/detectCloudflare";
export default async function ChatPage({
params,
}: {
params: Promise<{ chatId: string }>;
}) {
const { chatId } = await params;
const context = await initContext();
const chatData = await getChatOneFromCache(chatId, context.userId);
if (!chatData) {
// notFound(); だとページ全体が404になってしまう
return (
<ChatAreaContainer chatId={chatId}>
<p>指定されたチャットのデータが見つかりません。</p>
</ChatAreaContainer>
);
}
const pagesList = await getPagesList();
const targetLang = pagesList.find(
(lang) => lang.id === chatData.section.pagePath.split("/")[0]
);
const targetPage = targetLang?.pages.find(
(page) => page.slug === chatData.section.pagePath.split("/")[1]
);
const sections =
targetLang && targetPage
? await getMarkdownSections(targetLang.id, targetPage.slug)
: [];
const targetSection = sections.find((sec) => sec.id === chatData.sectionId);
return (
<ChatAreaContainer chatId={chatId}>
<ChatAreaContent
chatId={chatId}
chatData={chatData}
targetLang={targetLang}
targetPage={targetPage}
targetSection={targetSection}
/>
</ChatAreaContainer>
);
}
async function getChatOneFromCache(chatId: string, userId?: string) {
"use cache";
cacheLife("days");
cacheTag(cacheKeyForChat(chatId));
if (!userId) {
return null;
}
if (isCloudflare()) {
const cache = await caches.open("chatHistory");
const cachedResponse = await cache.match(cacheKeyForChat(chatId));
if (cachedResponse) {
const data = (await cachedResponse.json()) as ChatWithMessages;
return data;
}
}
const context = await initContext({ userId });
const chatData = await getChatOne(chatId, context);
return chatData;
}