import React from 'react';
import ReactDOM from 'react-dom';
import { BrowserRouter as Router, Route, Link, Switch, Redirect } from 'react-router-dom';
import * as qs from 'query-string';
import Icon from './components/Icon';
import { Discover, Discussions, Blog, Tag, Home } from './components/Feeds';
import Settings from './components/Settings';
import Contacts from './components/Contacts';
import Chat from './components/Chat';
import Post from './components/Post';
import Thread from './components/Thread';
import LoginButton from './components/LoginButton';
import Footer from './components/Footer';
import Avatar from './components/Avatar';
const elClassHidden = 'header--hidden'
const elClassBackground = 'header--background';
class App extends React.Component {
constructor(props) {
super(props);
let params = qs.parse(window.location.search)
if (params.hash) {
window.localStorage.hash = params.hash
window.history.replaceState({}, document.title, `${window.location.protocol}//${window.location.host}${window.location.pathname}`)
}
this.state = {
visitor: {
uid: Number(window.localStorage.uid) || 0,
hash: window.localStorage.hash || params.hash || ''
},
headerClassName: ''
};
this.dHeight = 0;
this.wHeight = 0;
this.wScrollCurrent = 0;
this.wScrollBefore = 0;
this.wScrollDiff = 0;
}
componentDidMount() {
window.addEventListener('scroll', this.throttle(500, () => {
this.dHeight = document.body.offsetHeight;
this.wHeight = window.innerHeight;
this.wScrollCurrent = window.pageYOffset;
this.wScrollDiff = this.wScrollBefore - this.wScrollCurrent;
if (this.wScrollCurrent <= 0) {
// scrolled to the very top; element sticks to the top
this.setState({
headerClassName: ''
})
} else if (this.wScrollDiff > 0 && this.state.headerClassName.indexOf(elClassHidden) >= 0) {
// scrolled up; element slides in
this.setState({
headerClassName: elClassBackground
})
} else if (this.wScrollDiff < 0) {
// scrolled down
if (this.wScrollCurrent + this.wHeight >= this.dHeight && this.state.headerClassName.indexOf(elClassHidden) >= 0) {
// scrolled to the very bottom; element slides in
this.setState({
headerClassName: elClassBackground
})
} else {
// scrolled down; element slides out
this.setState({
headerClassName: [elClassHidden, elClassBackground].join(' ')
})
}
}
this.wScrollBefore = this.wScrollCurrent;
}));
this.auth(this.state.visitor.hash)
}
throttle(delay, fn) {
var last, deferTimer;
return function () {
var context = this, args = arguments, now = +new Date;
if (last && now < last + delay) {
clearTimeout(deferTimer);
deferTimer = setTimeout(
function () {
last = now;
fn.apply(context, args);
},
delay);
} else {
last = now;
fn.apply(context, args);
}
};
};
render() {
const user = this.state.visitor;
return (
} />
} />
} />
} />
} />
} />
} />
} />
} />
} />
)
}
auth = (data) => {
if (data) {
window.localStorage.hash = data;
fetch(`https://api.juick.com/users?hash=${data}`)
.then(response => {
return response.json()
})
.then(users => {
let visitor = users[0];
visitor.hash = data;
window.localStorage.uid = visitor.uid;
this.setState({
visitor: visitor
})
}).catch(reason => {
window.localStorage.clear()
window.location.reload()
})
}
}
}
let container = document.createElement('div');
ReactDOM.render(, container);
let body = document.getElementById('wrapper').parentNode;
body.replaceChild(container.getElementsByTagName('header')[0], body.querySelector('#header'));
body.replaceChild(container.querySelector('#wrapper'), body.querySelector('#wrapper'));
body.replaceChild(container.querySelector('#footer'), body.querySelector('#footer'));