Skip to content

Commit cab9f4f

Browse files
ref(dashboards): Use useHotkeys for ESC key handling in widget builder
Replace raw useEffect + addEventListener with the useHotkeys hook from @sentry/scraps/hotkey, matching the pattern used by the global drawer. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 0373efb commit cab9f4f

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

static/app/views/dashboards/widgetBuilder/components/widgetBuilderSlideout.tsx

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
1-
import {
2-
Fragment,
3-
useCallback,
4-
useEffect,
5-
useMemo,
6-
useState,
7-
type RefCallback,
8-
} from 'react';
1+
import {Fragment, useCallback, useMemo, useState, type RefCallback} from 'react';
92
import {useTheme} from '@emotion/react';
103
import styled from '@emotion/styled';
114
import isEqual from 'lodash/isEqual';
125

136
import {Alert} from '@sentry/scraps/alert';
147
import {Button} from '@sentry/scraps/button';
8+
import {useHotkeys} from '@sentry/scraps/hotkey';
159
import {Flex} from '@sentry/scraps/layout';
1610
import {ExternalLink, Link} from '@sentry/scraps/link';
1711
import {SlideOverPanel} from '@sentry/scraps/slideOverPanel';
@@ -236,15 +230,16 @@ export function WidgetBuilderSlideout({
236230
});
237231
}, [initialState, onClose, state]);
238232

239-
useEffect(() => {
240-
function handleKeyDown(event: KeyboardEvent) {
241-
if (event.key === 'Escape' && !event.defaultPrevented && !isModalVisible) {
242-
onCloseWithModal();
243-
}
244-
}
245-
document.addEventListener('keydown', handleKeyDown);
246-
return () => document.removeEventListener('keydown', handleKeyDown);
247-
}, [onCloseWithModal, isModalVisible]);
233+
useHotkeys([
234+
{
235+
match: 'Escape',
236+
callback: () => {
237+
if (!isModalVisible) {
238+
onCloseWithModal();
239+
}
240+
},
241+
},
242+
]);
248243

249244
const breadcrumbs = customizeFromLibrary
250245
? [

0 commit comments

Comments
 (0)