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.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 styled from '@emotion/styled';
  21. import { differenceInDays } from 'date-fns';
  22. import { Highlight, Note, themeBorder, themeColor } from 'design-system';
  23. import * as React from 'react';
  24. import { WrappedComponentProps, injectIntl } from 'react-intl';
  25. import Tooltip from '../../../components/controls/Tooltip';
  26. import DateFormatter, { longFormatterOption } from '../../../components/intl/DateFormatter';
  27. import DateFromNow from '../../../components/intl/DateFromNow';
  28. import DateTimeFormatter, { formatterOption } from '../../../components/intl/DateTimeFormatter';
  29. import { translate, translateWithParameters } from '../../../helpers/l10n';
  30. import { getNewCodePeriodDate, getNewCodePeriodLabel } from '../../../helpers/new-code-period';
  31. import { ComponentQualifier } from '../../../types/component';
  32. import { NewCodeDefinitionType } from '../../../types/new-code-definition';
  33. import { ComponentMeasure, Period } from '../../../types/types';
  34. export interface LeakPeriodLegendProps {
  35. component: ComponentMeasure;
  36. period: Period;
  37. }
  38. class LeakPeriodLegend extends React.PureComponent<LeakPeriodLegendProps & WrappedComponentProps> {
  39. formatDate = (date: string) => {
  40. return this.props.intl.formatDate(date, longFormatterOption);
  41. };
  42. formatDateTime = (date: string) => {
  43. return this.props.intl.formatTime(date, formatterOption);
  44. };
  45. render() {
  46. const { component, period } = this.props;
  47. if (component.qualifier === ComponentQualifier.Application) {
  48. return (
  49. <LeakPeriodLabel className="sw-px-2 sw-py-1 sw-rounded-1">
  50. {translate('issues.new_code_period')}
  51. </LeakPeriodLabel>
  52. );
  53. }
  54. const leakPeriodLabel = getNewCodePeriodLabel(
  55. period,
  56. period.mode === 'manual_baseline' ? this.formatDateTime : this.formatDate,
  57. );
  58. const label = (
  59. <LeakPeriodLabel className="sw-px-2 sw-py-1 sw-rounded-1">
  60. <Highlight>{translateWithParameters('component_measures.leak_legend.new_code')}</Highlight>{' '}
  61. {leakPeriodLabel}
  62. </LeakPeriodLabel>
  63. );
  64. if (period.mode === 'days' || period.mode === NewCodeDefinitionType.NumberOfDays) {
  65. return label;
  66. }
  67. const date = getNewCodePeriodDate(period);
  68. const tooltip = date && (
  69. <div>
  70. <DateFromNow date={date} />
  71. {', '}
  72. {differenceInDays(new Date(), date) < 1 ? (
  73. <DateTimeFormatter date={date} />
  74. ) : (
  75. <DateFormatter date={date} long />
  76. )}
  77. </div>
  78. );
  79. return <Tooltip overlay={tooltip}>{label}</Tooltip>;
  80. }
  81. }
  82. export default injectIntl(LeakPeriodLegend);
  83. const LeakPeriodLabel = styled(Note)`
  84. background-color: ${themeColor('newCodeLegend')};
  85. border: ${themeBorder('default', 'newCodeLegendBorder')};
  86. `;