2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2017-12-21 22:08:54 +01:00
|
|
|
import React from 'react';
|
|
|
|
import Router from 'component/router/index';
|
|
|
|
import ModalRouter from 'modal/modalRouter';
|
|
|
|
import ReactModal from 'react-modal';
|
|
|
|
import throttle from 'util/throttle';
|
2018-03-26 23:32:43 +02:00
|
|
|
import SideBar from 'component/sideBar';
|
|
|
|
import Header from 'component/header';
|
2019-01-23 16:38:40 +01:00
|
|
|
import { openContextMenu } from 'util/context-menu';
|
|
|
|
import EnhancedLayoutListener from 'util/enhanced-layout';
|
|
|
|
import Native from 'native';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-11-07 17:03:42 +01:00
|
|
|
const TWO_POINT_FIVE_MINUTES = 1000 * 60 * 2.5;
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
type Props = {
|
|
|
|
alertError: (string | {}) => void,
|
|
|
|
recordScroll: number => void,
|
|
|
|
currentStackIndex: number,
|
|
|
|
currentPageAttributes: { path: string, scrollY: number },
|
|
|
|
pageTitle: ?string,
|
2018-10-19 17:27:14 +02:00
|
|
|
theme: string,
|
2018-11-07 17:03:42 +01:00
|
|
|
updateBlockHeight: () => void,
|
2019-01-23 16:38:40 +01:00
|
|
|
toggleEnhancedLayout: () => void,
|
|
|
|
enhancedLayout: boolean,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2017-04-07 07:15:22 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
class App extends React.PureComponent<Props> {
|
2017-11-17 22:27:35 +01:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.mainContent = undefined;
|
2018-03-26 23:32:43 +02: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() {
|
2018-10-19 17:27:14 +02:00
|
|
|
const { alertError, theme } = this.props;
|
2017-07-16 18:29:46 +02:00
|
|
|
|
2018-03-26 23:32:43 +02: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
|
|
|
});
|
2018-10-18 18:45:24 +02:00
|
|
|
|
|
|
|
// $FlowFixMe
|
2019-02-13 17:27:20 +01:00
|
|
|
document.documentElement.setAttribute('data-mode', theme);
|
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() {
|
2019-01-23 16:38:40 +01:00
|
|
|
const { updateBlockHeight, toggleEnhancedLayout } = this.props;
|
2018-11-07 17:03:42 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
const mainContent = document.getElementById('content');
|
2017-11-17 22:27:35 +01:00
|
|
|
this.mainContent = mainContent;
|
2018-03-26 23:32:43 +02:00
|
|
|
if (this.mainContent) {
|
|
|
|
this.mainContent.addEventListener('scroll', throttle(this.scrollListener, 750));
|
|
|
|
}
|
2017-12-07 19:07:30 +01:00
|
|
|
|
2017-12-21 22:08:54 +01:00
|
|
|
ReactModal.setAppElement('#window'); // fuck this
|
2018-11-07 17:03:42 +01:00
|
|
|
|
2019-01-23 16:38:40 +01:00
|
|
|
this.enhance = new EnhancedLayoutListener(() => toggleEnhancedLayout());
|
|
|
|
|
2018-11-07 17:03:42 +01:00
|
|
|
updateBlockHeight();
|
|
|
|
setInterval(() => {
|
|
|
|
updateBlockHeight();
|
|
|
|
}, TWO_POINT_FIVE_MINUTES);
|
2017-07-16 18:29:46 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
componentWillReceiveProps(props: Props) {
|
|
|
|
const { pageTitle } = props;
|
|
|
|
this.setTitleFromProps(pageTitle);
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
componentDidUpdate(prevProps: Props) {
|
2018-10-19 17:27:14 +02:00
|
|
|
const { currentStackIndex: prevStackIndex, theme: prevTheme } = prevProps;
|
|
|
|
const { currentStackIndex, currentPageAttributes, theme } = this.props;
|
2017-11-17 22:27:35 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
if (this.mainContent && currentStackIndex !== prevStackIndex && currentPageAttributes) {
|
2017-11-17 22:27:35 +01:00
|
|
|
this.mainContent.scrollTop = currentPageAttributes.scrollY || 0;
|
|
|
|
}
|
2018-10-19 17:27:14 +02:00
|
|
|
|
|
|
|
if (prevTheme !== theme) {
|
|
|
|
// $FlowFixMe
|
2019-02-13 17:27:20 +01:00
|
|
|
document.documentElement.setAttribute('data-mode', theme);
|
2018-10-19 17:27:14 +02:00
|
|
|
}
|
2017-11-17 22:27:35 +01:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.mainContent) {
|
|
|
|
this.mainContent.removeEventListener('scroll', this.scrollListener);
|
|
|
|
}
|
2019-01-23 16:38:40 +01:00
|
|
|
|
|
|
|
this.enhance = null;
|
2018-03-26 23:32:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setTitleFromProps = (title: ?string) => {
|
|
|
|
window.document.title = title || 'LBRY';
|
|
|
|
};
|
|
|
|
|
|
|
|
scrollListener() {
|
|
|
|
const { recordScroll } = this.props;
|
2018-12-19 06:44:53 +01:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
if (this.mainContent) {
|
|
|
|
recordScroll(this.mainContent.scrollTop);
|
|
|
|
}
|
2017-08-30 14:48:32 +02:00
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
mainContent: ?HTMLElement;
|
2019-01-23 16:38:40 +01:00
|
|
|
enhance: ?any;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2017-05-04 05:44:08 +02:00
|
|
|
render() {
|
2019-01-23 16:38:40 +01:00
|
|
|
const { enhancedLayout } = this.props;
|
|
|
|
|
2017-06-06 23:19:12 +02:00
|
|
|
return (
|
2018-06-04 02:17:58 +02:00
|
|
|
<div id="window" onContextMenu={e => openContextMenu(e)}>
|
2018-06-25 08:07:45 +02:00
|
|
|
<Header />
|
2018-03-26 23:32:43 +02:00
|
|
|
<main className="page">
|
2019-01-23 16:38:40 +01:00
|
|
|
{enhancedLayout && (
|
|
|
|
<img
|
|
|
|
alt="Friendly gerbil"
|
|
|
|
className="yrbl--enhanced"
|
|
|
|
src={Native.imagePath('gerbil-happy.png')}
|
|
|
|
/>
|
|
|
|
)}
|
2019-02-22 07:59:50 +01:00
|
|
|
{/* @if TARGET='app' */}
|
2018-03-26 23:32:43 +02:00
|
|
|
<SideBar />
|
2019-02-22 07:59:50 +01:00
|
|
|
{/* @endif */}
|
2018-03-26 23:32:43 +02:00
|
|
|
<div className="content" id="content">
|
|
|
|
<Router />
|
|
|
|
<ModalRouter />
|
|
|
|
</div>
|
|
|
|
</main>
|
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;
|