11/*
22Copyright 2018 New Vector Ltd
3- Copyright 2019 The Matrix.org Foundation C.I.C.
3+ Copyright 2019, 2023 The Matrix.org Foundation C.I.C.
44
55Licensed under the Apache License, Version 2.0 (the "License");
66you 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
1515limitations under the License.
1616*/
1717
18- import React , { useCallback } from "react" ;
18+ import React , { useCallback , useContext } from "react" ;
19+ import { logger } from "matrix-js-sdk/src/logger" ;
1920import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
2021
2122import dis from "../../../dispatcher/dispatcher" ;
@@ -25,6 +26,7 @@ import { _t } from "../../../languageHandler";
2526import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
2627import EventTileBubble from "./EventTileBubble" ;
2728import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload" ;
29+ import RoomContext from "../../../contexts/RoomContext" ;
2830
2931interface IProps {
3032 /** The m.room.create MatrixEvent that this tile represents */
@@ -37,31 +39,50 @@ interface IProps {
3739 * room.
3840 */
3941export const RoomCreate : React . FC < IProps > = ( { mxEvent, timestamp } ) => {
42+ // Note: we ask the room for its predecessor here, instead of directly using
43+ // the information inside mxEvent. This allows us the flexibility later to
44+ // use a different predecessor (e.g. through MSC3946) and still display it
45+ // in the timeline location of the create event.
46+ const roomContext = useContext ( RoomContext ) ;
47+ const predecessor = roomContext . room . findPredecessor ( ) ;
48+
4049 const onLinkClicked = useCallback (
4150 ( e : React . MouseEvent ) : void => {
4251 e . preventDefault ( ) ;
4352
44- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
45-
4653 dis . dispatch < ViewRoomPayload > ( {
4754 action : Action . ViewRoom ,
48- event_id : predecessor [ "event_id" ] ,
55+ event_id : predecessor . eventId ,
4956 highlighted : true ,
50- room_id : predecessor [ "room_id" ] ,
57+ room_id : predecessor . roomId ,
5158 metricsTrigger : "Predecessor" ,
5259 metricsViaKeyboard : e . type !== "click" ,
5360 } ) ;
5461 } ,
55- [ mxEvent ] ,
62+ [ predecessor ] ,
5663 ) ;
57- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
58- if ( predecessor === undefined ) {
59- return < div /> ; // We should never have been instantiated in this case
64+
65+ if ( ! roomContext . room || roomContext . room . roomId !== mxEvent . getRoomId ( ) ) {
66+ logger . warn (
67+ "RoomCreate unexpectedly used outside of the context of the room containing this m.room.create event." ,
68+ ) ;
69+ return < > </ > ;
6070 }
61- const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor [ "room_id" ] ) ;
62- const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor [ "room_id" ] ) ;
71+
72+ if ( ! predecessor ) {
73+ logger . warn ( "RoomCreate unexpectedly used in a room with no predecessor." ) ;
74+ return < div /> ;
75+ }
76+
77+ const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor . roomId ) ;
78+ const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor . roomId ) ;
6379 permalinkCreator . load ( ) ;
64- const predecessorPermalink = permalinkCreator . forEvent ( predecessor [ "event_id" ] ) ;
80+ let predecessorPermalink : string ;
81+ if ( predecessor . eventId ) {
82+ predecessorPermalink = permalinkCreator . forEvent ( predecessor . eventId ) ;
83+ } else {
84+ predecessorPermalink = permalinkCreator . forRoom ( ) ;
85+ }
6586 const link = (
6687 < a href = { predecessorPermalink } onClick = { onLinkClicked } >
6788 { _t ( "Click here to see older messages." ) }
0 commit comments