tracesSearchBar.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. interface TracesSearchBarProps {
  12. handleClearSearch: (index: number) => boolean;
  13. handleSearch: (index: number, query: string) => void;
  14. queries: string[];
  15. }
  16. const getSpanName = (index: number) => {
  17. const spanNames = [
  18. t('Find traces where a span is'),
  19. t('and another span where'),
  20. t('and another span where'),
  21. ];
  22. return spanNames[index];
  23. };
  24. // Since trace explorer permits cross project searches,
  25. // autocompletion should also be cross projects.
  26. const ALL_PROJECTS = [-1];
  27. export function TracesSearchBar({
  28. queries,
  29. handleSearch,
  30. handleClearSearch,
  31. }: TracesSearchBarProps) {
  32. // TODO: load tags for autocompletion
  33. const organization = useOrganization();
  34. const canAddMoreQueries = queries.length <= 2;
  35. const localQueries = queries.length ? queries : [''];
  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 Span Condition')}
  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. align-items: center;
  99. justify-content: flex-start;
  100. width: 100%;
  101. gap: ${space(1)};
  102. `;
  103. const SpanLetter = styled('div')`
  104. background-color: ${p => p.theme.purple100};
  105. border-radius: ${p => p.theme.borderRadius};
  106. padding: ${space(1)} ${space(2)};
  107. text-align: center;
  108. min-width: 220px;
  109. color: ${p => p.theme.purple400};
  110. white-space: nowrap;
  111. font-weight: ${p => p.theme.fontWeightBold};
  112. `;
  113. const StyledSearchBar = styled(SearchBar)`
  114. width: 100%;
  115. `;
  116. const StyledButton = styled(Button)`
  117. height: 38px;
  118. `;