tracesSearchBar.tsx 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 {useSpanFieldSupportedTags} from 'sentry/views/performance/utils/useSpanFieldSupportedTags';
  12. import {ALL_PROJECTS} from './utils';
  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. // TODO: load tags for autocompletion
  32. const organization = useOrganization();
  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. projects: ALL_PROJECTS,
  39. });
  40. return (
  41. <TraceSearchBarsContainer>
  42. {localQueries.map((query, index) => (
  43. <TraceBar key={index}>
  44. <SpanLetter>{getSpanName(index)}</SpanLetter>
  45. <StyledSearchBar
  46. searchSource="trace-explorer"
  47. query={query}
  48. onSearch={(queryString: string) => handleSearch(index, queryString)}
  49. placeholder={t('Search for span attributes')}
  50. organization={organization}
  51. metricAlert={false}
  52. supportedTags={supportedTags}
  53. dataset={DiscoverDatasets.SPANS_INDEXED}
  54. projectIds={ALL_PROJECTS}
  55. savedSearchType={SavedSearchType.SPAN}
  56. />
  57. <StyledButton
  58. aria-label={t('Remove Span')}
  59. icon={<IconClose size="sm" />}
  60. size="sm"
  61. onClick={() => {
  62. trackAnalytics('trace_explorer.remove_span_condition', {
  63. organization,
  64. });
  65. if (queries.length >= 0) {
  66. handleClearSearch(index);
  67. }
  68. }}
  69. />
  70. </TraceBar>
  71. ))}
  72. {canAddMoreQueries ? (
  73. <Button
  74. aria-label={t('Add Query')}
  75. icon={<IconAdd size="xs" isCircled />}
  76. size="sm"
  77. onClick={() => {
  78. trackAnalytics('trace_explorer.add_span_condition', {
  79. organization,
  80. });
  81. handleSearch(localQueries.length, '');
  82. }}
  83. >
  84. {t('Add Another Span')}
  85. </Button>
  86. ) : null}
  87. </TraceSearchBarsContainer>
  88. );
  89. }
  90. const TraceSearchBarsContainer = styled('div')`
  91. display: flex;
  92. flex-direction: column;
  93. align-items: flex-start;
  94. justify-content: center;
  95. gap: ${space(1)};
  96. `;
  97. const TraceBar = styled('div')`
  98. display: flex;
  99. flex-direction: row;
  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. text-align: center;
  107. min-width: 220px;
  108. color: ${p => p.theme.purple400};
  109. white-space: nowrap;
  110. font-weight: ${p => p.theme.fontWeightBold};
  111. align-content: center;
  112. `;
  113. const StyledSearchBar = styled(SearchBar)`
  114. width: 100%;
  115. `;
  116. const StyledButton = styled(Button)`
  117. height: 38px;
  118. `;