@@ -55,20 +55,29 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
5555 > ( { } ) ;
5656 const [ attachments , setAttachments ] = useState < ReadonlyArray < DraftComposerImageAttachment > > ( [ ] ) ;
5757 const attachmentsRef = useRef ( attachments ) ;
58- attachmentsRef . current = attachments ;
59- // Attachments live in local state until submit copies them into the thread
60- // draft, but their image bytes already live in the app-owned attachment
61- // directory. Whatever is still here when the sheet unmounts was neither
62- // submitted nor removed; release those copies so dismissal does not leak
63- // storage. Cleanup is reference-checked, so submitted files stay.
64- useEffect (
65- ( ) => ( ) => {
66- if ( attachmentsRef . current . length > 0 ) {
67- scheduleUnusedComposerAttachmentCleanup ( attachmentsRef . current ) ;
68- }
69- } ,
70- [ ] ,
71- ) ;
58+ const isMountedRef = useRef ( true ) ;
59+ // Store file references before React commits, so closing the sheet can release them.
60+ const replaceAttachments = useCallback ( ( next : ReadonlyArray < DraftComposerImageAttachment > ) => {
61+ attachmentsRef . current = next ;
62+ setAttachments ( next ) ;
63+ } , [ ] ) ;
64+ useEffect ( ( ) => {
65+ isMountedRef . current = true ;
66+ return ( ) => {
67+ isMountedRef . current = false ;
68+ scheduleUnusedComposerAttachmentCleanup ( attachmentsRef . current ) ;
69+ } ;
70+ } , [ ] ) ;
71+
72+ function appendImages ( images : ReadonlyArray < DraftComposerImageAttachment > ) : void {
73+ if ( ! isMountedRef . current ) {
74+ scheduleUnusedComposerAttachmentCleanup ( images ) ;
75+ return ;
76+ }
77+ if ( images . length > 0 ) {
78+ replaceAttachments ( [ ...attachmentsRef . current , ...images ] ) ;
79+ }
80+ }
7281 const [ previewFile , setPreviewFile ] = useState < FilePreviewSource | null > ( null ) ;
7382
7483 const selectedLines = useMemo (
@@ -103,11 +112,9 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
103112 try {
104113 const images = await convertPastedImagesToAttachments ( {
105114 uris,
106- existingCount : attachments . length ,
115+ existingCount : attachmentsRef . current . length ,
107116 } ) ;
108- if ( images . length > 0 ) {
109- setAttachments ( ( current ) => [ ...current , ...images ] ) ;
110- }
117+ appendImages ( images ) ;
111118 } catch ( error ) {
112119 console . error ( "[review comment] error converting pasted images" , error ) ;
113120 }
@@ -143,11 +150,9 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
143150 } , [ selectedLines , selectedTheme , target ] ) ;
144151
145152 async function handlePickImages ( ) : Promise < void > {
146- const result = await pickComposerImages ( { existingCount : attachments . length } ) ;
147- if ( result . images . length > 0 ) {
148- setAttachments ( ( current ) => [ ...current , ...result . images ] ) ;
149- }
150- if ( result . error ) {
153+ const result = await pickComposerImages ( { existingCount : attachmentsRef . current . length } ) ;
154+ appendImages ( result . images ) ;
155+ if ( result . error && isMountedRef . current ) {
151156 setPendingConnectionError ( result . error ) ;
152157 }
153158 }
@@ -161,11 +166,11 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
161166 environmentId,
162167 threadId,
163168 text : formatReviewCommentContext ( target , commentText ) ,
164- attachments,
169+ attachments : attachmentsRef . current ,
165170 } ) ;
166- setAttachments ( [ ] ) ;
171+ replaceAttachments ( [ ] ) ;
167172 dismissComposer ( ) ;
168- } , [ attachments , commentText , dismissComposer , environmentId , target , threadId ] ) ;
173+ } , [ commentText , dismissComposer , environmentId , replaceAttachments , target , threadId ] ) ;
169174
170175 return (
171176 < View className = "flex-1 bg-sheet" >
@@ -291,10 +296,9 @@ export function ReviewCommentComposerSheet(props: ReviewCommentComposerSheetProp
291296 onPressPreview = { setPreviewFile }
292297 removeButtonPlacement = "gutter"
293298 onRemove = { ( imageId ) => {
294- const removed = attachments . find ( ( image ) => image . id === imageId ) ;
295- setAttachments ( ( current ) =>
296- current . filter ( ( image ) => image . id !== imageId ) ,
297- ) ;
299+ const current = attachmentsRef . current ;
300+ const removed = current . find ( ( image ) => image . id === imageId ) ;
301+ replaceAttachments ( current . filter ( ( image ) => image . id !== imageId ) ) ;
298302 if ( removed ) {
299303 scheduleUnusedComposerAttachmentCleanup ( [ removed ] ) ;
300304 }
0 commit comments