11import type { ISynctexBlockId } from "@fluffylabs/links-metadata" ;
22import { Button } from "@fluffylabs/shared-ui" ;
3- import { type ChangeEvent , type ChangeEventHandler , useCallback , useMemo , useRef , useState } from "react" ;
3+ import { useCallback , useContext , useEffect , useMemo , useRef , useState } from "react" ;
44import { useLatestCallback } from "../../../hooks/useLatestCallback" ;
55import { validateMath } from "../../../utils/validateMath" ;
6+ import { CodeSyncContext , type ICodeSyncContext } from "../../CodeSyncProvider/CodeSyncProvider" ;
67import { type IDecoratedNote , NoteSource } from "../../NotesProvider/types/DecoratedNote" ;
78import type { INoteV3 } from "../../NotesProvider/types/StorageNote" ;
89import type { ISingleNoteContext } from "./NoteContext" ;
@@ -18,7 +19,6 @@ type NewNoteProps = {
1819} ;
1920
2021export const NewNote = ( { version, onCancel, onSave, selectionStart, selectionEnd } : NewNoteProps ) => {
21- const [ noteContent , setNoteContent ] = useState ( "" ) ;
2222 const [ noteContentError , setNoteContentError ] = useState < string | null > ( null ) ;
2323 const [ labels , setLabels ] = useState < string [ ] > ( [ "local" ] ) ;
2424
@@ -30,6 +30,8 @@ export const NewNote = ({ version, onCancel, onSave, selectionStart, selectionEn
3030 } , [ latestOnCancel ] ) ;
3131
3232 const handleSaveClick = useCallback ( ( ) => {
33+ const noteContent = textAreaRef . current ?. value ?? "" ;
34+
3335 const mathValidationError = validateMath ( noteContent ) ;
3436 if ( mathValidationError ) {
3537 setNoteContentError ( mathValidationError ) ;
@@ -41,25 +43,19 @@ export const NewNote = ({ version, onCancel, onSave, selectionStart, selectionEn
4143 }
4244
4345 latestOnSave . current ( { noteContent, labels } ) ;
44- } , [ noteContent , latestOnSave , labels ] ) ;
46+ } , [ latestOnSave , labels ] ) ;
4547
4648 const currentVersionLink = "" ;
4749 const originalVersionLink = "" ;
4850
49- const handleNoteContentChange = useCallback ( ( event : ChangeEvent < HTMLTextAreaElement > ) => {
50- setNoteContent ( event . target . value ) ;
51- setNoteContentError ( null ) ;
52- } , [ ] ) ;
53-
54- const noteDirty = useDumbDirtyNoteObj ( { noteContent, labels, version } ) ;
51+ const noteDirty = useDumbDirtyNoteObj ( { labels, version } ) ;
5552 const note = useDumbNoteObj ( { version, selectionStart, selectionEnd } ) ;
5653
5754 const noteLayoutContext = useNewNoteLayoutContext ( {
5855 note,
5956 noteDirty,
6057 currentVersionLink,
6158 handleCancelClick,
62- handleNoteContentChange,
6359 handleNoteLabelsChange : setLabels ,
6460 handleSaveClick,
6561 originalVersionLink,
@@ -140,51 +136,76 @@ const useDumbNoteObj = ({
140136 ) ;
141137
142138const useDumbDirtyNoteObj = ( {
143- noteContent,
144139 labels,
145140 version,
146141} : {
147- noteContent : string ;
148142 labels : string [ ] ;
149143 version : string ;
150144} ) =>
151145 useMemo (
152146 ( ) =>
153147 ( {
154148 author : "" ,
155- content : noteContent ,
149+ content : "" ,
156150 labels,
157151 date : 0 ,
158152 noteVersion : 3 ,
159153 selectionEnd : createDumbISyntexBlockId ( ) ,
160154 selectionStart : createDumbISyntexBlockId ( ) ,
161155 version,
162156 } ) satisfies INoteV3 ,
163- [ noteContent , version , labels ] ,
157+ [ version , labels ] ,
164158 ) ;
165159
166160const useNewNoteLayoutContext = ( {
167161 handleSaveClick,
168162 handleCancelClick,
169163 note,
170164 noteDirty,
171- handleNoteContentChange,
172165 handleNoteLabelsChange,
173166 currentVersionLink,
174167 originalVersionLink,
168+ selectionEnd,
169+ selectionStart,
175170} : {
176171 handleSaveClick : ( ) => void ;
177172 handleCancelClick : ( ) => void ;
178173 note : IDecoratedNote ;
179174 noteDirty : INoteV3 ;
180- handleNoteContentChange : ChangeEventHandler < HTMLTextAreaElement > ;
181175 handleNoteLabelsChange : ( labels : string [ ] ) => void ;
182176 currentVersionLink : string ;
183177 originalVersionLink : string ;
184178 selectionStart : ISynctexBlockId ;
185179 selectionEnd : ISynctexBlockId ;
186- } ) =>
187- useMemo (
180+ } ) => {
181+ const { getSectionTitleAtSynctexBlock, getSubsectionTitleAtSynctexBlock } = useContext (
182+ CodeSyncContext ,
183+ ) as ICodeSyncContext ;
184+
185+ const [ sectionTitles , setSectionTitles ] = useState < { sectionTitle : string ; subSectionTitle : string } > ( {
186+ sectionTitle : "" ,
187+ subSectionTitle : "" ,
188+ } ) ;
189+
190+ useEffect ( ( ) => {
191+ let cancelled = false ;
192+ ( async ( ) => {
193+ if ( selectionStart && selectionEnd ) {
194+ const newSectionTitle = ( await getSectionTitleAtSynctexBlock ( selectionStart ) ) ?? "" ;
195+ const newSubSectionTitle = ( await getSubsectionTitleAtSynctexBlock ( selectionStart ) ) ?? "" ;
196+ if ( cancelled ) return ;
197+ setSectionTitles ( {
198+ sectionTitle : newSectionTitle ,
199+ subSectionTitle : newSubSectionTitle ,
200+ } ) ;
201+ }
202+ } ) ( ) ;
203+ return ( ) => {
204+ cancelled = true ;
205+ } ;
206+ } , [ selectionStart , selectionEnd , getSectionTitleAtSynctexBlock , getSubsectionTitleAtSynctexBlock ] ) ;
207+
208+ const context = useMemo (
188209 ( ) =>
189210 ( {
190211 active : true ,
@@ -196,21 +217,24 @@ const useNewNoteLayoutContext = ({
196217 isEditing : true ,
197218 note,
198219 noteDirty,
199- handleNoteContentChange,
200220 handleNoteLabelsChange,
201221 handleSelectNote : ( ) => { } ,
202222 noteOriginalVersionShort : "" ,
203223 currentVersionLink,
204224 originalVersionLink,
225+ sectionTitles,
205226 } ) satisfies ISingleNoteContext ,
206227 [
207228 noteDirty ,
208- handleNoteContentChange ,
209229 handleNoteLabelsChange ,
210230 note ,
211231 handleCancelClick ,
212232 handleSaveClick ,
213233 currentVersionLink ,
214234 originalVersionLink ,
235+ sectionTitles ,
215236 ] ,
216237 ) ;
238+
239+ return context ;
240+ } ;
0 commit comments