Skip to content

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

TypeScript

fix(lab): CommentComposerBase reads the latest onActionPendingChange instead of re-running its cleanup when it changes (#9149)

Cindy Zhangafcbd477f760
diff --git a/packages/lab/src/Comments/CommentComposer.test.tsx b/packages/lab/src/Comments/CommentComposer.test.tsx
index 30adf9f03d..cbca3126a4 100644
--- a/packages/lab/src/Comments/CommentComposer.test.tsx
+++ b/packages/lab/src/Comments/CommentComposer.test.tsx
@@ -200,6 +200,62 @@ describe('CommentComposer', () => {
expect(screen.getByTestId('draft').textContent).toBe('Ship it 🎉');
});
+ it('ends the upload busy state when onActionPendingChange is a new function each render', async () => {
+ let finishUpload: (images: {url: string; alt: string}[]) => void = () => {};
+ const onUploadImages = vi.fn(
+ () =>
+ new Promise<{url: string; alt: string}[]>(resolve => {
+ finishUpload = resolve;
+ }),
+ );
+ function Host() {
+ const [draft, setDraft] = useState('See this');
+ const [pending, setPending] = useState(false);
+ return (
+ <>
+ <CommentComposerBase
+ value={draft}
+ onChange={setDraft}
+ onSubmit={() => {}}
+ placeholder="Reply"
+ context={{kind: 'comment'}}
+ highlightColor="yellow"
+ triggers={[]}
+ agentCommands={[]}
+ hasEmojiPicker={false}
+ isSubmitting={false}
+ onUploadImages={onUploadImages}
+ // Inline: a new callback on every render of the host.
+ onActionPendingChange={isPending => setPending(isPending)}
+ />
+ <output data-testid="pending">{String(pending)}</output>
+ </>
+ );
+ }
+ const {container} = render(<Host />);
+ const input = container.querySelector('input[type="file"]');
+ if (!(input instanceof HTMLInputElement)) {
+ throw new Error('File input not found');
+ }
+ const file = new File(['png'], 'chart.png', {type: 'image/png'});
+ await act(async () => {
+ fireEvent.change(input, {target: {files: [file]}});
+ await Promise.resolve();
+ });
+ await settle();
+ expect(onUploadImages).toHaveBeenCalledWith([file]);
+ expect(screen.getByTestId('pending').textContent).toBe('true');
+
+ await act(async () => {
+ finishUpload([{url: 'https://example.com/a.png', alt: 'Chart'}]);
+ await Promise.resolve();
+ });
+ await settle();
+
+ expect(screen.getByTestId('pending').textContent).toBe('false');
+ expect(container.querySelector('[aria-busy="true"]')).toBeNull();
+ });
+
it('uploads attached images and appends them as Markdown', async () => {
const onChange = vi.fn();
const onUploadImages = vi
diff --git a/packages/lab/src/Comments/CommentComposer.tsx b/packages/lab/src/Comments/CommentComposer.tsx
index 858570f22b..fbb6953fae 100644
--- a/packages/lab/src/Comments/CommentComposer.tsx
+++ b/packages/lab/src/Comments/CommentComposer.tsx
@@ -239,6 +239,11 @@ export function CommentComposerBase({
latestValueRef.current = value;
const isMountedRef = useRef(true);
const actionPendingRef = useRef(false);
+ // The latest callback, read when reporting. An inline callback is new on
+ // every render; depending on it would re-run the unmount cleanup below on
+ // each render and leave the composer busy after its upload ends.
+ const onActionPendingChangeRef = useRef(onActionPendingChange);
+ onActionPendingChangeRef.current = onActionPendingChange;
const isSubmittingRef = useRef(isSubmitting);
isSubmittingRef.current = isSubmitting;
const imageInputRef = useRef<HTMLInputElement>(null);
@@ -269,27 +274,27 @@ export function CommentComposerBase({
const [isActionPending, setIsActionPending] = useState(false);
const [isPickerOpen, setIsPickerOpen] = useState(false);
- const updateActionPending = useCallback(
- (nextPending: boolean) => {
- if (actionPendingRef.current === nextPending) {
- return;
- }
- actionPendingRef.current = nextPending;
- setIsActionPending(nextPending);
- onActionPendingChange?.(nextPending);
- },
- [onActionPendingChange],
- );
+ const updateActionPending = useCallback((nextPending: boolean) => {
+ if (actionPendingRef.current === nextPending) {
+ return;
+ }
+ actionPendingRef.current = nextPending;
+ setIsActionPending(nextPending);
+ onActionPendingChangeRef.current?.(nextPending);
+ }, []);
+ // Tracks mount for the upload's async completion, and on unmount releases
+ // the host's pending-action hold, which the upload's finally skips once
+ // unmounted.
useEffect(() => {
isMountedRef.current = true;
return () => {
isMountedRef.current = false;
if (actionPendingRef.current) {
actionPendingRef.current = false;
- onActionPendingChange?.(false);
+ onActionPendingChangeRef.current?.(false);
}
};
- }, [onActionPendingChange]);
+ }, []);
const uploadImages = async (files: readonly File[]) => {
if (onUploadImages == null || files.length === 0) {