continuousProfileProvider.tsx 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import {useMemo, useState} from 'react';
  2. import {ContinuousProfileHeader} from 'sentry/components/profiling/continuousProfileHeader';
  3. import type {RequestState} from 'sentry/types/core';
  4. import type {EventTransaction} from 'sentry/types/event';
  5. import {useSentryEvent} from 'sentry/utils/profiling/hooks/useSentryEvent';
  6. import {decodeScalar} from 'sentry/utils/queryString';
  7. import {useLocation} from 'sentry/utils/useLocation';
  8. import useOrganization from 'sentry/utils/useOrganization';
  9. import {useParams} from 'sentry/utils/useParams';
  10. import {ContinuousProfileProvider, ProfileTransactionContext} from './profilesProvider';
  11. function isValidDate(date: string): boolean {
  12. return !isNaN(Date.parse(date));
  13. }
  14. interface FlamegraphViewProps {
  15. children: React.ReactNode;
  16. }
  17. export default function ProfileAndTransactionProvider(
  18. props: FlamegraphViewProps
  19. ): React.ReactElement {
  20. const organization = useOrganization();
  21. const params = useParams();
  22. const location = useLocation();
  23. const projectSlug = params.projectId!;
  24. const profileMeta = useMemo(() => {
  25. const start = decodeScalar(location.query.start);
  26. const end = decodeScalar(location.query.end);
  27. const profilerId = decodeScalar(location.query.profilerId);
  28. if (!start || !end || !profilerId) {
  29. return null;
  30. }
  31. if (!isValidDate(start) || !isValidDate(end)) {
  32. return null;
  33. }
  34. return {
  35. start,
  36. end,
  37. profiler_id: profilerId,
  38. };
  39. }, [location.query.start, location.query.end, location.query.profilerId]);
  40. const [profile, setProfile] = useState<RequestState<Profiling.ProfileInput>>({
  41. type: 'initial',
  42. });
  43. const profileTransaction = useSentryEvent<EventTransaction>(
  44. organization.slug,
  45. projectSlug,
  46. decodeScalar(location.query.eventId) || null
  47. );
  48. return (
  49. <ContinuousProfileProvider
  50. orgSlug={organization.slug}
  51. profileMeta={profileMeta}
  52. projectSlug={projectSlug}
  53. profile={profile}
  54. setProfile={setProfile}
  55. >
  56. <ProfileTransactionContext.Provider value={profileTransaction}>
  57. <ContinuousProfileHeader
  58. projectId={projectSlug}
  59. transaction={
  60. profileTransaction.type === 'resolved' ? profileTransaction.data : null
  61. }
  62. />
  63. {props.children}
  64. </ProfileTransactionContext.Provider>
  65. </ContinuousProfileProvider>
  66. );
  67. }