profileGroupProvider.tsx 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import {createContext, useContext, useMemo} from 'react';
  2. import {importProfile, ProfileGroup} from 'sentry/utils/profiling/profile/importProfile';
  3. type ProfileGroupContextValue = ProfileGroup;
  4. const ProfileGroupContext = createContext<ProfileGroupContextValue | null>(null);
  5. export function useProfileGroup() {
  6. const context = useContext(ProfileGroupContext);
  7. if (!context) {
  8. throw new Error('useProfileGroup was called outside of ProfileGroupProvider');
  9. }
  10. return context;
  11. }
  12. const LoadingGroup: ProfileGroup = {
  13. name: 'Loading',
  14. activeProfileIndex: 0,
  15. transactionID: null,
  16. metadata: {},
  17. measurements: {},
  18. traceID: '',
  19. profiles: [],
  20. };
  21. interface ProfileGroupProviderProps {
  22. children: React.ReactNode;
  23. input: Readonly<Profiling.ProfileInput> | null;
  24. traceID: string;
  25. type: 'flamegraph' | 'flamechart';
  26. }
  27. export function ProfileGroupProvider(props: ProfileGroupProviderProps) {
  28. const profileGroup = useMemo(() => {
  29. if (!props.input) {
  30. return LoadingGroup;
  31. }
  32. return importProfile(props.input, props.traceID, props.type);
  33. }, [props.input, props.traceID, props.type]);
  34. return (
  35. <ProfileGroupContext.Provider value={profileGroup}>
  36. {props.children}
  37. </ProfileGroupContext.Provider>
  38. );
  39. }