ref: move use-button-tracking from sentry hooks to react context #95220
+124
−121
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
To be able to decouple our
core/components
from getsentry specific logic, we have to stop using sentry-hooks insidecore/components
.However, button tracking logic is defined in getentry and injected via
'react-hook:use-button-tracking'
, which tracks button click events in sentry itself.The react idiomatic way to inject specific logic into a “generic” library is “dependency injection” via react context provided by the generic library.
In our case, that means
core/components/button
exposes a<TrackingContextProvider>
that consumers can render and pass a function that will be called when a button is clicked. This is similar to how react-aria allows link behaviour to be injected via theirRouterProvider
.This is what I’ve been doing in the base branch:
Now here’s the thing: sentry hooks do pretty much the same thing for us as react context - at least for things that we only use within react. But that means we’ve now created another layer of indirection. Basically, the flow is:
the sentry specific
SentryTrackingProvider
basically takes the value we’ve put into the hook-store and forwards it to the agnosticTrackingContextProvider
coming from our core components:This layer of indirection feels unnecessary, and passing hooks around as values is technically against the rules of react (see never pass hooks around as regular values).
In this PR, I’m trying to remove that indirection and just do:
At the top level, where we
import('getsentry/registerHooks')
, we now not only get backregisterHooks
, but also aSentryHooksProvider
that directly renders sentry+react specific things. Right now, it’s just theTrackingContext
, but it could be other things, too:We then take this Provider and have to drill it down a bunch of levels (this is the main drawback tbh), and render it as a top-level provider in our root route. This is necessary to be able to call hooks like
useRouter()
inside the custom hooks.With this, we’d have:
useButtonTracking
hook directly instead of passing it around as a valuereact-hook:use-button-tracking
sentry hookIf we agree to this approach, I’d like to follow up with removing all sentry hooks starting with
react-hook:
, and potentially even more sentry hooks that are only used inside react.Please let me know if I’m missing something, e.g. if those hooks are also used by self-hosted customers to show their own things?