Skip to content
Open
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
20 changes: 18 additions & 2 deletions frontend/src/lib/useLastPageFocus.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useRef, useState } from "react";
import { useLayoutEffect, useRef, useState } from "react";
import userEvent from "@testing-library/user-event";
import { afterEach, describe, expect, it } from "vitest";
import { act, cleanup, render, screen } from "../test/rtl";
Expand All @@ -8,12 +8,13 @@ import { useLastPageFocus } from "./useLastPageFocus";
// ends the read with these rows shown and says whether a page is left.
let finish: (rows: string[], more: boolean) => void = () => {};

function List({ first }: { first: string[] }) {
function List({ first, startOnCommit = false }: { first: string[]; startOnCommit?: boolean }) {
const [rows, setRows] = useState(first);
const [loading, setLoading] = useState(false);
const [more, setMore] = useState(true);
const box = useRef<HTMLUListElement | null>(null);
const search = useRef<HTMLInputElement | null>(null);
const loadMore = useRef<HTMLButtonElement | null>(null);
const mark = useLastPageFocus({
loading,
more,
Expand All @@ -23,6 +24,13 @@ function List({ first }: { first: string[] }) {
),
fallback: () => search.current,
});
useLayoutEffect(() => {
if (!startOnCommit) return;
// Start before the previous render's passive effects flush.
loadMore.current?.focus();
mark(first);
setLoading(true);
}, []);
finish = (next, left) => {
setRows(next);
setMore(left);
Expand All @@ -42,6 +50,7 @@ function List({ first }: { first: string[] }) {
</ul>
{more && (
<button
ref={loadMore}
type="button"
onClick={() => {
mark(rows);
Expand All @@ -63,6 +72,13 @@ async function pressLoadMore() {
afterEach(cleanup);

describe("useLastPageFocus", () => {
it("keeps a load marked before the previous render's passive effects flush", () => {
render(<List first={["a"]} startOnCommit />);
expect(screen.getByRole("button", { name: "Load more" })).toHaveFocus();
act(() => finish(["a", "b"], false));
expect(screen.getByRole("button", { name: "b" })).toHaveFocus();
});

it("focuses the first row the last page added", async () => {
render(<List first={["a"]} />);
await pressLoadMore();
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/lib/useLastPageFocus.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from "react";
import { useLayoutEffect, useRef } from "react";

export interface LastPageFocusOptions {
/** A Load more read is in flight. */
Expand All @@ -25,7 +25,8 @@ export function useLastPageFocus({
fallback,
}: LastPageFocusOptions): (shown: Iterable<string>) => void {
const before = useRef<Set<string> | null>(null);
useEffect(() => {
// Finish with this commit so an older passive effect cannot consume a new mark.
useLayoutEffect(() => {
const had = before.current;
if (had === null || loading) return;
before.current = null;
Expand Down