Skip to content

Commit 0d7d117

Browse files
committed
fix(mobile): release image files when review input closes
Track accepted image files before React commits the attachment list. Release late picker and paste results after the review sheet unmounts. Keep submit and remove operations in sync with those local references. Reject an oversized JPEG export before writing its bytes to disk. Created with GPT-6 Astra (preview) in Codex.
1 parent 1640364 commit 0d7d117

3 files changed

Lines changed: 52 additions & 32 deletions

File tree

‎apps/mobile/src/features/review/ReviewCommentComposerSheet.tsx‎

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

‎apps/mobile/src/lib/composerFiles.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,19 @@ describe("composer file attachments", () => {
233233
expect(mocks.copy).not.toHaveBeenCalled();
234234
});
235235

236+
it("rejects an oversized JPEG fallback before writing it", async () => {
237+
const base64 = "/9j/" + "A".repeat(Math.ceil(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3) * 4);
238+
mocks.open.mockImplementation(() => otherMagic());
239+
mocks.size.mockReturnValue(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
240+
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [{ ...photo, base64 }] });
241+
242+
await expect(pickComposerImages({ existingCount: 0 })).resolves.toEqual({
243+
images: [],
244+
error: "'photo.jpg' exceeds the 10 MB attachment limit.",
245+
});
246+
expect(mocks.write.mock.calls.length).toBe(0);
247+
});
248+
236249
it.each([
237250
{ label: "no export", base64: null },
238251
// Android's quality-1 export is the raw original, not a JPEG transcode.

‎apps/mobile/src/lib/composerImages.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export type DraftComposerAttachment = DraftComposerImageAttachment | DraftCompos
4747
/** Any composer attachment whose bytes live in the app-owned attachment directory. */
4848
export type FileBackedComposerAttachment = DraftComposerAttachment & { readonly fileUri: string };
4949

50-
/** Files have a local copy. Images can have one after a file-backed draft is restored. */
50+
/** Files and new images have local copies. Older images may still be inline. */
5151
export function isFileBackedComposerAttachment(
5252
attachment: DraftComposerAttachment,
5353
): attachment is FileBackedComposerAttachment {
@@ -145,8 +145,11 @@ export async function persistComposerAttachmentFile(
145145
return destination.uri;
146146
}
147147

148-
/** Lands in-memory image bytes (clipboard pastes) in the owned attachment directory. */
148+
/** Writes clipboard or picker JPEG bytes into the owned attachment directory. */
149149
async function persistComposerImageBase64(base64: string, name: string): Promise<string> {
150+
if (estimateBase64ByteSize(base64) > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
151+
throw new Error(fileAttachmentTooLargeMessage(name, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES));
152+
}
150153
const { Directory, File, Paths } = await import("expo-file-system");
151154
const directory = new Directory(Paths.document, COMPOSER_ATTACHMENT_DIRECTORY);
152155
directory.create({ idempotent: true, intermediates: true });

0 commit comments

Comments
 (0)