Move connectUserStream to UI component

This commit is contained in:
Alex Gleason 2020-04-07 20:11:37 -05:00
parent 3f041a53eb
commit e003e084e5
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
2 changed files with 30 additions and 27 deletions

View file

@ -11,7 +11,6 @@ import UI from '../features/ui';
import Introduction from '../features/introduction'; import Introduction from '../features/introduction';
import { fetchCustomEmojis } from '../actions/custom_emojis'; import { fetchCustomEmojis } from '../actions/custom_emojis';
import { hydrateStore } from '../actions/store'; import { hydrateStore } from '../actions/store';
import { connectUserStream } from '../actions/streaming';
import { IntlProvider, addLocaleData } from 'react-intl'; import { IntlProvider, addLocaleData } from 'react-intl';
import { getLocale } from '../locales'; import { getLocale } from '../locales';
import initialState from '../initial_state'; import initialState from '../initial_state';
@ -39,9 +38,6 @@ const mapStateToProps = (state) => {
return { return {
showIntroduction, showIntroduction,
me,
accessToken: state.getIn(['auth', 'user', 'access_token']),
streamingUrl: state.getIn(['instance', 'urls', 'streaming_api']),
} }
} }
@ -52,29 +48,7 @@ class GabSocialMount extends React.PureComponent {
showIntroduction: PropTypes.bool, showIntroduction: PropTypes.bool,
}; };
componentDidMount() {
this.componentDidUpdate();
}
componentDidUpdate(prevProps) {
const keys = ['accessToken', 'streamingUrl'];
const credsSet = keys.every(p => this.props[p]);
if (!this.disconnect && credsSet) {
this.disconnect = store.dispatch(connectUserStream());
}
}
componentWillUnmount () {
if (this.disconnect) {
this.disconnect();
this.disconnect = null;
}
}
render () { render () {
const { me, streamingUrl } = this.props;
if (me == null || !streamingUrl) return null;
// Disabling introduction for launch // Disabling introduction for launch
// const { showIntroduction } = this.props; // const { showIntroduction } = this.props;
// //
@ -85,7 +59,7 @@ class GabSocialMount extends React.PureComponent {
return ( return (
<BrowserRouter basename='/web'> <BrowserRouter basename='/web'>
<ScrollContext> <ScrollContext>
<Route component={UI} /> <Route path='/' component={UI} />
</ScrollContext> </ScrollContext>
</BrowserRouter> </BrowserRouter>
); );

View file

@ -31,6 +31,7 @@ import SearchPage from 'gabsocial/pages/search_page';
import HomePage from 'gabsocial/pages/home_page'; import HomePage from 'gabsocial/pages/home_page';
import GroupSidebarPanel from '../groups/sidebar_panel'; import GroupSidebarPanel from '../groups/sidebar_panel';
import SidebarMenu from '../../components/sidebar_menu'; import SidebarMenu from '../../components/sidebar_menu';
import { connectUserStream } from '../../actions/streaming';
import { import {
Status, Status,
@ -87,6 +88,8 @@ const mapStateToProps = state => {
hasMediaAttachments: state.getIn(['compose', 'media_attachments']).size > 0, hasMediaAttachments: state.getIn(['compose', 'media_attachments']).size > 0,
dropdownMenuIsOpen: state.getIn(['dropdown_menu', 'openId']) !== null, dropdownMenuIsOpen: state.getIn(['dropdown_menu', 'openId']) !== null,
me: state.get('me'), me: state.get('me'),
accessToken: state.getIn(['auth', 'user', 'access_token']),
streamingUrl: state.getIn(['instance', 'urls', 'streaming_api']),
} }
}; };
@ -353,6 +356,23 @@ class UI extends React.PureComponent {
} }
} }
connectStreaming = (prevProps) => {
const { dispatch } = this.props;
const keys = ['accessToken', 'streamingUrl'];
const credsSet = keys.every(p => this.props[p]);
if (!this.disconnect && credsSet) {
this.disconnect = dispatch(connectUserStream());
}
}
disconnectStreaming = () => {
if (this.disconnect) {
this.disconnect();
this.disconnect = null;
}
}
componentWillMount () { componentWillMount () {
const { me } = this.props; const { me } = this.props;
window.addEventListener('beforeunload', this.handleBeforeUnload, false); window.addEventListener('beforeunload', this.handleBeforeUnload, false);
@ -387,6 +407,11 @@ class UI extends React.PureComponent {
this.hotkeys.__mousetrap__.stopCallback = (e, element) => { this.hotkeys.__mousetrap__.stopCallback = (e, element) => {
return ['TEXTAREA', 'SELECT', 'INPUT'].includes(element.tagName); return ['TEXTAREA', 'SELECT', 'INPUT'].includes(element.tagName);
}; };
this.connectStreaming();
}
componentDidUpdate(prevProps) {
this.connectStreaming();
} }
componentWillUnmount () { componentWillUnmount () {
@ -396,6 +421,7 @@ class UI extends React.PureComponent {
document.removeEventListener('drop', this.handleDrop); document.removeEventListener('drop', this.handleDrop);
document.removeEventListener('dragleave', this.handleDragLeave); document.removeEventListener('dragleave', this.handleDragLeave);
document.removeEventListener('dragend', this.handleDragEnd); document.removeEventListener('dragend', this.handleDragEnd);
this.disconnectStreaming();
} }
setRef = c => { setRef = c => {
@ -502,9 +528,12 @@ class UI extends React.PureComponent {
} }
render () { render () {
const { streamingUrl } = this.props;
const { draggingOver } = this.state; const { draggingOver } = this.state;
const { intl, children, isComposing, location, dropdownMenuIsOpen, me } = this.props; const { intl, children, isComposing, location, dropdownMenuIsOpen, me } = this.props;
if (me == null || !streamingUrl) return null;
const handlers = me ? { const handlers = me ? {
help: this.handleHotkeyToggleHelp, help: this.handleHotkeyToggleHelp,
new: this.handleHotkeyNew, new: this.handleHotkeyNew,