Skip to content

Commit 5555899

Browse files
fix(preview): add preview sdk load check (#4391)
* fix(preview): add preview sdk load check * Update src/elements/content-preview/__tests__/ContentPreview.test.js Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --------- Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 454ede0 commit 5555899

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

src/elements/content-preview/ContentPreview.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,8 @@ class ContentPreview extends React.PureComponent<Props, State> {
228228

229229
stagedFile: ?BoxItem;
230230

231+
previewLibraryLoaded: boolean = false;
232+
231233
updateVersionToCurrent: ?() => void;
232234

233235
dynamicOnPreviewLoadAction: ?() => void;
@@ -452,6 +454,13 @@ class ContentPreview extends React.PureComponent<Props, State> {
452454
const fileVersionId = getProp(file, 'file_version.id');
453455
let loadPreview = false;
454456

457+
// Check if preview library just became available and we haven't loaded preview yet
458+
// This handles cases where library loads asynchronously after file is already set
459+
if (!this.previewLibraryLoaded && this.isPreviewLibraryLoaded() && file && !this.preview) {
460+
this.previewLibraryLoaded = true;
461+
return true;
462+
}
463+
455464
if (selectedVersionId !== prevSelectedVersionId) {
456465
const isPreviousCurrent = fileVersionId === prevSelectedVersionId || !prevSelectedVersionId;
457466
const isSelectedCurrent = fileVersionId === selectedVersionId || !selectedVersionId;
@@ -808,8 +817,9 @@ class ContentPreview extends React.PureComponent<Props, State> {
808817
...rest
809818
}: Props = this.props;
810819
const { file, selectedVersion, startAt }: State = this.state;
820+
this.previewLibraryLoaded = this.isPreviewLibraryLoaded();
811821

812-
if (!this.isPreviewLibraryLoaded() || !file || !tokenOrTokenFunction) {
822+
if (!this.previewLibraryLoaded || !file || !tokenOrTokenFunction) {
813823
return;
814824
}
815825

src/elements/content-preview/__tests__/ContentPreview.test.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ describe('elements/content-preview/ContentPreview', () => {
175175

176176
expect(instance.shouldLoadPreview({ selectedVersion: { id: '1' } })).toBe(false);
177177
});
178+
179+
test("should return true if the preview library just became available and we haven't loaded preview yet", () => {
180+
instance.previewLibraryLoaded = false;
181+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
182+
instance.preview = undefined;
183+
expect(instance.shouldLoadPreview({ file })).toBe(true);
184+
expect(instance.previewLibraryLoaded).toBe(true);
185+
});
178186
});
179187

180188
describe('canDownload()', () => {
@@ -494,6 +502,35 @@ describe('elements/content-preview/ContentPreview', () => {
494502
}
495503
},
496504
);
505+
506+
test('should return if the preview library is not loaded', async () => {
507+
const wrapper = getWrapper(props);
508+
wrapper.setState({ file });
509+
const instance = wrapper.instance();
510+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(false);
511+
const spy = jest.spyOn(instance, 'getFileId');
512+
await instance.loadPreview();
513+
expect(spy).not.toHaveBeenCalled();
514+
});
515+
516+
test('should return early if the file is not set', async () => {
517+
const wrapper = getWrapper(props);
518+
const instance = wrapper.instance();
519+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
520+
const spy = jest.spyOn(instance, 'getFileId');
521+
await instance.loadPreview();
522+
expect(spy).not.toHaveBeenCalled();
523+
});
524+
525+
test('should return if the token is not set', async () => {
526+
const wrapper = getWrapper({ ...props, token: undefined });
527+
wrapper.setState({ file });
528+
const instance = wrapper.instance();
529+
instance.isPreviewLibraryLoaded = jest.fn().mockReturnValue(true);
530+
const spy = jest.spyOn(instance, 'getFileId');
531+
await instance.loadPreview();
532+
expect(spy).not.toHaveBeenCalled();
533+
});
497534
});
498535

499536
describe('fetchFile()', () => {

0 commit comments

Comments
 (0)