Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(content-answers): lazy load content answers in preview #3720

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/elements/common/content-answers/ContentAnswers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ import getProp from 'lodash/get';
import ContentAnswersModal, { ExternalProps as ContentAnswersModalExternalProps } from './ContentAnswersModal';
import ContentAnswersOpenButton from './ContentAnswersOpenButton';
// @ts-ignore: no ts definition
// eslint-disable-next-line import/named
import { BoxItem } from '../../common/types/core';

interface ExternalProps extends ContentAnswersModalExternalProps {
show?: boolean;
}

interface Props {
className?: string;
file: BoxItem;
}

const ContentAnswers = ({ file, onAsk, onRequestClose, ...rest }: ContentAnswersModalExternalProps & Props) => {
const ContentAnswers = (props: ContentAnswersModalExternalProps & Props) => {
const { className = '', file, onAsk, onRequestClose, ...rest } = props;

const [isModalOpen, setIsModalOpen] = useState(false);
const [hasQuestions, setHasQuestions] = useState(false);
const [isHighlighted, setIsHighlighted] = useState(false);
Expand Down Expand Up @@ -44,7 +46,7 @@ const ContentAnswers = ({ file, onAsk, onRequestClose, ...rest }: ContentAnswers

const currentExtension = getProp(file, 'extension');
return (
<div className="be-ContentAnswers">
<div className={`be-ContentAnswers ${className}`}>
<ContentAnswersOpenButton
fileExtension={currentExtension}
isHighlighted={isHighlighted}
Expand Down
2 changes: 0 additions & 2 deletions src/elements/common/content-answers/ContentAnswersModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,10 @@ import { DOCUMENT_SUGGESTED_QUESTIONS, SPREADSHEET_FILE_EXTENSIONS } from './con
import withCurrentUser from '../current-user';

// @ts-ignore: no ts definition
// eslint-disable-next-line import/named
import { BoxItem, User } from '../../../common/types/core';
// @ts-ignore: no ts definition
import APIFactory from '../../../api';
// @ts-ignore: no ts definition
// eslint-disable-next-line import/named
import { ElementsXhrError } from '../../common/types/api';

import messages from './messages';
Expand Down
14 changes: 11 additions & 3 deletions src/elements/content-preview/preview-header/PreviewHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import type { IntlShape } from 'react-intl';
import classNames from 'classnames';
import getProp from 'lodash/get';
import AsyncLoad from '../../common/async-load';
// $FlowFixMe typescript component
import ContentAnswers from '../../common/content-answers';
import FileInfo from './FileInfo';
import IconClose from '../../../icons/general/IconClose';
import IconDownload from '../../../icons/general/IconDownloadSolid';
Expand Down Expand Up @@ -42,6 +40,10 @@ type Props = {
token: ?string,
};

const LoadableContentAnswers = AsyncLoad({
// $FlowFixMe TypeScript component
loader: () => import(/* webpackMode: "lazy", webpackChunkName: "content-answers" */ '../../common/content-answers'),
});
const LoadableContentOpenWith = AsyncLoad({
loader: () => import(/* webpackMode: "lazy", webpackChunkName: "content-open-with" */ '../../content-open-with'),
});
Expand Down Expand Up @@ -101,7 +103,13 @@ const PreviewHeader = ({
{...contentOpenWithProps}
/>
)}
{shouldRenderAnswers && <ContentAnswers file={file} {...contentAnswersProps} />}
{shouldRenderAnswers && (
<LoadableContentAnswers
className="bcpr-PreviewHeader-contentAnswers"
file={file}
{...contentAnswersProps}
/>
)}
{canAnnotate && (
<>
<PlainButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@
.bcpr-PreviewHeader-controls {
display: flex;

.bcpr-PreviewHeader-contentAnswers {
display: flex;
align-items: center;
padding: 0 5px;
}
Comment on lines +92 to +96
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is vertical alignment issue when used alongside OpenWith

Before
Screenshot 2024-10-18 at 11 21 10 AM

After
Screenshot 2024-10-18 at 11 20 34 AM

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤨


.be & .bcpr-bcow-btn {
margin: 0 10px 0 0;
padding: 0 5px;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as React from 'react';
import { shallow } from 'enzyme';

import ContentAnswers from '../../../common/content-answers';
import PreviewHeader from '..';

describe('elements/content-preview/preview-header/PreviewHeader', () => {
Expand Down Expand Up @@ -31,7 +29,7 @@ describe('elements/content-preview/preview-header/PreviewHeader', () => {
const contentAnswersProps = { show };
const wrapper = getWrapper({ contentAnswersProps, file });

expect(wrapper.exists(ContentAnswers)).toBe(expected);
expect(wrapper.exists('.bcpr-PreviewHeader-contentAnswers')).toBe(expected);
Copy link
Contributor Author

@tjuanitas tjuanitas Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the component is now lazy loaded, so use classname to target since the component is now labeled as ForwardRef in tree

});

it('should render ContentAnswers correctly and provided the correct props', () => {
Expand All @@ -41,7 +39,7 @@ describe('elements/content-preview/preview-header/PreviewHeader', () => {
contentAnswersProps,
file,
});
expect(wrapper.find(ContentAnswers).prop('file')).toBe(file);
expect(wrapper.find('.bcpr-PreviewHeader-contentAnswers').prop('file')).toBe(file);
});

test.each([
Expand Down
Loading