Skip to content

Commit 5b62c0d

Browse files
committed
ref: decouple button from HookStore
1 parent 38cc81c commit 5b62c0d

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

static/app/components/core/button/useButtonFunctionality.tsx

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
1-
// eslint-disable-next-line boundaries/element-types
2-
import HookStore from 'sentry/stores/hookStore';
1+
import {useButtonTracking} from 'sentry/components/core/trackingContext';
32

43
import type {
54
DO_NOT_USE_ButtonProps as ButtonProps,
65
DO_NOT_USE_LinkButtonProps as LinkButtonProps,
76
} from './types';
87

9-
const defaultCreateButtonTracking = (props: ButtonProps) => {
10-
const hasAnalyticsDebug = window.localStorage?.getItem('DEBUG_ANALYTICS') === '1';
11-
return () => {
12-
const hasCustomAnalytics =
13-
props.analyticsEventName || props.analyticsEventKey || props.analyticsParams;
14-
if (hasCustomAnalytics && hasAnalyticsDebug) {
15-
// eslint-disable-next-line no-console
16-
console.log('buttonAnalyticsEvent', {
17-
eventKey: props.analyticsEventKey,
18-
eventName: props.analyticsEventName,
19-
priority: props.priority,
20-
href: 'href' in props ? props.href : undefined,
21-
...props.analyticsParams,
22-
});
23-
}
24-
};
25-
};
26-
278
export function useButtonFunctionality(props: ButtonProps | LinkButtonProps) {
289
// Fallbacking aria-label to string children is not necessary as screen
2910
// readers natively understand that scenario. Leaving it here for a bunch of
@@ -32,10 +13,7 @@ export function useButtonFunctionality(props: ButtonProps | LinkButtonProps) {
3213
props['aria-label'] ??
3314
(typeof props.children === 'string' ? props.children : undefined);
3415

35-
const createButtonTracking =
36-
HookStore.get('react-hook:use-button-tracking')[0] ?? defaultCreateButtonTracking;
37-
38-
const buttonTracking = createButtonTracking({
16+
const buttonTracking = useButtonTracking({
3917
analyticsEventName: props.analyticsEventName,
4018
analyticsEventKey: props.analyticsEventKey,
4119
analyticsParams: {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import {createContext, useContext} from 'react';
2+
3+
import type {DO_NOT_USE_ButtonProps as ButtonProps} from './button/types';
4+
5+
const defaultButtonTracking = (props: ButtonProps) => {
6+
const hasAnalyticsDebug = window.localStorage?.getItem('DEBUG_ANALYTICS') === '1';
7+
return () => {
8+
const hasCustomAnalytics =
9+
props.analyticsEventName || props.analyticsEventKey || props.analyticsParams;
10+
if (hasCustomAnalytics && hasAnalyticsDebug) {
11+
// eslint-disable-next-line no-console
12+
console.log('buttonAnalyticsEvent', {
13+
eventKey: props.analyticsEventKey,
14+
eventName: props.analyticsEventName,
15+
priority: props.priority,
16+
href: 'href' in props ? props.href : undefined,
17+
...props.analyticsParams,
18+
});
19+
}
20+
};
21+
};
22+
23+
const TrackingContext = createContext<{
24+
useButtonTracking?: (props: ButtonProps) => () => void;
25+
}>({});
26+
27+
export const TrackingContextProvider = TrackingContext.Provider;
28+
29+
export const useButtonTracking = (props: ButtonProps) => {
30+
const context = useContext(TrackingContext);
31+
32+
return context.useButtonTracking?.(props) ?? defaultButtonTracking(props);
33+
};

static/app/main.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1-
import {useState} from 'react';
1+
import {useMemo, useState} from 'react';
22
import {createBrowserRouter, RouterProvider} from 'react-router-dom';
33
import {wrapCreateBrowserRouterV6} from '@sentry/react';
44
import {ReactQueryDevtools} from '@tanstack/react-query-devtools';
55

66
import {AppQueryClientProvider} from 'sentry/appQueryClient';
7+
import {TrackingContextProvider} from 'sentry/components/core/trackingContext';
78
import {OnboardingContextProvider} from 'sentry/components/onboarding/onboardingContext';
89
import {ThemeAndStyleProvider} from 'sentry/components/themeAndStyleProvider';
910
import {USE_REACT_QUERY_DEVTOOL} from 'sentry/constants';
1011
import {routes} from 'sentry/routes';
12+
import HookStore from 'sentry/stores/hookStore';
1113
import {DANGEROUS_SET_REACT_ROUTER_6_HISTORY} from 'sentry/utils/browserHistory';
1214

1315
import {buildReactRouter6Routes} from './utils/reactRouter6Compat/router';
@@ -22,12 +24,16 @@ function buildRouter() {
2224

2325
function Main() {
2426
const [router] = useState(buildRouter);
27+
const useButtonTracking = HookStore.get('react-hook:use-button-tracking')[0];
28+
const trackingContextValue = useMemo(() => ({useButtonTracking}), [useButtonTracking]);
2529

2630
return (
2731
<AppQueryClientProvider>
2832
<ThemeAndStyleProvider>
2933
<OnboardingContextProvider>
30-
<RouterProvider router={router} />
34+
<TrackingContextProvider value={trackingContextValue}>
35+
<RouterProvider router={router} />
36+
</TrackingContextProvider>
3137
</OnboardingContextProvider>
3238
{USE_REACT_QUERY_DEVTOOL && (
3339
<ReactQueryDevtools initialIsOpen={false} buttonPosition="bottom-left" />

0 commit comments

Comments
 (0)