tracesSearchBar.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import styled from '@emotion/styled';
  2. import {Button} from 'sentry/components/button';
  3. import SearchBar from 'sentry/components/events/searchBar';
  4. import {IconAdd, IconClose} from 'sentry/icons';
  5. import {t} from 'sentry/locale';
  6. import {space} from 'sentry/styles/space';
  7. import {trackAnalytics} from 'sentry/utils/analytics';
  8. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {useSpanFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
  11. import {ALL_PROJECTS} from './utils';
  12. interface TracesSearchBarProps {
  13. handleClearSearch: (index: number) => boolean;
  14. handleSearch: (index: number, query: string) => void;
  15. queries: string[];
  16. }
  17. const getSpanName = (index: number) => {
  18. const spanNames = [
  19. t('Find traces where a span is'),
  20. t('and another span where'),
  21. t('and another span where'),
  22. ];
  23. return spanNames[index];
  24. };
  25. export function TracesSearchBar({
  26. queries,
  27. handleSearch,
  28. handleClearSearch,
  29. }: TracesSearchBarProps) {
  30. // TODO: load tags for autocompletion
  31. const organization = useOrganization();
  32. const canAddMoreQueries = queries.length <= 2;
  33. const localQueries = queries.length ? queries : [''];
  34. // Since trace explorer permits cross project searches,
  35. // autocompletion should also be cross projects.
  36. const supportedTags = useSpanFieldSupportedTags({
  37. projects: ALL_PROJECTS,
  38. });
  39. return (
  40. <TraceSearchBarsContainer>
  41. {localQueries.map((query, index) => (
  42. <TraceBar key={index}>
  43. <SpanLetter>{getSpanName(index)}</SpanLetter>
  44. <StyledSearchBar
  45. searchSource="trace-explorer"
  46. query={query}
  47. onSearch={(queryString: string) => handleSearch(index, queryString)}
  48. placeholder={t('Search for span attributes')}
  49. organization={organization}
  50. metricAlert={false}
  51. supportedTags={supportedTags}
  52. dataset={DiscoverDatasets.SPANS_INDEXED}
  53. projectIds={ALL_PROJECTS}
  54. />
  55. <StyledButton
  56. aria-label={t('Remove Span')}
  57. icon={<IconClose size="sm" />}
  58. size="sm"
  59. onClick={() => {
  60. trackAnalytics('trace_explorer.remove_span_condition', {
  61. organization,
  62. });
  63. if (queries.length >= 0) {
  64. handleClearSearch(index);
  65. }
  66. }}
  67. />
  68. </TraceBar>
  69. ))}
  70. {canAddMoreQueries ? (
  71. <Button
  72. aria-label={t('Add Query')}
  73. icon={<IconAdd size="xs" isCircled />}
  74. size="sm"
  75. onClick={() => {
  76. trackAnalytics('trace_explorer.add_span_condition', {
  77. organization,
  78. });
  79. handleSearch(localQueries.length, '');
  80. }}
  81. >
  82. {t('Add Another Span')}
  83. </Button>
  84. ) : null}
  85. </TraceSearchBarsContainer>
  86. );
  87. }
  88. const TraceSearchBarsContainer = styled('div')`
  89. display: flex;
  90. flex-direction: column;
  91. align-items: flex-start;
  92. justify-content: center;
  93. gap: ${space(1)};
  94. `;
  95. const TraceBar = styled('div')`
  96. display: flex;
  97. flex-direction: row;
  98. width: 100%;
  99. gap: ${space(1)};
  100. `;
  101. const SpanLetter = styled('div')`
  102. background-color: ${p => p.theme.purple100};
  103. border-radius: ${p => p.theme.borderRadius};
  104. text-align: center;
  105. min-width: 220px;
  106. color: ${p => p.theme.purple400};
  107. white-space: nowrap;
  108. font-weight: ${p => p.theme.fontWeightBold};
  109. align-content: center;
  110. `;
  111. const StyledSearchBar = styled(SearchBar)`
  112. width: 100%;
  113. `;
  114. const StyledButton = styled(Button)`
  115. height: 38px;
  116. `;