flamegraphThemeProvider.tsx 2.0 KB

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