Skip to content

Commit 69be418

Browse files
authored
feat(content-sidebar): add ability to defer sidebar data fetching requests (#4410)
* feat(content-sidebar): add ability to defer sidebar data fetching requests * fix: address comments * fix: address comments * fix: address comments * fix: address comments
1 parent 02bc171 commit 69be418

File tree

5 files changed

+89
-2
lines changed

5 files changed

+89
-2
lines changed

src/elements/content-sidebar/ActivitySidebar.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ type ExternalProps = {
9999
onTaskUpdate: () => any,
100100
onTaskView: (id: string, isCreator: boolean) => any,
101101
routerDisabled?: boolean,
102+
/** When true, enables data fetching. When false, defers data fetching. Used to prioritize preview loading. */
103+
shouldFetchSidebarData?: boolean,
102104
} & ErrorContextProps &
103105
WithAnnotatorContextProps;
104106

@@ -185,7 +187,20 @@ class ActivitySidebar extends React.PureComponent<Props, State> {
185187
}
186188

187189
componentDidMount() {
188-
this.fetchFeedItems(true);
190+
const { shouldFetchSidebarData = true } = this.props;
191+
if (shouldFetchSidebarData) {
192+
this.fetchFeedItems(true);
193+
}
194+
}
195+
196+
componentDidUpdate(prevProps: Props) {
197+
const { shouldFetchSidebarData = true } = this.props;
198+
const { shouldFetchSidebarData: prevShouldFetchSidebarData = true } = prevProps;
199+
200+
// Fetch when fetch is enabled
201+
if (!prevShouldFetchSidebarData && shouldFetchSidebarData) {
202+
this.fetchFeedItems(true);
203+
}
189204
}
190205

191206
handleAnnotationDelete = ({ id, permissions }: { id: string, permissions: AnnotationPermission }) => {

src/elements/content-sidebar/ContentSidebar.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ type Props = {
9494
responseInterceptor?: Function,
9595
sharedLink?: string,
9696
sharedLinkPassword?: string,
97-
theme?: Theme,
97+
/** When true, enables data fetching. When false, defers data fetching. Used to prioritize preview loading. */
98+
shouldFetchSidebarData?: boolean,
9899
signSidebarProps: SignSidebarProps,
100+
theme?: Theme,
99101
token: Token,
100102
versionsSidebarProps: VersionsSidebarProps,
101103
} & ErrorContextProps &
@@ -355,6 +357,7 @@ class ContentSidebar extends React.Component<Props, State> {
355357
className,
356358
currentUser,
357359
defaultView,
360+
shouldFetchSidebarData,
358361
detailsSidebarProps,
359362
docGenSidebarProps,
360363
features,
@@ -399,6 +402,7 @@ class ContentSidebar extends React.Component<Props, State> {
399402
boxAISidebarProps={boxAISidebarProps}
400403
className={className}
401404
currentUser={currentUser}
405+
shouldFetchSidebarData={shouldFetchSidebarData}
402406
detailsSidebarProps={detailsSidebarProps}
403407
docGenSidebarProps={docGenSidebarProps}
404408
file={file}

src/elements/content-sidebar/Sidebar.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ type Props = {
7070
onPanelChange?: (name: string, isInitialState: boolean) => void,
7171
onVersionChange?: Function,
7272
onVersionHistoryClick?: Function,
73+
/** When true, enables data fetching. When false, defers data fetching. Used to prioritize preview loading. */
74+
shouldFetchSidebarData?: boolean,
7375
signSidebarProps: SignSidebarProps,
7476
theme?: Theme,
7577
versionsSidebarProps: VersionsSidebarProps,
@@ -297,6 +299,7 @@ class Sidebar extends React.Component<Props, State> {
297299
className,
298300
currentUser,
299301
currentUserError,
302+
shouldFetchSidebarData,
300303
detailsSidebarProps,
301304
docGenSidebarProps,
302305
file,
@@ -359,6 +362,7 @@ class Sidebar extends React.Component<Props, State> {
359362
boxAISidebarProps={boxAISidebarProps}
360363
currentUser={currentUser}
361364
currentUserError={currentUserError}
365+
shouldFetchSidebarData={shouldFetchSidebarData}
362366
elementId={this.id}
363367
defaultPanel={defaultPanel}
364368
detailsSidebarProps={detailsSidebarProps}

src/elements/content-sidebar/SidebarPanels.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ type Props = {
7171
onPanelChange?: (name: string, isInitialState?: boolean) => void,
7272
onVersionChange?: Function,
7373
onVersionHistoryClick?: Function,
74+
/** When true, enables data fetching. When false, defers data fetching. Used to prioritize preview loading. */
75+
shouldFetchSidebarData?: boolean,
7476
versionsSidebarProps: VersionsSidebarProps,
7577
};
7678

@@ -219,6 +221,7 @@ class SidebarPanels extends React.Component<Props, State> {
219221
currentUser,
220222
currentUserError,
221223
defaultPanel = '',
224+
shouldFetchSidebarData,
222225
detailsSidebarProps,
223226
docGenSidebarProps,
224227
elementId,
@@ -329,6 +332,7 @@ class SidebarPanels extends React.Component<Props, State> {
329332
this.handlePanelRender(SIDEBAR_VIEW_ACTIVITY);
330333
return (
331334
<LoadableActivitySidebar
335+
shouldFetchSidebarData={shouldFetchSidebarData}
332336
elementId={elementId}
333337
currentUser={currentUser}
334338
currentUserError={currentUserError}

src/elements/content-sidebar/__tests__/ActivitySidebar.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,66 @@ describe('elements/content-sidebar/ActivitySidebar', () => {
110110
test('should fetch the file and refresh the cache and fetch the current user', () => {
111111
expect(instance.fetchFeedItems).toHaveBeenCalledWith(true);
112112
});
113+
114+
test('should not fetch feed items when shouldFetchSidebarData is false', () => {
115+
jest.restoreAllMocks();
116+
jest.spyOn(ActivitySidebarComponent.prototype, 'fetchFeedItems');
117+
118+
getWrapper({ shouldFetchSidebarData: false });
119+
120+
expect(ActivitySidebarComponent.prototype.fetchFeedItems).not.toHaveBeenCalled();
121+
});
122+
123+
test('should fetch feed items when shouldFetchSidebarData is true', () => {
124+
jest.restoreAllMocks();
125+
jest.spyOn(ActivitySidebarComponent.prototype, 'fetchFeedItems');
126+
127+
getWrapper({ shouldFetchSidebarData: true });
128+
129+
expect(ActivitySidebarComponent.prototype.fetchFeedItems).toHaveBeenCalledWith(true);
130+
});
131+
});
132+
133+
describe('componentDidUpdate()', () => {
134+
test('should fetch feed items when shouldFetchSidebarData changes from false to true', () => {
135+
const wrapper = getWrapper({ shouldFetchSidebarData: false });
136+
const instance = wrapper.instance();
137+
instance.fetchFeedItems = jest.fn();
138+
139+
wrapper.setProps({ shouldFetchSidebarData: true });
140+
141+
expect(instance.fetchFeedItems).toHaveBeenCalledWith(true);
142+
});
143+
144+
test('should not fetch feed items when shouldFetchSidebarData remains false', () => {
145+
const wrapper = getWrapper({ shouldFetchSidebarData: false });
146+
const instance = wrapper.instance();
147+
instance.fetchFeedItems = jest.fn();
148+
149+
wrapper.setProps({ shouldFetchSidebarData: false });
150+
151+
expect(instance.fetchFeedItems).not.toHaveBeenCalled();
152+
});
153+
154+
test('should not fetch feed items when shouldFetchSidebarData remains true', () => {
155+
const wrapper = getWrapper({ shouldFetchSidebarData: true });
156+
const instance = wrapper.instance();
157+
instance.fetchFeedItems = jest.fn();
158+
159+
wrapper.setProps({ shouldFetchSidebarData: true });
160+
161+
expect(instance.fetchFeedItems).not.toHaveBeenCalled();
162+
});
163+
164+
test('should not fetch feed items when shouldFetchSidebarData changes from true to false', () => {
165+
const wrapper = getWrapper({ shouldFetchSidebarData: true });
166+
const instance = wrapper.instance();
167+
instance.fetchFeedItems = jest.fn();
168+
169+
wrapper.setProps({ shouldFetchSidebarData: false });
170+
171+
expect(instance.fetchFeedItems).not.toHaveBeenCalled();
172+
});
113173
});
114174

115175
describe('render()', () => {

0 commit comments

Comments
 (0)