Files
ThreadNet-Web/src/components/views/audio_messages/Clock.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

57 lines
1.8 KiB
TypeScript
Raw Normal View History

2021-03-25 17:12:26 -06:00
/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2021-2023 The Matrix.org Foundation C.I.C.
2021-03-25 17:12:26 -06:00
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
2021-03-25 17:12:26 -06:00
*/
import React, { HTMLProps } from "react";
import { Temporal } from "temporal-polyfill";
2021-10-22 17:23:32 -05:00
2021-08-27 16:21:29 +02:00
import { formatSeconds } from "../../../DateUtils";
2021-03-25 17:12:26 -06:00
interface Props extends Pick<HTMLProps<HTMLSpanElement>, "aria-live" | "role"> {
2021-03-25 17:12:26 -06:00
seconds: number;
formatFn: (seconds: number) => string;
2021-03-25 17:12:26 -06:00
}
/**
* Clock which represents time periods rather than absolute time.
* Simply converts seconds using formatFn.
* Defaulting to formatSeconds().
* Note that in this case hours will not be displayed, making it possible to see "82:29".
2021-03-25 17:12:26 -06:00
*/
export default class Clock extends React.Component<Props> {
public static defaultProps = {
formatFn: formatSeconds,
};
public shouldComponentUpdate(nextProps: Readonly<Props>): boolean {
2021-04-27 20:27:36 -06:00
const currentFloor = Math.floor(this.props.seconds);
const nextFloor = Math.floor(nextProps.seconds);
return currentFloor !== nextFloor;
}
private calculateDuration(seconds: number): string | undefined {
if (isNaN(seconds)) return undefined;
return new Temporal.Duration(0, 0, 0, 0, 0, 0, Math.round(seconds))
.round({ smallestUnit: "seconds", largestUnit: "hours" })
.toString();
}
public render(): React.ReactNode {
const { seconds, role } = this.props;
return (
<time
dateTime={this.calculateDuration(seconds)}
aria-live={this.props["aria-live"]}
role={role}
className="mx_Clock"
>
{this.props.formatFn(seconds)}
</time>
);
2021-03-25 17:12:26 -06:00
}
}