Skip to content

Commit f512ff6

Browse files
fix(web): composer banners, menus and pickers act in their handlers (pingdotgg#25238)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent f279070 commit f512ff6

11 files changed

Lines changed: 316 additions & 316 deletions

‎apps/web/src/components/chat/ChatComposer.tsx‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2277,34 +2277,33 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
22772277
stashQueue,
22782278
isStashMenuOpen,
22792279
setIsStashMenuOpen,
2280-
stashPulse,
2280+
stashPulseKey,
22812281
restoreStashEntryStable,
22822282
deleteStashEntryStable,
22832283
toggleStashMenuStable,
22842284
} = useComposerStash({
22852285
environmentId,
22862286
composerDraftTarget,
22872287
promptRef,
2288-
composerImagesRef,
2289-
composerFilesRef,
2290-
composerTerminalContextsRef,
2291-
keybindings,
2292-
terminalOpen,
2288+
readAttachmentDraft: () => ({
2289+
images: composerImagesRef.current,
2290+
files: composerFilesRef.current,
2291+
}),
2292+
readTerminalContexts: () => composerTerminalContextsRef.current,
2293+
routeThreadRef,
22932294
projectSelectionRequired,
2294-
isRevertingCheckpoint,
22952295
pendingUserInputs,
22962296
activePendingProgress,
22972297
prompt,
22982298
composerMenuOpen,
2299-
isComposerModelPickerOpen,
23002299
isComposerCollapsedMobile,
23012300
composerPreviewAnnotations,
23022301
composerReviewComments,
23032302
composerThreadContexts,
2304-
attachmentTargetKeyRef,
2305-
composerDraftTargetKeyRef,
2303+
attachmentTargetKey,
2304+
isComposerDraftTargetOpen: (key) => composerDraftTargetKeyRef.current === key,
23062305
composerEditorRef,
2307-
isRevertingCheckpointRef,
2306+
isRevertingCheckpoint: () => isRevertingCheckpointRef.current,
23082307
pendingThreadImportsRef,
23092308
setComposerCursor,
23102309
setComposerTrigger,
@@ -3578,8 +3577,7 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
35783577
<ComposerStashBadge
35793578
count={stashQueue.length}
35803579
menuOpen={isStashMenuOpen}
3581-
pulseKey={stashPulse.key}
3582-
pulsing={stashPulse.active}
3580+
pulseKey={stashPulseKey}
35833581
onToggleMenu={toggleStashMenuStable}
35843582
/>
35853583
</ComposerBanner.Dock>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { InfoIcon } from "lucide-react";
2+
import { expect, it, vi } from "vite-plus/test";
3+
import { userEvent } from "vite-plus/test/browser";
4+
5+
import { render } from "../../../test/render";
6+
import { ComposerBannerStack, type ComposerBannerStackItem } from "./ComposerBannerStack";
7+
8+
it("commits a dismissal when the thread switches during the exit transition", async () => {
9+
const onDismiss = vi.fn();
10+
const item: ComposerBannerStackItem = {
11+
id: "notice",
12+
variant: "info",
13+
icon: <InfoIcon />,
14+
title: "Thread A notice",
15+
dismissLabel: "Dismiss notice",
16+
onDismiss,
17+
};
18+
await using screen = await render(<ComposerBannerStack key="thread-a" items={[item]} />);
19+
20+
await userEvent.click(screen.getByRole("button", { name: "Dismiss notice" }));
21+
// The composer keys the stack by thread, so switching threads remounts it mid-exit.
22+
await screen.rerender(<ComposerBannerStack key="thread-b" items={[]} />);
23+
24+
await vi.waitFor(() => expect(onDismiss).toHaveBeenCalledTimes(1));
25+
});

‎apps/web/src/components/chat/ComposerBannerStack.tsx‎

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { InfoIcon } from "lucide-react";
2-
import { useEffect, useId, useLayoutEffect, useRef, useState, type ReactNode } from "react";
2+
import { useId, useLayoutEffect, useRef, useState, type ReactNode } from "react";
3+
import { flushSync } from "react-dom";
34

45
import { cn } from "~/lib/utils";
56
import { Button } from "../ui/button";
@@ -51,39 +52,25 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
5152
const noticesRef = useRef<HTMLDivElement>(null);
5253
const peekRef = useRef<HTMLButtonElement>(null);
5354
const expandedItemsRef = useRef<HTMLDivElement>(null);
54-
const pendingFocusRef = useRef<"peek" | "notice" | null>(null);
5555
const expandedItemsId = useId();
5656
const [requestedExitingItemId, setExitingItemId] = useState<string | null>(null);
57-
const dismissTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
5857
const exitingItemId =
5958
requestedExitingItemId !== null && items.some((item) => item.id === requestedExitingItemId)
6059
? requestedExitingItemId
6160
: null;
6261

63-
useEffect(() => {
64-
return () => {
65-
if (dismissTimeoutRef.current) {
66-
clearTimeout(dismissTimeoutRef.current);
67-
}
68-
};
69-
}, []);
70-
7162
if (items.length < 2 && stackExpanded) {
7263
setStackExpanded(false);
7364
}
7465

75-
useLayoutEffect(() => {
76-
if (stackExpanded && pendingFocusRef.current === "notice") {
77-
pendingFocusRef.current = null;
78-
const firstControl = expandedItemsRef.current?.querySelector<HTMLElement>(
79-
'button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]',
80-
);
81-
(firstControl ?? expandedItemsRef.current)?.focus({ preventScroll: true });
82-
} else if (!stackExpanded && pendingFocusRef.current === "peek") {
83-
pendingFocusRef.current = null;
84-
peekRef.current?.focus({ preventScroll: true });
85-
}
86-
}, [stackExpanded]);
66+
const expandAndFocusNotice = () => {
67+
flushSync(() => setStackExpanded(true));
68+
const container = expandedItemsRef.current;
69+
const firstControl = container?.querySelector<HTMLElement>(
70+
'button:not(:disabled), a[href], input:not(:disabled), [tabindex="0"]',
71+
);
72+
(firstControl ?? container)?.focus({ preventScroll: true });
73+
};
8774

8875
const orderedItems = items.toSorted((a, b) => bannerPriority(a) - bannerPriority(b));
8976
const frontItem = orderedItems[0];
@@ -106,13 +93,9 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
10693
return;
10794
}
10895
setExitingItemId(item.id);
109-
if (dismissTimeoutRef.current) {
110-
clearTimeout(dismissTimeoutRef.current);
111-
}
112-
dismissTimeoutRef.current = setTimeout(() => {
113-
dismissTimeoutRef.current = null;
114-
item.onDismiss?.();
115-
}, DISMISS_TRANSITION_MS);
96+
// A timer, not transitionend: the dismissal must commit even when the stack unmounts
97+
// (thread switch) or a collapsed composer hides it mid-exit, and transitionend never fires then.
98+
setTimeout(() => item.onDismiss?.(), DISMISS_TRANSITION_MS);
11699
};
117100

