Skip to content

Make annotations list lazy load #156

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"queue": "^6.0.2",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-intersection-observer": "^9.15.1",
"react-intl": "^7.0.1",
"sorted-btree": "^1.8.1"
},
Expand Down
13 changes: 13 additions & 0 deletions src/common/components/common/preview.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useContext, useRef } from 'react';
import { FormattedMessage, useIntl } from 'react-intl';
import { useInView } from 'react-intersection-observer';
import cx from 'classnames';
import Editor from './editor';
import ExpandableEditor from './expandable-editor';
Expand Down Expand Up @@ -138,6 +139,17 @@ export function SidebarPreview(props) {
const { platform } = useContext(ReaderContext);
const lastImageRef = useRef();

// Set up intersection observer
const { ref, inView } = useInView({
triggerOnce: true, // Only trigger once when the element comes into view.
threshold: 0, // As soon as one pixel is visible.
});

// If the component is not in view on mount, render a placeholder.
if (!inView) {
return <div ref={ref} style={{ minHeight: '100px' }} />;
}

// Store and render the last image to avoid flickering when annotation manager removes
// old image, but the new one isn't generated yet
if (props.annotation.image) {
Expand Down Expand Up @@ -287,6 +299,7 @@ export function SidebarPreview(props) {

return (
<div
ref={ref}
onContextMenu={handleContextMenu}
className={cx('preview', {
'read-only': props.readOnly, ...expandedState
Expand Down