tracesSearchBar.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import styled from '@emotion/styled';
  2. import {Button} from 'sentry/components/button';
  3. import SearchBar, {getHasTag} 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 type {TagCollection} from 'sentry/types';
  8. import {DiscoverDatasets} from 'sentry/utils/discover/types';
  9. import useOrganization from 'sentry/utils/useOrganization';
  10. import {SpanIndexedField} from 'sentry/views/starfish/types';
  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 = [t('Span A'), t('Span B'), t('Span C')];
  18. return spanNames[index];
  19. };
  20. const omitSupportedTags = [SpanIndexedField.SPAN_AI_PIPELINE_GROUP];
  21. const getTracesSupportedTags = () => {
  22. const tags: TagCollection = Object.fromEntries(
  23. Object.values(SpanIndexedField)
  24. .filter(v => !omitSupportedTags.includes(v))
  25. .map(v => [v, {key: v, name: v}])
  26. );
  27. tags.has = getHasTag(tags);
  28. return tags;
  29. };
  30. export function TracesSearchBar({
  31. queries,
  32. handleSearch,
  33. handleClearSearch,
  34. }: TracesSearchBarProps) {
  35. // TODO: load tags for autocompletion
  36. const organization = useOrganization();
  37. const canAddMoreQueries = queries.length <= 2;
  38. const localQueries = queries.length ? queries : [''];
  39. const supportedTags = getTracesSupportedTags();
  40. return (
  41. <TraceSearchBarsContainer>
  42. {localQueries.map((query, index) => (
  43. <TraceBar key={index}>
  44. <SpanLetter>{getSpanName(index)}</SpanLetter>
  45. <StyledSearchBar
  46. query={query}
  47. onSearch={(queryString: string) => handleSearch(index, queryString)}
  48. placeholder={t(
  49. 'Search for traces containing a span matching these attributes'
  50. )}
  51. organization={organization}
  52. metricAlert={false}
  53. supportedTags={supportedTags}
  54. dataset={DiscoverDatasets.SPANS_INDEXED}
  55. />
  56. <StyledButton
  57. aria-label={t('Remove span')}
  58. icon={<IconClose size="sm" />}
  59. size="sm"
  60. onClick={() => (queries.length === 0 ? false : handleClearSearch(index))}
  61. />
  62. </TraceBar>
  63. ))}
  64. {canAddMoreQueries ? (
  65. <Button
  66. aria-label={t('Add query')}
  67. icon={<IconAdd size="xs" isCircled />}
  68. size="sm"
  69. onClick={() => handleSearch(localQueries.length, '')}
  70. >
  71. {t('Add Span')}
  72. </Button>
  73. ) : null}
  74. </TraceSearchBarsContainer>
  75. );
  76. }
  77. const TraceSearchBarsContainer = styled('div')`
  78. display: flex;
  79. flex-direction: column;
  80. align-items: flex-start;
  81. justify-content: center;
  82. gap: ${space(1)};
  83. `;
  84. const TraceBar = styled('div')`
  85. display: flex;
  86. flex-direction: row;
  87. align-items: center;
  88. justify-content: flex-start;
  89. width: 100%;
  90. gap: ${space(1)};
  91. `;
  92. const SpanLetter = styled('div')`
  93. background-color: ${p => p.theme.purple100};
  94. border-radius: ${p => p.theme.borderRadius};
  95. padding: ${space(1)} ${space(2)};
  96. color: ${p => p.theme.purple400};
  97. white-space: nowrap;
  98. font-weight: 800;
  99. `;
  100. const StyledSearchBar = styled(SearchBar)`
  101. width: 100%;
  102. `;
  103. const StyledButton = styled(Button)`
  104. height: 38px;
  105. `;