2016-02-23 15:46:27 +00:00
|
|
|
/*
|
2020-10-30 17:29:32 +00:00
|
|
|
Copyright 2015, 2016, 2017, 2020 The Matrix.org Foundation C.I.C.
|
2016-02-23 15:46:27 +00: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-03-08 13:28:07 +00:00
|
|
|
import React, { ReactNode } from "react";
|
2021-06-18 16:22:31 +01:00
|
|
|
import { lexicographicCompare } from "matrix-js-sdk/src/utils";
|
2021-06-29 13:11:58 +01:00
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-10-22 17:23:32 -05:00
|
|
|
import { throttle } from "lodash";
|
2022-02-22 12:18:08 +00:00
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
2022-02-24 14:39:25 +00:00
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
2021-06-18 16:22:31 +01:00
|
|
|
|
|
|
|
|
import { MatrixClientPeg } from "../../../MatrixClientPeg";
|
|
|
|
|
import AppsDrawer from "./AppsDrawer";
|
2018-12-24 15:09:10 +00:00
|
|
|
import SettingsStore from "../../../settings/SettingsStore";
|
2020-03-28 00:51:01 +00:00
|
|
|
import AutoHideScrollbar from "../../structures/AutoHideScrollbar";
|
2021-06-16 12:07:58 +01:00
|
|
|
import { UIFeature } from "../../../settings/UIFeature";
|
2021-06-22 17:23:13 +01:00
|
|
|
import ResizeNotifier from "../../../utils/ResizeNotifier";
|
2022-08-30 15:13:39 -04:00
|
|
|
import LegacyCallViewForRoom from "../voip/LegacyCallViewForRoom";
|
2021-06-16 12:07:58 +01:00
|
|
|
import { objectHasDiff } from "../../../utils/objects";
|
2017-05-30 16:09:57 +02:00
|
|
|
|
2020-10-30 17:29:32 +00:00
|
|
|
interface IProps {
|
|
|
|
|
// js-sdk room object
|
2021-07-01 23:23:03 +01:00
|
|
|
room: Room;
|
|
|
|
|
userId: string;
|
|
|
|
|
showApps: boolean; // Render apps
|
|
|
|
|
resizeNotifier: ResizeNotifier;
|
2023-03-08 13:28:07 +00:00
|
|
|
children?: ReactNode;
|
2020-10-30 17:29:32 +00:00
|
|
|
}
|
2016-03-08 12:16:34 +00:00
|
|
|
|
2020-10-30 17:29:32 +00:00
|
|
|
interface Counter {
|
2021-07-01 23:23:03 +01:00
|
|
|
title: string;
|
|
|
|
|
value: number;
|
|
|
|
|
link: string;
|
|
|
|
|
severity: string;
|
|
|
|
|
stateKey: string;
|
2020-10-30 17:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface IState {
|
2021-07-01 23:23:03 +01:00
|
|
|
counters: Counter[];
|
2020-10-30 17:29:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default class AuxPanel extends React.Component<IProps, IState> {
|
2022-12-16 12:29:59 +00:00
|
|
|
public static defaultProps = {
|
2018-02-09 13:23:34 +00:00
|
|
|
showApps: true,
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
2018-02-09 13:23:34 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public constructor(props: IProps) {
|
2020-08-29 12:14:16 +01:00
|
|
|
super(props);
|
2019-01-08 11:14:31 +00:00
|
|
|
|
2020-08-29 12:14:16 +01:00
|
|
|
this.state = {
|
2021-06-16 12:07:58 +01:00
|
|
|
counters: this.computeCounters(),
|
2020-08-29 12:14:16 +01:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentDidMount(): void {
|
2018-12-24 14:27:50 +00:00
|
|
|
const cli = MatrixClientPeg.get();
|
2021-06-16 12:06:41 +01:00
|
|
|
if (SettingsStore.getValue("feature_state_counters")) {
|
2022-02-24 14:39:25 +00:00
|
|
|
cli.on(RoomStateEvent.Events, this.onRoomStateEvents);
|
2021-06-16 12:06:41 +01:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
public componentWillUnmount(): void {
|
2022-02-24 14:39:25 +00:00
|
|
|
if (SettingsStore.getValue("feature_state_counters")) {
|
|
|
|
|
MatrixClientPeg.get()?.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
|
2018-12-24 14:27:50 +00:00
|
|
|
}
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
public shouldComponentUpdate(nextProps: IProps, nextState: IState): boolean {
|
2021-02-19 00:00:10 +00:00
|
|
|
return objectHasDiff(this.props, nextProps) || objectHasDiff(this.state, nextState);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2016-03-08 12:16:34 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private onRoomStateEvents = (ev: MatrixEvent): void => {
|
2022-02-24 14:39:25 +00:00
|
|
|
if (ev.getType() === "re.jki.counter") {
|
|
|
|
|
this.updateCounters();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
private updateCounters = throttle(
|
|
|
|
|
() => {
|
2021-06-16 12:07:58 +01:00
|
|
|
this.setState({ counters: this.computeCounters() });
|
2021-07-01 18:35:38 +01:00
|
|
|
},
|
|
|
|
|
500,
|
|
|
|
|
{ leading: true, trailing: true },
|
|
|
|
|
);
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2023-01-12 13:25:14 +00:00
|
|
|
private computeCounters(): Counter[] {
|
|
|
|
|
const counters: Counter[] = [];
|
2019-01-08 11:14:31 +00:00
|
|
|
|
2020-08-17 13:12:18 -06:00
|
|
|
if (this.props.room && SettingsStore.getValue("feature_state_counters")) {
|
2019-01-08 11:14:31 +00:00
|
|
|
const stateEvs = this.props.room.currentState.getStateEvents("re.jki.counter");
|
2023-04-17 08:31:58 +01:00
|
|
|
stateEvs.sort((a, b) => lexicographicCompare(a.getStateKey()!, b.getStateKey()!));
|
2019-01-08 11:14:31 +00:00
|
|
|
|
2020-10-30 17:29:32 +00:00
|
|
|
for (const ev of stateEvs) {
|
2019-01-08 11:14:31 +00:00
|
|
|
const title = ev.getContent().title;
|
|
|
|
|
const value = ev.getContent().value;
|
|
|
|
|
const link = ev.getContent().link;
|
|
|
|
|
const severity = ev.getContent().severity || "normal";
|
2023-04-17 08:31:58 +01:00
|
|
|
const stateKey = ev.getStateKey()!;
|
2019-01-08 11:14:31 +00:00
|
|
|
|
2022-05-09 16:52:05 -06:00
|
|
|
// We want a non-empty title but can accept falsy values (e.g.
|
2019-03-11 14:04:12 +00:00
|
|
|
// zero)
|
|
|
|
|
if (title && value !== undefined) {
|
2019-01-08 11:14:31 +00:00
|
|
|
counters.push({
|
2020-10-30 17:29:32 +00:00
|
|
|
title,
|
|
|
|
|
value,
|
|
|
|
|
link,
|
|
|
|
|
severity,
|
|
|
|
|
stateKey,
|
2021-06-29 13:11:58 +01:00
|
|
|
});
|
2019-01-08 11:14:31 +00:00
|
|
|
}
|
2020-10-30 17:29:32 +00:00
|
|
|
}
|
2019-01-08 11:14:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return counters;
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
2019-01-08 11:14:31 +00:00
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2017-05-17 21:15:57 +01:00
|
|
|
const callView = (
|
2022-08-30 15:13:39 -04:00
|
|
|
<LegacyCallViewForRoom
|
2020-12-03 17:45:49 +00:00
|
|
|
roomId={this.props.room.roomId}
|
2021-03-02 14:09:11 +01:00
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
2021-11-23 11:13:51 +01:00
|
|
|
showApps={this.props.showApps}
|
2016-02-23 15:46:27 +00:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
|
2020-09-16 11:38:50 +01:00
|
|
|
let appsDrawer;
|
|
|
|
|
if (SettingsStore.getValue(UIFeature.Widgets)) {
|
|
|
|
|
appsDrawer = (
|
|
|
|
|
<AppsDrawer
|
|
|
|
|
room={this.props.room}
|
|
|
|
|
userId={this.props.userId}
|
|
|
|
|
showApps={this.props.showApps}
|
|
|
|
|
resizeNotifier={this.props.resizeNotifier}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
2017-05-17 21:15:57 +01:00
|
|
|
|
2023-02-13 11:39:16 +00:00
|
|
|
let stateViews: JSX.Element | null = null;
|
2020-08-17 13:12:18 -06:00
|
|
|
if (this.state.counters && SettingsStore.getValue("feature_state_counters")) {
|
2023-02-13 11:39:16 +00:00
|
|
|
const counters: JSX.Element[] = [];
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2019-01-08 11:14:31 +00:00
|
|
|
this.state.counters.forEach((counter, idx) => {
|
|
|
|
|
const title = counter.title;
|
|
|
|
|
const value = counter.value;
|
|
|
|
|
const link = counter.link;
|
|
|
|
|
const severity = counter.severity;
|
|
|
|
|
const stateKey = counter.stateKey;
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2020-09-16 11:38:50 +01:00
|
|
|
let span = (
|
|
|
|
|
<span>
|
|
|
|
|
{title}: {value}
|
|
|
|
|
</span>
|
|
|
|
|
);
|
2018-12-24 14:27:50 +00:00
|
|
|
|
2019-03-12 10:00:10 +00:00
|
|
|
if (link) {
|
2018-12-24 14:27:50 +00:00
|
|
|
span = (
|
2020-02-23 22:14:29 +00:00
|
|
|
<a href={link} target="_blank" rel="noreferrer noopener">
|
2019-03-12 10:00:10 +00:00
|
|
|
{span}
|
|
|
|
|
</a>
|
2018-12-24 14:27:50 +00:00
|
|
|
);
|
|
|
|
|
}
|
2019-03-12 10:00:10 +00:00
|
|
|
|
|
|
|
|
span = (
|
|
|
|
|
<span
|
|
|
|
|
className="m_RoomView_auxPanel_stateViews_span"
|
|
|
|
|
data-severity={severity}
|
2021-07-19 22:43:11 +01:00
|
|
|
key={"x-" + stateKey}
|
2019-03-12 10:00:10 +00:00
|
|
|
>
|
2021-07-19 22:43:11 +01:00
|
|
|
{span}
|
2019-03-12 10:00:10 +00:00
|
|
|
</span>
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
counters.push(span);
|
|
|
|
|
counters.push(
|
|
|
|
|
<span className="m_RoomView_auxPanel_stateViews_delim" key={"delim" + idx}>
|
|
|
|
|
{" "}
|
2020-10-30 17:29:32 +00:00
|
|
|
─{" "}
|
|
|
|
|
</span>,
|
2019-03-12 10:00:10 +00:00
|
|
|
);
|
2018-12-24 14:27:50 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (counters.length > 0) {
|
|
|
|
|
counters.pop(); // remove last deliminator
|
|
|
|
|
stateViews = <div className="m_RoomView_auxPanel_stateViews">{counters}</div>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 15:46:27 +00:00
|
|
|
return (
|
2021-08-27 13:31:43 +02:00
|
|
|
<AutoHideScrollbar className="mx_RoomView_auxPanel">
|
2018-12-24 14:27:50 +00:00
|
|
|
{stateViews}
|
2021-08-27 13:31:43 +02:00
|
|
|
{this.props.children}
|
2017-05-17 21:15:57 +01:00
|
|
|
{appsDrawer}
|
2016-02-23 15:46:27 +00:00
|
|
|
{callView}
|
2020-03-28 00:51:01 +00:00
|
|
|
</AutoHideScrollbar>
|
2016-02-23 15:46:27 +00:00
|
|
|
);
|
2020-08-29 12:14:16 +01:00
|
|
|
}
|
|
|
|
|
}
|