useInternalFlamegraphDebugMode.ts 896 B

12345678910111213141516171819202122232425262728293031
  1. import {useEffect, useState} from 'react';
  2. const FLAMEGRAPH_DEBUG_MODE_KEY = '__fgdb__';
  3. export function useInternalFlamegraphDebugMode() {
  4. const [isEnabled, setIsEnabled] = useState(
  5. sessionStorage.getItem(FLAMEGRAPH_DEBUG_MODE_KEY) === '1'
  6. );
  7. useEffect(() => {
  8. function handleKeyDown(evt: KeyboardEvent) {
  9. const isCtrlOrMeta = evt.ctrlKey || evt.metaKey;
  10. if (isCtrlOrMeta && evt.shiftKey && evt.code === 'KeyI') {
  11. setIsEnabled(val => {
  12. const next = !val;
  13. if (next) {
  14. sessionStorage.setItem(FLAMEGRAPH_DEBUG_MODE_KEY, '1');
  15. } else {
  16. sessionStorage.removeItem(FLAMEGRAPH_DEBUG_MODE_KEY);
  17. }
  18. return next;
  19. });
  20. }
  21. }
  22. window.addEventListener('keydown', handleKeyDown);
  23. return () => window.removeEventListener('keydown', handleKeyDown);
  24. });
  25. return isEnabled;
  26. }