tracesSearchBar.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 {SavedSearchType} from 'sentry/types/group';
  8. import {trackAnalytics} from 'sentry/utils/analytics';
  9. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  10. import useOrganization from 'sentry/utils/useOrganization';
  11. import usePageFilters from 'sentry/utils/usePageFilters';
  12. import {useSpanFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
  13. interface TracesSearchBarProps {
  14. handleClearSearch: (index: number) => boolean;
  15. handleSearch: (index: number, query: string) => void;
  16. queries: string[];
  17. }
  18. const getSpanName = (index: number) => {
  19. const spanNames = [
  20. t('Find traces where a span is'),
  21. t('and another span where'),
  22. t('and another span where'),
  23. ];
  24. return spanNames[index];
  25. };
  26. export function TracesSearchBar({
  27. queries,
  28. handleSearch,
  29. handleClearSearch,
  30. }: TracesSearchBarProps) {
  31. const organization = useOrganization();
  32. const {selection} = usePageFilters();
  33. const canAddMoreQueries = queries.length <= 2;
  34. const localQueries = queries.length ? queries : [''];
  35. // Since trace explorer permits cross project searches,
  36. // autocompletion should also be cross projects.
  37. const supportedTags = useSpanFieldSupportedTags();
  38. return (
  39. <TraceSearchBarsContainer>
  40. {localQueries.map((query, index) => (
  41. <TraceBar key={index}>
  42. <SpanLetter>{getSpanName(index)}</SpanLetter>
  43. <StyledSearchBar
  44. searchSource="trace-explorer"
  45. query={query}
  46. onSearch={(queryString: string) => handleSearch(index, queryString)}
  47. placeholder={t('Search for span attributes')}
  48. organization={organization}
  49. metricAlert={false}
  50. supportedTags={supportedTags}
  51. dataset={DiscoverDatasets.SPANS_INDEXED}
  52. projectIds={selection.projects}
  53. savedSearchType={SavedSearchType.SPAN}
  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. `;