From 35c5608e1ac3d49c731bbb924fd1277107c52d4c Mon Sep 17 00:00:00 2001
From: mgabdev <>
Date: Fri, 5 Jun 2020 15:28:46 -0400
Subject: [PATCH] Updated HomePage sidebar panels to lazy load
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
• Updated:
- HomePage sidebar panels (WhoToFollowPanel, ListsPanel, GroupsPanel) to lazy load on scroll of 25px. Otherwise, dont load.
- WhoToFollowPanel, ListsPanel, GroupsPanel have functionality to load data once
• Added:
- List component has showLoading prop
• Removed:
- Unused IntersectionObserver code in HomePage
---
app/javascript/gabsocial/components/list.js | 5 +-
.../components/panel/groups_panel.js | 37 ++++++++------
.../gabsocial/components/panel/lists_panel.js | 39 ++++++++++++---
.../components/panel/who_to_follow_panel.js | 25 +++++++++-
app/javascript/gabsocial/pages/home_page.js | 50 ++++++++++++-------
5 files changed, 114 insertions(+), 42 deletions(-)
diff --git a/app/javascript/gabsocial/components/list.js b/app/javascript/gabsocial/components/list.js
index 1c3b5c47..8566a01e 100644
--- a/app/javascript/gabsocial/components/list.js
+++ b/app/javascript/gabsocial/components/list.js
@@ -21,6 +21,7 @@ export default class List extends ImmutablePureComponent {
]),
onLoadMore: PropTypes.func,
hasMore: PropTypes.bool,
+ showLoading: PropTypes.bool,
}
render() {
@@ -30,7 +31,8 @@ export default class List extends ImmutablePureComponent {
emptyMessage,
hasMore,
size,
- onLoadMore
+ onLoadMore,
+ showLoading,
} = this.props
const Wrapper = !!scrollKey ? ScrollableList : Dummy
@@ -42,6 +44,7 @@ export default class List extends ImmutablePureComponent {
hasMore={hasMore}
scrollKey={scrollKey}
emptyMessage={emptyMessage}
+ showLoading={showLoading}
>
{
items.map((item, i) => (
diff --git a/app/javascript/gabsocial/components/panel/groups_panel.js b/app/javascript/gabsocial/components/panel/groups_panel.js
index f583a711..75e8abaf 100644
--- a/app/javascript/gabsocial/components/panel/groups_panel.js
+++ b/app/javascript/gabsocial/components/panel/groups_panel.js
@@ -30,6 +30,7 @@ class GroupSidebarPanel extends ImmutablePureComponent {
isLazy: PropTypes.bool,
isSlim: PropTypes.bool,
onFetchGroups: PropTypes.func.isRequired,
+ shouldLoad: PropTypes.bool,
}
state = {
@@ -40,38 +41,39 @@ class GroupSidebarPanel extends ImmutablePureComponent {
'groupIds',
'isLazy',
'isSlim',
+ 'shouldLoad',
]
- componentDidMount() {
- if (!this.props.isLazy) {
- this.props.onFetchGroups('member')
- }
- }
-
static getDerivedStateFromProps(nextProps, prevState) {
- if (!nextProps.isHidden && nextProps.isIntersecting && !prevState.fetched) {
- return {
- fetched: true
- }
+ if (nextProps.shouldLoad && !prevState.fetched) {
+ return { fetched: true }
}
return null
}
componentDidUpdate(prevProps, prevState, snapshot) {
- if (!prevState.fetched && this.state.fetched && this.props.isLazy) {
+ if (!prevState.fetched && this.state.fetched) {
this.props.onFetchGroups('member')
}
}
+ componentDidMount() {
+ if (!this.props.isLazy) {
+ this.props.onFetchGroups('member')
+ this.setState({ fetched: true })
+ }
+ }
+
render() {
const { intl, groupIds, slim } = this.props
- const count = groupIds.count()
-
- if (count === 0) return null
+ const { fetched } = this.state
+ const count = !!groupIds ? groupIds.count() : 0
const maxCount = slim ? 12 : 6
+ if (count === 0 && fetched) return null
+
return (
maxCount ? '/groups/browse/member' : undefined}
noPadding={slim}
>
-
+
{
- groupIds.slice(0, maxCount).map((groupId, i) => (
+ groupIds && groupIds.slice(0, maxCount).map((groupId, i) => (
({
+ const listItems = !!lists && lists.slice(0, maxCount).map((list) => ({
to: `/lists/${list.get('id')}`,
title: list.get('title'),
}))
@@ -65,9 +87,14 @@ class ListsPanel extends ImmutablePureComponent {
noPadding
>
-
+
)
}
+
}
\ No newline at end of file
diff --git a/app/javascript/gabsocial/components/panel/who_to_follow_panel.js b/app/javascript/gabsocial/components/panel/who_to_follow_panel.js
index 6d2b6c27..ce19ea31 100644
--- a/app/javascript/gabsocial/components/panel/who_to_follow_panel.js
+++ b/app/javascript/gabsocial/components/panel/who_to_follow_panel.js
@@ -30,14 +30,37 @@ class WhoToFollowPanel extends ImmutablePureComponent {
fetchSuggestions: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
suggestions: ImmutablePropTypes.list.isRequired,
+ isLazy: PropTypes.bool,
+ }
+
+ state = {
+ fetched: !this.props.isLazy,
}
updateOnProps = [
'suggestions',
+ 'isLazy',
+ 'shouldLoad',
]
+ static getDerivedStateFromProps(nextProps, prevState) {
+ if (nextProps.shouldLoad && !prevState.fetched) {
+ return { fetched: true }
+ }
+
+ return null
+ }
+
+ componentDidUpdate(prevProps, prevState) {
+ if (!prevState.fetched && this.state.fetched) {
+ this.props.fetchSuggestions()
+ }
+ }
+
componentDidMount() {
- this.props.fetchSuggestions()
+ if (!this.props.isLazy) {
+ this.props.fetchSuggestions()
+ }
}
render() {
diff --git a/app/javascript/gabsocial/pages/home_page.js b/app/javascript/gabsocial/pages/home_page.js
index ea485147..64ba06f8 100644
--- a/app/javascript/gabsocial/pages/home_page.js
+++ b/app/javascript/gabsocial/pages/home_page.js
@@ -1,10 +1,9 @@
import { Fragment } from 'react'
+import throttle from 'lodash.throttle'
import { openModal } from '../actions/modal'
import { defineMessages, injectIntl } from 'react-intl'
import { MODAL_HOME_TIMELINE_SETTINGS } from '../constants'
import { me } from '../initial_state'
-// import IntersectionObserverArticle from '../components/intersection_observer_article'
-// import IntersectionObserverWrapper from '../features/ui/util/intersection_observer_wrapper'
import PageTitle from '../features/ui/util/page_title'
import GroupsPanel from '../components/panel/groups_panel'
import ListsPanel from '../components/panel/lists_panel'
@@ -47,23 +46,37 @@ class HomePage extends PureComponent {
isPro: PropTypes.bool,
}
- // intersectionObserverWrapper = new IntersectionObserverWrapper()
+ state = {
+ lazyLoaded: false,
+ }
- // componentDidMount() {
- // this.attachIntersectionObserver()
- // }
+ componentDidMount() {
+ this.window = window
+ this.documentElement = document.scrollingElement || document.documentElement
- // componentWillUnmount() {
- // this.detachIntersectionObserver()
- // }
+ this.window.addEventListener('scroll', this.handleScroll)
+ }
- // attachIntersectionObserver() {
- // this.intersectionObserverWrapper.connect()
- // }
+ componentWillUnmount() {
+ this.detachScrollListener()
+ }
- // detachIntersectionObserver() {
- // this.intersectionObserverWrapper.disconnect()
- // }
+ detachScrollListener = () => {
+ this.window.removeEventListener('scroll', this.handleScroll)
+ }
+
+ handleScroll = throttle(() => {
+ if (this.window) {
+ const { scrollTop } = this.documentElement
+
+ if (scrollTop > 25 && !this.state.lazyLoaded) {
+ this.setState({ lazyLoaded: true })
+ this.detachScrollListener()
+ }
+ }
+ }, 150, {
+ trailing: true,
+ })
render() {
const {
@@ -73,6 +86,7 @@ class HomePage extends PureComponent {
onOpenHomePageSettingsModal,
isPro,
} = this.props
+ const { lazyLoaded } = this.state
return (
-
-
-
+
+
+
)}