content.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {useCallback, useMemo} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import * as Layout from 'sentry/components/layouts/thirds';
  5. import {DatePageFilter} from 'sentry/components/organizations/datePageFilter';
  6. import {EnvironmentPageFilter} from 'sentry/components/organizations/environmentPageFilter';
  7. import PageFilterBar from 'sentry/components/organizations/pageFilterBar';
  8. import {ProjectPageFilter} from 'sentry/components/organizations/projectPageFilter';
  9. import type {CursorHandler} from 'sentry/components/pagination';
  10. import Pagination from 'sentry/components/pagination';
  11. import type {SmartSearchBarProps} from 'sentry/components/smartSearchBar';
  12. import {space} from 'sentry/styles/space';
  13. import {defined} from 'sentry/utils';
  14. import {decodeInteger, decodeScalar} from 'sentry/utils/queryString';
  15. import {MutableSearch} from 'sentry/utils/tokenizeSearch';
  16. import {useLocation} from 'sentry/utils/useLocation';
  17. import {useIndexedSpans} from 'sentry/views/starfish/queries/useIndexedSpans';
  18. import {fields} from './data';
  19. import {TraceRow} from './traceRow';
  20. import {TracesSearchBar} from './tracesSearchBar';
  21. const DEFAULT_PER_PAGE = 20;
  22. export function Content() {
  23. const location = useLocation();
  24. const query = useMemo(() => {
  25. return decodeScalar(location.query.query, '');
  26. }, [location.query.query]);
  27. const limit = useMemo(() => {
  28. return decodeInteger(location.query.perPage, DEFAULT_PER_PAGE);
  29. }, [location.query.perPage]);
  30. const handleSearch: SmartSearchBarProps['onSearch'] = useCallback(
  31. (searchQuery: string) => {
  32. browserHistory.push({
  33. ...location,
  34. query: {
  35. ...location.query,
  36. cursor: undefined,
  37. query: searchQuery || undefined,
  38. },
  39. });
  40. },
  41. [location]
  42. );
  43. const handleCursor: CursorHandler = useCallback((newCursor, pathname, newQuery) => {
  44. browserHistory.push({
  45. pathname,
  46. query: {...newQuery, cursor: newCursor},
  47. });
  48. }, []);
  49. const filters = useMemo(() => new MutableSearch(query ?? '').filters, [query]);
  50. const spansQuery = useIndexedSpans({
  51. fields,
  52. filters,
  53. limit,
  54. sorts: [],
  55. referrer: 'api.trace-explorer.table',
  56. });
  57. const traces = useMemo(() => {
  58. const data = (spansQuery.data ?? []).reduce((acc, span) => {
  59. const traceId = span.trace;
  60. if (!defined(traceId)) {
  61. // TODO: warn missing trace id
  62. return acc;
  63. }
  64. let spansList = acc.get(traceId);
  65. if (!defined(spansList)) {
  66. spansList = [];
  67. acc.set(traceId, spansList);
  68. }
  69. spansList.push(span);
  70. return acc;
  71. }, new Map());
  72. return Array.from(data);
  73. }, [spansQuery.data]);
  74. return (
  75. <LayoutMain fullWidth>
  76. <PageFilterBar condensed>
  77. <ProjectPageFilter />
  78. <EnvironmentPageFilter />
  79. <DatePageFilter />
  80. </PageFilterBar>
  81. <TracesSearchBar query={query} handleSearch={handleSearch} />
  82. {traces.map(([traceId, spans]) => (
  83. <TraceRow key={traceId} traceId={traceId} spans={spans} />
  84. ))}
  85. <StyledPagination pageLinks={spansQuery.pageLinks} onCursor={handleCursor} />
  86. </LayoutMain>
  87. );
  88. }
  89. const LayoutMain = styled(Layout.Main)`
  90. display: flex;
  91. flex-direction: column;
  92. gap: ${space(2)};
  93. `;
  94. const StyledPagination = styled(Pagination)`
  95. margin: 0px;
  96. `;