Convert PresenceLabel to TS

Signed-off-by: Šimon Brandner <simon.bra.ag@gmail.com>
This commit is contained in:
Šimon Brandner 2021-06-23 19:18:36 +02:00
parent 87d27593de
commit e25940cbdd
No known key found for this signature in database
GPG key ID: 9760693FDD98A790

View file

@ -15,26 +15,23 @@ limitations under the License.
*/ */
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types';
import { _t } from '../../../languageHandler'; import { _t } from '../../../languageHandler';
import {replaceableComponent} from "../../../utils/replaceableComponent"; import {replaceableComponent} from "../../../utils/replaceableComponent";
@replaceableComponent("views.rooms.PresenceLabel") interface IProps {
export default class PresenceLabel extends React.Component {
static propTypes = {
// number of milliseconds ago this user was last active. // number of milliseconds ago this user was last active.
// zero = unknown // zero = unknown
activeAgo: PropTypes.number, activeAgo?: number,
// if true, activeAgo is an approximation and "Now" should // if true, activeAgo is an approximation and "Now" should
// be shown instead // be shown instead
currentlyActive: PropTypes.bool, currentlyActive?: boolean,
// offline, online, etc // offline, online, etc
presenceState: PropTypes.string, presenceState?: string,
}; }
@replaceableComponent("views.rooms.PresenceLabel")
export default class PresenceLabel extends React.Component<IProps> {
static defaultProps = { static defaultProps = {
activeAgo: -1, activeAgo: -1,
presenceState: null, presenceState: null,
@ -42,13 +39,13 @@ export default class PresenceLabel extends React.Component {
// Return duration as a string using appropriate time units // Return duration as a string using appropriate time units
// XXX: This would be better handled using a culture-aware library, but we don't use one yet. // XXX: This would be better handled using a culture-aware library, but we don't use one yet.
getDuration(time) { private getDuration(time: number): string {
if (!time) return; if (!time) return;
const t = parseInt(time / 1000); const t = time / 1000;
const s = t % 60; const s = t % 60;
const m = parseInt(t / 60) % 60; const m = t / 60 % 60;
const h = parseInt(t / (60 * 60)) % 24; const h = t / (60 * 60) % 24;
const d = parseInt(t / (60 * 60 * 24)); const d = t / (60 * 60 * 24);
if (t < 60) { if (t < 60) {
if (t < 0) { if (t < 0) {
return _t("%(duration)ss", { duration: 0 }); return _t("%(duration)ss", { duration: 0 });
@ -64,7 +61,7 @@ export default class PresenceLabel extends React.Component {
return _t("%(duration)sd", { duration: d }); return _t("%(duration)sd", { duration: d });
} }
getPrettyPresence(presence, activeAgo, currentlyActive) { private getPrettyPresence(presence: string, activeAgo: number, currentlyActive: boolean): string {
if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) { if (!currentlyActive && activeAgo !== undefined && activeAgo > 0) {
const duration = this.getDuration(activeAgo); const duration = this.getDuration(activeAgo);
if (presence === "online") return _t("Online for %(duration)s", { duration: duration }); if (presence === "online") return _t("Online for %(duration)s", { duration: duration });