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.

GraphsLegendItem.tsx 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { useTheme } from '@emotion/react';
  21. import styled from '@emotion/styled';
  22. import classNames from 'classnames';
  23. import {
  24. Badge,
  25. CloseIcon,
  26. FlagWarningIcon,
  27. InteractiveIcon,
  28. Theme,
  29. themeBorder,
  30. themeColor,
  31. } from 'design-system';
  32. import * as React from 'react';
  33. import { FormattedMessage, useIntl } from 'react-intl';
  34. import { DEPRECATED_ACTIVITY_METRICS } from '../../helpers/constants';
  35. import { translateWithParameters } from '../../helpers/l10n';
  36. import { MetricKey } from '../../types/metrics';
  37. import Tooltip from '../controls/Tooltip';
  38. import { ChartLegend } from './ChartLegend';
  39. import { getDeprecatedTranslationKeyForTooltip } from './utils';
  40. interface Props {
  41. className?: string;
  42. index: number;
  43. metric: string;
  44. name: string;
  45. showWarning?: boolean;
  46. removeMetric?: (metric: string) => void;
  47. }
  48. export function GraphsLegendItem({
  49. className,
  50. index,
  51. metric,
  52. name,
  53. removeMetric,
  54. showWarning,
  55. }: Props) {
  56. const intl = useIntl();
  57. const theme = useTheme() as Theme;
  58. const isActionable = removeMetric !== undefined;
  59. const isDeprecated = DEPRECATED_ACTIVITY_METRICS.includes(metric as MetricKey);
  60. return (
  61. <StyledLegendItem
  62. className={classNames('sw-px-2 sw-py-1 sw-rounded-2', className)}
  63. isActionable={isActionable}
  64. >
  65. {showWarning ? (
  66. <FlagWarningIcon className="sw-mr-2" />
  67. ) : (
  68. <ChartLegend className="sw-mr-2" index={index} />
  69. )}
  70. <span className="sw-body-sm" style={{ color: themeColor('graphCursorLineColor')({ theme }) }}>
  71. {name}
  72. </span>
  73. {isDeprecated && (
  74. <Tooltip
  75. overlay={
  76. <FormattedMessage
  77. id={getDeprecatedTranslationKeyForTooltip(metric as MetricKey)}
  78. tagName="div"
  79. />
  80. }
  81. >
  82. <div>
  83. <Badge className="sw-ml-1">{intl.formatMessage({ id: 'deprecated' })}</Badge>
  84. </div>
  85. </Tooltip>
  86. )}
  87. {isActionable && (
  88. <InteractiveIcon
  89. Icon={CloseIcon}
  90. aria-label={translateWithParameters('project_activity.graphs.custom.remove_metric', name)}
  91. className="sw-ml-2"
  92. size="small"
  93. onClick={() => removeMetric(metric)}
  94. />
  95. )}
  96. </StyledLegendItem>
  97. );
  98. }
  99. interface GraphPillsProps {
  100. isActionable: boolean;
  101. }
  102. const StyledLegendItem = styled.div<GraphPillsProps>`
  103. display: flex;
  104. align-items: center;
  105. border: ${(props) =>
  106. props.isActionable ? themeBorder('default', 'buttonSecondaryBorder') : 'none'};
  107. `;