Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/media-stale-banner-focus.md
Original file line number Diff line number Diff line change
@@ -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.
9 changes: 8 additions & 1 deletion packages/ui-react/src/components/activity/hygiene-media.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ export function MediaFinding({ item, controller: c }: { item: InboxActionItem; c
const [texts, setTexts] = useState<Partial<Record<Row, string>>>({});
const [preview, setPreview] = useState<Preview | null>(null);
const [banner, setBanner] = useState<string | null>(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<string | null>(null);
const aborter = useRef<AbortController | null>(null);
Expand Down Expand Up @@ -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;
Expand Down
9 changes: 7 additions & 2 deletions packages/ui-react/tests/browser/hygiene-media.layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down Expand Up @@ -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();
Expand Down
Loading