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.

68 lines
2.1 KiB
TypeScript
Raw Normal View History

2021-03-25 17:12:26 -06:00
/*
2023-03-21 10:08:44 +01:00
Copyright 2021 - 2023 The Matrix.org Foundation C.I.C.
2021-03-25 17:12:26 -06:00
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React, { HTMLProps } from "react";
import { Temporal } from "proposal-temporal";
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 constructor(props: Props) {
2021-03-25 17:12:26 -06:00
super(props);
}
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 {
return new Temporal.Duration(0, 0, 0, 0, 0, 0, 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
}
}