@@ -5,6 +5,7 @@ import { MarkdownSection } from "./splitMarkdown";
55import { ChatForm } from "./chatForm" ;
66import { Heading , StyledMarkdown } from "./markdown" ;
77import { useChatHistoryContext } from "./chatHistory" ;
8+ import { useSidebarMdContext } from "./dynamicMdContext" ;
89import clsx from "clsx" ;
910
1011// MarkdownSectionに追加で、ユーザーが今そのセクションを読んでいるかどうか、などの動的な情報を持たせる
@@ -19,26 +20,34 @@ interface PageContentProps {
1920 docs_id : string ;
2021}
2122export function PageContent ( props : PageContentProps ) {
23+ const { setSidebarMdContent } = useSidebarMdContext ( ) ;
24+
25+ // SSR用のローカルstate
2226 const [ dynamicMdContent , setDynamicMdContent ] = useState <
2327 DynamicMarkdownSection [ ]
2428 > (
25- // useEffectで更新するのとは別に、SSRのための初期値
2629 props . splitMdContent . map ( ( section , i ) => ( {
2730 ...section ,
2831 inView : false ,
2932 sectionId : `${ props . docs_id } -${ i } ` ,
3033 } ) )
3134 ) ;
35+
3236 useEffect ( ( ) => {
33- // props.splitMdContentが変わったときにdynamicMdContentを更新
34- setDynamicMdContent (
35- props . splitMdContent . map ( ( section , i ) => ( {
36- ...section ,
37- inView : false ,
38- sectionId : `${ props . docs_id } -${ i } ` ,
39- } ) )
40- ) ;
41- } , [ props . splitMdContent , props . docs_id ] ) ;
37+ // props.splitMdContentが変わったときにローカルstateとcontextの両方を更新
38+ const newContent = props . splitMdContent . map ( ( section , i ) => ( {
39+ ...section ,
40+ inView : false ,
41+ sectionId : `${ props . docs_id } -${ i } ` ,
42+ } ) ) ;
43+ setDynamicMdContent ( newContent ) ;
44+ setSidebarMdContent ( newContent ) ;
45+
46+ // クリーンアップ:コンポーネントがアンマウントされたらcontextをクリア
47+ return ( ) => {
48+ setSidebarMdContent ( [ ] ) ;
49+ } ;
50+ } , [ props . splitMdContent , props . docs_id , setSidebarMdContent ] ) ;
4251
4352 const sectionRefs = useRef < Array < HTMLDivElement | null > > ( [ ] ) ;
4453 // sectionRefsの長さをsplitMdContentに合わせる
@@ -48,7 +57,7 @@ export function PageContent(props: PageContentProps) {
4857
4958 useEffect ( ( ) => {
5059 const handleScroll = ( ) => {
51- setDynamicMdContent ( ( prevDynamicMdContent ) => {
60+ const updateContent = ( prevDynamicMdContent : DynamicMarkdownSection [ ] ) => {
5261 const dynMdContent = prevDynamicMdContent . slice ( ) ; // Reactの変更検知のために新しい配列を作成
5362 for ( let i = 0 ; i < sectionRefs . current . length ; i ++ ) {
5463 if ( sectionRefs . current . at ( i ) && dynMdContent . at ( i ) ) {
@@ -58,14 +67,18 @@ export function PageContent(props: PageContentProps) {
5867 }
5968 }
6069 return dynMdContent ;
61- } ) ;
70+ } ;
71+
72+ // ローカルstateとcontextの両方を更新
73+ setDynamicMdContent ( updateContent ) ;
74+ setSidebarMdContent ( updateContent ) ;
6275 } ;
6376 window . addEventListener ( "scroll" , handleScroll ) ;
6477 handleScroll ( ) ;
6578 return ( ) => {
6679 window . removeEventListener ( "scroll" , handleScroll ) ;
6780 } ;
68- } , [ ] ) ;
81+ } , [ setSidebarMdContent ] ) ;
6982
7083 const [ isFormVisible , setIsFormVisible ] = useState ( false ) ;
7184
0 commit comments