2018-08-20 16:20:59 +01:00
|
|
|
/*
|
|
|
|
|
Copyright 2018 New Vector Ltd
|
2023-02-01 15:44:46 +00:00
|
|
|
Copyright 2019, 2023 The Matrix.org Foundation C.I.C.
|
2018-08-20 16:20:59 +01:00
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-01 15:44:46 +00:00
|
|
|
import React, { useCallback, useContext } from "react";
|
|
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2018-08-20 16:20:59 +01:00
|
|
|
|
2020-05-13 20:41:41 -06:00
|
|
|
import dis from "../../../dispatcher/dispatcher";
|
2021-11-25 17:49:43 -03:00
|
|
|
import { Action } from "../../../dispatcher/actions";
|
2019-09-30 20:39:58 -06:00
|
|
|
import { RoomPermalinkCreator } from "../../../utils/permalinks/Permalinks";
|
2018-08-20 16:20:59 +01:00
|
|
|
import { _t } from "../../../languageHandler";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
2020-11-04 17:00:07 +00:00
|
|
|
import EventTileBubble from "./EventTileBubble";
|
2022-02-10 14:29:55 +00:00
|
|
|
import { ViewRoomPayload } from "../../../dispatcher/payloads/ViewRoomPayload";
|
2023-02-01 15:44:46 +00:00
|
|
|
import RoomContext from "../../../contexts/RoomContext";
|
|
|
|
|
import { useRoomState } from "../../../hooks/useRoomState";
|
2023-02-02 13:39:13 +00:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2021-09-24 20:08:15 +02:00
|
|
|
|
|
|
|
|
interface IProps {
|
2023-01-27 15:23:23 +00:00
|
|
|
/** The m.room.create MatrixEvent that this tile represents */
|
2021-09-24 20:08:15 +02:00
|
|
|
mxEvent: MatrixEvent;
|
2022-01-25 13:10:17 +00:00
|
|
|
timestamp?: JSX.Element;
|
2021-09-24 20:08:15 +02:00
|
|
|
}
|
2018-08-20 16:20:59 +01:00
|
|
|
|
2023-01-27 15:23:23 +00:00
|
|
|
/**
|
|
|
|
|
* A message tile showing that this room was created as an upgrade of a previous
|
|
|
|
|
* room.
|
|
|
|
|
*/
|
2023-01-30 10:02:32 +00:00
|
|
|
export const RoomCreate: React.FC<IProps> = ({ mxEvent, timestamp }) => {
|
2023-02-02 13:39:13 +00:00
|
|
|
const msc3946ProcessDynamicPredecessor = SettingsStore.getValue("feature_dynamic_room_predecessors");
|
|
|
|
|
|
2023-02-01 15:44:46 +00:00
|
|
|
// Note: we ask the room for its predecessor here, instead of directly using
|
|
|
|
|
// the information inside mxEvent. This allows us the flexibility later to
|
|
|
|
|
// use a different predecessor (e.g. through MSC3946) and still display it
|
|
|
|
|
// in the timeline location of the create event.
|
|
|
|
|
const roomContext = useContext(RoomContext);
|
|
|
|
|
const predecessor = useRoomState(
|
|
|
|
|
roomContext.room,
|
2023-02-02 13:39:13 +00:00
|
|
|
useCallback(
|
|
|
|
|
(state) => state.findPredecessor(msc3946ProcessDynamicPredecessor),
|
|
|
|
|
[msc3946ProcessDynamicPredecessor],
|
|
|
|
|
),
|
2023-02-01 15:44:46 +00:00
|
|
|
);
|
|
|
|
|
|
2023-01-30 10:02:32 +00:00
|
|
|
const onLinkClicked = useCallback(
|
|
|
|
|
(e: React.MouseEvent): void => {
|
|
|
|
|
e.preventDefault();
|
2018-08-20 16:20:59 +01:00
|
|
|
|
2023-01-30 10:02:32 +00:00
|
|
|
dis.dispatch<ViewRoomPayload>({
|
|
|
|
|
action: Action.ViewRoom,
|
2023-02-01 15:44:46 +00:00
|
|
|
event_id: predecessor.eventId,
|
2023-01-30 10:02:32 +00:00
|
|
|
highlighted: true,
|
2023-02-01 15:44:46 +00:00
|
|
|
room_id: predecessor.roomId,
|
2023-01-30 10:02:32 +00:00
|
|
|
metricsTrigger: "Predecessor",
|
|
|
|
|
metricsViaKeyboard: e.type !== "click",
|
|
|
|
|
});
|
|
|
|
|
},
|
2023-02-01 15:44:46 +00:00
|
|
|
[predecessor?.eventId, predecessor?.roomId],
|
2023-01-30 10:02:32 +00:00
|
|
|
);
|
2023-02-01 15:44:46 +00:00
|
|
|
|
|
|
|
|
if (!roomContext.room || roomContext.room.roomId !== mxEvent.getRoomId()) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
"RoomCreate unexpectedly used outside of the context of the room containing this m.room.create event.",
|
|
|
|
|
);
|
|
|
|
|
return <></>;
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2023-02-01 15:44:46 +00:00
|
|
|
|
|
|
|
|
if (!predecessor) {
|
|
|
|
|
logger.warn("RoomCreate unexpectedly used in a room with no predecessor.");
|
|
|
|
|
return <div />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const prevRoom = MatrixClientPeg.get().getRoom(predecessor.roomId);
|
|
|
|
|
if (!prevRoom) {
|
|
|
|
|
logger.warn(`Failed to find predecessor room with id ${predecessor.roomId}`);
|
|
|
|
|
return <></>;
|
|
|
|
|
}
|
|
|
|
|
const permalinkCreator = new RoomPermalinkCreator(prevRoom, predecessor.roomId);
|
2023-01-30 10:02:32 +00:00
|
|
|
permalinkCreator.load();
|
2023-02-01 15:44:46 +00:00
|
|
|
let predecessorPermalink: string;
|
|
|
|
|
if (predecessor.eventId) {
|
|
|
|
|
predecessorPermalink = permalinkCreator.forEvent(predecessor.eventId);
|
|
|
|
|
} else {
|
|
|
|
|
predecessorPermalink = permalinkCreator.forRoom();
|
|
|
|
|
}
|
2023-01-30 10:02:32 +00:00
|
|
|
const link = (
|
|
|
|
|
<a href={predecessorPermalink} onClick={onLinkClicked}>
|
|
|
|
|
{_t("Click here to see older messages.")}
|
|
|
|
|
</a>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<EventTileBubble
|
|
|
|
|
className="mx_CreateEvent"
|
|
|
|
|
title={_t("This room is a continuation of another conversation.")}
|
|
|
|
|
subtitle={link}
|
|
|
|
|
timestamp={timestamp}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
};
|