diff --git a/web/src/components/Memo.tsx b/web/src/components/Memo.tsx index 0a94a983..c4ce54a6 100644 --- a/web/src/components/Memo.tsx +++ b/web/src/components/Memo.tsx @@ -27,10 +27,11 @@ interface Props { showFull?: boolean; showVisibility?: boolean; showRelatedMemos?: boolean; + lazyRendering?: boolean; } const Memo: React.FC = (props: Props) => { - const { memo, showCreator, showFull, showVisibility, showRelatedMemos } = props; + const { memo, showCreator, showFull, showVisibility, showRelatedMemos, lazyRendering } = props; const { i18n } = useTranslation(); const t = useTranslate(); const filterStore = useFilterStore(); @@ -38,6 +39,7 @@ const Memo: React.FC = (props: Props) => { const memoStore = useMemoStore(); const memoCacheStore = useMemoCacheStore(); const userV1Store = useUserV1Store(); + const [shouldRender, setShouldRender] = useState(lazyRendering ? false : true); const [createdTimeStr, setCreatedTimeStr] = useState(getRelativeTimeString(memo.displayTs)); const [relatedMemoList, setRelatedMemoList] = useState([]); const memoContainerRef = useRef(null); @@ -77,6 +79,29 @@ const Memo: React.FC = (props: Props) => { }; }, [i18n.language]); + // Lazy rendering. + useEffect(() => { + if (shouldRender) { + return; + } + + const root = document.body.querySelector("#root"); + if (root) { + const checkShouldRender = () => { + if (root.scrollTop + window.innerHeight > (memoContainerRef.current?.offsetTop || 0)) { + setShouldRender(true); + root.removeEventListener("scroll", checkShouldRender); + return true; + } + }; + + if (checkShouldRender()) { + return; + } + root.addEventListener("scroll", checkShouldRender); + } + }, [lazyRendering]); + const handleTogglePinMemoBtnClick = async () => { try { if (memo.pinned) { @@ -221,6 +246,15 @@ const Memo: React.FC = (props: Props) => { } }; + if (!shouldRender) { + return ( +
+ ); + } + return ( <>
diff --git a/web/src/components/MemoList.tsx b/web/src/components/MemoList.tsx index e26c5c68..940d9d86 100644 --- a/web/src/components/MemoList.tsx +++ b/web/src/components/MemoList.tsx @@ -149,7 +149,7 @@ const MemoList: React.FC = (props: Props) => { return (
{sortedMemos.map((memo) => ( - + ))} {isFetching ? (