Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 737b3fc

Browse files
committed
Handle permalinks in room topic
Fixes: element-hq/element-web#23395
1 parent 985bde7 commit 737b3fc

File tree

2 files changed

+84
-2
lines changed

2 files changed

+84
-2
lines changed

src/components/views/elements/RoomTopic.tsx

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import MatrixClientContext from "../../../contexts/MatrixClientContext";
3131
import AccessibleButton from "./AccessibleButton";
3232
import TooltipTarget from "./TooltipTarget";
3333
import { Linkify, topicToHtml } from "../../../HtmlUtils";
34+
import { tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
3435

3536
interface IProps extends React.HTMLProps<HTMLDivElement> {
3637
room: Room;
@@ -46,12 +47,27 @@ export default function RoomTopic({ room, ...props }: IProps): JSX.Element {
4647
const onClick = useCallback(
4748
(e: React.MouseEvent<HTMLDivElement>) => {
4849
props.onClick?.(e);
50+
4951
const target = e.target as HTMLElement;
50-
if (target.tagName.toUpperCase() === "A") {
52+
53+
if (target.tagName.toUpperCase() !== "A") {
54+
dis.fire(Action.ShowRoomTopic);
55+
return;
56+
}
57+
58+
const anchor: HTMLLinkElement | null = e.target as HTMLLinkElement;
59+
60+
if (!anchor) {
5161
return;
5262
}
5363

54-
dis.fire(Action.ShowRoomTopic);
64+
const localHref = tryTransformPermalinkToLocalHref(anchor.href);
65+
66+
if (localHref !== anchor.href) {
67+
// it could be converted to a localHref -> therefore handle locally
68+
e.preventDefault();
69+
window.location.hash = localHref;
70+
}
5571
},
5672
[props],
5773
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
Copyright 2023 The Matrix.org Foundation C.I.C.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import React from "react";
18+
import { Room } from "matrix-js-sdk/src/models/room";
19+
import { fireEvent, render, screen } from "@testing-library/react";
20+
21+
import { mkEvent, stubClient } from "../../../test-utils";
22+
import { MatrixClientPeg } from "../../../../src/MatrixClientPeg";
23+
import RoomTopic from "../../../../src/components/views/elements/RoomTopic";
24+
25+
describe("<RoomTopic/>", () => {
26+
const originalHref = window.location.href;
27+
28+
afterEach(() => {
29+
window.location.href = originalHref;
30+
});
31+
32+
function runClickTest(topic, clickText, expectedHref) {
33+
stubClient();
34+
35+
const room = new Room("!pMBteVpcoJRdCJxDmn:matrix.org", MatrixClientPeg.safeGet(), "@alice:example.org");
36+
const topicEvent = mkEvent({
37+
type: "m.room.topic",
38+
room: "!pMBteVpcoJRdCJxDmn:matrix.org",
39+
user: "@alice:example.org",
40+
content: { topic },
41+
ts: 123,
42+
event: true,
43+
});
44+
45+
room.addLiveEvents([topicEvent]);
46+
47+
render(<RoomTopic room={room} />);
48+
49+
fireEvent.click(screen.getByText(clickText));
50+
expect(window.location.href).toEqual(expectedHref);
51+
}
52+
53+
it("should capture permalink clicks", () => {
54+
const permalink =
55+
"https://matrix.to/#/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
56+
const expectedHref =
57+
"http://localhost/#/room/!pMBteVpcoJRdCJxDmn:matrix.org/$K4Kg0fL-GKpW1EQ6lS36bP4eUXadWJFkdK_FH73Df8A?via=matrix.org";
58+
runClickTest(`... ${permalink} ...`, permalink, expectedHref);
59+
});
60+
61+
it("should not capture non-permalink clicks", () => {
62+
const link = "https://matrix.org";
63+
const expectedHref = originalHref;
64+
runClickTest(`... ${link} ...`, link, expectedHref);
65+
});
66+
});

0 commit comments

Comments
 (0)