Skip to content

Commit 67726bb

Browse files
authored
feat(ui): Remove lazy renderer component prop (#69823)
1 parent eab0711 commit 67726bb

File tree

4 files changed

+32
-50
lines changed

4 files changed

+32
-50
lines changed

static/app/components/events/eventReplay/index.tsx

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Fragment, useCallback} from 'react';
1+
import {Fragment, lazy} from 'react';
22
import ReactLazyLoad from 'react-lazyload';
33
import styled from '@emotion/styled';
44

@@ -36,6 +36,10 @@ export const REPLAY_CLIP_OFFSETS = {
3636
durationBeforeMs: 5_000,
3737
};
3838

39+
const ReplayOnboardingPanel = lazy(() => import('./replayInlineOnboardingPanel'));
40+
const ReplayPreview = lazy(() => import('./replayPreview'));
41+
const ReplayClipPreview = lazy(() => import('./replayClipPreview'));
42+
3943
function EventReplayContent({
4044
event,
4145
group,
@@ -46,13 +50,6 @@ function EventReplayContent({
4650
const router = useRouter();
4751
const {getReplayCountForIssue} = useReplayCountForIssues();
4852

49-
const replayOnboardingPanel = useCallback(
50-
() => import('./replayInlineOnboardingPanel'),
51-
[]
52-
);
53-
const replayPreview = useCallback(() => import('./replayPreview'), []);
54-
const replayClipPreview = useCallback(() => import('./replayClipPreview'), []);
55-
5653
const hasReplayClipFeature = organization.features.includes(
5754
'issue-details-inline-replay-viewer'
5855
);
@@ -69,7 +66,7 @@ function EventReplayContent({
6966
return (
7067
<ErrorBoundary mini>
7168
<LazyLoad
72-
component={replayOnboardingPanel}
69+
LazyComponent={ReplayOnboardingPanel}
7370
platform={platform}
7471
projectId={projectId}
7572
/>
@@ -148,12 +145,12 @@ function EventReplayContent({
148145
{hasReplayClipFeature ? (
149146
<LazyLoad
150147
{...commonProps}
151-
component={replayClipPreview}
148+
LazyComponent={ReplayClipPreview}
152149
clipOffsets={REPLAY_CLIP_OFFSETS}
153150
overlayContent={overlayContent}
154151
/>
155152
) : (
156-
<LazyLoad {...commonProps} component={replayPreview} />
153+
<LazyLoad {...commonProps} LazyComponent={ReplayPreview} />
157154
)}
158155
</ReactLazyLoad>
159156
</ReplayGroupContextProvider>

static/app/components/lazyLoad.spec.tsx

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import {lazy} from 'react';
2+
13
import {render, screen} from 'sentry-test/reactTestingLibrary';
24

35
import LazyLoad from 'sentry/components/lazyLoad';
@@ -15,7 +17,6 @@ function BarComponent({}: TestProps) {
1517
}
1618

1719
type ResolvedComponent = {default: React.ComponentType<TestProps>};
18-
type GetComponent = () => Promise<ResolvedComponent>;
1920

2021
describe('LazyLoad', function () {
2122
afterEach(() => {
@@ -25,7 +26,7 @@ describe('LazyLoad', function () {
2526
it('renders with a loading indicator when promise is not resolved yet', function () {
2627
const importTest = new Promise<ResolvedComponent>(() => {});
2728
const getComponent = () => importTest;
28-
render(<LazyLoad component={getComponent} />);
29+
render(<LazyLoad LazyComponent={lazy(getComponent)} />);
2930

3031
// Should be loading
3132
expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
@@ -37,7 +38,7 @@ describe('LazyLoad', function () {
3738
doResolve = resolve;
3839
});
3940

40-
render(<LazyLoad component={() => importFoo} />);
41+
render(<LazyLoad LazyComponent={lazy(() => importFoo)} />);
4142

4243
// Should be loading
4344
expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
@@ -58,7 +59,7 @@ describe('LazyLoad', function () {
5859
);
5960

6061
try {
61-
render(<LazyLoad component={getComponent} />);
62+
render(<LazyLoad LazyComponent={lazy(getComponent)} />);
6263
} catch (err) {
6364
// ignore
6465
}
@@ -78,7 +79,7 @@ describe('LazyLoad', function () {
7879
});
7980

8081
// First render Foo
81-
const {rerender} = render(<LazyLoad component={() => importFoo} />);
82+
const {rerender} = render(<LazyLoad LazyComponent={lazy(() => importFoo)} />);
8283
expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
8384

8485
// resolve with foo
@@ -90,22 +91,21 @@ describe('LazyLoad', function () {
9091
doResolve = resolve;
9192
});
9293

93-
rerender(<LazyLoad component={() => importBar} />);
94+
rerender(<LazyLoad LazyComponent={lazy(() => importBar)} />);
9495
expect(screen.getByTestId('loading-indicator')).toBeInTheDocument();
9596

9697
// resolve with bar
9798
doResolve!({default: BarComponent});
9899
expect(await screen.findByText('my bar component')).toBeInTheDocument();
99100

100101
// Update component prop to a mock to make sure it isn't re-called
101-
const getComponent2: GetComponent = jest.fn(
102-
() => new Promise<ResolvedComponent>(() => {})
103-
);
104-
rerender(<LazyLoad component={getComponent2} />);
102+
const getComponent2 = jest.fn(() => new Promise<ResolvedComponent>(() => {}));
103+
const LazyGet = lazy(getComponent2);
104+
rerender(<LazyLoad LazyComponent={LazyGet} />);
105105
expect(getComponent2).toHaveBeenCalledTimes(1);
106106

107107
// Does not refetch on other prop changes
108-
rerender(<LazyLoad component={getComponent2} testProp />);
108+
rerender(<LazyLoad LazyComponent={LazyGet} testProp />);
109109
expect(getComponent2).toHaveBeenCalledTimes(1);
110110
});
111111
});

static/app/components/lazyLoad.tsx

+9-27
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,19 @@
11
import type {ErrorInfo} from 'react';
2-
import {Component, lazy, Suspense, useMemo} from 'react';
2+
import {Component, Suspense} from 'react';
33
import styled from '@emotion/styled';
44
import * as Sentry from '@sentry/react';
55

66
import LoadingError from 'sentry/components/loadingError';
77
import LoadingIndicator from 'sentry/components/loadingIndicator';
88
import {t} from 'sentry/locale';
99
import {isWebpackChunkLoadingError} from 'sentry/utils';
10-
import retryableImport from 'sentry/utils/retryableImport';
11-
12-
type PromisedImport<C> = Promise<{default: C}>;
13-
14-
type ComponentType = React.ComponentType<any>;
15-
16-
type Props<C extends ComponentType> = React.ComponentProps<C> & {
17-
/**
18-
* Wrap the component with lazy() before passing it to LazyLoad.
19-
*/
20-
LazyComponent?: React.LazyExoticComponent<C>;
2110

11+
type Props<C extends React.LazyExoticComponent<C>> = React.ComponentProps<C> & {
2212
/**
23-
* Accepts a function to trigger the import resolution of the component.
24-
* @deprecated Use `LazyComponent` instead and keep lazy() calls out of the render path.
13+
* Wrap the component with `lazy()` before passing it to LazyLoad.
14+
* This should be declared outside of the render funciton.
2515
*/
26-
component?: () => PromisedImport<C>;
16+
LazyComponent: C;
2717

2818
/**
2919
* Override the default fallback component.
@@ -42,20 +32,11 @@ type Props<C extends ComponentType> = React.ComponentProps<C> & {
4232
*
4333
* <LazyLoad LazyComponent={LazyComponent} someComponentProps={...} />
4434
*/
45-
function LazyLoad<C extends ComponentType>({
46-
component,
47-
loadingFallback,
35+
function LazyLoad<C extends React.LazyExoticComponent<any>>({
4836
LazyComponent,
37+
loadingFallback,
4938
...props
5039
}: Props<C>) {
51-
const LazyLoadedComponent = useMemo(() => {
52-
if (LazyComponent) {
53-
return LazyComponent;
54-
}
55-
56-
return lazy<C>(() => retryableImport(component));
57-
}, [component, LazyComponent]);
58-
5940
return (
6041
<ErrorBoundary>
6142
<Suspense
@@ -67,7 +48,8 @@ function LazyLoad<C extends ComponentType>({
6748
)
6849
}
6950
>
70-
<LazyLoadedComponent {...(props as React.ComponentProps<C>)} />
51+
{/* Props are strongly typed when passed in, but seem to conflict with LazyExoticComponent */}
52+
<LazyComponent {...(props as any)} />
7153
</Suspense>
7254
</ErrorBoundary>
7355
);

static/app/routes.tsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@ import {IndexRoute, Route} from './components/route';
2828

2929
const hook = (name: HookName) => HookStore.get(name).map(cb => cb());
3030

31-
const SafeLazyLoad = errorHandler(LazyLoad);
31+
// LazyExoticComponent Props get crazy when wrapped in an additional layer
32+
const SafeLazyLoad = errorHandler(LazyLoad) as unknown as React.ComponentType<
33+
typeof LazyLoad
34+
>;
3235

3336
// NOTE: makeLazyloadComponent is exported for use in the sentry.io (getsentry)
3437
// pirvate routing tree.

0 commit comments

Comments
 (0)