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
82 changes: 82 additions & 0 deletions src/features/messages/ThreadPanel.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ vi.mock("./MessageComposer", () => ({
data-placeholder={placeholder}
data-focus-request={focusRequest}
>
<textarea aria-label="Reply" />
<button type="button" onClick={() => onSend?.("own-reply")}>
Send fixture reply
</button>
Expand Down Expand Up @@ -2881,6 +2882,87 @@ it("an open relayed group stays open when paging adds an earlier member", async
).toHaveAttribute("aria-expanded", "false");
});

it.each([
["route", "after"],
["route", "before"],
["reveal request", "after"],
["reveal request", "before"],
] as const)(
"a %s into the open thread leaves focus in the editor the person typed into while it retargets (focused %s the request)",
async (kind, focused) => {
vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockReturnValue(
new DOMRect(0, 0, 500, 500),
);
const h = messagesHarness();
const reply = { ...row, id: "d".repeat(64), content: "the reply" };
h.snapshot.replies = [reply];
h.snapshot.target = reply;
h.snapshot.targetStatus = "ready";
const props = h.props as ThreadPanelProps;
Object.assign(props, { retainThread: true });
h.render();
const editor = screen.getByRole("textbox", { name: "Reply" });
// A click into the editor, with the browser's own focus move.
const enter = () => {
fireEvent.pointerDown(editor);
editor.focus();
fireEvent.pointerUp(editor);
fireEvent.click(editor);
};
// The person is already writing in the editor as the request arrives.
if (focused === "before") act(enter);
let release!: (value: "selected") => void;
h.view.retarget.mockImplementation(
() =>
new Promise((resolve) => {
release = resolve;
}),
);
const navigation = { ...ordinaryNavigation(), entryId: "reply" };
navigation.target.messageId = reply.id;
Object.assign(
props,
{ messageId: reply.id },
kind === "route"
? { navigation }
: { revealSelected: true, revealRequest: 1 },
);
h.rerenderProps();
expect(h.view.retarget).toHaveBeenCalledOnce();
// The editor stays usable during the read: the person clicks into it and
// types, or keeps typing.
act(() => {
if (focused === "after") enter();
fireEvent.keyDown(editor, { key: "a" });
fireEvent(
editor,
new InputEvent("beforeinput", {
bubbles: true,
cancelable: true,
inputType: "insertText",
data: "a",
}),
);
});
await act(async () => release("selected"));
await waitFor(() =>
expect(
vi
.mocked(HTMLElement.prototype.scrollIntoView)
.mock.contexts.some(
(context) =>
(context as HTMLElement).dataset.messageId === reply.id,
),
).toBe(true),
);
if (kind === "route")
await waitFor(() =>
expect(navigation.complete).toHaveBeenCalledWith({ status: "opened" }),
);
expect(editor).toHaveFocus();
},
);

it("reopens collapsed activity for a new reveal of the same step", async () => {
const h = messagesHarness();
const replies = ["d", "e"].map((id, index) => ({
Expand Down
24 changes: 22 additions & 2 deletions src/features/messages/ThreadPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ import { useListedChannel } from "../relay/listed-channel";
import type { CSSProperties } from "react";
import styles from "./Messages.module.css";
import { Reading, readingPositioned } from "./use-reading";
import { useMessageReveal } from "./use-message-reveal";
import {
useMessageReveal,
watchArrival,
type RevealArrival,
} from "./use-message-reveal";
import { usePendingRequest } from "./use-pending-request";
import type { PageNavigation } from "../navigation/service";
import { messageViewKey } from "./view-key";
Expand Down Expand Up @@ -255,6 +259,8 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
// Settles with its selection, so a retarget's line is decided for it.
unreadRequest: ThreadPanelProps["unreadRequest"];
revision: number;
/** The person's input since a request confirmed in place arrived. */
arrival?: RevealArrival | undefined;
};
const current = useRef<Opening | undefined>(undefined);
const [opening, setOpening] = useState<Opening>();
Expand Down Expand Up @@ -299,6 +305,7 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
const signal = navigation
? AbortSignal.any([request.signal, navigation.signal])
: request.signal;
let stopArrival: (() => void) | undefined;
if (!retrying.current) {
setError(undefined);
setFailure(undefined);
Expand All @@ -325,6 +332,10 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
}
if (prior?.view.snapshot().root) {
if (navigation) setRouting(navigation);
// The editor stays usable while the read runs, so focus the person
// moves or text they type meanwhile belongs to this request's reveal.
const arrival = { input: false };
stopArrival = watchArrival(arrival);
let result: ThreadRetarget;
try {
result = await prior.view.retarget(messageId, signal);
Expand All @@ -341,6 +352,7 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
navigation,
revealRequest,
unreadRequest,
arrival,
};
current.current = next;
setOpening(next);
Expand Down Expand Up @@ -399,7 +411,10 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
}
}
void select();
return () => request.abort();
return () => {
request.abort();
stopArrival?.();
};
}, [
session,
channelId,
Expand Down Expand Up @@ -430,6 +445,7 @@ function RetainedThreadPanel(props: ThreadPanelProps) {
routePending={!!routing}
revealRequest={opening.revealRequest}
unreadRequest={opening.unreadRequest}
arrival={opening.arrival}
selectionFailure={
failure && { ...failure, pending, retry: retryNavigation }
}
Expand Down Expand Up @@ -592,10 +608,13 @@ function ThreadMessages({
selectionFailure,
onResolvedRoot,
routePending,
arrival,
}: {
intro?: ReactNode;
subthreadId?: string | undefined;
onResolvedRoot?: ThreadPanelProps["onResolvedRoot"];
/** The person's input since the request retargeted in place arrived. */
arrival?: RevealArrival | undefined;
/** A route to this reader is on its way: until it lands, a reader not yet
* shown stays hidden and unread rather than showing a place the route
* replaces. */
Expand Down Expand Up @@ -1229,6 +1248,7 @@ function ThreadMessages({
snapshot.targetStatus === "ready" && snapshot.target?.id === messageId,
complete: completeTarget,
prepare: prepareTarget,
arrival,
});
// Leaving a visit once it was presented, by navigating on, hiding or
// unmounting, records where the reader was. A return counts only once
Expand Down
Loading
Loading