@devries-cameron20 / t3code Mirror · read-only / commits / b424c6e429
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 15:45 UTC

fix(web): an armed pull request confirm no longer outlives its button (#25530)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>

Julius Marminge committed Oct 26, 2026 at 09:07 UTC b424c6e429f189067897de45d7445d900feadc6a
5 files changed +182 -53
apps/web/src/components/pullRequest/LinkPullRequestDialog.browser.test.tsx new
+39
@@ -0,0 +1,39 @@
1 +import { EnvironmentId, ThreadId } from "@t3tools/contracts";
2 +import { expect, it, vi } from "vite-plus/test";
3 +import { page } from "vite-plus/test/browser";
4 +
5 +vi.mock(import("~/hooks/usePullRequestLinking"), async (original) => ({
6 + ...(await original()),
7 + usePullRequestLinking: (() => ({
8 + mode: "multiple",
9 + canLink: () => true,
10 + changeLink: vi.fn(),
11 + })) as never,
12 +}));
13 +vi.mock(import("~/state/entities"), async (original) => ({
14 + ...(await original()),
15 + useProjects: () => [],
16 + useThreadShell: () => null,
17 +}));
18 +
19 +import { RegistryContext } from "@effect/atom-react";
20 +
21 +import { appAtomRegistry } from "~/rpc/atomRegistry";
22 +import { act, render } from "../../../test/render";
23 +import { LinkPullRequestDialogHost, openLinkPullRequestDialog } from "./LinkPullRequestDialog";
24 +
25 +it("focuses the reference input when the dialog opens", async () => {
26 + await using _screen = await render(
27 + <RegistryContext.Provider value={appAtomRegistry}>
28 + <LinkPullRequestDialogHost />
29 + </RegistryContext.Provider>,
30 + );
31 + await act(() =>
32 + openLinkPullRequestDialog({
33 + environmentId: EnvironmentId.make("environment"),
34 + threadId: ThreadId.make("thread"),
35 + }),
36 + );
37 +
38 + await expect.element(page.getByPlaceholder("Pull request URL or #42")).toHaveFocus();
39 +});
apps/web/src/components/pullRequest/LinkPullRequestDialog.tsx
+2 -7
@@ -5,7 +5,7 @@ import {
5 } from "@t3tools/client-runtime/pull-request-link-input";
6 import { useAtomValue } from "@effect/atom-react";
7 import { settlePromise, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
8 -import { useEffect, useRef, useState } from "react";
8 +import { useRef, useState } from "react";
9
10 import { useProjects, useThreadShell } from "~/state/entities";
11 import { usePullRequestLinking } from "~/hooks/usePullRequestLinking";
@@ -85,11 +85,6 @@ function LinkPullRequestDialog({
85 setSubmitError(null);
86 }
87 }
88 - useEffect(() => {
89 - if (!open) return;
90 - const frame = window.requestAnimationFrame(() => inputRef.current?.focus());
91 - return () => window.cancelAnimationFrame(frame);
92 - }, [open]);
88
89 const resolved = resolveLinkPullRequestInput({
90 reference,
@@ -126,7 +121,7 @@ function LinkPullRequestDialog({
121
122 return (
123 <Dialog open={open} onOpenChange={(next) => (pending ? undefined : onOpenChange(next))}>
129 - <DialogPopup className="max-w-xl">
124 + <DialogPopup className="max-w-xl" initialFocus={inputRef}>
125 <DialogHeader>
126 <DialogTitle>Link pull request</DialogTitle>
127 <DialogDescription>
apps/web/src/components/pullRequest/PullRequestCommentBody.tsx
+12 -12
@@ -1,4 +1,4 @@
1 -import { useEffect, useId, useRef, useState, type ComponentProps } from "react";
1 +import { useId, useRef, useState, type ComponentProps } from "react";
2
3 import { cn } from "~/lib/utils";
4 import { Button } from "../ui/button";
@@ -11,25 +11,25 @@ export function PullRequestCommentBody({
11 }: ComponentProps<typeof PullRequestMarkdown>) {
12 const [expanded, setExpanded] = useState(false);
13 const [overflowing, setOverflowing] = useState(false);
14 - const content = useRef<HTMLDivElement>(null);
14 + const clip = useRef<HTMLDivElement>(null);
15 const id = useId();
16
17 - useEffect(() => {
18 - const element = content.current;
19 - if (!element) return;
20 - return observeResize(element, ([entry]) => {
21 - if (entry) setOverflowing(entry.contentRect.height > 240);
22 - });
23 - }, []);
24 -
17 return (
18 <div className={cn("min-w-0", className)}>
19 <div
20 + ref={clip}
21 id={id}
22 className={cn("relative", !expanded && "max-h-[240px] overflow-hidden")}
23 onFocusCapture={() => setExpanded(true)}
24 >
32 - <div ref={content}>
25 + <div
26 + ref={(element) => {
27 + if (element === null) return;
28 + return observeResize(element, ([entry]) => {
29 + if (entry) setOverflowing(entry.contentRect.height > 240);
30 + });
31 + }}
32 + >
33 <PullRequestMarkdown {...props} />
34 </div>
35 {overflowing && !expanded ? (
@@ -47,7 +47,7 @@ export function PullRequestCommentBody({
47 aria-expanded={expanded}
48 aria-controls={id}
49 onClick={() => {
50 - if (expanded) content.current?.parentElement?.scrollIntoView({ block: "nearest" });
50 + if (expanded) clip.current?.scrollIntoView({ block: "nearest" });
51 setExpanded(!expanded);
52 }}
53 >
apps/web/src/components/pullRequest/useInlineActionConfirm.browser.test.tsx new
+78
@@ -0,0 +1,78 @@
1 +import { render } from "../../../test/render";
2 +import { expect, it, vi } from "vite-plus/test";
3 +import { userEvent } from "vite-plus/test/browser";
4 +
5 +import { useInlineActionConfirm } from "./useInlineActionConfirm";
6 +
7 +function MergeButton({ scopeKey, onConfirm }: { scopeKey: string; onConfirm: () => void }) {
8 + const confirm = useInlineActionConfirm(scopeKey);
9 + return (
10 + <button type="button" {...confirm.props("merge", onConfirm)}>
11 + {confirm.isArmed("merge") ? "Confirm merge?" : "Merge"}
12 + </button>
13 + );
14 +}
15 +
16 +function pressEscape(): KeyboardEvent {
17 + const event = new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true });
18 + window.dispatchEvent(event);
19 + return event;
20 +}
21 +
22 +it("confirms on the second click and disarms on Escape", async () => {
23 + const onConfirm = vi.fn();
24 + await using screen = await render(<MergeButton scopeKey="a" onConfirm={onConfirm} />);
25 + const button = screen.getByRole("button");
26 +
27 + await userEvent.click(button);
28 + await expect.element(button).toHaveTextContent("Confirm merge?");
29 + expect(pressEscape().defaultPrevented).toBe(true);
30 + await expect.element(button).toHaveTextContent("Merge");
31 +
32 + await userEvent.click(button);
33 + await userEvent.click(button);
34 + expect(onConfirm).toHaveBeenCalledTimes(1);
35 + await expect.element(button).toHaveTextContent("Merge");
36 +});
37 +
38 +it("stops swallowing Escape once the scope changes", async () => {
39 + const onConfirm = vi.fn();
40 + await using screen = await render(<MergeButton scopeKey="a" onConfirm={onConfirm} />);
41 + const button = screen.getByRole("button");
42 +
43 + await userEvent.click(button);
44 + await expect.element(button).toHaveTextContent("Confirm merge?");
45 + await screen.rerender(<MergeButton scopeKey="b" onConfirm={onConfirm} />);
46 + await expect.element(button).toHaveTextContent("Merge");
47 + expect(pressEscape().defaultPrevented).toBe(false);
48 +
49 + await screen.rerender(<MergeButton scopeKey="a" onConfirm={onConfirm} />);
50 + await expect.element(button).toHaveTextContent("Merge");
51 +});
52 +
53 +// The hook's owner stays mounted while the armed button goes away, as when the primary
54 +// action changes under an armed merge button.
55 +function ToolbarWithMergeButton({ shown }: { shown: boolean }) {
56 + const confirm = useInlineActionConfirm("a");
57 + return shown ? (
58 + <button type="button" {...confirm.props("merge", () => undefined)}>
59 + {confirm.isArmed("merge") ? "Confirm merge?" : "Merge"}
60 + </button>
61 + ) : (
62 + <span>gone</span>
63 + );
64 +}
65 +
66 +it("stops swallowing Escape once the armed button unmounts", async () => {
67 + await using screen = await render(<ToolbarWithMergeButton shown />);
68 + const button = screen.getByRole("button");
69 +
70 + await userEvent.click(button);
71 + await expect.element(button).toHaveTextContent("Confirm merge?");
72 + await screen.rerender(<ToolbarWithMergeButton shown={false} />);
73 + await expect.element(screen.getByText("gone")).toBeVisible();
74 + expect(pressEscape().defaultPrevented).toBe(false);
75 +
76 + await screen.rerender(<ToolbarWithMergeButton shown />);
77 + await expect.element(screen.getByRole("button")).toHaveTextContent("Merge");
78 +});
apps/web/src/components/pullRequest/useInlineActionConfirm.ts
+51 -34
@@ -1,51 +1,68 @@
1 -import { useEffect, useState, type MouseEvent } from "react";
1 +import { useState, type MouseEvent } from "react";
2 +
3 +interface ArmedAction {
4 + readonly scopeKey: string;
5 + readonly action: string;
6 + readonly element: HTMLElement;
7 + // Stable per arming, so React attaches it once. Detaching it (disarm, scope change, or the
8 + // armed element unmounting) removes the listeners and disarms.
9 + readonly ref: (node: HTMLElement | null) => (() => void) | undefined;
10 +}
11
12 export function useInlineActionConfirm(scopeKey: string) {
4 - const [armed, setArmed] = useState<{
5 - scopeKey: string;
6 - action: string;
7 - element: HTMLElement;
8 - } | null>(null);
13 + const [armed, setArmed] = useState<ArmedAction | null>(null);
14 + if (armed !== null && armed.scopeKey !== scopeKey) setArmed(null);
15 const active = armed?.scopeKey === scopeKey ? armed : null;
16
11 - useEffect(() => {
12 - if (!armed) return;
13 - const disarm = () => setArmed(null);
14 - if (armed.scopeKey !== scopeKey) {
15 - disarm();
16 - return;
17 - }
18 - const onPointerDown = (event: PointerEvent) => {
19 - if (event.target instanceof Node && !armed.element.contains(event.target)) disarm();
20 - };
21 - const onKeyDown = (event: KeyboardEvent) => {
22 - if (event.key === "Escape") {
23 - event.preventDefault();
24 - event.stopPropagation();
25 - disarm();
26 - }
27 - };
28 - const timeout = window.setTimeout(disarm, 3000);
29 - document.addEventListener("pointerdown", onPointerDown, true);
30 - window.addEventListener("keydown", onKeyDown, true);
31 - window.addEventListener("blur", disarm);
32 - return () => {
33 - window.clearTimeout(timeout);
34 - document.removeEventListener("pointerdown", onPointerDown, true);
35 - window.removeEventListener("keydown", onKeyDown, true);
36 - window.removeEventListener("blur", disarm);
17 + const arm = (action: string, element: HTMLElement) => {
18 + const entry: ArmedAction = {
19 + scopeKey,
20 + action,
21 + element,
22 + ref: (node) => {
23 + if (node === null) return undefined;
24 + const controller = new AbortController();
25 + const { signal } = controller;
26 + const release = () => setArmed((current) => (current === entry ? null : current));
27 + document.addEventListener(
28 + "pointerdown",
29 + (event) => {
30 + if (event.target instanceof Node && !node.contains(event.target)) release();
31 + },
32 + { capture: true, signal },
33 + );
34 + window.addEventListener(
35 + "keydown",
36 + (event) => {
37 + if (event.key !== "Escape") return;
38 + event.preventDefault();
39 + event.stopPropagation();
40 + release();
41 + },
42 + { capture: true, signal },
43 + );
44 + window.addEventListener("blur", release, { signal });
45 + const timeout = window.setTimeout(release, 3000);
46 + return () => {
47 + controller.abort();
48 + window.clearTimeout(timeout);
49 + release();
50 + };
51 + },
52 };
38 - }, [armed, scopeKey]);
53 + setArmed(entry);
54 + };
55
56 return {
57 isArmed: (action: string) => active?.action === action,
58 props: (action: string, onConfirm: () => void) => ({
59 + ref: active?.action === action ? active.ref : undefined,
60 onClick: (event: MouseEvent<HTMLElement>) => {
61 if (active?.action === action && active.element === event.currentTarget) {
62 setArmed(null);
63 onConfirm();
64 } else {
48 - setArmed({ scopeKey, action, element: event.currentTarget });
65 + arm(action, event.currentTarget);
66 }
67 },
68 onBlur: () => setArmed(null),