azure-devops-ui
Version:
React components for building web UI in Azure DevOps
68 lines (67 loc) • 2.58 kB
JavaScript
import "../../../CommonImports";
import "../../../Core/core.css";
import * as React from "react";
import * as DateUtil from '../../../Utilities/Date';
import { Time } from "../Time/Time";
import * as Resources from '../../../Resources.Widgets';
import { format } from '../../../Core/Util/String';
export class Duration extends React.Component {
constructor(props) {
super(props);
this.getTimeString = () => {
return DateUtil.duration(this.props.startDate, this.props.endDate);
};
/**
* Returns time in milliseconds for next refresh.
*
* @return A number indicating time to refresh in milliseconds
*/
this.getNextInterval = () => {
return Duration.durationNextInterval(this.props.startDate, this.props.endDate);
};
this.state = {
tooltipProps: {}
};
}
static getDerivedStateFromProps(props) {
const tooltipProps = props.tooltipProps === undefined
? {
renderContent: function () {
return format(Resources.Started, DateUtil.tooltipString(props.startDate));
}
}
: props.tooltipProps;
return { tooltipProps: tooltipProps };
}
render() {
return (React.createElement(Time, { ariaHidden: this.props.ariaHidden, ariaLabel: this.props.ariaLabel ? this.props.ariaLabel : DateUtil.tooltipString(this.props.startDate), className: this.props.className, getNextInterval: this.getNextInterval, getTimeString: this.getTimeString, tabIndex: this.props.tabIndex, tooltipProps: this.state.tooltipProps }));
}
/**
* Returns time in milliseconds for next refresh.
*
* @return A number indicating time to refresh in milliseconds
*/
static durationNextInterval(startDate, endDate, now) {
if (!endDate) {
if (!now) {
endDate = new Date();
}
else {
endDate = now;
}
// Getting the difference in seconds between now and the specified date
const diff = endDate.getTime() - startDate.getTime();
let interval = DateUtil.minute;
if (diff < DateUtil.day) {
interval = DateUtil.second;
}
else {
interval = DateUtil.minute - (diff % DateUtil.minute);
}
return interval;
}
else {
return -1;
}
}
}