lbry-desktop/src/renderer/component/app/view.jsx

94 lines
2.2 KiB
React
Raw Normal View History

2018-01-04 06:05:20 +01:00
// @flow
import React from 'react';
import Router from 'component/router/index';
import Header from 'component/header';
import Theme from 'component/theme';
import ModalRouter from 'modal/modalRouter';
import ReactModal from 'react-modal';
import throttle from 'util/throttle';
2017-04-07 07:15:22 +02:00
2018-01-04 06:05:20 +01:00
type Props = {
alertError: (string | {}) => void,
recordScroll: number => void,
currentStackIndex: number,
currentPageAttributes: { path: string, scrollY: number },
pageTitle: ?string,
};
class App extends React.PureComponent<Props> {
2017-11-17 22:27:35 +01:00
constructor() {
super();
this.mainContent = undefined;
2018-01-04 06:05:20 +01:00
(this: any).scrollListener = this.scrollListener.bind(this);
2017-11-17 22:27:35 +01:00
}
2017-05-04 05:44:08 +02:00
componentWillMount() {
2017-11-16 22:39:10 +01:00
const { alertError } = this.props;
2017-07-16 18:29:46 +02:00
2018-01-04 06:05:20 +01:00
// TODO: create type for this object
// it lives in jsonrpc.js
document.addEventListener('unhandledError', (event: any) => {
2017-07-16 18:29:46 +02:00
alertError(event.detail);
2017-04-07 07:15:22 +02:00
});
2017-11-17 22:27:35 +01:00
}
2017-04-07 07:15:22 +02:00
2017-11-17 22:27:35 +01:00
componentDidMount() {
const mainContent = document.getElementById('main-content');
2017-11-17 22:27:35 +01:00
this.mainContent = mainContent;
2018-01-04 06:05:20 +01:00
if (this.mainContent) {
this.mainContent.addEventListener('scroll', throttle(this.scrollListener, 750));
}
ReactModal.setAppElement('#window'); // fuck this
2017-07-16 18:29:46 +02:00
}
2018-01-04 06:05:20 +01:00
componentWillReceiveProps(props: Props) {
const { pageTitle } = props;
this.setTitleFromProps(pageTitle);
}
2018-01-04 06:05:20 +01:00
componentDidUpdate(prevProps: Props) {
2017-11-17 22:27:35 +01:00
const { currentStackIndex: prevStackIndex } = prevProps;
const { currentStackIndex, currentPageAttributes } = this.props;
2018-01-04 06:05:20 +01:00
if (this.mainContent && currentStackIndex !== prevStackIndex) {
2017-11-17 22:27:35 +01:00
this.mainContent.scrollTop = currentPageAttributes.scrollY || 0;
}
}
2018-01-04 06:05:20 +01:00
componentWillUnmount() {
if (this.mainContent) {
// having issues with this
// $FlowFixMe
this.mainContent.removeEventListener('scroll');
}
}
2018-01-04 06:05:20 +01:00
setTitleFromProps = (title: ?string) => {
window.document.title = title || 'LBRY';
};
scrollListener() {
const { recordScroll } = this.props;
if (this.mainContent) {
recordScroll(this.mainContent.scrollTop);
}
}
mainContent: ?HTMLElement;
2017-05-04 05:44:08 +02:00
render() {
2017-06-06 23:19:12 +02:00
return (
<div id="window">
2017-09-07 02:52:34 +02:00
<Theme />
2017-06-06 23:19:12 +02:00
<Header />
2018-01-04 06:05:20 +01:00
<Router />
<ModalRouter />
2017-04-21 04:31:50 +02:00
</div>
2017-06-06 23:19:12 +02:00
);
2017-04-07 07:15:22 +02:00
}
2017-05-04 05:44:08 +02:00
}
2017-04-07 07:15:22 +02:00
2017-06-06 06:21:55 +02:00
export default App;