Merge pull request #6094 from matrix-org/gsouquet/timeline-scroll-performance
commit
d6cd3c5ed4
|
@ -16,36 +16,44 @@ limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import PropTypes from 'prop-types';
|
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";
|
||||||
|
|
||||||
import * as WhoIsTyping from '../../../WhoIsTyping';
|
import * as WhoIsTyping from '../../../WhoIsTyping';
|
||||||
import Timer from '../../../utils/Timer';
|
import Timer from '../../../utils/Timer';
|
||||||
import {MatrixClientPeg} from '../../../MatrixClientPeg';
|
import { MatrixClientPeg } from '../../../MatrixClientPeg';
|
||||||
import MemberAvatar from '../avatars/MemberAvatar';
|
import MemberAvatar from '../avatars/MemberAvatar';
|
||||||
import {replaceableComponent} from "../../../utils/replaceableComponent";
|
import { replaceableComponent } from "../../../utils/replaceableComponent";
|
||||||
|
|
||||||
|
interface IProps {
|
||||||
|
// the room this statusbar is representing.
|
||||||
|
room: Room;
|
||||||
|
onShown?: () => void;
|
||||||
|
onHidden?: () => void;
|
||||||
|
// Number of names to display in typing indication. E.g. set to 3, will
|
||||||
|
// result in "X, Y, Z and 100 others are typing."
|
||||||
|
whoIsTypingLimit: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface IState {
|
||||||
|
usersTyping: RoomMember[];
|
||||||
|
// 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
|
||||||
|
delayedStopTypingTimers: Record<string, Timer>;
|
||||||
|
}
|
||||||
|
|
||||||
@replaceableComponent("views.rooms.WhoIsTypingTile")
|
@replaceableComponent("views.rooms.WhoIsTypingTile")
|
||||||
export default class WhoIsTypingTile extends React.Component {
|
export default class WhoIsTypingTile extends React.Component<IProps, IState> {
|
||||||
static propTypes = {
|
|
||||||
// the room this statusbar is representing.
|
|
||||||
room: PropTypes.object.isRequired,
|
|
||||||
onShown: PropTypes.func,
|
|
||||||
onHidden: PropTypes.func,
|
|
||||||
// Number of names to display in typing indication. E.g. set to 3, will
|
|
||||||
// result in "X, Y, Z and 100 others are typing."
|
|
||||||
whoIsTypingLimit: PropTypes.number,
|
|
||||||
};
|
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
whoIsTypingLimit: 3,
|
whoIsTypingLimit: 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
state = {
|
state = {
|
||||||
usersTyping: WhoIsTyping.usersTypingApartFromMe(this.props.room),
|
usersTyping: WhoIsTyping.usersTypingApartFromMe(this.props.room),
|
||||||
// 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
|
|
||||||
delayedStopTypingTimers: {},
|
delayedStopTypingTimers: {},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -71,37 +79,39 @@ export default class WhoIsTypingTile extends React.Component {
|
||||||
client.removeListener("RoomMember.typing", this.onRoomMemberTyping);
|
client.removeListener("RoomMember.typing", this.onRoomMemberTyping);
|
||||||
client.removeListener("Room.timeline", this.onRoomTimeline);
|
client.removeListener("Room.timeline", this.onRoomTimeline);
|
||||||
}
|
}
|
||||||
Object.values(this.state.delayedStopTypingTimers).forEach((t) => t.abort());
|
Object.values(this.state.delayedStopTypingTimers).forEach((t) => (t as Timer).abort());
|
||||||
}
|
}
|
||||||
|
|
||||||
_isVisible(state) {
|
private _isVisible(state: IState): boolean {
|
||||||
return state.usersTyping.length !== 0 || Object.keys(state.delayedStopTypingTimers).length !== 0;
|
return state.usersTyping.length !== 0 || Object.keys(state.delayedStopTypingTimers).length !== 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
isVisible = () => {
|
public isVisible = (): boolean => {
|
||||||
return this._isVisible(this.state);
|
return this._isVisible(this.state);
|
||||||
};
|
};
|
||||||
|
|
||||||
onRoomTimeline = (event, room) => {
|
private onRoomTimeline = (event: MatrixEvent, room: Room): void => {
|
||||||
if (room?.roomId === this.props.room?.roomId) {
|
if (room?.roomId === this.props.room?.roomId) {
|
||||||
const userId = event.getSender();
|
const userId = event.getSender();
|
||||||
// remove user from usersTyping
|
// remove user from usersTyping
|
||||||
const usersTyping = this.state.usersTyping.filter((m) => m.userId !== userId);
|
const usersTyping = this.state.usersTyping.filter((m) => m.userId !== userId);
|
||||||
this.setState({usersTyping});
|
if (usersTyping.length !== this.state.usersTyping.length) {
|
||||||
|
this.setState({usersTyping});
|
||||||
|
}
|
||||||
// abort timer if any
|
// abort timer if any
|
||||||
this._abortUserTimer(userId);
|
this.abortUserTimer(userId);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onRoomMemberTyping = (ev, member) => {
|
private onRoomMemberTyping = (): void => {
|
||||||
const usersTyping = WhoIsTyping.usersTypingApartFromMeAndIgnored(this.props.room);
|
const usersTyping = WhoIsTyping.usersTypingApartFromMeAndIgnored(this.props.room);
|
||||||
this.setState({
|
this.setState({
|
||||||
delayedStopTypingTimers: this._updateDelayedStopTypingTimers(usersTyping),
|
delayedStopTypingTimers: this.updateDelayedStopTypingTimers(usersTyping),
|
||||||
usersTyping,
|
usersTyping,
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
_updateDelayedStopTypingTimers(usersTyping) {
|
private updateDelayedStopTypingTimers(usersTyping: RoomMember[]): Record<string, Timer> {
|
||||||
const usersThatStoppedTyping = this.state.usersTyping.filter((a) => {
|
const usersThatStoppedTyping = this.state.usersTyping.filter((a) => {
|
||||||
return !usersTyping.some((b) => a.userId === b.userId);
|
return !usersTyping.some((b) => a.userId === b.userId);
|
||||||
});
|
});
|
||||||
|
@ -129,7 +139,7 @@ export default class WhoIsTypingTile extends React.Component {
|
||||||
delayedStopTypingTimers[m.userId] = timer;
|
delayedStopTypingTimers[m.userId] = timer;
|
||||||
timer.start();
|
timer.start();
|
||||||
timer.finished().then(
|
timer.finished().then(
|
||||||
() => this._removeUserTimer(m.userId), // on elapsed
|
() => this.removeUserTimer(m.userId), // on elapsed
|
||||||
() => {/* aborted */},
|
() => {/* aborted */},
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -139,15 +149,15 @@ export default class WhoIsTypingTile extends React.Component {
|
||||||
return delayedStopTypingTimers;
|
return delayedStopTypingTimers;
|
||||||
}
|
}
|
||||||
|
|
||||||
_abortUserTimer(userId) {
|
private abortUserTimer(userId: string): void {
|
||||||
const timer = this.state.delayedStopTypingTimers[userId];
|
const timer = this.state.delayedStopTypingTimers[userId];
|
||||||
if (timer) {
|
if (timer) {
|
||||||
timer.abort();
|
timer.abort();
|
||||||
this._removeUserTimer(userId);
|
this.removeUserTimer(userId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_removeUserTimer(userId) {
|
private removeUserTimer(userId: string): void {
|
||||||
const timer = this.state.delayedStopTypingTimers[userId];
|
const timer = this.state.delayedStopTypingTimers[userId];
|
||||||
if (timer) {
|
if (timer) {
|
||||||
const delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
|
const delayedStopTypingTimers = Object.assign({}, this.state.delayedStopTypingTimers);
|
||||||
|
@ -156,7 +166,7 @@ export default class WhoIsTypingTile extends React.Component {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_renderTypingIndicatorAvatars(users, limit) {
|
private renderTypingIndicatorAvatars(users: RoomMember[], limit: number): JSX.Element[] {
|
||||||
let othersCount = 0;
|
let othersCount = 0;
|
||||||
if (users.length > limit) {
|
if (users.length > limit) {
|
||||||
othersCount = users.length - limit + 1;
|
othersCount = users.length - limit + 1;
|
||||||
|
@ -210,7 +220,7 @@ export default class WhoIsTypingTile extends React.Component {
|
||||||
return (
|
return (
|
||||||
<li className="mx_WhoIsTypingTile" aria-atomic="true">
|
<li className="mx_WhoIsTypingTile" aria-atomic="true">
|
||||||
<div className="mx_WhoIsTypingTile_avatars">
|
<div className="mx_WhoIsTypingTile_avatars">
|
||||||
{ this._renderTypingIndicatorAvatars(usersTyping, this.props.whoIsTypingLimit) }
|
{ this.renderTypingIndicatorAvatars(usersTyping, this.props.whoIsTypingLimit) }
|
||||||
</div>
|
</div>
|
||||||
<div className="mx_WhoIsTypingTile_label">
|
<div className="mx_WhoIsTypingTile_label">
|
||||||
{ typingString }
|
{ typingString }
|
Loading…
Reference in New Issue