gab-social/app/javascript/gabsocial/containers/gabsocial.js

161 lines
4.6 KiB
JavaScript
Raw Permalink Normal View History

2020-04-30 05:34:50 +01:00
'use strict'
import React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2020-04-30 05:34:50 +01:00
import { Provider } from 'react-redux'
import configureStore from '../store/configureStore'
import { BrowserRouter, Route } from 'react-router-dom'
import moment from 'moment-mini'
2020-04-30 05:34:50 +01:00
import { ScrollContext } from 'react-router-scroll-4'
import { IntlProvider, addLocaleData } from 'react-intl'
import { fetchCustomEmojis } from '../actions/custom_emojis'
import { fetchPromotions } from '../actions/promotions'
2020-12-03 22:13:11 +00:00
import { fetchChatConversationUnreadCount } from '../actions/chat_conversations'
2020-04-30 05:34:50 +01:00
import { hydrateStore } from '../actions/store'
import { MIN_ACCOUNT_CREATED_AT_ONBOARDING } from '../constants'
import {
connectUserStream,
connectStatusUpdateStream,
connectChatMessagesStream,
2020-04-30 05:34:50 +01:00
} from '../actions/streaming'
import { saveWindowDimensions } from '../actions/settings'
2020-04-30 05:34:50 +01:00
import { getLocale } from '../locales'
import initialState from '../initial_state'
import { me, isFirstSession } from '../initial_state'
2020-04-30 05:34:50 +01:00
import UI from '../features/ui'
import IntroductionPage from '../pages/introduction_page'
2020-04-30 05:34:50 +01:00
import ErrorBoundary from '../components/error_boundary'
import Display from './display'
2019-07-02 08:10:25 +01:00
2020-04-30 05:34:50 +01:00
const { localeData, messages } = getLocale()
addLocaleData(localeData)
2019-07-02 08:10:25 +01:00
2020-04-30 05:34:50 +01:00
export const store = configureStore()
const hydrateAction = hydrateStore(initialState)
2019-07-02 08:10:25 +01:00
2020-04-30 05:34:50 +01:00
store.dispatch(hydrateAction)
store.dispatch(fetchCustomEmojis())
store.dispatch(fetchPromotions())
2020-12-03 22:13:11 +00:00
store.dispatch(fetchChatConversationUnreadCount())
store.dispatch(saveWindowDimensions())
2019-07-02 08:10:25 +01:00
const mapStateToProps = (state) => ({
accountCreatedAt: !!me ? state.getIn(['accounts', me, 'created_at']) : undefined,
shownOnboarding: state.getIn(['settings', 'shownOnboarding']),
})
@connect(mapStateToProps)
class GabSocialMount extends React.PureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
shownOnboarding: PropTypes.bool.isRequired,
accountCreatedAt: PropTypes.string,
}
state = {
shownOnboarding: this.props.shownOnboarding,
shouldShow: false,
}
componentDidMount() {
if (!!me && this.props.accountCreatedAt) {
//If first time opening app, and is new user, show onboarding
const accountCreatedAtValue = moment(this.props.accountCreatedAt).valueOf()
const shouldShow = isFirstSession && !this.state.shownOnboarding && accountCreatedAtValue > MIN_ACCOUNT_CREATED_AT_ONBOARDING
if (shouldShow) this.setState({ shouldShow })
}
this.handleResize()
window.addEventListener('resize', this.handleResize, false)
}
componentWillUnmount () {
window.removeEventListener('resize', this.handleResize, false)
}
handleResize = () => {
this.props.dispatch(saveWindowDimensions())
}
2019-07-02 08:10:25 +01:00
render () {
const { shownOnboarding, shouldShow } = this.state
if (!shownOnboarding && shouldShow) {
return (
<BrowserRouter>
<Route path='/' component={IntroductionPage} />
</BrowserRouter>
)
}
2019-07-02 08:10:25 +01:00
return (
<BrowserRouter>
<ScrollContext>
<Route path='/' component={UI} />
</ScrollContext>
</BrowserRouter>
2020-04-30 05:34:50 +01:00
)
2019-07-02 08:10:25 +01:00
}
}
export default class GabSocial extends React.PureComponent {
2019-07-02 08:10:25 +01:00
static propTypes = {
locale: PropTypes.string.isRequired,
2020-04-30 05:34:50 +01:00
}
2019-07-02 08:10:25 +01:00
componentDidMount() {
2020-04-11 23:29:19 +01:00
if (!!me) {
this.disconnect = store.dispatch(connectUserStream())
store.dispatch(connectStatusUpdateStream())
// store.dispatch(connectChatMessagesStream(me))
2020-04-11 23:29:19 +01:00
}
2020-05-04 19:44:37 +01:00
console.log('%cGab Social ', [
2020-05-04 19:44:37 +01:00
, 'color: #30CE7D'
, 'display: block'
, 'line-height: 80px'
, 'font-family: system-ui, -apple-system, BlinkMacSystemFont, Roboto, Ubuntu, "Helvetica Neue", sans-serif'
, 'font-size: 36px'
, 'text-align: left'
, 'font-weight: 800'
].join(';'))
console.log('%cThis is a browser feature intended for developers.\nIf someone told you to copy and paste something here it is a scam and will give them access to your Gab account. ', [
, 'color: #000'
, 'display: block'
, 'line-height: 30px'
, 'font-family: system-ui, -apple-system, BlinkMacSystemFont, Roboto, Ubuntu, "Helvetica Neue", sans-serif'
, 'font-size: 20px'
, 'text-align: left'
, 'font-weight: 600'
2020-05-04 19:44:37 +01:00
].join(';'))
2019-07-02 08:10:25 +01:00
}
componentWillUnmount () {
if (this.disconnect) {
2020-04-30 05:34:50 +01:00
this.disconnect()
this.disconnect = null
2019-07-02 08:10:25 +01:00
}
}
render () {
2020-04-30 05:34:50 +01:00
const { locale } = this.props
2019-07-02 08:10:25 +01:00
return (
<IntlProvider locale={locale} messages={messages}>
<Provider store={store}>
2020-04-30 05:34:50 +01:00
<Display>
<ErrorBoundary>
<GabSocialMount />
</ErrorBoundary>
</Display>
2019-07-02 08:10:25 +01:00
</Provider>
</IntlProvider>
2020-04-30 05:34:50 +01:00
)
2019-07-02 08:10:25 +01:00
}
}