Use Room.findPredecessor when rendering RoomCreate tiles (#10012)
* Use Room.findPredecessor to render RoomCreate tiles * Handle state updates * Handle missing predecessor * Check for missing a room
This commit is contained in:
parent
371a3c0d36
commit
33b89a5709
2 changed files with 78 additions and 18 deletions
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
Copyright 2018 New Vector Ltd
|
Copyright 2018 New Vector Ltd
|
||||||
Copyright 2019 The Matrix.org Foundation C.I.C.
|
Copyright 2019, 2023 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
Licensed under the Apache License, Version 2.0 (the "License");
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
you may not use this file except in compliance with the License.
|
you may not use this file except in compliance with the License.
|
||||||
|
@ -15,7 +15,8 @@ See the License for the specific language governing permissions and
|
||||||
limitations under the License.
|
limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React, { useCallback } from "react";
|
import React, { useCallback, useContext } from "react";
|
||||||
|
import { logger } from "matrix-js-sdk/src/logger";
|
||||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||||
|
|
||||||
import dis from "../../../dispatcher/dispatcher";
|
import dis from "../../../dispatcher/dispatcher";
|
||||||
|
@ -25,6 +26,8 @@ import { _t } from "../../../languageHandler";
|
||||||
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
||||||
import EventTileBubble from "./EventTileBubble";
|
import EventTileBubble from "./EventTileBubble";
|
||||||
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
||||||
|
import RoomContext from "../../../contexts/RoomContext";
|
||||||
|
import { useRoomState } from "../../../hooks/useRoomState";
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
/** The m.room.create MatrixEvent that this tile represents */
|
/** The m.room.create MatrixEvent that this tile represents */
|
||||||
|
@ -37,31 +40,57 @@ interface IProps {
|
||||||
* room.
|
* room.
|
||||||
*/
|
*/
|
||||||
export const RoomCreate: React.FC<IProps> = ({ mxEvent, timestamp }) => {
|
export const RoomCreate: React.FC<IProps> = ({ mxEvent, timestamp }) => {
|
||||||
|
// Note: we ask the room for its predecessor here, instead of directly using
|
||||||
|
// the information inside mxEvent. This allows us the flexibility later to
|
||||||
|
// use a different predecessor (e.g. through MSC3946) and still display it
|
||||||
|
// in the timeline location of the create event.
|
||||||
|
const roomContext = useContext(RoomContext);
|
||||||
|
const predecessor = useRoomState(
|
||||||
|
roomContext.room,
|
||||||
|
useCallback((state) => state.findPredecessor(), []),
|
||||||
|
);
|
||||||
|
|
||||||
const onLinkClicked = useCallback(
|
const onLinkClicked = useCallback(
|
||||||
(e: React.MouseEvent): void => {
|
(e: React.MouseEvent): void => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
const predecessor = mxEvent.getContent()["predecessor"];
|
|
||||||
|
|
||||||
dis.dispatch<ViewRoomPayload>({
|
dis.dispatch<ViewRoomPayload>({
|
||||||
action: Action.ViewRoom,
|
action: Action.ViewRoom,
|
||||||
event_id: predecessor["event_id"],
|
event_id: predecessor.eventId,
|
||||||
highlighted: true,
|
highlighted: true,
|
||||||
room_id: predecessor["room_id"],
|
room_id: predecessor.roomId,
|
||||||
metricsTrigger: "Predecessor",
|
metricsTrigger: "Predecessor",
|
||||||
metricsViaKeyboard: e.type !== "click",
|
metricsViaKeyboard: e.type !== "click",
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
[mxEvent],
|
[predecessor?.eventId, predecessor?.roomId],
|
||||||
);
|
);
|
||||||
const predecessor = mxEvent.getContent()["predecessor"];
|
|
||||||
if (predecessor === undefined) {
|
if (!roomContext.room || roomContext.room.roomId !== mxEvent.getRoomId()) {
|
||||||
return <div />; // We should never have been instantiated in this case
|
logger.warn(
|
||||||
|
"RoomCreate unexpectedly used outside of the context of the room containing this m.room.create event.",
|
||||||
|
);
|
||||||
|
return <></>;
|
||||||
}
|
}
|
||||||
const prevRoom = MatrixClientPeg.get().getRoom(predecessor["room_id"]);
|
|
||||||
const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor["room_id"]);
|
if (!predecessor) {
|
||||||
|
logger.warn("RoomCreate unexpectedly used in a room with no predecessor.");
|
||||||
|
return <div />;
|
||||||
|
}
|
||||||
|
|
||||||
|
const prevRoom = MatrixClientPeg.get().getRoom(predecessor.roomId);
|
||||||
|
if (!prevRoom) {
|
||||||
|
logger.warn(`Failed to find predecessor room with id ${predecessor.roomId}`);
|
||||||
|
return <></>;
|
||||||
|
}
|
||||||
|
const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor.roomId);
|
||||||
permalinkCreator.load();
|
permalinkCreator.load();
|
||||||
const predecessorPermalink = permalinkCreator.forEvent(predecessor["event_id"]);
|
let predecessorPermalink: string;
|
||||||
|
if (predecessor.eventId) {
|
||||||
|
predecessorPermalink = permalinkCreator.forEvent(predecessor.eventId);
|
||||||
|
} else {
|
||||||
|
predecessorPermalink = permalinkCreator.forRoom();
|
||||||
|
}
|
||||||
const link = (
|
const link = (
|
||||||
<a href={predecessorPermalink} onClick={onLinkClicked}>
|
<a href={predecessorPermalink} onClick={onLinkClicked}>
|
||||||
{_t("Click here to see older messages.")}
|
{_t("Click here to see older messages.")}
|
||||||
|
|
|
@ -18,13 +18,16 @@ import React from "react";
|
||||||
import { act, render, screen, waitFor } from "@testing-library/react";
|
import { act, render, screen, waitFor } from "@testing-library/react";
|
||||||
import userEvent from "@testing-library/user-event";
|
import userEvent from "@testing-library/user-event";
|
||||||
import { mocked } from "jest-mock";
|
import { mocked } from "jest-mock";
|
||||||
import { EventType, MatrixEvent } from "matrix-js-sdk/src/matrix";
|
import { EventType, MatrixEvent, Room } from "matrix-js-sdk/src/matrix";
|
||||||
|
|
||||||
import dis from "../../../../src/dispatcher/dispatcher";
|
import dis from "../../../../src/dispatcher/dispatcher";
|
||||||
import SettingsStore from "../../../../src/settings/SettingsStore";
|
import SettingsStore from "../../../../src/settings/SettingsStore";
|
||||||
import { RoomCreate } from "../../../../src/components/views/messages/RoomCreate";
|
import { RoomCreate } from "../../../../src/components/views/messages/RoomCreate";
|
||||||
import { stubClient } from "../../../test-utils/test-utils";
|
import { stubClient, upsertRoomStateEvents } from "../../../test-utils/test-utils";
|
||||||
import { Action } from "../../../../src/dispatcher/actions";
|
import { Action } from "../../../../src/dispatcher/actions";
|
||||||
|
import RoomContext from "../../../../src/contexts/RoomContext";
|
||||||
|
import { getRoomContext } from "../../../test-utils";
|
||||||
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
||||||
|
|
||||||
jest.mock("../../../../src/dispatcher/dispatcher");
|
jest.mock("../../../../src/dispatcher/dispatcher");
|
||||||
|
|
||||||
|
@ -33,6 +36,7 @@ describe("<RoomCreate />", () => {
|
||||||
const roomId = "!room:server.org";
|
const roomId = "!room:server.org";
|
||||||
const createEvent = new MatrixEvent({
|
const createEvent = new MatrixEvent({
|
||||||
type: EventType.RoomCreate,
|
type: EventType.RoomCreate,
|
||||||
|
state_key: "",
|
||||||
sender: userId,
|
sender: userId,
|
||||||
room_id: roomId,
|
room_id: roomId,
|
||||||
content: {
|
content: {
|
||||||
|
@ -40,6 +44,20 @@ describe("<RoomCreate />", () => {
|
||||||
},
|
},
|
||||||
event_id: "$create",
|
event_id: "$create",
|
||||||
});
|
});
|
||||||
|
const createEventWithoutPredecessor = new MatrixEvent({
|
||||||
|
type: EventType.RoomCreate,
|
||||||
|
state_key: "",
|
||||||
|
sender: userId,
|
||||||
|
room_id: roomId,
|
||||||
|
content: {},
|
||||||
|
event_id: "$create",
|
||||||
|
});
|
||||||
|
stubClient();
|
||||||
|
const client = mocked(MatrixClientPeg.get());
|
||||||
|
const room = new Room(roomId, client, userId);
|
||||||
|
upsertRoomStateEvents(room, [createEvent]);
|
||||||
|
const roomNoPredecessors = new Room(roomId, client, userId);
|
||||||
|
upsertRoomStateEvents(roomNoPredecessors, [createEventWithoutPredecessor]);
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
jest.clearAllMocks();
|
jest.clearAllMocks();
|
||||||
|
@ -54,21 +72,34 @@ describe("<RoomCreate />", () => {
|
||||||
jest.spyOn(SettingsStore, "setValue").mockRestore();
|
jest.spyOn(SettingsStore, "setValue").mockRestore();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function renderRoomCreate(room: Room) {
|
||||||
|
return render(
|
||||||
|
<RoomContext.Provider value={getRoomContext(room, {})}>
|
||||||
|
<RoomCreate mxEvent={createEvent} />
|
||||||
|
</RoomContext.Provider>,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
it("Renders as expected", () => {
|
it("Renders as expected", () => {
|
||||||
const roomCreate = render(<RoomCreate mxEvent={createEvent} />);
|
const roomCreate = renderRoomCreate(room);
|
||||||
expect(roomCreate.asFragment()).toMatchSnapshot();
|
expect(roomCreate.asFragment()).toMatchSnapshot();
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Links to the old version of the room", () => {
|
it("Links to the old version of the room", () => {
|
||||||
render(<RoomCreate mxEvent={createEvent} />);
|
renderRoomCreate(room);
|
||||||
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
|
expect(screen.getByText("Click here to see older messages.")).toHaveAttribute(
|
||||||
"href",
|
"href",
|
||||||
"https://matrix.to/#/old_room_id/tombstone_event_id",
|
"https://matrix.to/#/old_room_id/tombstone_event_id",
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Shows an empty div if there is no predecessor", () => {
|
||||||
|
renderRoomCreate(roomNoPredecessors);
|
||||||
|
expect(screen.queryByText("Click here to see older messages.", { exact: false })).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
it("Opens the old room on click", async () => {
|
it("Opens the old room on click", async () => {
|
||||||
render(<RoomCreate mxEvent={createEvent} />);
|
renderRoomCreate(room);
|
||||||
const link = screen.getByText("Click here to see older messages.");
|
const link = screen.getByText("Click here to see older messages.");
|
||||||
|
|
||||||
await act(() => userEvent.click(link));
|
await act(() => userEvent.click(link));
|
||||||
|
|
Loading…
Reference in a new issue