2018-11-12 18:09:51 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import React from 'react';
|
2021-05-25 16:34:19 +03:00
|
|
|
import Room from "matrix-js-sdk/src/models/room";
|
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
|
|
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
|
|
|
|
2019-12-20 04:19:56 +03:00
|
|
|
import * as WhoIsTyping from '../../../WhoIsTyping';
|
2019-01-17 18:30:55 +03:00
|
|
|
import Timer from '../../../utils/Timer';
|
2021-05-25 16:10:16 +03:00
|
|
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
2018-11-12 18:09:51 +03:00
|
|
|
import MemberAvatar from '../avatars/MemberAvatar';
|
2021-05-25 16:10:16 +03:00
|
|
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
2021-06-02 12:42:17 +03:00
|
|
|
import { compare } from "../../../utils/strings";
|
2021-05-25 16:10:16 +03:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
// the room this statusbar is representing.
|
2021-05-25 16:34:19 +03:00
|
|
|
room: Room;
|
|
|
|
onShown?: () => void;
|
|
|
|
onHidden?: () => void;
|
2021-05-25 16:10:16 +03:00
|
|
|
// Number of names to display in typing indication. E.g. set to 3, will
|
|
|
|
// result in "X, Y, Z and 100 others are typing."
|
2021-05-25 16:34:19 +03:00
|
|
|
whoIsTypingLimit: number;
|
2021-05-25 16:10:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
interface IState {
|
2021-05-25 16:34:19 +03:00
|
|
|
usersTyping: RoomMember[];
|
2021-05-25 16:10:16 +03:00
|
|
|
// a map with userid => Timer to delay
|
|
|
|
// hiding the "x is typing" message for a
|
|
|
|
// user so hiding it can coincide
|
|
|
|
// with the sent message by the other side
|
|
|
|
// resulting in less timeline jumpiness
|
2021-05-25 16:34:19 +03:00
|
|
|
delayedStopTypingTimers: Record<string, Timer>;
|
2021-05-25 16:10:16 +03:00
|
|
|
}
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2021-03-09 06:12:00 +03:00
|
|
|
@replaceableComponent("views.rooms.WhoIsTypingTile")
|
2021-05-25 16:10:16 +03:00
|
|
|
export default class WhoIsTypingTile extends React.Component<IProps, IState> {
|
2020-08-29 14:14:16 +03:00
|
|
|
static defaultProps = {
|
|
|
|
whoIsTypingLimit: 3,
|
|
|
|
};
|
|
|
|
|
|
|
|
state = {
|
|
|
|
usersTyping: WhoIsTyping.usersTypingApartFromMe(this.props.room),
|
|
|
|
delayedStopTypingTimers: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-11-12 20:19:18 +03:00
|
|
|
MatrixClientPeg.get().on("RoomMember.typing", this.onRoomMemberTyping);
|
2019-01-17 18:30:55 +03:00
|
|
|
MatrixClientPeg.get().on("Room.timeline", this.onRoomTimeline);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentDidUpdate(_, prevState) {
|
2019-03-20 19:02:53 +03:00
|
|
|
const wasVisible = this._isVisible(prevState);
|
|
|
|
const isVisible = this._isVisible(this.state);
|
|
|
|
if (this.props.onShown && !wasVisible && isVisible) {
|
|
|
|
this.props.onShown();
|
2019-03-28 20:29:48 +03:00
|
|
|
} else if (this.props.onHidden && wasVisible && !isVisible) {
|
2019-03-20 19:02:53 +03:00
|
|
|
this.props.onHidden();
|
2018-11-13 11:15:16 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-11-13 11:15:16 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
componentWillUnmount() {
|
2018-11-12 18:09:51 +03:00
|
|
|
// we may have entirely lost our client as we're logging out before clicking login on the guest bar...
|
|
|
|
const client = MatrixClientPeg.get();
|
|
|
|
if (client) {
|
2018-11-12 20:19:18 +03:00
|
|
|
client.removeListener("RoomMember.typing", this.onRoomMemberTyping);
|
2019-01-17 18:30:55 +03:00
|
|
|
client.removeListener("Room.timeline", this.onRoomTimeline);
|
|
|
|
}
|
2021-05-25 16:34:19 +03:00
|
|
|
Object.values(this.state.delayedStopTypingTimers).forEach((t) => (t as Timer).abort());
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-01-17 18:30:55 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private _isVisible(state: IState): boolean {
|
2019-03-20 19:02:53 +03:00
|
|
|
return state.usersTyping.length !== 0 || Object.keys(state.delayedStopTypingTimers).length !== 0;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-03-20 19:02:53 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
public isVisible = (): boolean => {
|
2019-03-20 19:02:53 +03:00
|
|
|
return this._isVisible(this.state);
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-01-21 19:35:40 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private onRoomTimeline = (event: MatrixEvent, room: Room): void => {
|
2021-01-29 14:15:30 +03:00
|
|
|
if (room?.roomId === this.props.room?.roomId) {
|
2019-01-17 20:26:11 +03:00
|
|
|
const userId = event.getSender();
|
2019-01-17 20:32:46 +03:00
|
|
|
// remove user from usersTyping
|
2019-01-17 20:26:11 +03:00
|
|
|
const usersTyping = this.state.usersTyping.filter((m) => m.userId !== userId);
|
2021-05-25 14:45:19 +03:00
|
|
|
if (usersTyping.length !== this.state.usersTyping.length) {
|
|
|
|
this.setState({usersTyping});
|
|
|
|
}
|
2019-01-17 20:32:53 +03:00
|
|
|
// abort timer if any
|
2021-05-25 16:34:19 +03:00
|
|
|
this.abortUserTimer(userId);
|
2018-11-12 18:09:51 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private onRoomMemberTyping = (): void => {
|
2019-01-17 18:30:55 +03:00
|
|
|
const usersTyping = WhoIsTyping.usersTypingApartFromMeAndIgnored(this.props.room);
|
2018-11-12 18:09:51 +03:00
|
|
|
this.setState({
|
2021-05-25 16:34:19 +03:00
|
|
|
delayedStopTypingTimers: this.updateDelayedStopTypingTimers(usersTyping),
|
2019-01-17 18:30:55 +03:00
|
|
|
usersTyping,
|
|
|
|
});
|
2020-08-29 14:14:16 +03:00
|
|
|
};
|
2019-01-17 18:30:55 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private updateDelayedStopTypingTimers(usersTyping: RoomMember[]): Record<string, Timer> {
|
2019-01-17 18:30:55 +03:00
|
|
|
const usersThatStoppedTyping = this.state.usersTyping.filter((a) => {
|
|
|
|
return !usersTyping.some((b) => a.userId === b.userId);
|
|
|
|
});
|
|
|
|
const usersThatStartedTyping = usersTyping.filter((a) => {
|
|
|
|
return !this.state.usersTyping.some((b) => a.userId === b.userId);
|
2018-11-12 18:09:51 +03:00
|
|
|
});
|
2019-01-17 18:30:55 +03:00
|
|
|
// abort all the timers for the users that started typing again
|
|
|
|
usersThatStartedTyping.forEach((m) => {
|
2019-01-17 20:52:22 +03:00
|
|
|
const timer = this.state.delayedStopTypingTimers[m.userId];
|
2019-01-22 13:37:18 +03:00
|
|
|
if (timer) {
|
|
|
|
timer.abort();
|
|
|
|
}
|
2019-01-17 18:30:55 +03:00
|
|
|
});
|
2019-01-17 20:52:22 +03:00
|
|
|
// prepare new delayedStopTypingTimers object to update state with
|
|
|
|
let delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
|
2019-01-17 18:30:55 +03:00
|
|
|
// remove members that started typing again
|
2019-01-17 20:52:22 +03:00
|
|
|
delayedStopTypingTimers = usersThatStartedTyping.reduce((delayedStopTypingTimers, m) => {
|
|
|
|
delete delayedStopTypingTimers[m.userId];
|
|
|
|
return delayedStopTypingTimers;
|
|
|
|
}, delayedStopTypingTimers);
|
2019-01-17 18:30:55 +03:00
|
|
|
// start timer for members that stopped typing
|
2019-01-17 20:52:22 +03:00
|
|
|
delayedStopTypingTimers = usersThatStoppedTyping.reduce((delayedStopTypingTimers, m) => {
|
|
|
|
if (!delayedStopTypingTimers[m.userId]) {
|
2019-01-17 18:30:55 +03:00
|
|
|
const timer = new Timer(5000);
|
2019-01-17 20:52:22 +03:00
|
|
|
delayedStopTypingTimers[m.userId] = timer;
|
2019-01-17 18:30:55 +03:00
|
|
|
timer.start();
|
|
|
|
timer.finished().then(
|
2021-05-25 16:34:19 +03:00
|
|
|
() => this.removeUserTimer(m.userId), // on elapsed
|
2019-01-17 18:30:55 +03:00
|
|
|
() => {/* aborted */},
|
|
|
|
);
|
|
|
|
}
|
2019-01-17 20:52:22 +03:00
|
|
|
return delayedStopTypingTimers;
|
|
|
|
}, delayedStopTypingTimers);
|
2019-01-17 18:30:55 +03:00
|
|
|
|
2019-01-17 20:52:22 +03:00
|
|
|
return delayedStopTypingTimers;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private abortUserTimer(userId: string): void {
|
2019-01-17 20:52:22 +03:00
|
|
|
const timer = this.state.delayedStopTypingTimers[userId];
|
2019-01-17 18:30:55 +03:00
|
|
|
if (timer) {
|
|
|
|
timer.abort();
|
2021-05-25 16:34:19 +03:00
|
|
|
this.removeUserTimer(userId);
|
2019-01-17 18:30:55 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private removeUserTimer(userId: string): void {
|
2019-01-17 20:52:22 +03:00
|
|
|
const timer = this.state.delayedStopTypingTimers[userId];
|
2019-01-17 18:30:55 +03:00
|
|
|
if (timer) {
|
2019-01-17 20:52:22 +03:00
|
|
|
const delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
|
|
|
|
delete delayedStopTypingTimers[userId];
|
|
|
|
this.setState({delayedStopTypingTimers});
|
2019-01-17 18:30:55 +03:00
|
|
|
}
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2019-01-17 18:30:55 +03:00
|
|
|
|
2021-05-25 16:34:19 +03:00
|
|
|
private renderTypingIndicatorAvatars(users: RoomMember[], limit: number): JSX.Element[] {
|
2018-11-12 18:09:51 +03:00
|
|
|
let othersCount = 0;
|
|
|
|
if (users.length > limit) {
|
|
|
|
othersCount = users.length - limit + 1;
|
|
|
|
users = users.slice(0, limit - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
const avatars = users.map((u) => {
|
|
|
|
return (
|
|
|
|
<MemberAvatar
|
|
|
|
key={u.userId}
|
|
|
|
member={u}
|
|
|
|
width={24}
|
|
|
|
height={24}
|
|
|
|
resizeMethod="crop"
|
2019-02-24 05:30:45 +03:00
|
|
|
viewUserOnClick={true}
|
2018-11-12 18:09:51 +03:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (othersCount > 0) {
|
|
|
|
avatars.push(
|
2018-11-12 20:19:18 +03:00
|
|
|
<span className="mx_WhoIsTypingTile_remainingAvatarPlaceholder" key="others">
|
2018-11-12 18:09:51 +03:00
|
|
|
+{ othersCount }
|
|
|
|
</span>,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return avatars;
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
2018-11-12 18:09:51 +03:00
|
|
|
|
2020-08-29 14:14:16 +03:00
|
|
|
render() {
|
2019-01-17 18:30:55 +03:00
|
|
|
let usersTyping = this.state.usersTyping;
|
2019-01-17 20:52:22 +03:00
|
|
|
const stoppedUsersOnTimer = Object.keys(this.state.delayedStopTypingTimers)
|
2019-01-17 18:30:55 +03:00
|
|
|
.map((userId) => this.props.room.getMember(userId));
|
2019-01-17 20:32:53 +03:00
|
|
|
// append the users that have been reported not typing anymore
|
|
|
|
// but have a timeout timer running so they can disappear
|
|
|
|
// when a message comes in
|
2019-01-17 18:30:55 +03:00
|
|
|
usersTyping = usersTyping.concat(stoppedUsersOnTimer);
|
2019-01-21 19:49:46 +03:00
|
|
|
// sort them so the typing members don't change order when
|
|
|
|
// moved to delayedStopTypingTimers
|
2021-06-02 12:42:17 +03:00
|
|
|
usersTyping.sort((a, b) => compare(a.name, b.name));
|
2019-01-17 18:30:55 +03:00
|
|
|
|
2018-11-12 18:09:51 +03:00
|
|
|
const typingString = WhoIsTyping.whoIsTypingString(
|
2019-01-17 18:30:55 +03:00
|
|
|
usersTyping,
|
2018-11-12 18:09:51 +03:00
|
|
|
this.props.whoIsTypingLimit,
|
|
|
|
);
|
2018-11-12 20:19:18 +03:00
|
|
|
if (!typingString) {
|
2021-05-20 18:11:33 +03:00
|
|
|
return null;
|
2018-11-12 18:09:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2020-02-14 13:12:41 +03:00
|
|
|
<li className="mx_WhoIsTypingTile" aria-atomic="true">
|
2018-11-12 20:19:18 +03:00
|
|
|
<div className="mx_WhoIsTypingTile_avatars">
|
2021-05-25 16:34:19 +03:00
|
|
|
{ this.renderTypingIndicatorAvatars(usersTyping, this.props.whoIsTypingLimit) }
|
2018-11-12 18:09:51 +03:00
|
|
|
</div>
|
2018-11-12 20:19:18 +03:00
|
|
|
<div className="mx_WhoIsTypingTile_label">
|
2019-05-19 17:23:43 +03:00
|
|
|
{ typingString }
|
2018-11-12 18:09:51 +03:00
|
|
|
</div>
|
2018-11-13 11:15:43 +03:00
|
|
|
</li>
|
2018-11-12 18:09:51 +03:00
|
|
|
);
|
2020-08-29 14:14:16 +03:00
|
|
|
}
|
|
|
|
}
|