UNPKG

azure-devops-ui

Version:

React components for building web UI in Azure DevOps

79 lines (78 loc) 3.33 kB
import "../../../CommonImports"; import "../../../Core/core.css"; import * as React from "react"; import * as DateUtil from '../../../Utilities/Date'; import { Time } from "../Time/Time"; export class Ago extends React.Component { constructor(props) { super(props); this.getTimeString = () => { var _a; return DateUtil.ago(this.props.date, this.props.format, (_a = this.props.currentDate) !== null && _a !== void 0 ? _a : new Date(), this.props.locale); }; /** * Returns time in milliseconds for next refresh. * * @return A number indicating time to refresh in milliseconds */ this.getNextInterval = () => { return Ago.agoNextInterval(this.props.date, this.props.format); }; this.state = { tooltipProps: {} }; } static getDerivedStateFromProps(props) { const tooltipProps = props.tooltipProps === undefined ? { renderContent: function () { return DateUtil.tooltipString(props.date, undefined, props.tooltipTimeFormat); } } : props.tooltipProps; return { tooltipProps }; } render() { return (React.createElement(Time, { ariaLabel: this.props.ariaLabel ? this.props.ariaLabel : DateUtil.tooltipString(this.props.date), className: this.props.className, dateTime: this.props.date, 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 agoNextInterval(date, format, now) { if (!now) { now = new Date(); } if (!format) { format = DateUtil.AgoFormat.Compact; } // Getting the difference in seconds between now and the specified date const diff = now.getTime() - date.getTime(); let interval; if (diff < 2 * DateUtil.minute) { interval = 2 * DateUtil.minute - diff; } else if (diff < 59 * DateUtil.minute && format === DateUtil.AgoFormat.Compact) { interval = DateUtil.minute - (diff % DateUtil.minute); } else if (now.toDateString() === date.toDateString() && format === DateUtil.AgoFormat.Compact) { interval = DateUtil.hour - (diff % DateUtil.hour); } else if ((format === DateUtil.AgoFormat.Extended && now.toDateString() === date.toDateString()) || (diff < DateUtil.week && now.getDay() !== date.getDay())) { const tomorrow = new Date(now); tomorrow.setDate(now.getDate() + 1); tomorrow.setHours(0, 0, 0); interval = tomorrow.getTime() - now.getTime(); } else if (now.getFullYear() === date.getFullYear()) { const nextYear = new Date(now.getFullYear() + 1, 0, 1); interval = nextYear.getTime() - now.getTime(); } if (!interval || interval > DateUtil.week) { interval = -1; } return interval; } }