spansTable.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {Fragment, useMemo} from 'react';
  2. import Pagination from 'sentry/components/pagination';
  3. import type {NewQuery} from 'sentry/types/organization';
  4. import EventView from 'sentry/utils/discover/eventView';
  5. import {getFieldRenderer} from 'sentry/utils/discover/fieldRenderers';
  6. import {useLocation} from 'sentry/utils/useLocation';
  7. import useOrganization from 'sentry/utils/useOrganization';
  8. import usePageFilters from 'sentry/utils/usePageFilters';
  9. import {
  10. Table,
  11. TableBody,
  12. TableBodyCell,
  13. TableHead,
  14. TableHeadCell,
  15. TableRow,
  16. useTableStyles,
  17. } from 'sentry/views/explore/components/table';
  18. import {useDataset} from 'sentry/views/explore/hooks/useDataset';
  19. import {useSampleFields} from 'sentry/views/explore/hooks/useSampleFields';
  20. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  21. import {useUserQuery} from 'sentry/views/explore/hooks/useUserQuery';
  22. import {useSpansQuery} from 'sentry/views/insights/common/queries/useSpansQuery';
  23. interface SpansTableProps {}
  24. export function SpansTable({}: SpansTableProps) {
  25. const location = useLocation();
  26. const organization = useOrganization();
  27. const {selection} = usePageFilters();
  28. const [dataset] = useDataset();
  29. const [fields] = useSampleFields();
  30. const [sorts] = useSorts({fields});
  31. const [query] = useUserQuery();
  32. const eventView = useMemo(() => {
  33. const discoverQuery: NewQuery = {
  34. id: undefined,
  35. name: 'Explore - Span Samples',
  36. fields,
  37. orderby: sorts.map(sort => `${sort.kind === 'desc' ? '-' : ''}${sort.field}`),
  38. query,
  39. version: 2,
  40. dataset,
  41. };
  42. return EventView.fromNewQueryWithPageFilters(discoverQuery, selection);
  43. }, [dataset, fields, sorts, query, selection]);
  44. const result = useSpansQuery({
  45. eventView,
  46. initialData: [],
  47. referrer: 'api.explore.spans-samples-table',
  48. });
  49. const {tableStyles} = useTableStyles({
  50. items: fields.map(field => {
  51. return {
  52. label: field,
  53. value: field,
  54. };
  55. }),
  56. });
  57. const meta = result.meta ?? {};
  58. return (
  59. <Fragment>
  60. <Table style={tableStyles}>
  61. <TableHead>
  62. <TableRow>
  63. {fields.map((field, i) => (
  64. <TableHeadCell key={field} isFirst={i === 0}>
  65. {field}
  66. </TableHeadCell>
  67. ))}
  68. </TableRow>
  69. </TableHead>
  70. <TableBody>
  71. {result.data?.map((row, i) => (
  72. <TableRow key={i}>
  73. {fields.map(field => {
  74. const renderer = getFieldRenderer(field, meta.fields, false);
  75. return (
  76. <TableBodyCell key={field}>
  77. {renderer(row, {
  78. location,
  79. organization,
  80. unit: meta?.units?.[field],
  81. })}
  82. </TableBodyCell>
  83. );
  84. })}
  85. </TableRow>
  86. ))}
  87. </TableBody>
  88. </Table>
  89. <Pagination pageLinks={result.pageLinks} />
  90. </Fragment>
  91. );
  92. }