@@ -20,17 +20,28 @@ interface PageContentProps {
2020 docs_id : string ;
2121}
2222export function PageContent ( props : PageContentProps ) {
23- const { dynamicMdContent, setDynamicMdContent } = useDynamicMdContext ( ) ;
23+ const { setDynamicMdContent } = useDynamicMdContext ( ) ;
24+
25+ // SSR用のローカルstate
26+ const [ localDynamicMdContent , setLocalDynamicMdContent ] = useState <
27+ DynamicMarkdownSection [ ]
28+ > (
29+ props . splitMdContent . map ( ( section , i ) => ( {
30+ ...section ,
31+ inView : false ,
32+ sectionId : `${ props . docs_id } -${ i } ` ,
33+ } ) )
34+ ) ;
2435
2536 useEffect ( ( ) => {
26- // props.splitMdContentが変わったときにdynamicMdContentを更新
27- setDynamicMdContent (
28- props . splitMdContent . map ( ( section , i ) => ( {
29- ... section ,
30- inView : false ,
31- sectionId : ` ${ props . docs_id } - ${ i } ` ,
32- } ) )
33- ) ;
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+ setLocalDynamicMdContent ( newContent ) ;
44+ setDynamicMdContent ( newContent ) ;
3445 } , [ props . splitMdContent , props . docs_id , setDynamicMdContent ] ) ;
3546
3647 const sectionRefs = useRef < Array < HTMLDivElement | null > > ( [ ] ) ;
@@ -41,7 +52,7 @@ export function PageContent(props: PageContentProps) {
4152
4253 useEffect ( ( ) => {
4354 const handleScroll = ( ) => {
44- setDynamicMdContent ( ( prevDynamicMdContent ) => {
55+ const updateContent = ( prevDynamicMdContent : DynamicMarkdownSection [ ] ) => {
4556 const dynMdContent = prevDynamicMdContent . slice ( ) ; // Reactの変更検知のために新しい配列を作成
4657 for ( let i = 0 ; i < sectionRefs . current . length ; i ++ ) {
4758 if ( sectionRefs . current . at ( i ) && dynMdContent . at ( i ) ) {
@@ -51,7 +62,11 @@ export function PageContent(props: PageContentProps) {
5162 }
5263 }
5364 return dynMdContent ;
54- } ) ;
65+ } ;
66+
67+ // ローカルstateとcontextの両方を更新
68+ setLocalDynamicMdContent ( updateContent ) ;
69+ setDynamicMdContent ( updateContent ) ;
5570 } ;
5671 window . addEventListener ( "scroll" , handleScroll ) ;
5772 handleScroll ( ) ;
@@ -71,7 +86,7 @@ export function PageContent(props: PageContentProps) {
7186 gridTemplateColumns : `1fr auto` ,
7287 } }
7388 >
74- { dynamicMdContent . map ( ( section , index ) => (
89+ { localDynamicMdContent . map ( ( section , index ) => (
7590 < >
7691 < div
7792 className = "max-w-200"
@@ -127,7 +142,7 @@ export function PageContent(props: PageContentProps) {
127142 < div className = "fixed bottom-4 right-4 left-4 lg:left-84 z-20" >
128143 < ChatForm
129144 documentContent = { props . documentContent }
130- sectionContent = { dynamicMdContent }
145+ sectionContent = { localDynamicMdContent }
131146 docs_id = { props . docs_id }
132147 close = { ( ) => setIsFormVisible ( false ) }
133148 />
0 commit comments