flamegraphOptionsMenu.tsx 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {Fragment, useMemo} from 'react';
  2. import Button from 'sentry/components/button';
  3. import CompositeSelect from 'sentry/components/forms/compositeSelect';
  4. import {IconSliders} from 'sentry/icons';
  5. import {t} from 'sentry/locale';
  6. import {CanvasPoolManager} from 'sentry/utils/profiling/canvasScheduler';
  7. import {FlamegraphPreferences} from 'sentry/utils/profiling/flamegraph/flamegraphStateProvider/flamegraphPreferences';
  8. import {useFlamegraphPreferences} from 'sentry/utils/profiling/flamegraph/useFlamegraphPreferences';
  9. interface FlamegraphOptionsMenuProps {
  10. canvasPoolManager: CanvasPoolManager;
  11. }
  12. function FlamegraphOptionsMenu({
  13. canvasPoolManager,
  14. }: FlamegraphOptionsMenuProps): React.ReactElement {
  15. const [{colorCoding, xAxis}, dispatch] = useFlamegraphPreferences();
  16. const options = useMemo(() => {
  17. return [
  18. {
  19. label: t('X Axis'),
  20. value: 'x axis',
  21. defaultValue: xAxis,
  22. options: Object.entries(X_AXIS).map(([value, label]) => ({
  23. label,
  24. value,
  25. })),
  26. onChange: value =>
  27. dispatch({
  28. type: 'set xAxis',
  29. payload: value,
  30. }),
  31. },
  32. {
  33. label: t('Color Coding'),
  34. value: 'by symbol name',
  35. defaultValue: colorCoding,
  36. options: Object.entries(COLOR_CODINGS).map(([value, label]) => ({
  37. label,
  38. value,
  39. })),
  40. onChange: value =>
  41. dispatch({
  42. type: 'set color coding',
  43. payload: value,
  44. }),
  45. },
  46. ];
  47. // If we add color and xAxis it updates the memo and the component is re-rendered (losing hovered state)
  48. // Not ideal, but since we are only passing default value I guess we can live with it
  49. // eslint-disable-next-line react-hooks/exhaustive-deps
  50. }, [dispatch]);
  51. return (
  52. <Fragment>
  53. <Button size="xs" onClick={() => canvasPoolManager.dispatch('reset zoom', [])}>
  54. {t('Reset Zoom')}
  55. </Button>
  56. <CompositeSelect
  57. triggerLabel={t('Options')}
  58. triggerProps={{
  59. icon: <IconSliders size="xs" />,
  60. size: 'xs',
  61. }}
  62. placement="bottom right"
  63. sections={options}
  64. />
  65. </Fragment>
  66. );
  67. }
  68. const X_AXIS: Record<FlamegraphPreferences['xAxis'], string> = {
  69. standalone: t('Standalone'),
  70. transaction: t('Transaction'),
  71. };
  72. const COLOR_CODINGS: Record<FlamegraphPreferences['colorCoding'], string> = {
  73. 'by symbol name': t('By Symbol Name'),
  74. 'by library': t('By Library'),
  75. 'by system / application': t('By System / Application'),
  76. 'by recursion': t('By Recursion'),
  77. };
  78. export {FlamegraphOptionsMenu};