-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpageContent.tsx
More file actions
333 lines (314 loc) · 11.1 KB
/
pageContent.tsx
File metadata and controls
333 lines (314 loc) · 11.1 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"use client";
import { Fragment, useCallback, useEffect, useRef, useState } from "react";
import { ChatForm } from "./chatForm";
import { StyledMarkdown } from "@/markdown/markdown";
import { useSidebarMdContext } from "@/sidebar";
import clsx from "clsx";
import { PageTransition } from "./pageTransition";
import {
DynamicMarkdownSection,
LanguageEntry,
MarkdownSection,
PageEntry,
PagePath,
SectionId,
} from "@/lib/docs";
import { Heading } from "@/markdown/heading";
import Link from "next/link";
import { useChatId } from "@/(docs)/chatAreaState";
import { ChatWithMessages } from "@/lib/chatHistory";
interface PageContentProps {
splitMdContent: MarkdownSection[];
langEntry: LanguageEntry;
pageEntry: PageEntry;
prevPage?: PageEntry;
nextPage?: PageEntry;
path: PagePath;
chatHistories: ChatWithMessages[];
}
export function PageContent(props: PageContentProps) {
const { setSidebarMdContent } = useSidebarMdContext();
const { splitMdContent, pageEntry, path, chatHistories } = props;
const initDynamicMdContent = useCallback(() => {
const newContent: DynamicMarkdownSection[] = splitMdContent.map(
(section) => ({
...section,
inView: false,
replacedContent: section.rawContent,
replacedRange: [],
})
);
const chatDiffs = chatHistories.map((chat) => chat.diff).flat();
chatDiffs.sort((a, b) => a.createdAt.getTime() - b.createdAt.getTime());
for (const diff of chatDiffs) {
const targetSection = newContent.find((s) => s.id === diff.sectionId);
if (targetSection) {
const startIndex = targetSection.replacedContent.indexOf(diff.search);
if (startIndex !== -1) {
const endIndex = startIndex + diff.search.length;
const replaceLen = diff.replace.length;
const diffLen = replaceLen - diff.search.length; // 文字列長の増減分
// 1. 文字列の置換
targetSection.replacedContent =
targetSection.replacedContent.slice(0, startIndex) +
diff.replace +
targetSection.replacedContent.slice(endIndex);
// 2. 既存のハイライト範囲のズレを補正(今回の置換箇所より後ろにあるものをシフト)
targetSection.replacedRange = targetSection.replacedRange.map((h) => {
if (h.start >= endIndex) {
// 完全に後ろにある場合は単純にシフト
return {
start: h.start + diffLen,
end: h.end + diffLen,
id: h.id,
};
}
if (h.end >= endIndex) {
return { start: h.start, end: h.end + diffLen, id: h.id };
}
return h;
});
// 3. 今回の置換箇所を新たなハイライト範囲として追加
targetSection.replacedRange.push({
start: startIndex,
end: startIndex + replaceLen,
id: diff.chatId,
});
} else {
// TODO: md5ハッシュを参照し過去バージョンのドキュメントへ適用を試みる
console.error(
`Failed to apply diff: search string "${diff.search}" not found in section ${targetSection.id}`
);
}
} else {
console.error(
`Failed to apply diff: section with id "${diff.sectionId}" not found`
);
}
}
return newContent;
}, [splitMdContent, chatHistories]);
// SSR用のローカルstate
const [dynamicMdContent, setDynamicMdContent] = useState<
DynamicMarkdownSection[]
>(() => initDynamicMdContent());
useEffect(() => {
// props.splitMdContentが変わったとき, チャットのdiffが変わった時に
// ローカルstateとcontextの両方を更新
const newContent = initDynamicMdContent();
setDynamicMdContent(newContent);
setSidebarMdContent(path, newContent);
}, [initDynamicMdContent, path, setSidebarMdContent]);
const sectionRefs = useRef<Array<HTMLDivElement | null>>([]);
// sectionRefsの長さをsplitMdContentに合わせる
while (sectionRefs.current.length < props.splitMdContent.length) {
sectionRefs.current.push(null);
}
useEffect(() => {
const handleScroll = () => {
const updateContent = (
prevDynamicMdContent: DynamicMarkdownSection[]
) => {
const dynMdContent = prevDynamicMdContent.slice(); // Reactの変更検知のために新しい配列を作成
for (let i = 0; i < sectionRefs.current.length; i++) {
if (sectionRefs.current.at(i) && dynMdContent.at(i)) {
const rect = sectionRefs.current.at(i)!.getBoundingClientRect();
dynMdContent.at(i)!.inView =
rect.top < window.innerHeight * 0.9 &&
rect.bottom >= window.innerHeight * 0.1;
}
}
return dynMdContent;
};
// ローカルstateとcontextの両方を更新
setDynamicMdContent(updateContent);
setSidebarMdContent(path, updateContent);
};
window.addEventListener("scroll", handleScroll);
handleScroll();
return () => {
window.removeEventListener("scroll", handleScroll);
};
}, [setSidebarMdContent, path]);
const [isFormVisible, setIsFormVisible] = useState(false);
return (
<div className="flex-1 p-4 flex flex-col">
<div
className="max-w-full mx-auto grid"
style={{
gridTemplateColumns: `1fr auto`,
}}
>
<Heading className="max-w-docs" level={1}>
第{pageEntry.index}章: {pageEntry.title}
</Heading>
<div />
{dynamicMdContent.map((section, index) => (
<Fragment key={section.id}>
<div
className="min-w-1/2 max-w-docs text-justify"
id={section.id} // 目次からaタグで飛ぶために必要
ref={(el) => {
sectionRefs.current[index] = el;
}}
>
{/* ドキュメントのコンテンツ */}
<StyledMarkdown
content={section.replacedContent}
replacedRange={section.replacedRange}
/>
</div>
<div>
<ChatListForSection
sectionId={section.id}
dynamicMdContent={dynamicMdContent}
chatHistories={chatHistories}
/>
</div>
</Fragment>
))}
<PageTransition
lang={path.lang}
prevPage={props.prevPage}
nextPage={props.nextPage}
/>
<div />
</div>
{isFormVisible ? (
// leftは sidebarの幅 + 4
// replがz-10, chatAreaがz-35を使用することからそれの上にするためz-40
<div className="fixed bottom-4 right-4 left-4 has-sidebar:left-[calc(var(--container-sidebar)+1rem)] z-40">
<ChatForm
path={path}
sectionContent={dynamicMdContent}
close={() => setIsFormVisible(false)}
/>
</div>
) : (
<button
className="fixed bottom-4 right-4 btn btn-soft btn-secondary rounded-full shadow-md z-50"
onClick={() => setIsFormVisible(true)}
>
AIに質問
</button>
)}
</div>
);
}
function ChatListForSection(props: {
dynamicMdContent: DynamicMarkdownSection[];
sectionId: SectionId;
chatHistories: ChatWithMessages[];
}) {
const { dynamicMdContent, sectionId, chatHistories } = props;
const filteredChatHistories = chatHistories.filter(
(c) =>
c.sectionId === sectionId ||
// 対象のセクションが存在しないものは、introセクション(index=0)にフォールバックする
(dynamicMdContent[0].id === sectionId &&
dynamicMdContent.every((sec) => c.sectionId !== sec.id))
);
const chatId = useChatId();
if (filteredChatHistories.length === 0) {
// チャットがないなら何も表示しない
return null;
}
return (
<>
{/*xl以上の幅かつチャットを表示していない → チャットリストを表示
see also globals.css
*/}
<ul
className={clsx(
chatId === null
? "hidden has-chat-1:block"
: "hidden has-chat-2:block",
"mt-2 ml-4 w-full max-w-chat-list",
"menu menu-sm",
"rounded-lg shadow-sm bg-base-200"
)}
>
<li className="menu-title flex-row items-center gap-1">
<ChatIcon />
AIへの質問
<span className="badge badge-sm badge-soft badge-secondary">
{filteredChatHistories.length}
</span>
</li>
{filteredChatHistories.map(({ title, chatId }) => (
<li key={chatId}>
<Link
className="text-wrap text-justify"
href={`/chat/${chatId}`}
scroll={false}
>
{title}
</Link>
</li>
))}
</ul>
{/*xl未満 or xl以上でチャットを表示している → 小さいボタンを表示*/}
<details
className={clsx(
chatId === null
? "block has-chat-1:hidden"
: "block has-chat-2:block",
"dropdown dropdown-end",
"mt-2 ml-2"
)}
>
<summary className="btn btn-outline btn-secondary btn-sm">
<ChatIcon />
{filteredChatHistories.length}
</summary>
<ul
className={clsx(
"menu menu-sm dropdown-content",
"w-max max-w-[75vw]",
"z-30",
"rounded-lg shadow-sm bg-base-200/60 backdrop-blur-xs"
)}
>
{filteredChatHistories.map(({ title, chatId }) => (
<li key={chatId}>
<Link
className="text-wrap text-justify"
href={`/chat/${chatId}`}
scroll={false}
>
{title}
</Link>
</li>
))}
</ul>
</details>
</>
);
}
function ChatIcon() {
return (
<>
{/*<!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools -->*/}
<svg
className="w-4 h-4"
viewBox="3.5 2.5 18 18"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
fillRule="evenodd"
clipRule="evenodd"
d="M5.5 12C5.49988 14.613 6.95512 17.0085 9.2741 18.2127C11.5931 19.4169 14.3897 19.2292 16.527 17.726L19.5 18V12C19.5 8.13401 16.366 5 12.5 5C8.63401 5 5.5 8.13401 5.5 12Z"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M9.5 13.25C9.08579 13.25 8.75 13.5858 8.75 14C8.75 14.4142 9.08579 14.75 9.5 14.75V13.25ZM13.5 14.75C13.9142 14.75 14.25 14.4142 14.25 14C14.25 13.5858 13.9142 13.25 13.5 13.25V14.75ZM9.5 10.25C9.08579 10.25 8.75 10.5858 8.75 11C8.75 11.4142 9.08579 11.75 9.5 11.75V10.25ZM15.5 11.75C15.9142 11.75 16.25 11.4142 16.25 11C16.25 10.5858 15.9142 10.25 15.5 10.25V11.75ZM9.5 14.75H13.5V13.25H9.5V14.75ZM9.5 11.75H15.5V10.25H9.5V11.75Z"
fill="currentColor"
/>
</svg>
</>
);
}