flamegraphThemeProvider.tsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import {createContext, useMemo} from 'react';
  2. import ConfigStore from 'sentry/stores/configStore';
  3. import {useLegacyStore} from 'sentry/stores/useLegacyStore';
  4. import {
  5. makeColorMapByImage,
  6. makeColorMapByRecursion,
  7. makeColorMapBySystemVsApplication,
  8. } from '../colors/utils';
  9. import {
  10. DarkFlamegraphTheme,
  11. FlamegraphTheme,
  12. LightFlamegraphTheme,
  13. } from './flamegraphTheme';
  14. import {useFlamegraphPreferencesValue} from './useFlamegraphPreferences';
  15. export const FlamegraphThemeContext = createContext<FlamegraphTheme | null>(null);
  16. interface FlamegraphThemeProviderProps {
  17. children: React.ReactNode;
  18. }
  19. function FlamegraphThemeProvider(
  20. props: FlamegraphThemeProviderProps
  21. ): React.ReactElement {
  22. const {theme} = useLegacyStore(ConfigStore);
  23. const flamegraphPreferences = useFlamegraphPreferencesValue();
  24. const activeFlamegraphTheme = useMemo((): FlamegraphTheme => {
  25. const base = theme === 'light' ? LightFlamegraphTheme : DarkFlamegraphTheme;
  26. switch (flamegraphPreferences.colorCoding) {
  27. case 'by symbol name': {
  28. return base;
  29. }
  30. case 'by recursion': {
  31. return {...base, COLORS: {...base.COLORS, COLOR_MAP: makeColorMapByRecursion}};
  32. }
  33. case 'by library': {
  34. return {...base, COLORS: {...base.COLORS, COLOR_MAP: makeColorMapByImage}};
  35. }
  36. case 'by system / application': {
  37. return {
  38. ...base,
  39. COLORS: {...base.COLORS, COLOR_MAP: makeColorMapBySystemVsApplication},
  40. };
  41. }
  42. default: {
  43. throw new TypeError(
  44. `Unsupported flamegraph color coding ${flamegraphPreferences.colorCoding}`
  45. );
  46. }
  47. }
  48. }, [theme, flamegraphPreferences.colorCoding]);
  49. return (
  50. <FlamegraphThemeContext.Provider value={activeFlamegraphTheme}>
  51. {props.children}
  52. </FlamegraphThemeContext.Provider>
  53. );
  54. }
  55. export {FlamegraphThemeProvider};