convert MemberEventListSummary and ELS to Typescript

Signed-off-by: Michael Telatynski <7t3chguy@gmail.com>
pull/21833/head
Michael Telatynski 2020-09-29 10:28:07 +01:00
parent 7fa1214cf1
commit 8a9d38b702
3 changed files with 143 additions and 108 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2019 The Matrix.org Foundation C.I.C. Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License"); Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. you may not use this file except in compliance with the License.
@ -14,15 +14,41 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, {useEffect} from 'react'; import React, {ReactChildren, useEffect} from 'react';
import PropTypes from 'prop-types'; import {MatrixEvent} from "matrix-js-sdk/src/models/event";
import {RoomMember} from "matrix-js-sdk/src/models/room-member";
import MemberAvatar from '../avatars/MemberAvatar'; import MemberAvatar from '../avatars/MemberAvatar';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import {MatrixEvent, RoomMember} from "matrix-js-sdk";
import {useStateToggle} from "../../../hooks/useStateToggle"; import {useStateToggle} from "../../../hooks/useStateToggle";
import AccessibleButton from "./AccessibleButton"; import AccessibleButton from "./AccessibleButton";
const EventListSummary = ({events, children, threshold=3, onToggle, startExpanded, summaryMembers=[], summaryText}) => { interface IProps {
// An array of member events to summarise
events: MatrixEvent[];
// The minimum number of events needed to trigger summarisation
threshold?: number;
// Whether or not to begin with state.expanded=true
startExpanded?: boolean,
// The list of room members for which to show avatars next to the summary
summaryMembers?: RoomMember[],
// The text to show as the summary of this event list
summaryText?: string,
// An array of EventTiles to render when expanded
children: ReactChildren,
// Called when the event list expansion is toggled
onToggle?(): void;
}
const EventListSummary: React.FC<IProps> = ({
events,
children,
threshold = 3,
onToggle,
startExpanded,
summaryMembers = [],
summaryText,
}) => {
const [expanded, toggleExpanded] = useStateToggle(startExpanded); const [expanded, toggleExpanded] = useStateToggle(startExpanded);
// Whenever expanded changes call onToggle // Whenever expanded changes call onToggle
@ -75,22 +101,4 @@ const EventListSummary = ({events, children, threshold=3, onToggle, startExpande
); );
}; };
EventListSummary.propTypes = {
// An array of member events to summarise
events: PropTypes.arrayOf(PropTypes.instanceOf(MatrixEvent)).isRequired,
// An array of EventTiles to render when expanded
children: PropTypes.arrayOf(PropTypes.element).isRequired,
// The minimum number of events needed to trigger summarisation
threshold: PropTypes.number,
// Called when the event list expansion is toggled
onToggle: PropTypes.func,
// Whether or not to begin with state.expanded=true
startExpanded: PropTypes.bool,
// The list of room members for which to show avatars next to the summary
summaryMembers: PropTypes.arrayOf(PropTypes.instanceOf(RoomMember)),
// The text to show as the summary of this event list
summaryText: PropTypes.string,
};
export default EventListSummary; export default EventListSummary;

View File

