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:
Johannes Marbach 2023-06-21 20:09:22 +02:00 committed by GitHub
parent 767cd628f9
commit 9d9c55d92e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 94 additions and 2 deletions

View file

@ -31,6 +31,7 @@ import MatrixClientContext from "../../../contexts/MatrixClientContext";
import AccessibleButton from "./AccessibleButton";
import TooltipTarget from "./TooltipTarget";
import { Linkify, topicToHtml } from "../../../HtmlUtils";
import { tryTransformPermalinkToLocalHref } from "../../../utils/permalinks/Permalinks";
interface IProps extends React.HTMLProps<HTMLDivElement> {
room: Room;
@ -46,12 +47,22 @@ export default function RoomTopic({ room, ...props }: IProps): JSX.Element {
const onClick = useCallback(
(e: React.MouseEvent<HTMLDivElement>) => {
props.onClick?.(e);
const target = e.target as HTMLElement;
if (target.tagName.toUpperCase() === "A") {
if (target.tagName.toUpperCase() !== "A") {
dis.fire(Action.ShowRoomTopic);
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],
);