Skip to content

Commit

Permalink
fix(feedback): Create a store so we can track feedback integration lo…
Browse files Browse the repository at this point in the history
…ading and re-render (#73056)

This adds a context that we can use to store the loaded Feedback
integration, and re-render children when that thing is loaded. It's got
to be at the top of the react heirarchy like this because feedback is
also loaded high in the tree and rendered everywhere.

See getsentry/getsentry#14390

Related to getsentry/team-replay#438
  • Loading branch information
ryan953 authored Jun 20, 2024
1 parent 9346b55 commit 583ac6c
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
10 changes: 9 additions & 1 deletion static/app/components/feedback/widget/useFeedbackWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import * as Sentry from '@sentry/react';
import {t} from 'sentry/locale';
import ConfigStore from 'sentry/stores/configStore';
import {useLegacyStore} from 'sentry/stores/useLegacyStore';
import useAsyncSDKIntegrationStore from 'sentry/views/app/asyncSDKIntegrationProvider';

type FeedbackIntegration = NonNullable<ReturnType<typeof Sentry.getFeedback>>;

interface Props {
buttonRef?: RefObject<HTMLButtonElement> | RefObject<HTMLAnchorElement>;
Expand All @@ -18,7 +21,12 @@ export default function useFeedbackWidget({
messagePlaceholder,
}: Props) {
const config = useLegacyStore(ConfigStore);
const feedback = Sentry.getFeedback();
const {state} = useAsyncSDKIntegrationStore();

// TODO(ryan953): remove the fallback `?? Sentry.getFeedback()` after
// getsentry is calling `store.add(feedback);`
const feedback =
(state.Feedback as FeedbackIntegration | undefined) ?? Sentry.getFeedback();

useEffect(() => {
if (!feedback) {
Expand Down
28 changes: 28 additions & 0 deletions static/app/views/app/asyncSDKIntegrationProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {createContext, useContext, useState} from 'react';
import type {addIntegration} from '@sentry/react';

type Integration = Parameters<typeof addIntegration>[0];

type State = Record<string, Integration | undefined>;

const context = createContext<{
setState: React.Dispatch<React.SetStateAction<State>>;
state: State;
}>({
setState: () => {},
state: {},
});

export function AsyncSDKIntegrationContextProvider({
children,
}: {
children: React.ReactNode;
}) {
const [state, setState] = useState<State>({});

return <context.Provider value={{setState, state}}>{children}</context.Provider>;
}

export default function useAsyncSDKIntegrationStore() {
return useContext(context);
}
15 changes: 9 additions & 6 deletions static/app/views/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {useColorscheme} from 'sentry/utils/useColorscheme';
import {useHotkeys} from 'sentry/utils/useHotkeys';
import {useUser} from 'sentry/utils/useUser';
import type {InstallWizardProps} from 'sentry/views/admin/installWizard';
import {AsyncSDKIntegrationContextProvider} from 'sentry/views/app/asyncSDKIntegrationProvider';
import {OrganizationContextProvider} from 'sentry/views/organizationContext';

import SystemAlerts from './systemAlerts';
Expand Down Expand Up @@ -232,12 +233,14 @@ function App({children, params}: Props) {
return (
<Profiler id="App" onRender={onRenderCallback}>
<OrganizationContextProvider>
<MainContainer tabIndex={-1} ref={mainContainerRef}>
<GlobalModal onClose={handleModalClose} />
<SystemAlerts className="messages-container" />
<Indicators className="indicators-container" />
<ErrorBoundary>{renderBody()}</ErrorBoundary>
</MainContainer>
<AsyncSDKIntegrationContextProvider>
<MainContainer tabIndex={-1} ref={mainContainerRef}>
<GlobalModal onClose={handleModalClose} />
<SystemAlerts className="messages-container" />
<Indicators className="indicators-container" />
<ErrorBoundary>{renderBody()}</ErrorBoundary>
</MainContainer>
</AsyncSDKIntegrationContextProvider>
</OrganizationContextProvider>
</Profiler>
);
Expand Down

0 comments on commit 583ac6c

Please sign in to comment.