Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(CommentComposer): a busy composer refuses pasted and dropped images (#10498)

Cindy Zhang6a4bc478edcc
diff --git a/packages/lab/src/Comments/CommentComposer.test.tsx b/packages/lab/src/Comments/CommentComposer.test.tsx
index 9cc53d3bdd..b89d6197ef 100644
--- a/packages/lab/src/Comments/CommentComposer.test.tsx
+++ b/packages/lab/src/Comments/CommentComposer.test.tsx
@@ -310,6 +310,60 @@ describe('CommentComposer', () => {
expect(onUploadImages).not.toHaveBeenCalled();
});
+ it('refuses images pasted or dropped into the input while a post is in flight (AST-085 FR2)', async () => {
+ const onChange = vi.fn();
+ const onUploadImages = vi
+ .fn()
+ .mockResolvedValue([{url: 'https://example.com/a.png', alt: 'Chart'}]);
+ render(
+ <CommentComposer
+ value="Draft"
+ onChange={onChange}
+ onSubmit={() => {}}
+ onUploadImages={onUploadImages}
+ isSubmitting
+ />,
+ );
+ const textbox = screen.getByRole('textbox');
+ const file = new File(['png'], 'chart.png', {type: 'image/png'});
+ fireEvent.paste(textbox, {
+ clipboardData: {files: [file], getData: () => ''},
+ });
+ fireEvent.paste(textbox, {
+ clipboardData: {files: [file], getData: () => ''},
+ });
+ fireEvent.drop(textbox, {dataTransfer: {files: [file], types: ['Files']}});
+ await settle();
+ expect(onUploadImages).not.toHaveBeenCalled();
+ expect(onChange).not.toHaveBeenCalled();
+ });
+
+ it('starts no second upload from a paste while an upload is pending', async () => {
+ const onUploadImages = vi.fn(
+ () => new Promise<{url: string; alt: string}[]>(() => {}),
+ );
+ render(
+ <CommentComposer
+ value="Draft"
+ onChange={() => {}}
+ onSubmit={() => {}}
+ onUploadImages={onUploadImages}
+ />,
+ );
+ const textbox = screen.getByRole('textbox');
+ const file = new File(['png'], 'chart.png', {type: 'image/png'});
+ fireEvent.paste(textbox, {
+ clipboardData: {files: [file], getData: () => ''},
+ });
+ await settle();
+ fireEvent.paste(textbox, {
+ clipboardData: {files: [file], getData: () => ''},
+ });
+ fireEvent.drop(textbox, {dataTransfer: {files: [file], types: ['Files']}});
+ await settle();
+ expect(onUploadImages).toHaveBeenCalledTimes(1);
+ });
+
it('does not open the image chooser from the error banner while busy', async () => {
const onUploadImages = vi.fn();
const props = {
diff --git a/packages/lab/src/Comments/CommentComposer.tsx b/packages/lab/src/Comments/CommentComposer.tsx
index fbb6953fae..d993cfba02 100644
--- a/packages/lab/src/Comments/CommentComposer.tsx
+++ b/packages/lab/src/Comments/CommentComposer.tsx
@@ -328,8 +328,15 @@ export function CommentComposerBase({
}
};
+ // The picker, a paste, and a drop all arrive here. A busy composer starts
+ // no upload, so none of them can add images to a draft in flight.
const beginImageUpload = (files: readonly File[]) => {
- if (onUploadImages == null || files.length === 0) {
+ if (
+ onUploadImages == null ||
+ files.length === 0 ||
+ isSubmittingRef.current ||
+ actionPendingRef.current
+ ) {
return;
}
const images = files.filter(file => file.type.startsWith('image/'));