tracesSearchBar.tsx 4.1 KB

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