@@ -11,6 +11,7 @@ import {
1111 useRef ,
1212 useState ,
1313} from 'react'
14+ import { createLogger } from '@sim/logger'
1415import { Paperclip } from 'lucide-react'
1516import { useParams } from 'next/navigation'
1617import { Button , Tooltip } from '@/components/emcn'
@@ -58,6 +59,8 @@ import type { ChatContext } from '@/stores/panel'
5859
5960export type { FileAttachmentForApi } from '@/app/workspace/[workspaceId]/home/types'
6061
62+ const logger = createLogger ( 'UserInput' )
63+
6164function getCaretAnchor (
6265 textarea : HTMLTextAreaElement ,
6366 caretPos : number
@@ -190,34 +193,42 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us
190193 useEffect ( ( ) => {
191194 if ( hasRestoredDraftRef . current || ! draftScopeKey ) return
192195 hasRestoredDraftRef . current = true
196+ let restoredContexts : ChatContext [ ] | null = null
197+ let restoredFiles : AttachedFile [ ] | null = null
198+ let caretText : string | null = null
193199 try {
194200 const draft = useMothershipDraftsStore . getState ( ) . drafts [ draftScopeKey ]
195201 if ( ! draft ) return
196202 if ( draft . contexts ?. length ) {
197- contextManagement . setSelectedContexts ( draft . contexts )
203+ restoredContexts = draft . contexts
198204 }
199205 if ( draft . fileAttachments ?. length ) {
200- files . restoreAttachedFiles (
201- draft . fileAttachments . map ( ( a ) => ( {
202- id : a . id ,
203- name : a . filename ,
204- size : a . size ,
205- type : a . media_type ,
206- path : a . path ?? '' ,
207- key : a . key ,
208- uploading : false ,
209- } ) )
210- )
206+ restoredFiles = draft . fileAttachments . map ( ( a ) => ( {
207+ id : a . id ,
208+ name : a . filename ,
209+ size : a . size ,
210+ type : a . media_type ,
211+ path : a . path ?? '' ,
212+ key : a . key ,
213+ uploading : false ,
214+ } ) )
211215 }
212216 if ( typeof draft . text === 'string' && draft . text . length > 0 ) {
213- const textarea = textareaRef . current
214- if ( textarea ) {
215- textarea . focus ( )
216- textarea . setSelectionRange ( draft . text . length , draft . text . length )
217- }
217+ caretText = draft . text
218218 }
219- } catch {
219+ } catch ( err ) {
220+ logger . error ( 'Failed to read draft, clearing' , { err } )
220221 useMothershipDraftsStore . getState ( ) . clearDraft ( draftScopeKey )
222+ return
223+ }
224+ if ( restoredContexts ) contextManagement . setSelectedContexts ( restoredContexts )
225+ if ( restoredFiles ) files . restoreAttachedFiles ( restoredFiles )
226+ if ( caretText !== null ) {
227+ const textarea = textareaRef . current
228+ if ( textarea ) {
229+ textarea . focus ( )
230+ textarea . setSelectionRange ( caretText . length , caretText . length )
231+ }
221232 }
222233 } , [ ] ) // eslint-disable-line react-hooks/exhaustive-deps -- intentional mount-only restore
223234
0 commit comments