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 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 { injectIntl, InjectedIntlProps } from 'react-intl';
  22. import DateFromNow from '../../../components/intl/DateFromNow';
  23. import DateFormatter, { longFormatterOption } from '../../../components/intl/DateFormatter';
  24. import DateTimeFormatter from '../../../components/intl/DateTimeFormatter';
  25. import Tooltip from '../../../components/controls/Tooltip';
  26. import { getPeriodDate, getPeriodLabel } from '../../../helpers/periods';
  27. import { translateWithParameters } from '../../../helpers/l10n';
  28. import { differenceInDays } from '../../../helpers/dates';
  29. interface Props {
  30. period: T.Period;
  31. }
  32. export class LeakPeriodLegend extends React.PureComponent<Props & InjectedIntlProps> {
  33. formatDate = (date: string) => {
  34. return this.props.intl.formatDate(date, longFormatterOption);
  35. };
  36. render() {
  37. const { period } = this.props;
  38. const leakPeriodLabel = getPeriodLabel(period, this.formatDate);
  39. if (!leakPeriodLabel) {
  40. return null;
  41. }
  42. if (period.mode === 'days') {
  43. return (
  44. <div className="overview-legend overview-legend-spaced-line">
  45. {translateWithParameters('overview.new_code_period_x', leakPeriodLabel)}
  46. </div>
  47. );
  48. }
  49. const leakPeriodDate = getPeriodDate(period);
  50. if (!leakPeriodDate) {
  51. return null;
  52. }
  53. const formattedDateFunction = (formattedLeakPeriodDate: string) => (
  54. <span>
  55. {translateWithParameters(
  56. period.mode === 'previous_analysis'
  57. ? 'overview.previous_analysis_on_x'
  58. : 'overview.started_on_x',
  59. formattedLeakPeriodDate
  60. )}
  61. </span>
  62. );
  63. const tooltip =
  64. differenceInDays(new Date(), leakPeriodDate) < 1 ? (
  65. <DateTimeFormatter date={leakPeriodDate}>{formattedDateFunction}</DateTimeFormatter>
  66. ) : (
  67. <DateFormatter date={leakPeriodDate} long={true}>
  68. {formattedDateFunction}
  69. </DateFormatter>
  70. );
  71. return (
  72. <Tooltip overlay={tooltip}>
  73. <div className="overview-legend">
  74. {translateWithParameters('overview.new_code_period_x', leakPeriodLabel)}
  75. <br />
  76. <DateFromNow date={leakPeriodDate}>
  77. {fromNow => (
  78. <span className="note">
  79. {translateWithParameters(
  80. period.mode === 'previous_analysis'
  81. ? 'overview.previous_analysis_x'
  82. : 'overview.started_x',
  83. fromNow
  84. )}
  85. </span>
  86. )}
  87. </DateFromNow>
  88. </div>
  89. </Tooltip>
  90. );
  91. }
  92. }
  93. export default injectIntl(LeakPeriodLegend);