tracesSearchBar.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. supportedTags={supportedTags}
  50. dataset={DiscoverDatasets.SPANS_INDEXED}
  51. projectIds={selection.projects}
  52. savedSearchType={SavedSearchType.SPAN}
  53. />
  54. <StyledButton
  55. aria-label={t('Remove Span')}
  56. icon={<IconClose size="sm" />}
  57. size="sm"
  58. onClick={() => {
  59. trackAnalytics('trace_explorer.remove_span_condition', {
  60. organization,
  61. });
  62. if (queries.length >= 0) {
  63. handleClearSearch(index);
  64. }
  65. }}
  66. />
  67. </TraceBar>
  68. ))}
  69. {canAddMoreQueries ? (
  70. <Button
  71. aria-label={t('Add Query')}
  72. icon={<IconAdd size="xs" isCircled />}
  73. size="sm"
  74. onClick={() => {
  75. trackAnalytics('trace_explorer.add_span_condition', {
  76. organization,
  77. });
  78. handleSearch(localQueries.length, '');
  79. }}
  80. >
  81. {t('Add Another Span')}
  82. </Button>
  83. ) : null}
  84. </TraceSearchBarsContainer>
  85. );
  86. }
  87. const TraceSearchBarsContainer = styled('div')`
  88. display: flex;
  89. flex-direction: column;
  90. align-items: flex-start;
  91. justify-content: center;
  92. gap: ${space(1)};
  93. `;
  94. const TraceBar = styled('div')`
  95. display: flex;
  96. flex-direction: row;
  97. width: 100%;
  98. gap: ${space(1)};
  99. `;
  100. const SpanLetter = styled('div')`
  101. background-color: ${p => p.theme.purple100};
  102. border-radius: ${p => p.theme.borderRadius};
  103. text-align: center;
  104. min-width: 220px;
  105. color: ${p => p.theme.purple400};
  106. white-space: nowrap;
  107. font-weight: ${p => p.theme.fontWeightBold};
  108. align-content: center;
  109. `;
  110. const StyledSearchBar = styled(SearchBar)`
  111. width: 100%;
  112. `;
  113. const StyledButton = styled(Button)`
  114. height: 38px;
  115. `;