Handle permalinks in room topic (#11115)
* Handle permalinks in room topic Fixes: vector-im/element-web#23395 * Add test for clicking non-link
This commit is contained in:
parent
767cd628f9
commit
9d9c55d92e
2 changed files with 94 additions and 2 deletions
|
@ -31,6 +31,7 @@ import MatrixClientContext from "../../../contexts/MatrixClientContext";
|
||||||
import AccessibleButton from "./AccessibleButton";
|
import AccessibleButton from "./AccessibleButton";
|
||||||
import TooltipTarget from "./TooltipTarget";
|
import TooltipTarget from "./TooltipTarget";
|
||||||
import { Linkify, topicToHtml } from "../../../HtmlUtils";
|
import { Linkify, topicToHtml } from "../../../HtmlUtils";
|
||||||
|
import { tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
|
||||||
|
|
||||||
interface IProps extends React.HTMLProps<HTMLDivElement> {
|
interface IProps extends React.HTMLProps<HTMLDivElement> {
|
||||||
room: Room;
|
room: Room;
|
||||||
|
@ -46,12 +47,22 @@ export default function RoomTopic({ room, ...props }: IProps): JSX.Element {
|
||||||
const onClick = useCallback(
|
const onClick = useCallback(
|
||||||
(e: React.MouseEvent<HTMLDivElement>) => {
|
(e: React.MouseEvent<HTMLDivElement>) => {
|
||||||
props.onClick?.(e);
|
props.onClick?.(e);
|
||||||
|
|
||||||
const target = e.target as HTMLElement;
|
const target = e.target as HTMLElement;
|
||||||
if (target.tagName.toUpperCase() === "A") {
|
|
||||||
|
if (target.tagName.toUpperCase() !== "A") {
|
||||||
|
dis.fire(Action.ShowRoomTopic);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
dis.fire(Action.ShowRoomTopic);
|
const anchor = e.target as HTMLLinkElement;
|
||||||
|
const localHref = tryTransformPermalinkToLocalHref(anchor.href);
|
||||||
|
|
||||||
|
if (localHref !== anchor.href) {
|
||||||
|
// it could be converted to a localHref -> therefore handle locally
|
||||||
|
e.preventDefault();
|
||||||
|
window.location.hash = localHref;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[props],
|
[props],
|
||||||
);
|
);
|
||||||
|
|
81
test/components/views/elements/RoomTopic-test.tsx
Normal file
81
test/components/views/elements/RoomTopic-test.tsx
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
/*
|
||||||
|
Copyright 2023 The Matrix.org Foundation C.I.C.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import React from "react";
|
||||||
|
import { Room } from "matrix-js-sdk/src/models/room";
|
||||||
|
import { fireEvent, render, screen } from "@testing-library/react";
|
||||||
|
|
||||||
|
import { mkEvent, stubClient } from "../../../test-utils";
|
||||||
|
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
|
||||||
|
import RoomTopic from "../../../../src/components/views/elements/RoomTopic";
|
||||||
|
import dis from "../../../../src/dispatcher/dispatcher";
|
||||||
|
import { Action } from "../../../../src/dispatcher/actions";
|
||||||
|
|
||||||
|
jest.mock("../../../../src/dispatcher/dispatcher");
|
||||||
|
|
||||||
|
describe("<RoomTopic/>", () => {
|
||||||
|
const originalHref = window.location.href;
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
window.location.href = originalHref;
|
||||||
|
});
|
||||||
|
|
||||||
|
function runClickTest(topic: string, clickText: string) {
|
||||||
|
stubClient();
|
||||||
|
|
||||||
|
const room = new Room("!pMBteVpcoJRdCJxDmn:matrix.org", MatrixClientPeg.safeGet(), "@alice:example.org");
|
||||||
|
const topicEvent = mkEvent({
|
||||||
|
type: "m.room.topic",
|
||||||
|
room: "!pMBteVpcoJRdCJxDmn:matrix.org",
|
||||||
|
user: "@alice:example.org",
|
||||||
|
content: { topic },
|
||||||
|
ts: 123,
|
||||||
|
event: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
room.addLiveEvents([topicEvent]);
|
||||||
|
|
||||||
|
render(<RoomTopic room={room} />);
|
||||||
|
|
||||||
|
fireEvent.click(screen.getByText(clickText));
|
||||||
|
}
|
||||||
|
|
||||||
|
it("should capture permalink clicks", () => {
|
||||||
|
const permalink =
|
||||||
|
"https://matrix.to/#/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
|
||||||
|
const expectedHref =
|
||||||
|
"http://localhost/#/room/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
|
||||||
|
runClickTest(`... ${permalink} ...`, permalink);
|
||||||
|
expect(window.location.href).toEqual(expectedHref);
|
||||||
|
expect(dis.fire).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should not capture non-permalink clicks", () => {
|
||||||
|
const link = "https://matrix.org";
|
||||||
|
const expectedHref = originalHref;
|
||||||
|
runClickTest(`... ${link} ...`, link);
|
||||||
|
expect(window.location.href).toEqual(expectedHref);
|
||||||
|
expect(dis.fire).toHaveBeenCalledTimes(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("should open topic dialog when not clicking a link", () => {
|
||||||
|
const topic = "foobar";
|
||||||
|
const expectedHref = originalHref;
|
||||||
|
runClickTest(topic, topic);
|
||||||
|
expect(window.location.href).toEqual(expectedHref);
|
||||||
|
expect(dis.fire).toHaveBeenCalledWith(Action.ShowRoomTopic);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in a new issue