You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LeakPeriodLegend.tsx 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as React from 'react';
  21. import * as classNames from 'classnames';
  22. import { injectIntl, InjectedIntlProps } from 'react-intl';
  23. import DateFromNow from '../../../components/intl/DateFromNow';
  24. import DateFormatter, { longFormatterOption } from '../../../components/intl/DateFormatter';
  25. import DateTimeFormatter from '../../../components/intl/DateTimeFormatter';
  26. import Tooltip from '../../../components/controls/Tooltip';
  27. import { getPeriodLabel, getPeriodDate } from '../../../helpers/periods';
  28. import { translate, translateWithParameters } from '../../../helpers/l10n';
  29. import { differenceInDays } from '../../../helpers/dates';
  30. interface Props {
  31. className?: string;
  32. component: T.ComponentMeasure;
  33. period: T.Period;
  34. }
  35. export class LeakPeriodLegend extends React.PureComponent<Props & InjectedIntlProps> {
  36. formatDate = (date: string) => {
  37. return this.props.intl.formatDate(date, longFormatterOption);
  38. };
  39. render() {
  40. const { className, component, period } = this.props;
  41. const leakClass = classNames('domain-measures-header leak-box', className);
  42. if (component.qualifier === 'APP') {
  43. return <div className={leakClass}>{translate('issues.new_code_period')}</div>;
  44. }
  45. const leakPeriodLabel = getPeriodLabel(period, this.formatDate);
  46. if (!leakPeriodLabel) {
  47. return null;
  48. }
  49. const label = (
  50. <div className={leakClass}>
  51. {translateWithParameters('overview.new_code_period_x', leakPeriodLabel)}
  52. </div>
  53. );
  54. if (period.mode === 'days') {
  55. return label;
  56. }
  57. const date = getPeriodDate(period);
  58. const tooltip = date && (
  59. <div>
  60. <DateFromNow date={date} />
  61. {', '}
  62. {differenceInDays(new Date(), date) < 1 ? (
  63. <DateTimeFormatter date={date} />
  64. ) : (
  65. <DateFormatter date={date} long={true} />
  66. )}
  67. </div>
  68. );
  69. return <Tooltip overlay={tooltip}>{label}</Tooltip>;
  70. }
  71. }
  72. export default injectIntl(LeakPeriodLegend);