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
|
|
|
|
2024-09-09 14:57:16 +01:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only
|
|
|
|
|
Please see LICENSE files in the repository root for full details.
|
2021-03-25 17:12:26 -06:00
|
|
|
*/
|
|
|
|
|
|
2022-02-08 11:20:03 +00:00
|
|
|
import React, { HTMLProps } from "react";
|
2024-07-03 18:02:10 +01:00
|
|
|
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
|
|
|
|
2022-11-10 11:53:49 +01:00
|
|
|
interface Props extends Pick<HTMLProps<HTMLSpanElement>, "aria-live" | "role"> {
|
2021-03-25 17:12:26 -06:00
|
|
|
seconds: number;
|
2023-04-25 17:10:46 +01:00
|
|
|
formatFn: (seconds: number) => string;
|
2021-03-25 17:12:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-04-25 17:10:46 +01:00
|
|
|
* Clock which represents time periods rather than absolute time.
|
2022-11-10 11:53:49 +01:00
|
|
|
* 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
|
|
|
*/
|
2022-11-10 11:53:49 +01: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;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-03 18:02:10 +01:00
|
|
|
private calculateDuration(seconds: number): string | undefined {
|
|
|
|
|
if (isNaN(seconds)) return undefined;
|
|
|
|
|
return new Temporal.Duration(0, 0, 0, 0, 0, 0, Math.round(seconds))
|
2023-04-25 17:10:46 +01:00
|
|
|
.round({ smallestUnit: "seconds", largestUnit: "hours" })
|
|
|
|
|
.toString();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-13 17:01:43 +00:00
|
|
|
public render(): React.ReactNode {
|
2023-04-25 17:10:46 +01:00
|
|
|
const { seconds, role } = this.props;
|
2022-06-14 18:13:13 -06:00
|
|
|
return (
|
2023-04-25 17:10:46 +01:00
|
|
|
<time
|
|
|
|
|
dateTime={this.calculateDuration(seconds)}
|
|
|
|
|
aria-live={this.props["aria-live"]}
|
|
|
|
|
role={role}
|
|
|
|
|
className="mx_Clock"
|
|
|
|
|
>
|
|
|
|
|
{this.props.formatFn(seconds)}
|
|
|
|
|
</time>
|
2022-02-08 11:20:03 +00:00
|
|
|
);
|
2021-03-25 17:12:26 -06:00
|
|
|
}
|
|
|
|
|
}
|