From 5baaa6b77e7db8318b802ebc169d994a82b7e97d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 14 Sep 2021 18:53:31 +0200 Subject: [PATCH 1/4] Convert MemberStatusMessageAvatar to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- ...vatar.js => MemberStatusMessageAvatar.tsx} | 59 ++++++++++--------- .../views/rooms/MessageComposer.tsx | 2 +- 2 files changed, 33 insertions(+), 28 deletions(-) rename src/components/views/avatars/{MemberStatusMessageAvatar.js => MemberStatusMessageAvatar.tsx} (76%) diff --git a/src/components/views/avatars/MemberStatusMessageAvatar.js b/src/components/views/avatars/MemberStatusMessageAvatar.tsx similarity index 76% rename from src/components/views/avatars/MemberStatusMessageAvatar.js rename to src/components/views/avatars/MemberStatusMessageAvatar.tsx index 82b7b8e400..f7965a635e 100644 --- a/src/components/views/avatars/MemberStatusMessageAvatar.js +++ b/src/components/views/avatars/MemberStatusMessageAvatar.tsx @@ -15,43 +15,48 @@ limitations under the License. */ import React, { createRef } from 'react'; -import PropTypes from 'prop-types'; import { MatrixClientPeg } from '../../../MatrixClientPeg'; import { _t } from "../../../languageHandler"; import MemberAvatar from '../avatars/MemberAvatar'; import classNames from 'classnames'; import StatusMessageContextMenu from "../context_menus/StatusMessageContextMenu"; import SettingsStore from "../../../settings/SettingsStore"; -import { ContextMenu, ContextMenuButton } from "../../structures/ContextMenu"; +import { ChevronFace, ContextMenu, ContextMenuButton } from "../../structures/ContextMenu"; import { replaceableComponent } from "../../../utils/replaceableComponent"; +import { RoomMember } from "matrix-js-sdk/src/models/room-member"; +import { ResizeMethod } from "matrix-js-sdk/src/@types/partials"; + +interface IProps { + member: RoomMember; + width?: number; + height?: number; + resizeMethod?: ResizeMethod; +} + +interface IState { + hasStatus: boolean; + menuDisplayed: boolean; +} @replaceableComponent("views.avatars.MemberStatusMessageAvatar") -export default class MemberStatusMessageAvatar extends React.Component { - static propTypes = { - member: PropTypes.object.isRequired, - width: PropTypes.number, - height: PropTypes.number, - resizeMethod: PropTypes.string, - }; - - static defaultProps = { +export default class MemberStatusMessageAvatar extends React.Component { + public static defaultProps: Partial = { width: 40, height: 40, resizeMethod: 'crop', }; + private button = createRef(); - constructor(props) { + constructor(props: IProps) { super(props); this.state = { hasStatus: this.hasStatus, menuDisplayed: false, }; - - this._button = createRef(); } - componentDidMount() { + public componentDidMount(): void { if (this.props.member.userId !== MatrixClientPeg.get().getUserId()) { throw new Error("Cannot use MemberStatusMessageAvatar on anyone but the logged in user"); } @@ -62,44 +67,44 @@ export default class MemberStatusMessageAvatar extends React.Component { if (!user) { return; } - user.on("User._unstable_statusMessage", this._onStatusMessageCommitted); + user.on("User._unstable_statusMessage", this.onStatusMessageCommitted); } - componentWillUnmount() { + public componentWillUnmount(): void { const { user } = this.props.member; if (!user) { return; } user.removeListener( "User._unstable_statusMessage", - this._onStatusMessageCommitted, + this.onStatusMessageCommitted, ); } - get hasStatus() { + private get hasStatus(): boolean { const { user } = this.props.member; if (!user) { return false; } - return !!user._unstable_statusMessage; + return !!user.unstable_statusMessage; } - _onStatusMessageCommitted = () => { + private onStatusMessageCommitted = (): void => { // The `User` object has observed a status message change. this.setState({ hasStatus: this.hasStatus, }); }; - openMenu = () => { + private openMenu = (): void => { this.setState({ menuDisplayed: true }); }; - closeMenu = () => { + private closeMenu = (): void => { this.setState({ menuDisplayed: false }); }; - render() { + public render(): JSX.Element { const avatar = Date: Tue, 14 Sep 2021 18:58:20 +0200 Subject: [PATCH 2/4] Convert GenericElementContextMenu to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- ...tMenu.js => GenericElementContextMenu.tsx} | 36 +++++++++---------- 1 file changed, 16 insertions(+), 20 deletions(-) rename src/components/views/context_menus/{GenericElementContextMenu.js => GenericElementContextMenu.tsx} (67%) diff --git a/src/components/views/context_menus/GenericElementContextMenu.js b/src/components/views/context_menus/GenericElementContextMenu.tsx similarity index 67% rename from src/components/views/context_menus/GenericElementContextMenu.js rename to src/components/views/context_menus/GenericElementContextMenu.tsx index 87d44ef0d3..a0a8c89b37 100644 --- a/src/components/views/context_menus/GenericElementContextMenu.js +++ b/src/components/views/context_menus/GenericElementContextMenu.tsx @@ -15,45 +15,41 @@ limitations under the License. */ import React from 'react'; -import PropTypes from 'prop-types'; import { replaceableComponent } from "../../../utils/replaceableComponent"; -/* +interface IProps { + element: React.ReactNode; + // Function to be called when the parent window is resized + // This can be used to reposition or close the menu on resize and + // ensure that it is not displayed in a stale position. + onResize?: () => void; +} + +/** * This component can be used to display generic HTML content in a contextual * menu. */ - @replaceableComponent("views.context_menus.GenericElementContextMenu") -export default class GenericElementContextMenu extends React.Component { - static propTypes = { - element: PropTypes.element.isRequired, - // Function to be called when the parent window is resized - // This can be used to reposition or close the menu on resize and - // ensure that it is not displayed in a stale position. - onResize: PropTypes.func, - }; - - constructor(props) { +export default class GenericElementContextMenu extends React.Component { + constructor(props: IProps) { super(props); - this.resize = this.resize.bind(this); } - componentDidMount() { - this.resize = this.resize.bind(this); + public componentDidMount(): void { window.addEventListener("resize", this.resize); } - componentWillUnmount() { + public componentWillUnmount(): void { window.removeEventListener("resize", this.resize); } - resize() { + private resize = (): void => { if (this.props.onResize) { this.props.onResize(); } - } + }; - render() { + public render(): JSX.Element { return
{ this.props.element }
; } } From 11e61075b4a8407b309c2c48439dc64954d7c6e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 14 Sep 2021 18:59:54 +0200 Subject: [PATCH 3/4] Convert GenericTextContextMenu to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- ...extContextMenu.js => GenericTextContextMenu.tsx} | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) rename src/components/views/context_menus/{GenericTextContextMenu.js => GenericTextContextMenu.tsx} (86%) diff --git a/src/components/views/context_menus/GenericTextContextMenu.js b/src/components/views/context_menus/GenericTextContextMenu.tsx similarity index 86% rename from src/components/views/context_menus/GenericTextContextMenu.js rename to src/components/views/context_menus/GenericTextContextMenu.tsx index 474732e88b..3ca158dd02 100644 --- a/src/components/views/context_menus/GenericTextContextMenu.js +++ b/src/components/views/context_menus/GenericTextContextMenu.tsx @@ -15,16 +15,15 @@ limitations under the License. */ import React from 'react'; -import PropTypes from 'prop-types'; import { replaceableComponent } from "../../../utils/replaceableComponent"; -@replaceableComponent("views.context_menus.GenericTextContextMenu") -export default class GenericTextContextMenu extends React.Component { - static propTypes = { - message: PropTypes.string.isRequired, - }; +interface IProps { + message: string; +} - render() { +@replaceableComponent("views.context_menus.GenericTextContextMenu") +export default class GenericTextContextMenu extends React.Component { + public render(): JSX.Element { return
{ this.props.message }
; } } From 0f55fde03a9d8c8764bf97106b55bb955e70e795 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Brandner?= Date: Tue, 14 Sep 2021 19:09:24 +0200 Subject: [PATCH 4/4] Convert StatusMessageContextMenu to TS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Šimon Brandner --- .../avatars/MemberStatusMessageAvatar.tsx | 2 +- ...xtMenu.js => StatusMessageContextMenu.tsx} | 68 ++++++++++--------- 2 files changed, 37 insertions(+), 33 deletions(-) rename src/components/views/context_menus/{StatusMessageContextMenu.js => StatusMessageContextMenu.tsx} (71%) diff --git a/src/components/views/avatars/MemberStatusMessageAvatar.tsx b/src/components/views/avatars/MemberStatusMessageAvatar.tsx index f7965a635e..8c703b3b32 100644 --- a/src/components/views/avatars/MemberStatusMessageAvatar.tsx +++ b/src/components/views/avatars/MemberStatusMessageAvatar.tsx @@ -137,7 +137,7 @@ export default class MemberStatusMessageAvatar extends React.Component - + ); } diff --git a/src/components/views/context_menus/StatusMessageContextMenu.js b/src/components/views/context_menus/StatusMessageContextMenu.tsx similarity index 71% rename from src/components/views/context_menus/StatusMessageContextMenu.js rename to src/components/views/context_menus/StatusMessageContextMenu.tsx index e05b05116c..954dc3f5c0 100644 --- a/src/components/views/context_menus/StatusMessageContextMenu.js +++ b/src/components/views/context_menus/StatusMessageContextMenu.tsx @@ -14,53 +14,59 @@ See the License for the specific language governing permissions and limitations under the License. */ -import React from 'react'; -import PropTypes from 'prop-types'; +import React, { ChangeEvent } from 'react'; import { _t } from '../../../languageHandler'; import { MatrixClientPeg } from '../../../MatrixClientPeg'; -import * as sdk from '../../../index'; -import AccessibleButton from '../elements/AccessibleButton'; +import AccessibleButton, { ButtonEvent } from '../elements/AccessibleButton'; import { replaceableComponent } from "../../../utils/replaceableComponent"; +import { User } from "matrix-js-sdk/src/models/user"; +import Spinner from "../elements/Spinner"; + +interface IProps { + // js-sdk User object. Not required because it might not exist. + user?: User; +} + +interface IState { + message: string; + waiting: boolean; +} @replaceableComponent("views.context_menus.StatusMessageContextMenu") -export default class StatusMessageContextMenu extends React.Component { - static propTypes = { - // js-sdk User object. Not required because it might not exist. - user: PropTypes.object, - }; - - constructor(props) { +export default class StatusMessageContextMenu extends React.Component { + constructor(props: IProps) { super(props); this.state = { message: this.comittedStatusMessage, + waiting: false, }; } - componentDidMount() { + public componentDidMount(): void { const { user } = this.props; if (!user) { return; } - user.on("User._unstable_statusMessage", this._onStatusMessageCommitted); + user.on("User._unstable_statusMessage", this.onStatusMessageCommitted); } - componentWillUnmount() { + public componentWillUnmount(): void { const { user } = this.props; if (!user) { return; } user.removeListener( "User._unstable_statusMessage", - this._onStatusMessageCommitted, + this.onStatusMessageCommitted, ); } - get comittedStatusMessage() { - return this.props.user ? this.props.user._unstable_statusMessage : ""; + get comittedStatusMessage(): string { + return this.props.user ? this.props.user.unstable_statusMessage : ""; } - _onStatusMessageCommitted = () => { + private onStatusMessageCommitted = (): void => { // The `User` object has observed a status message change. this.setState({ message: this.comittedStatusMessage, @@ -68,14 +74,14 @@ export default class StatusMessageContextMenu extends React.Component { }); }; - _onClearClick = (e) => { + private onClearClick = (): void=> { MatrixClientPeg.get()._unstable_setStatusMessage(""); this.setState({ waiting: true, }); }; - _onSubmit = (e) => { + private onSubmit = (e: ButtonEvent): void => { e.preventDefault(); MatrixClientPeg.get()._unstable_setStatusMessage(this.state.message); this.setState({ @@ -83,27 +89,25 @@ export default class StatusMessageContextMenu extends React.Component { }); }; - _onStatusChange = (e) => { + private onStatusChange = (e: ChangeEvent): void => { // The input field's value was changed. this.setState({ - message: e.target.value, + message: (e.target as HTMLInputElement).value, }); }; - render() { - const Spinner = sdk.getComponent('views.elements.Spinner'); - + public render(): JSX.Element { let actionButton; if (this.comittedStatusMessage) { if (this.state.message === this.comittedStatusMessage) { actionButton = { _t("Clear status") } ; } else { actionButton = { _t("Update status") } ; @@ -112,7 +116,7 @@ export default class StatusMessageContextMenu extends React.Component { actionButton = { _t("Set status") } ; @@ -120,13 +124,13 @@ export default class StatusMessageContextMenu extends React.Component { let spinner = null; if (this.state.waiting) { - spinner = ; + spinner = ; } const form =
{ actionButton }