@@ -5,7 +5,7 @@ import { MarkdownSection } from "./splitMarkdown";
55import { ChatForm } from "./chatForm" ;
66import { Heading , StyledMarkdown } from "./markdown" ;
77import { useChatHistoryContext } from "./chatHistory" ;
8- import { useSidebarMdContext } from "./dynamicMdContext " ;
8+ import { useSidebarMdContext } from "../sidebar " ;
99import clsx from "clsx" ;
1010
1111// MarkdownSectionに追加で、ユーザーが今そのセクションを読んでいるかどうか、などの動的な情報を持たせる
@@ -21,7 +21,7 @@ interface PageContentProps {
2121}
2222export function PageContent ( props : PageContentProps ) {
2323 const { setSidebarMdContent } = useSidebarMdContext ( ) ;
24-
24+
2525 // SSR用のローカルstate
2626 const [ dynamicMdContent , setDynamicMdContent ] = useState <
2727 DynamicMarkdownSection [ ]
@@ -41,12 +41,7 @@ export function PageContent(props: PageContentProps) {
4141 sectionId : `${ props . docs_id } -${ i } ` ,
4242 } ) ) ;
4343 setDynamicMdContent ( newContent ) ;
44- setSidebarMdContent ( newContent ) ;
45-
46- // クリーンアップ:コンポーネントがアンマウントされたらcontextをクリア
47- return ( ) => {
48- setSidebarMdContent ( [ ] ) ;
49- } ;
44+ setSidebarMdContent ( props . docs_id , newContent ) ;
5045 } , [ props . splitMdContent , props . docs_id , setSidebarMdContent ] ) ;
5146
5247 const sectionRefs = useRef < Array < HTMLDivElement | null > > ( [ ] ) ;
@@ -57,28 +52,31 @@ export function PageContent(props: PageContentProps) {
5752
5853 useEffect ( ( ) => {
5954 const handleScroll = ( ) => {
60- const updateContent = ( prevDynamicMdContent : DynamicMarkdownSection [ ] ) => {
55+ const updateContent = (
56+ prevDynamicMdContent : DynamicMarkdownSection [ ]
57+ ) => {
6158 const dynMdContent = prevDynamicMdContent . slice ( ) ; // Reactの変更検知のために新しい配列を作成
6259 for ( let i = 0 ; i < sectionRefs . current . length ; i ++ ) {
6360 if ( sectionRefs . current . at ( i ) && dynMdContent . at ( i ) ) {
6461 const rect = sectionRefs . current . at ( i ) ! . getBoundingClientRect ( ) ;
6562 dynMdContent . at ( i ) ! . inView =
66- rect . top < window . innerHeight && rect . bottom >= 0 ;
63+ rect . top < window . innerHeight * 0.9 &&
64+ rect . bottom >= window . innerHeight * 0.1 ;
6765 }
6866 }
6967 return dynMdContent ;
7068 } ;
71-
69+
7270 // ローカルstateとcontextの両方を更新
7371 setDynamicMdContent ( updateContent ) ;
74- setSidebarMdContent ( updateContent ) ;
72+ setSidebarMdContent ( props . docs_id , updateContent ) ;
7573 } ;
7674 window . addEventListener ( "scroll" , handleScroll ) ;
7775 handleScroll ( ) ;
7876 return ( ) => {
7977 window . removeEventListener ( "scroll" , handleScroll ) ;
8078 } ;
81- } , [ setSidebarMdContent ] ) ;
79+ } , [ setSidebarMdContent , props . docs_id ] ) ;
8280
8381 const [ isFormVisible , setIsFormVisible ] = useState ( false ) ;
8482
@@ -107,8 +105,9 @@ export function PageContent(props: PageContentProps) {
107105 </ div >
108106 < div key = { `${ index } -chat` } >
109107 { /* 右側に表示するチャット履歴欄 */ }
110- { chatHistories . filter ( ( c ) => c . sectionId === section . sectionId ) . map (
111- ( { chatId, messages} ) => (
108+ { chatHistories
109+ . filter ( ( c ) => c . sectionId === section . sectionId )
110+ . map ( ( { chatId, messages } ) => (
112111 < div
113112 key = { chatId }
114113 className = "max-w-xs mb-2 p-2 text-sm border border-base-content/10 rounded-sm shadow-sm bg-base-100"
@@ -136,8 +135,7 @@ export function PageContent(props: PageContentProps) {
136135 ) ) }
137136 </ div >
138137 </ div >
139- )
140- ) }
138+ ) ) }
141139 </ div >
142140 </ >
143141 ) ) }
0 commit comments