useIssuesTraceTree.tsx 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {useEffect, useState} from 'react';
  2. import type {TraceSplitResults} from 'sentry/utils/performance/quickTrace/types';
  3. import type {QueryStatus, UseApiQueryResult} from 'sentry/utils/queryClient';
  4. import useApi from 'sentry/utils/useApi';
  5. import useOrganization from 'sentry/utils/useOrganization';
  6. import useProjects from 'sentry/utils/useProjects';
  7. import {IssuesTraceTree} from 'sentry/views/performance/newTraceDetails/traceModels/issuesTraceTree';
  8. import type {ReplayRecord} from 'sentry/views/replays/types';
  9. import type {TraceTree} from '../traceModels/traceTree';
  10. import type {TraceMetaQueryResults} from './useTraceMeta';
  11. type UseTraceTreeParams = {
  12. meta: TraceMetaQueryResults;
  13. replay: ReplayRecord | null;
  14. trace: UseApiQueryResult<TraceSplitResults<TraceTree.Transaction> | undefined, any>;
  15. traceSlug?: string;
  16. };
  17. function getTraceViewQueryStatus(
  18. traceQueryStatus: QueryStatus,
  19. traceMetaQueryStatus: QueryStatus
  20. ): QueryStatus {
  21. if (traceQueryStatus === 'error' || traceMetaQueryStatus === 'error') {
  22. return 'error';
  23. }
  24. if (traceQueryStatus === 'pending' || traceMetaQueryStatus === 'pending') {
  25. return 'pending';
  26. }
  27. return 'success';
  28. }
  29. export function useIssuesTraceTree({
  30. trace,
  31. meta,
  32. replay,
  33. traceSlug,
  34. }: UseTraceTreeParams): IssuesTraceTree {
  35. const api = useApi();
  36. const {projects} = useProjects();
  37. const organization = useOrganization();
  38. const [tree, setTree] = useState<IssuesTraceTree>(IssuesTraceTree.Empty());
  39. useEffect(() => {
  40. const status = getTraceViewQueryStatus(trace.status, meta.status);
  41. if (status === 'error') {
  42. setTree(t =>
  43. t.type === 'error'
  44. ? t
  45. : IssuesTraceTree.Error({
  46. project_slug: projects?.[0]?.slug ?? '',
  47. event_id: traceSlug,
  48. })
  49. );
  50. return;
  51. }
  52. if (
  53. trace?.data?.transactions.length === 0 &&
  54. trace?.data?.orphan_errors.length === 0
  55. ) {
  56. setTree(t => (t.type === 'empty' ? t : IssuesTraceTree.Empty()));
  57. return;
  58. }
  59. if (status === 'pending') {
  60. setTree(t =>
  61. t.type === 'loading'
  62. ? t
  63. : IssuesTraceTree.Loading({
  64. project_slug: projects?.[0]?.slug ?? '',
  65. event_id: traceSlug,
  66. })
  67. );
  68. return;
  69. }
  70. if (trace.data && meta.data) {
  71. const newTree = IssuesTraceTree.FromTrace(trace.data, {
  72. meta: meta.data,
  73. replay: replay,
  74. });
  75. setTree(newTree);
  76. newTree.build();
  77. return;
  78. }
  79. }, [
  80. api,
  81. organization,
  82. projects,
  83. replay,
  84. meta.status,
  85. trace.status,
  86. trace.data,
  87. meta.data,
  88. traceSlug,
  89. ]);
  90. return tree;
  91. }