@ -16,32 +16,60 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React from 'react'; import React, { ReactChildren } from 'react';
import PropTypes from 'prop-types'; import { MatrixEvent } from "matrix-js-sdk/src/models/event";
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import { formatCommaSeparatedList } from '../../../utils/FormattingUtils'; import { formatCommaSeparatedList } from '../../../utils/FormattingUtils';
import * as sdk from "../../../index"; import { isValid3pidInvite } from "../../../RoomInvite";
import {MatrixEvent} from "matrix-js-sdk"; import EventListSummary from "./EventListSummary";
import {isValid3pidInvite} from "../../../RoomInvite";
export default class MemberEventListSummary extends React.Component { interface IProps {
static propTypes = { // An array of member events to summarise
// An array of member events to summarise events: MatrixEvent[];
events: PropTypes.arrayOf(PropTypes.instanceOf(MatrixEvent)).isRequired, // The maximum number of names to show in either each summary e.g. 2 would result "A, B and 234 others left"
// An array of EventTiles to render when expanded summaryLength?: number;
children: PropTypes.array.isRequired, // The maximum number of avatars to display in the summary
// The maximum number of names to show in either each summary e.g. 2 would result "A, B and 234 others left" avatarsMaxLength?: number;
summaryLength: PropTypes.number, // The minimum number of events needed to trigger summarisation
// The maximum number of avatars to display in the summary threshold?: number,
avatarsMaxLength: PropTypes.number, // Whether or not to begin with state.expanded=true
// The minimum number of events needed to trigger summarisation startExpanded?: boolean,
threshold: PropTypes.number, // An array of EventTiles to render when expanded
// Called when the MELS expansion is toggled children: ReactChildren;
onToggle: PropTypes.func, // Called when the MELS expansion is toggled
// Whether or not to begin with state.expanded=true onToggle?(): void,
startExpanded: PropTypes.bool, }
};
interface IUserEvents {
// The original event
mxEvent: MatrixEvent;
// The display name of the user (if not, then user ID)
displayName: string;
// The original index of the event in this.props.events
index: number;
}
enum TransitionType {
Joined = "joined",
Left = "left",
JoinedAndLeft = "joined_and_left",
LeftAndJoined = "left_and_joined",
InviteReject = "invite_reject",
InviteWithdrawal = "invite_withdrawal",
Invited = "invited",
Banned = "banned",
Unbanned = "unbanned",
Kicked = "kicked",
ChangedName = "changed_name",
ChangedAvatar = "changed_avatar",
NoChange = "no_change",
}
const SEP = ",";
export default class MemberEventListSummary extends React.Component<IProps> {
static defaultProps = { static defaultProps = {
summaryLength: 1, summaryLength: 1,
threshold: 3, threshold: 3,
@ -62,30 +90,28 @@ export default class MemberEventListSummary extends React.Component {
/** /**
* Generate the text for users aggregated by their transition sequences (`eventAggregates`) where * Generate the text for users aggregated by their transition sequences (`eventAggregates`) where
* the sequences are ordered by `orderedTransitionSequences`. * the sequences are ordered by `orderedTransitionSequences`.
* @param {object[]} eventAggregates a map of transition sequence to array of user display names * @param {object} eventAggregates a map of transition sequence to array of user display names
* or user IDs. * or user IDs.
* @param {string[]} orderedTransitionSequences an array which is some ordering of * @param {string[]} orderedTransitionSequences an array which is some ordering of
* `Object.keys(eventAggregates)`. * `Object.keys(eventAggregates)`.
* @returns {string} the textual summary of the aggregated events that occurred. * @returns {string} the textual summary of the aggregated events that occurred.
*/ */
_generateSummary(eventAggregates, orderedTransitionSequences) { private generateSummary(eventAggregates: Record<string, string[]>, orderedTransitionSequences: string[]) {
const summaries = orderedTransitionSequences.map((transitions) => { const summaries = orderedTransitionSequences.map((transitions) => {
const userNames = eventAggregates[transitions]; const userNames = eventAggregates[transitions];
const nameList = this._renderNameList(userNames); const nameList = this.renderNameList(userNames);
const splitTransitions = transitions.split(','); const splitTransitions = transitions.split(SEP) as TransitionType[];
// Some neighbouring transitions are common, so canonicalise some into "pair" // Some neighbouring transitions are common, so canonicalise some into "pair"
// transitions // transitions
const canonicalTransitions = this._getCanonicalTransitions(splitTransitions); const canonicalTransitions = MemberEventListSummary.getCanonicalTransitions(splitTransitions);
// Transform into consecutive repetitions of the same transition (like 5 // Transform into consecutive repetitions of the same transition (like 5
// consecutive 'joined_and_left's) // consecutive 'joined_and_left's)
const coalescedTransitions = this._coalesceRepeatedTransitions( const coalescedTransitions = MemberEventListSummary.coalesceRepeatedTransitions(canonicalTransitions);
canonicalTransitions,
);
const descs = coalescedTransitions.map((t) => { const descs = coalescedTransitions.map((t) => {
return this._getDescriptionForTransition( return MemberEventListSummary.getDescriptionForTransition(
t.transitionType, userNames.length, t.repeats, t.transitionType, userNames.length, t.repeats,
); );
}); });
@ -108,7 +134,7 @@ export default class MemberEventListSummary extends React.Component {
* more items in `users` than `this.props.summaryLength`, which is the number of names * more items in `users` than `this.props.summaryLength`, which is the number of names
* included before "and [n] others". * included before "and [n] others".
*/ */
_renderNameList(users) { private renderNameList(users: string[]) {
return formatCommaSeparatedList(users, this.props.summaryLength); return formatCommaSeparatedList(users, this.props.summaryLength);
} }
@ -119,22 +145,22 @@ export default class MemberEventListSummary extends React.Component {
* @param {string[]} transitions an array of transitions. * @param {string[]} transitions an array of transitions.
* @returns {string[]} an array of transitions. * @returns {string[]} an array of transitions.
*/ */
_getCanonicalTransitions(transitions) { private static getCanonicalTransitions(transitions: TransitionType[]): TransitionType[] {
const modMap = { const modMap = {
'joined': { [TransitionType.Joined]: {
'after': 'left', after: TransitionType.Left,
'newTransition': 'joined_and_left', newTransition: TransitionType.JoinedAndLeft,
}, },
'left': { [TransitionType.Left]: {
'after': 'joined', after: TransitionType.Joined,
'newTransition': 'left_and_joined', newTransition: TransitionType.LeftAndJoined,
}, },
// $currentTransition : { // $currentTransition : {
// 'after' : $nextTransition, // 'after' : $nextTransition,
// 'newTransition' : 'new_transition_type', // 'newTransition' : 'new_transition_type',
// }, // },
}; };
const res = []; const res: TransitionType[] = [];
for (let i = 0; i < transitions.length; i++) { for (let i = 0; i < transitions.length; i++) {
const t = transitions[i]; const t = transitions[i];
@ -166,8 +192,12 @@ export default class MemberEventListSummary extends React.Component {
* @param {string[]} transitions the array of transitions to transform. * @param {string[]} transitions the array of transitions to transform.
* @returns {object[]} an array of coalesced transitions. * @returns {object[]} an array of coalesced transitions.
*/ */
_coalesceRepeatedTransitions(transitions) { private static coalesceRepeatedTransitions(transitions: TransitionType[]) {
const res = []; const res: {
transitionType: TransitionType;
repeats: number;
}[] = [];
for (let i = 0; i < transitions.length; i++) { for (let i = 0; i < transitions.length; i++) {
if (res.length > 0 && res[res.length - 1].transitionType === transitions[i]) { if (res.length > 0 && res[res.length - 1].transitionType === transitions[i]) {
res[res.length - 1].repeats += 1; res[res.length - 1].repeats += 1;
@ -189,7 +219,7 @@ export default class MemberEventListSummary extends React.Component {
* @param {number} repeats the number of times the transition was repeated in a row. * @param {number} repeats the number of times the transition was repeated in a row.
* @returns {string} the written Human Readable equivalent of the transition. * @returns {string} the written Human Readable equivalent of the transition.
*/ */
_getDescriptionForTransition(t, userCount, repeats) { private static getDescriptionForTransition(t: TransitionType, userCount: number, repeats: number) {
// The empty interpolations 'severalUsers' and 'oneUser' // The empty interpolations 'severalUsers' and 'oneUser'
// are there only to show translators to non-English languages // are there only to show translators to non-English languages
// that the verb is conjugated to plural or singular Subject. // that the verb is conjugated to plural or singular Subject.
@ -217,12 +247,18 @@ export default class MemberEventListSummary extends React.Component {
break; break;
case "invite_reject": case "invite_reject":
res = (userCount > 1) res = (userCount > 1)
? _t("%(severalUsers)srejected their invitations %(count)s times", { severalUsers: "", count: repeats }) ? _t("%(severalUsers)srejected their invitations %(count)s times", {
severalUsers: "",
count: repeats,
})
: _t("%(oneUser)srejected their invitation %(count)s times", { oneUser: "", count: repeats }); : _t("%(oneUser)srejected their invitation %(count)s times", { oneUser: "", count: repeats });
break; break;
case "invite_withdrawal": case "invite_withdrawal":
res = (userCount > 1) res = (userCount > 1)
? _t("%(severalUsers)shad their invitations withdrawn %(count)s times", { severalUsers: "", count: repeats }) ? _t("%(severalUsers)shad their invitations withdrawn %(count)s times", {
severalUsers: "",
count: repeats,
})
: _t("%(oneUser)shad their invitation withdrawn %(count)s times", { oneUser: "", count: repeats }); : _t("%(oneUser)shad their invitation withdrawn %(count)s times", { oneUser: "", count: repeats });
break; break;
case "invited": case "invited":
@ -265,8 +301,8 @@ export default class MemberEventListSummary extends React.Component {
return res; return res;
} }
_getTransitionSequence(events) { private static getTransitionSequence(events: MatrixEvent[]) {
return events.map(this._getTransition); return events.map(MemberEventListSummary.getTransition);
} }
/** /**
@ -277,60 +313,60 @@ export default class MemberEventListSummary extends React.Component {
* @returns {string?} the transition type given to this event. This defaults to `null` * @returns {string?} the transition type given to this event. This defaults to `null`
* if a transition is not recognised. * if a transition is not recognised.
*/ */
_getTransition(e) { private static getTransition(e: MatrixEvent): TransitionType {
if (e.mxEvent.getType() === 'm.room.third_party_invite') { if (e.mxEvent.getType() === 'm.room.third_party_invite') {
// Handle 3pid invites the same as invites so they get bundled together // Handle 3pid invites the same as invites so they get bundled together
if (!isValid3pidInvite(e.mxEvent)) { if (!isValid3pidInvite(e.mxEvent)) {
return 'invite_withdrawal'; return TransitionType.InviteWithdrawal;
} }
return 'invited'; return TransitionType.Invited;
} }
switch (e.mxEvent.getContent().membership) { switch (e.mxEvent.getContent().membership) {
case 'invite': return 'invited'; case 'invite': return TransitionType.Invited;
case 'ban': return 'banned'; case 'ban': return TransitionType.Banned;
case 'join': case 'join':
if (e.mxEvent.getPrevContent().membership === 'join') { if (e.mxEvent.getPrevContent().membership === 'join') {
if (e.mxEvent.getContent().displayname !== if (e.mxEvent.getContent().displayname !==
e.mxEvent.getPrevContent().displayname) { e.mxEvent.getPrevContent().displayname) {
return 'changed_name'; return TransitionType.ChangedName;
} else if (e.mxEvent.getContent().avatar_url !== } else if (e.mxEvent.getContent().avatar_url !==
e.mxEvent.getPrevContent().avatar_url) { e.mxEvent.getPrevContent().avatar_url) {
return 'changed_avatar'; return TransitionType.ChangedAvatar;
} }
// console.log("MELS ignoring duplicate membership join event"); // console.log("MELS ignoring duplicate membership join event");
return 'no_change'; return TransitionType.NoChange;
} else { } else {
return 'joined'; return TransitionType.Joined;
} }
case 'leave': case 'leave':
if (e.mxEvent.getSender() === e.mxEvent.getStateKey()) { if (e.mxEvent.getSender() === e.mxEvent.getStateKey()) {
switch (e.mxEvent.getPrevContent().membership) { switch (e.mxEvent.getPrevContent().membership) {
case 'invite': return 'invite_reject'; case 'invite': return TransitionType.InviteReject;
default: return 'left'; default: return TransitionType.Left;
} }
} }
switch (e.mxEvent.getPrevContent().membership) { switch (e.mxEvent.getPrevContent().membership) {
case 'invite': return 'invite_withdrawal'; case 'invite': return TransitionType.InviteWithdrawal;
case 'ban': return 'unbanned'; case 'ban': return TransitionType.Unbanned;
// sender is not target and made the target leave, if not from invite/ban then this is a kick // sender is not target and made the target leave, if not from invite/ban then this is a kick
default: return 'kicked'; default: return TransitionType.Kicked;
} }
default: return null; default: return null;
} }
} }
_getAggregate(userEvents) { getAggregate(userEvents: Record<string, IUserEvents[]>) {
// A map of aggregate type to arrays of display names. Each aggregate type // A map of aggregate type to arrays of display names. Each aggregate type
// is a comma-delimited string of transitions, e.g. "joined,left,kicked". // is a comma-delimited string of transitions, e.g. "joined,left,kicked".
// The array of display names is the array of users who went through that // The array of display names is the array of users who went through that
// sequence during eventsToRender. // sequence during eventsToRender.
const aggregate = { const aggregate: Record<string, string[]> = {
// $aggregateType : []:string // $aggregateType : []:string
}; };
// A map of aggregate types to the indices that order them (the index of // A map of aggregate types to the indices that order them (the index of
// the first event for a given transition sequence) // the first event for a given transition sequence)
const aggregateIndices = { const aggregateIndices: Record<string, number> = {
// $aggregateType : int // $aggregateType : int
}; };
@ -340,7 +376,7 @@ export default class MemberEventListSummary extends React.Component {
const firstEvent = userEvents[userId][0]; const firstEvent = userEvents[userId][0];
const displayName = firstEvent.displayName; const displayName = firstEvent.displayName;
const seq = this._getTransitionSequence(userEvents[userId]); const seq = MemberEventListSummary.getTransitionSequence(userEvents[userId]).join(SEP);
if (!aggregate[seq]) { if (!aggregate[seq]) {
aggregate[seq] = []; aggregate[seq] = [];
aggregateIndices[seq] = -1; aggregateIndices[seq] = -1;
@ -349,8 +385,9 @@ export default class MemberEventListSummary extends React.Component {
aggregate[seq].push(displayName); aggregate[seq].push(displayName);
if (aggregateIndices[seq] === -1 || if (aggregateIndices[seq] === -1 ||
firstEvent.index < aggregateIndices[seq]) { firstEvent.index < aggregateIndices[seq]
aggregateIndices[seq] = firstEvent.index; ) {
aggregateIndices[seq] = firstEvent.index;
} }
}, },
); );
@ -364,19 +401,10 @@ export default class MemberEventListSummary extends React.Component {
render() { render() {
const eventsToRender = this.props.events; const eventsToRender = this.props.events;
// Map user IDs to an array of objects: // Object mapping user IDs to an array of IUserEvents:
const userEvents = { const userEvents: Record<string, IUserEvents[]> = {};
// $userId : [{
// // The original event
// mxEvent: e,
// // The display name of the user (if not, then user ID)
// displayName: e.target.name || userId,
// // The original index of the event in this.props.events
// index: index,
// }]
};
const avatarMembers = []; const avatarMembers: RoomMember[] = [];
eventsToRender.forEach((e, index) => { eventsToRender.forEach((e, index) => {
const userId = e.getStateKey(); const userId = e.getStateKey();
// Initialise a user's events // Initialise a user's events
@ -399,14 +427,13 @@ export default class MemberEventListSummary extends React.Component {
}); });
}); });
const aggregate = this._getAggregate(userEvents); const aggregate = this.getAggregate(userEvents);
// Sort types by order of lowest event index within sequence // Sort types by order of lowest event index within sequence
const orderedTransitionSequences = Object.keys(aggregate.names).sort( const orderedTransitionSequences = Object.keys(aggregate.names).sort(
(seq1, seq2) => aggregate.indices[seq1] > aggregate.indices[seq2], (seq1, seq2) => aggregate.indices[seq2] - aggregate.indices[seq1],
); );
const EventListSummary = sdk.getComponent("views.elements.EventListSummary");
return <EventListSummary return <EventListSummary
events={this.props.events} events={this.props.events}
threshold={this.props.threshold} threshold={this.props.threshold}
@ -414,6 +441,6 @@ export default class MemberEventListSummary extends React.Component {
startExpanded={this.props.startExpanded} startExpanded={this.props.startExpanded}
children={this.props.children} children={this.props.children}
summaryMembers={avatarMembers} summaryMembers={avatarMembers}
summaryText={this._generateSummary(aggregate.names, orderedTransitionSequences)} />; summaryText={this.generateSummary(aggregate.names, orderedTransitionSequences)} />;
} }
} }

View File

@ -14,11 +14,11 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import {useState} from "react"; import {Dispatch, SetStateAction, useState} from "react";
// Hook to simplify toggling of a boolean state value // Hook to simplify toggling of a boolean state value
// Returns value, method to toggle boolean value and method to set the boolean value // Returns value, method to toggle boolean value and method to set the boolean value
export const useStateToggle = (initialValue: boolean) => { export const useStateToggle = (initialValue: boolean): [boolean, () => void, Dispatch<SetStateAction<boolean>>] => {
const [value, setValue] = useState(initialValue); const [value, setValue] = useState(initialValue);
const toggleValue = () => { const toggleValue = () => {
setValue(!value); setValue(!value);