11/*
2- Copyright 2018 New Vector Ltd
2+ oopyright 2018 New Vector Ltd
3+ mcrete
34Copyright 2019 The Matrix.org Foundation C.I.C.
45
56Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,7 +16,8 @@ See the License for the specific language governing permissions and
1516limitations under the License.
1617*/
1718
18- import React , { useCallback } from "react" ;
19+ import React , { useCallback , useContext } from "react" ;
20+ import { logger } from "matrix-js-sdk/src/logger" ;
1921import { MatrixEvent } from "matrix-js-sdk/src/models/event" ;
2022
2123import dis from "../../../dispatcher/dispatcher" ;
@@ -25,6 +27,7 @@ import { _t } from "../../../languageHandler";
2527import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
2628import EventTileBubble from "./EventTileBubble" ;
2729import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload" ;
30+ import RoomContext from "../../../contexts/RoomContext" ;
2831
2932interface IProps {
3033 /** The m.room.create MatrixEvent that this tile represents */
@@ -37,31 +40,50 @@ interface IProps {
3740 * room.
3841 */
3942export const RoomCreate : React . FC < IProps > = ( { mxEvent, timestamp } ) => {
43+ // Note: we ask the room for its predecessor here, instead of directly using
44+ // the information inside mxEvent. This allows us the flexibility later to
45+ // use a different predecessor (e.g. through MSC3946) and still display it
46+ // in the timeline location of the create event.
47+ const roomContext = useContext ( RoomContext ) ;
48+ const predecessor = roomContext . room . findPredecessor ( ) ;
49+
4050 const onLinkClicked = useCallback (
4151 ( e : React . MouseEvent ) : void => {
4252 e . preventDefault ( ) ;
4353
44- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
45-
4654 dis . dispatch < ViewRoomPayload > ( {
4755 action : Action . ViewRoom ,
48- event_id : predecessor [ "event_id" ] ,
56+ event_id : predecessor . eventId ,
4957 highlighted : true ,
50- room_id : predecessor [ "room_id" ] ,
58+ room_id : predecessor . roomId ,
5159 metricsTrigger : "Predecessor" ,
5260 metricsViaKeyboard : e . type !== "click" ,
5361 } ) ;
5462 } ,
55- [ mxEvent ] ,
63+ [ predecessor ] ,
5664 ) ;
57- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
58- if ( predecessor === undefined ) {
59- return < div /> ; // We should never have been instantiated in this case
65+
66+ if ( ! roomContext . room || roomContext . room . roomId !== mxEvent . getRoomId ( ) ) {
67+ logger . warn (
68+ "RoomCreate unexpectedly used outside of the context of the room containing this m.room.create event." ,
69+ ) ;
70+ return < > </ > ;
6071 }
61- const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor [ "room_id" ] ) ;
62- const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor [ "room_id" ] ) ;
72+
73+ if ( ! predecessor ) {
74+ logger . warn ( "RoomCreate unexpectedly used in a room with no predecessor." ) ;
75+ return < div /> ;
76+ }
77+
78+ const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor . roomId ) ;
79+ const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor . roomId ) ;
6380 permalinkCreator . load ( ) ;
64- const predecessorPermalink = permalinkCreator . forEvent ( predecessor [ "event_id" ] ) ;
81+ let predecessorPermalink : string ;
82+ if ( predecessor . eventId ) {
83+ predecessorPermalink = permalinkCreator . forEvent ( predecessor . eventId ) ;
84+ } else {
85+ predecessorPermalink = permalinkCreator . forRoom ( ) ;
86+ }
6587 const link = (
6688 < a href = { predecessorPermalink } onClick = { onLinkClicked } >
6789 { _t ( "Click here to see older messages." ) }
0 commit comments