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,8 @@ import { _t } from "../../../languageHandler";
2526import { MatrixClientPeg } from "../../../MatrixClientPeg" ;
2627import EventTileBubble from "./EventTileBubble" ;
2728import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload" ;
29+ import RoomContext from "../../../contexts/RoomContext" ;
30+ import { useRoomState } from "../../../hooks/useRoomState" ;
2831
2932interface IProps {
3033 /** The m.room.create MatrixEvent that this tile represents */
@@ -37,31 +40,57 @@ 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 = useRoomState (
49+ roomContext . room ,
50+ useCallback ( ( state ) => state . findPredecessor ( ) , [ ] ) ,
51+ ) ;
52+
4053 const onLinkClicked = useCallback (
4154 ( e : React . MouseEvent ) : void => {
4255 e . preventDefault ( ) ;
4356
44- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
45-
4657 dis . dispatch < ViewRoomPayload > ( {
4758 action : Action . ViewRoom ,
48- event_id : predecessor [ "event_id" ] ,
59+ event_id : predecessor . eventId ,
4960 highlighted : true ,
50- room_id : predecessor [ "room_id" ] ,
61+ room_id : predecessor . roomId ,
5162 metricsTrigger : "Predecessor" ,
5263 metricsViaKeyboard : e . type !== "click" ,
5364 } ) ;
5465 } ,
55- [ mxEvent ] ,
66+ [ predecessor ?. eventId , predecessor ?. roomId ] ,
5667 ) ;
57- const predecessor = mxEvent . getContent ( ) [ "predecessor" ] ;
58- if ( predecessor === undefined ) {
59- return < div /> ; // We should never have been instantiated in this case
68+
69+ if ( ! roomContext . room || roomContext . room . roomId !== mxEvent . getRoomId ( ) ) {
70+ logger . warn (
71+ "RoomCreate unexpectedly used outside of the context of the room containing this m.room.create event." ,
72+ ) ;
73+ return < > </ > ;
6074 }
61- const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor [ "room_id" ] ) ;
62- const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor [ "room_id" ] ) ;
75+
76+ if ( ! predecessor ) {
77+ logger . warn ( "RoomCreate unexpectedly used in a room with no predecessor." ) ;
78+ return < div /> ;
79+ }
80+
81+ const prevRoom = MatrixClientPeg . get ( ) . getRoom ( predecessor . roomId ) ;
82+ if ( ! prevRoom ) {
83+ logger . warn ( `Failed to find predecessor room with id ${ predecessor . roomId } ` ) ;
84+ return < > </ > ;
85+ }
86+ const permalinkCreator = new RoomPermalinkCreator ( prevRoom , predecessor . roomId ) ;
6387 permalinkCreator . load ( ) ;
64- const predecessorPermalink = permalinkCreator . forEvent ( predecessor [ "event_id" ] ) ;
88+ let predecessorPermalink : string ;
89+ if ( predecessor . eventId ) {
90+ predecessorPermalink = permalinkCreator . forEvent ( predecessor . eventId ) ;
91+ } else {
92+ predecessorPermalink = permalinkCreator . forRoom ( ) ;
93+ }
6594 const link = (
6695 < a href = { predecessorPermalink } onClick = { onLinkClicked } >
6796 { _t ( "Click here to see older messages." ) }
0 commit comments