gab-social/app/javascript/gabsocial/pages/home_page.js

147 lines
4.0 KiB
JavaScript
Raw Normal View History

import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import throttle from 'lodash.throttle'
2020-04-16 07:00:43 +01:00
import { defineMessages, injectIntl } from 'react-intl'
2020-11-15 18:48:32 +00:00
import { openModal } from '../actions/modal'
import {
MODAL_HOME_TIMELINE_SETTINGS,
LAZY_LOAD_SCROLL_OFFSET,
} from '../constants'
2020-05-14 07:03:22 +01:00
import { me } from '../initial_state'
2020-04-11 23:29:19 +01:00
import PageTitle from '../features/ui/util/page_title'
2020-02-24 21:56:07 +00:00
import DefaultLayout from '../layouts/default_layout'
2020-02-08 06:12:01 +00:00
import TimelineComposeBlock from '../components/timeline_compose_block'
2020-11-15 18:48:32 +00:00
import TabBar from '../components/tab_bar'
import WelcomeReminders from '../components/welcome_reminders'
import WrappedBundle from '../features/ui/util/wrapped_bundle'
import {
UserPanel,
GroupsPanel,
LinkFooter,
ListsPanel,
TrendsBreakingPanel,
UserSuggestionsPanel,
ProPanel,
ShopPanel,
ProgressPanel,
} from '../features/ui/util/async_components'
class HomePage extends React.PureComponent {
2020-03-24 04:39:12 +00:00
state = {
lazyLoaded: false,
}
2020-04-28 06:33:58 +01:00
componentDidMount() {
this.window = window
this.documentElement = document.scrollingElement || document.documentElement
2020-04-28 06:33:58 +01:00
this.window.addEventListener('scroll', this.handleScroll)
}
2020-04-28 06:33:58 +01:00
componentWillUnmount() {
this.detachScrollListener()
}
detachScrollListener = () => {
this.window.removeEventListener('scroll', this.handleScroll)
}
2020-04-28 06:33:58 +01:00
handleScroll = throttle(() => {
if (this.window) {
const { scrollTop } = this.documentElement
2020-11-15 18:48:32 +00:00
if (scrollTop > LAZY_LOAD_SCROLL_OFFSET && !this.state.lazyLoaded) {
this.setState({ lazyLoaded: true })
this.detachScrollListener()
}
}
}, 150, {
trailing: true,
})
2020-04-28 06:33:58 +01:00
onOpenHomePageSettingsModal = () => {
this.props.dispatch(openModal(MODAL_HOME_TIMELINE_SETTINGS))
}
2020-02-08 06:12:01 +00:00
render() {
2020-04-16 07:00:43 +01:00
const {
children,
intl,
2020-05-14 07:03:22 +01:00
isPro,
totalQueuedItemsCount,
2020-12-03 22:13:11 +00:00
unreadChatsCount,
2020-04-16 07:00:43 +01:00
} = this.props
const { lazyLoaded } = this.state
const title = intl.formatMessage(messages.home)
return (
2020-02-17 17:50:29 +00:00
<DefaultLayout
page='home'
title={title}
2020-02-19 23:57:07 +00:00
actions={[
{
icon: 'tv',
href: 'https://tv.gab.com',
},
2020-12-03 22:13:11 +00:00
{
icon: 'chat',
to: '/messages',
count: unreadChatsCount,
},
2020-05-07 00:40:54 +01:00
{
icon: 'search',
to: '/search',
2020-05-07 00:40:54 +01:00
},
2020-02-19 23:57:07 +00:00
]}
layout={[
UserPanel,
ProgressPanel,
<WrappedBundle component={ProPanel} componentParams={{ isPro: isPro }} />,
2020-12-09 20:02:43 +00:00
<WrappedBundle component={TrendsBreakingPanel} />,
<WrappedBundle component={ShopPanel} componentParams={{ isLazy: true, shouldLoad: lazyLoaded }} />,
// <WrappedBundle component={ListsPanel} componentParams={{ isLazy: true, shouldLoad: lazyLoaded }} />,
// <WrappedBundle component={UserSuggestionsPanel} componentParams={{ isLazy: true, shouldLoad: lazyLoaded }} />,
// <WrappedBundle component={GroupsPanel} componentParams={{ isLazy: true, shouldLoad: lazyLoaded, groupType: 'member' }} />,
LinkFooter,
]}
>
2020-05-03 06:22:49 +01:00
2020-04-16 07:00:43 +01:00
<PageTitle
path={title}
2020-04-16 07:00:43 +01:00
badge={totalQueuedItemsCount}
/>
2020-05-03 06:22:49 +01:00
2020-11-15 18:48:32 +00:00
<TimelineComposeBlock autoFocus={false} />
<WelcomeReminders />
2020-02-08 06:12:01 +00:00
{children}
2020-11-15 18:48:32 +00:00
2020-02-17 17:50:29 +00:00
</DefaultLayout>
)
}
}
const messages = defineMessages({
home: { id: 'home', defaultMessage: 'Home' },
})
const mapStateToProps = (state) => ({
totalQueuedItemsCount: state.getIn(['timelines', 'home', 'totalQueuedItemsCount']),
2020-12-03 22:13:11 +00:00
unreadChatsCount: state.getIn(['chats', 'chatsUnreadCount']),
isPro: state.getIn(['accounts', me, 'is_pro']),
})
HomePage.propTypes = {
children: PropTypes.node.isRequired,
dispatch: PropTypes.func.isRequired,
intl: PropTypes.object.isRequired,
isPro: PropTypes.bool,
2020-12-03 22:13:11 +00:00
unreadChatsCount: PropTypes.number.isRequired,
totalQueuedItemsCount: PropTypes.number.isRequired,
}
export default injectIntl(connect(mapStateToProps)(HomePage))