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.

IssueLocation.tsx 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 classNames from 'classnames';
  22. import { BaseLink, LocationMarker, StyledMarker, themeColor } from 'design-system';
  23. import React, { useCallback, useEffect, useMemo, useRef } from 'react';
  24. import { translate } from '../../../helpers/l10n';
  25. interface Props {
  26. concealed?: boolean;
  27. index: number;
  28. message: string | undefined;
  29. onClick: (index: number) => void;
  30. selected: boolean;
  31. }
  32. export default function IssueLocation(props: Props) {
  33. const { index, message, selected, concealed, onClick } = props;
  34. const node = useRef<HTMLElement | null>(null);
  35. const locationType = useMemo(() => getLocationType(message), [message]);
  36. const normalizedMessage = useMemo(() => message?.replace(/^(source|sink): /i, ''), [message]);
  37. useEffect(() => {
  38. if (selected && node.current) {
  39. node.current.scrollIntoView({
  40. block: 'nearest',
  41. behavior: 'smooth',
  42. });
  43. }
  44. }, [selected]);
  45. const handleClick = useCallback(
  46. (event: React.MouseEvent<HTMLAnchorElement>) => {
  47. event.preventDefault();
  48. onClick(index);
  49. },
  50. [index, onClick],
  51. );
  52. return (
  53. <StyledLink
  54. aria-label={normalizedMessage}
  55. aria-current={selected}
  56. onClick={handleClick}
  57. to={{}}
  58. >
  59. <StyledLocation
  60. className={classNames('sw-p-1 sw-rounded-1/2 sw-flex sw-gap-2 sw-body-sm', {
  61. selected,
  62. })}
  63. ref={(n) => (node.current = n)}
  64. >
  65. <LocationMarker selected={selected} text={concealed ? undefined : index + 1} />
  66. <span>
  67. {locationType && (
  68. <LocationMarker
  69. className="sw-inline sw-mr-2"
  70. selected={selected}
  71. text={locationType.toUpperCase()}
  72. />
  73. )}
  74. <StyledLocationName>
  75. {normalizedMessage ?? translate('issue.unnamed_location')}
  76. </StyledLocationName>
  77. </span>
  78. </StyledLocation>
  79. </StyledLink>
  80. );
  81. }
  82. const StyledLocation = styled.div`
  83. &.selected,
  84. &:hover {
  85. background-color: ${themeColor('codeLineLocationSelected')};
  86. }
  87. &:hover ${StyledMarker} {
  88. background-color: ${themeColor('codeLineLocationMarkerSelected')};
  89. }
  90. `;
  91. const StyledLink = styled(BaseLink)`
  92. color: ${themeColor('pageContent')};
  93. border: none;
  94. `;
  95. const StyledLocationName = styled.span`
  96. word-break: break-word;
  97. `;
  98. function getLocationType(message?: string) {
  99. if (message?.toLowerCase().startsWith('source')) {
  100. return 'source';
  101. } else if (message?.toLowerCase().startsWith('sink')) {
  102. return 'sink';
  103. }
  104. return null;
  105. }