diff --git a/.changeset/media-stale-banner-focus.md b/.changeset/media-stale-banner-focus.md new file mode 100644 index 0000000000..2be39a554b --- /dev/null +++ b/.changeset/media-stale-banner-focus.md @@ -0,0 +1,5 @@ +--- +"@schlessera/brain-ui-react": patch +--- + +On a stale apply, the media review card's "Changed since the preview" banner now reliably takes focus. Before, a slow frame could leave focus on the destination field. diff --git a/packages/ui-react/src/components/activity/hygiene-media.tsx b/packages/ui-react/src/components/activity/hygiene-media.tsx index 41ed9f1b5f..56d2df273c 100644 --- a/packages/ui-react/src/components/activity/hygiene-media.tsx +++ b/packages/ui-react/src/components/activity/hygiene-media.tsx @@ -316,6 +316,8 @@ export function MediaFinding({ item, controller: c }: { item: InboxActionItem; c const [texts, setTexts] = useState>>({}); const [preview, setPreview] = useState(null); const [banner, setBanner] = useState(null); + /** Counts stale applies; each one focuses the banner once it has rendered. */ + const [bannerFocus, setBannerFocus] = useState(0); const [filesOpen, setFilesOpen] = useState(false); const latest = useRef(null); const aborter = useRef(null); @@ -414,12 +416,17 @@ export function MediaFinding({ item, controller: c }: { item: InboxActionItem; c handled.current = item.version; if (outcome.status === "stale" && row) { setBanner(outcome.reason === "stale-approval" ? refusalText({ code: "stale-approval" }) : "Changed since the preview. Updated below. Check it before applying."); + setBannerFocus((n) => n + 1); void run(row, texts[row] ?? ""); - requestAnimationFrame(() => bannerRef.current?.focus()); } else if (receipt?.outcome === "partial") requestAnimationFrame(() => showFiles.current?.focus()); else requestAnimationFrame(() => recoveryRef.current?.focus()); // eslint-disable-next-line react-hooks/exhaustive-deps }, [item.version]); + // The banner is state set just above, so it mounts in a later commit than this card's + // outcome: focus it after that commit, never on a frame that may come first (#3657). + useEffect(() => { + if (bannerFocus > 0) bannerRef.current?.focus(); + }, [bannerFocus]); const current = preview?.state === "done" && row !== null && preview.row === row && preview.text === text ? preview : null; const replicatedPlan = row ? planFor(options.get(row), row, text) : null; diff --git a/packages/ui-react/tests/browser/hygiene-media.layout.tsx b/packages/ui-react/tests/browser/hygiene-media.layout.tsx index 3802825b5e..d7934c9117 100644 --- a/packages/ui-react/tests/browser/hygiene-media.layout.tsx +++ b/packages/ui-react/tests/browser/hygiene-media.layout.tsx @@ -39,7 +39,12 @@ afterEach(async () => { u?.dispose(); s?.close(); h?.remove(); - if (f) await commands.hygieneServer({ op: "stop", id: f.id }); + if (f) { + // Stop drains every forwarded request, so a failure while a plan is held must release it first, + // or the drain waits on the held preview until Node's fetch times out (#3657). + try { await commands.hygieneServer({ op: "set", id: f.id, patch: { releasePlans: true } }); } + finally { await commands.hygieneServer({ op: "stop", id: f.id }); } + } if (originalTheme === undefined) delete document.documentElement.dataset.theme; else document.documentElement.dataset.theme = originalTheme; }); @@ -318,7 +323,7 @@ test("dark 320: a preview that throws is a 409 the card states, and nothing was await radio("Move to a final location").click(); await destination("places/throw.png"); // A 409 with no newer Action states its failure only after the card's one-second wait for a - // newer version (`newerThan`, packages/ui-react/src/components/activity/hygiene-media.tsx:341-348), + // newer version (`newerThan`, packages/ui-react/src/components/activity/hygiene-media.tsx:343-350), // so the default one-second poll races that window. Allow the wait plus the request. await expect.poll(() => alertText(), { timeout: 5000 }).toBe("Couldn't prepare a preview. Nothing was changed."); expect(page.getByRole("button", { name: "Try again", exact: true }).query()).not.toBeNull();