118101
return (
@@ -126,7 +109,8 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
126109
onPointerEnter={(event) => {
127110
if (event.pointerType === "touch") return;
128111
if (document.activeElement === peekRef.current) {
129-
pendingFocusRef.current = "notice";
112+
expandAndFocusNotice();
113+
return;
130114
}
131115
setStackExpanded(true);
132116
}}
@@ -145,8 +129,8 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
145129
if (event.key !== "Escape" || !stackExpanded) return;
146130
event.preventDefault();
147131
event.stopPropagation();
148-
pendingFocusRef.current = "peek";
149-
setStackExpanded(false);
132+
flushSync(() => setStackExpanded(false));
133+
peekRef.current?.focus({ preventScroll: true });
150134
}}
151135
>
152136
{showCollapsedStackCap && firstStackedItem ? (
@@ -160,8 +144,7 @@ export function ComposerBannerStack({ className, items, attachedAbove }: Compose
160144
tabIndex={stackExpanded ? -1 : 0}
161145
onClick={(event) => {
162146
event.currentTarget.focus({ preventScroll: true });
163-
pendingFocusRef.current = "notice";
164-
setStackExpanded(true);
147+
expandAndFocusNotice();
165148
}}
166149
className={cn(stackExpanded && "pointer-events-none invisible opacity-0")}
167150
/>
Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
import { memo, useEffect, useState, type ReactNode } from "react";
1+
import { memo, Suspense, use, type ReactNode } from "react";
22

33
import {
44
createComposerImageThumbnail,
55
readComposerImageThumbnail,
66
} from "../../lib/imageCompression";
77

8-
export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
8+
export const ComposerImageThumbnail = memo(function ComposerImageThumbnail(props: {
9+
file: File;
10+
alt: string;
11+
className: string;
12+
fallback: ReactNode;
13+
}) {
14+
return (
15+
<Suspense fallback={props.fallback}>
16+
<ThumbnailImage {...props} />
17+
</Suspense>
18+
);
19+
});
20+
21+
function ThumbnailImage({
922
file,
1023
alt,
1124
className,
@@ -16,16 +29,8 @@ export const ComposerImageThumbnail = memo(function ComposerImageThumbnail({
1629
className: string;
1730
fallback: ReactNode;
1831
}) {
19-
const [preview, setPreview] = useState<{ file: File; src: string | null } | null>(null);
20-
useEffect(() => {
21-
let active = true;
22-
void createComposerImageThumbnail(file).then((src) => {
23-
if (active) setPreview({ file, src });
24-
});
25-
return () => {
26-
active = false;
27-
};
28-
}, [file]);
29-
const src = preview?.file === file ? preview.src : (readComposerImageThumbnail(file) ?? null);
32+
// A settled thumbnail paints synchronously instead of suspending for a tick.
33+
const settled = readComposerImageThumbnail(file);
34+
const src = settled === undefined ? use(createComposerImageThumbnail(file)) : settled;
3035
return src ? <img src={src} alt={alt} className={className} /> : fallback;
31-
});
36+
}

‎apps/web/src/components/chat/ComposerStashBadge.tsx‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ import { ComposerBanner } from "./ComposerBanner";
77
export const ComposerStashBadge = memo(function ComposerStashBadge(props: {
88
count: number;
99
menuOpen: boolean;
10+
/** Bumped per stash; each new key replays the pulse. 0 means never pulsed. */
1011
pulseKey: number;
11-
pulsing: boolean;
1212
onToggleMenu: () => void;
1313
}) {
1414
if (props.count === 0) return null;
15+
const pulsed = props.pulseKey > 0;
1516
const count = (
1617
<ComposerBanner.Count
18+
// Remounting the count per stash restarts its CSS pulse; no timer has to end it.
1719
key={props.pulseKey}
1820
className={cn(
19-
props.pulsing
20-
? "text-primary transition-[opacity,translate] duration-180 ease-out starting:translate-y-0.5 starting:opacity-0 motion-reduce:transition-none"
21-
: "text-muted-foreground",
21+
"text-muted-foreground",
22+
pulsed &&
23+
"animate-stash-pulse-count transition-[opacity,translate] duration-180 ease-out starting:translate-y-0.5 starting:opacity-0 motion-reduce:transition-none",
2224
)}
2325
>
2426
{props.count}
@@ -40,9 +42,11 @@ export const ComposerStashBadge = memo(function ComposerStashBadge(props: {
4042
className={cn(
4143
"transition-colors duration-200",
4244
props.menuOpen && "pointer-events-none",
43-
props.menuOpen || props.pulsing
44-
? "text-foreground"
45-
: "text-muted-foreground hover:text-foreground",
45+
props.menuOpen ? "text-foreground" : "text-muted-foreground hover:text-foreground",
46+
// The button keeps its identity (focus, hover) across stashes. Swapping between two
47+
// identical keyframes restarts the row's animation instead.
48+
pulsed &&
49+
(props.pulseKey % 2 === 0 ? "animate-stash-pulse-row-alt" : "animate-stash-pulse-row"),
4650
)}
4751
onPointerDown={(event) => event.preventDefault()}
4852
onClick={props.onToggleMenu}

0 commit comments

Comments
 (0)