@@ -29,9 +29,10 @@ export default function EditNoteScreen() {
2929 const [ noteData , setNoteData ] = useState < Note | null > ( null ) ;
3030 const [ attachments , setAttachments ] = useState < FileAttachment [ ] > ( [ ] ) ;
3131 const [ showAttachments , setShowAttachments ] = useState ( false ) ;
32+ const [ createdNoteId , setCreatedNoteId ] = useState < string | null > ( null ) ;
3233
3334 const keyboardHeight = useKeyboardHeight ( ) ;
34- const { editor, handleEditorLoad, loadNote } = useNoteEditor ( noteId as string ) ;
35+ const { editor, handleEditorLoad, loadNote } = useNoteEditor ( noteId as string || createdNoteId || undefined ) ;
3536
3637 // Calculate toolbar height (toolbar itself + padding + keyboard)
3738 // iOS: 60px toolbar + keyboard height + minimal padding for curved keyboard
@@ -62,21 +63,19 @@ export default function EditNoteScreen() {
6263 } , [ noteId , isEditing ] ) ;
6364
6465 const refreshAttachments = async ( ) => {
65- if ( isEditing && noteId ) {
66+ const currentNoteId = ( noteId as string ) || createdNoteId ;
67+ if ( currentNoteId ) {
6668 try {
67- const noteAttachments = await api . getAttachments ( noteId as string ) ;
69+ const noteAttachments = await api . getAttachments ( currentNoteId ) ;
6870 setAttachments ( noteAttachments ) ;
6971 } catch ( error ) {
7072 console . error ( 'Failed to refresh attachments:' , error ) ;
7173 }
7274 }
7375 } ;
7476
75- const handleSave = async ( ) => {
76- if ( ! title . trim ( ) ) {
77- Alert . alert ( 'Error' , 'Please enter a title for your note' ) ;
78- return ;
79- }
77+ const handleSave = async ( options ?: { skipNavigation ?: boolean } ) => {
78+ const titleToUse = title . trim ( ) || 'Untitled' ;
8079
8180 setIsSaving ( true ) ;
8281
@@ -86,36 +85,74 @@ export default function EditNoteScreen() {
8685 console . log ( 'Content to save:' , content ) ;
8786 }
8887
89- if ( isEditing && noteId ) {
90- await api . updateNote ( noteId as string , {
91- title : title . trim ( ) ,
88+ let savedNote : Note ;
89+
90+ if ( ( isEditing && noteId ) || createdNoteId ) {
91+ savedNote = await api . updateNote ( ( noteId as string ) || createdNoteId ! , {
92+ title : titleToUse ,
9293 content,
9394 } ) ;
9495 } else {
95- await api . createNote ( {
96- title : title . trim ( ) ,
96+ savedNote = await api . createNote ( {
97+ title : titleToUse ,
9798 content,
9899 folderId : folderId as string | undefined ,
99100 starred : false ,
100101 archived : false ,
101102 deleted : false ,
102103 hidden : false ,
103104 } ) ;
105+ setCreatedNoteId ( savedNote . id ) ;
106+ setTitle ( titleToUse ) ;
104107 }
105108
106109 await Haptics . notificationAsync ( Haptics . NotificationFeedbackType . Success ) ;
107110
108- setTimeout ( ( ) => {
109- router . back ( ) ;
110- } , NAVIGATION_DELAY ) ;
111+ if ( ! options ?. skipNavigation ) {
112+ setTimeout ( ( ) => {
113+ router . back ( ) ;
114+ } , NAVIGATION_DELAY ) ;
115+ } else {
116+ setIsSaving ( false ) ;
117+ // Refresh attachments if we just created the note and staying on the page
118+ if ( ! noteId && savedNote . id ) {
119+ try {
120+ const noteAttachments = await api . getAttachments ( savedNote . id ) ;
121+ setAttachments ( noteAttachments ) ;
122+ } catch ( error ) {
123+ console . error ( 'Failed to refresh attachments:' , error ) ;
124+ }
125+ }
126+ }
127+
128+ return savedNote ;
111129 } catch ( error ) {
112130 if ( __DEV__ ) console . error ( 'Failed to save note:' , error ) ;
113- Alert . alert ( 'Error' , `Failed to ${ isEditing ? 'update' : 'create' } note` ) ;
131+ Alert . alert ( 'Error' , `Failed to ${ isEditing || createdNoteId ? 'update' : 'create' } note` ) ;
114132 await Haptics . notificationAsync ( Haptics . NotificationFeedbackType . Error ) ;
115133 setIsSaving ( false ) ;
134+ return null ;
116135 }
117136 } ;
118137
138+ const handleToggleAttachments = ( ) => {
139+ setShowAttachments ( ! showAttachments ) ;
140+ } ;
141+
142+ const handleBeforeUpload = async ( ) : Promise < string | null > => {
143+ // If it's a new note (no noteId and no createdNoteId), prompt to save first
144+ if ( ! noteId && ! createdNoteId ) {
145+ Alert . alert (
146+ 'Save Note First' ,
147+ 'Please save your note before adding attachments.' ,
148+ [ { text : 'OK' } ]
149+ ) ;
150+ return null ;
151+ }
152+
153+ return ( noteId as string ) || createdNoteId ;
154+ } ;
155+
119156 const handleDelete = async ( ) => {
120157 if ( ! noteData || ! noteId ) return ;
121158
@@ -179,8 +216,8 @@ export default function EditNoteScreen() {
179216 showAttachments = { showAttachments }
180217 onBack = { ( ) => router . back ( ) }
181218 onDelete = { handleDelete }
182- onSave = { handleSave }
183- onToggleAttachments = { ( ) => setShowAttachments ( ! showAttachments ) }
219+ onSave = { ( ) => handleSave ( { skipNavigation : showAttachments && ! noteId && ! createdNoteId } ) }
220+ onToggleAttachments = { handleToggleAttachments }
184221 theme = { theme }
185222 />
186223
@@ -203,13 +240,14 @@ export default function EditNoteScreen() {
203240 </ View >
204241 ) }
205242
206- { isEditing && noteId && showAttachments && (
243+ { showAttachments && (
207244 < View style = { styles . attachmentsSection } >
208245 < FileUpload
209- noteId = { noteId as string }
246+ noteId = { ( noteId as string ) || createdNoteId || undefined }
210247 attachments = { attachments }
211248 onUploadComplete = { refreshAttachments }
212249 onDeleteComplete = { refreshAttachments }
250+ onBeforeUpload = { handleBeforeUpload }
213251 />
214252 </ View >
215253 ) }
@@ -223,7 +261,7 @@ export default function EditNoteScreen() {
223261 Platform . OS === 'android' && keyboardHeight > 0 && { marginBottom : keyboardHeight + 60 }
224262 ] } >
225263 < RichText
226- key = { noteId as string || 'new-note' }
264+ key = { ( noteId as string ) || createdNoteId || 'new-note' }
227265 editor = { editor }
228266 style = { { flex : 1 , backgroundColor : theme . colors . background } }
229267 onLoad = { handleEditorLoad }
0 commit comments