fix post visibility & optimization

This commit is contained in:
SrGooglo 2023-03-30 15:45:27 +00:00
parent 3b5b21fb19
commit 7c7fedd339
4 changed files with 86 additions and 44 deletions

View File

@ -35,6 +35,8 @@ export class PostsListsComponent extends React.Component {
resumingLoading: false, resumingLoading: false,
initialLoading: true, initialLoading: true,
topVisible: true,
realtimeUpdates: true, realtimeUpdates: true,
hasMore: true, hasMore: true,
@ -187,18 +189,38 @@ export class PostsListsComponent extends React.Component {
onScrollList = (e) => { onScrollList = (e) => {
const { scrollTop } = e.target const { scrollTop } = e.target
if (this.state.resumingLoading) {
return null
}
console.log("Scrolling => ", scrollTop) console.log("Scrolling => ", scrollTop)
if (scrollTop > 200) { if (scrollTop > 200) {
if (this.state.topVisible) {
this.setState({
topVisible: false,
})
if (typeof this.props.onTopVisibility === "function") {
this.props.onTopVisibility(false)
}
}
if (!this.props.realtime || this.state.resumingLoading) {
return null
}
this.setState({ this.setState({
realtimeUpdates: false, realtimeUpdates: false,
}) })
} else { } else {
if (!this.state.realtimeUpdates && !this.state.resumingLoading) { if (!this.state.topVisible) {
this.setState({
topVisible: true,
})
if (typeof this.props.onTopVisibility === "function") {
this.props.onTopVisibility(true)
}
}
if (this.props.realtime || !this.state.realtimeUpdates && !this.state.resumingLoading) {
this.onResumeRealtimeUpdates() this.onResumeRealtimeUpdates()
} }
} }
@ -227,11 +249,9 @@ export class PostsListsComponent extends React.Component {
} }
} }
if (this.props.realtime) {
if (this.listRef && this.listRef.current) { if (this.listRef && this.listRef.current) {
this.listRef.current.addEventListener("scroll", this.onScrollList) this.listRef.current.addEventListener("scroll", this.onScrollList)
} }
}
window._hacks = this._hacks window._hacks = this._hacks
} }
@ -251,11 +271,9 @@ export class PostsListsComponent extends React.Component {
} }
} }
if (this.props.realtime) {
if (this.listRef && this.listRef.current) { if (this.listRef && this.listRef.current) {
this.listRef.current.removeEventListener("scroll", this.onScrollList) this.listRef.current.removeEventListener("scroll", this.onScrollList)
} }
}
window._hacks = null window._hacks = null
} }

View File

@ -44,9 +44,7 @@ const TabRender = React.memo((props, ref) => {
// forwards ref to the tab // forwards ref to the tab
return <div className={classnames("fade-opacity-active", { "fade-opacity-leave": transitionActive })}> return <div className={classnames("fade-opacity-active", { "fade-opacity-leave": transitionActive })}>
{ {
React.createElement(Tab, { React.createElement(Tab, props)
...props,
})
} }
</div> </div>
}) })
@ -66,6 +64,8 @@ export default class Account extends React.Component {
isNotExistent: false, isNotExistent: false,
} }
profileRef = React.createRef()
contentRef = React.createRef() contentRef = React.createRef()
coverComponent = React.createRef() coverComponent = React.createRef()
@ -133,27 +133,15 @@ export default class Account extends React.Component {
isFollowed, isFollowed,
followers, followers,
}) })
}
// create intersection observer for cover onPostListTopVisibility = (to) => {
this.coverIntersectionObserver = new IntersectionObserver((e) => { console.log("onPostListTopVisibility", to)
if (e[0].intersectionRatio > 0) {
this.leftPanelRef.current.style.transform = "translate(0, -100px)" if (to) {
this.actionsRef.current.style.opacity = "1" this.profileRef.current.classList.remove("topHidden")
} else { } else {
this.leftPanelRef.current.style.transform = "translate(0, 0)" this.profileRef.current.classList.add("topHidden")
this.actionsRef.current.style.opacity = "0"
}
}, {
root: document.querySelector("#root"),
threshold: 0
})
this.coverIntersectionObserver.observe(this.coverComponent.current)
}
componentWillUnmount = () => {
if (this.coverIntersectionObserver) {
this.coverIntersectionObserver.disconnect()
} }
} }
@ -185,6 +173,8 @@ export default class Account extends React.Component {
return return
} }
this.onPostListTopVisibility(true)
key = key.toLowerCase() key = key.toLowerCase()
if (this.state.tabActiveKey === key) { if (this.state.tabActiveKey === key) {
@ -213,6 +203,7 @@ export default class Account extends React.Component {
} }
return <div return <div
ref={this.profileRef}
className="accountProfile" className="accountProfile"
id="profile" id="profile"
> >
@ -257,6 +248,7 @@ export default class Account extends React.Component {
<TabRender <TabRender
renderKey={this.state.tabActiveKey} renderKey={this.state.tabActiveKey}
state={this.state} state={this.state}
onTopVisibility={this.onPostListTopVisibility}
/> />
</div> </div>

View File

@ -8,6 +8,27 @@
width: 100%; width: 100%;
height: 100%; height: 100%;
&.topHidden {
.cover {
height: 0;
min-height: 0;
margin-bottom: 0;
outline: none;
}
.panels {
padding-top: 0;
.leftPanel {
transform: translate(0, 0);
.actions {
opacity: 0;
}
}
}
}
.cover { .cover {
z-index: 50; z-index: 50;
position: relative; position: relative;
@ -47,6 +68,8 @@
padding-top: 20px; padding-top: 20px;
height: 100%;
.leftPanel { .leftPanel {
position: sticky; position: sticky;
top: 0; top: 0;
@ -60,6 +83,8 @@
transition: all 0.3s ease-in-out; transition: all 0.3s ease-in-out;
transform: translate(0, -100px);
.userCard { .userCard {
position: sticky; position: sticky;
top: 0; top: 0;
@ -88,6 +113,14 @@
} }
} }
.content {
height: 100%;
.fade-opacity-active {
height: 100%;
}
}
.tabMenuWrapper { .tabMenuWrapper {
position: sticky; position: sticky;
top: 0; top: 0;

View File

@ -18,14 +18,13 @@ const emptyListRender = () => {
export default class UserPosts extends React.Component { export default class UserPosts extends React.Component {
render() { render() {
return <div className="userPosts"> return <PostsList
<PostsList onTopVisibility={this.props.onTopVisibility}
emptyListRender={emptyListRender} emptyListRender={emptyListRender}
loadFromModel={PostModel.getUserPosts} loadFromModel={PostModel.getUserPosts}
loadFromModelProps={{ loadFromModelProps={{
user_id: this.props.state.user._id, user_id: this.props.state.user._id,
}} }}
/> />
</div>
} }
} }