import React, { useState, useEffect } from 'react'; import PropTypes from 'prop-types'; import ReactRouterPropTypes from 'react-router-prop-types'; import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom'; import qs from 'qs'; import Icon from './components/Icon'; import { Discover, Discussions, Blog, Tag, Home } from './components/Feeds'; import { Friends, Readers } from './components/Users'; 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 { UserLink } from './components/UserInfo'; import Header from './components/Header'; import SearchBox from './components/SearchBox'; import cookies from 'react-cookies'; import { me } from './api'; export default function App(props) { let params = qs.parse(window.location.search.substring(1)); if (params.hash) { cookies.save('hash', params.hash, { path: '/' }); window.history.replaceState({}, document.title, `${window.location.protocol}//${window.location.host}${window.location.pathname}`); } const [visitor, setVisitor] = useState({ uid: 0, hash: cookies.load('hash') }); let updateStatus = () => { // refresh server visitor state (unread counters) me().then(visitor => { setVisitor(visitor); }); }; const [es, setEs] = useState(); useEffect(() => { const { hash } = visitor; if (hash) { me().then(visitor => auth(visitor)); } const eventParams = { hash: visitor.hash }; let url = new URL(`https://api.juick.com/events?${qs.stringify(eventParams)}`); let es = new EventSource(url); es.onopen = () => { console.log('online'); es.addEventListener('read', updateStatus); }; es.onerror = () => { es.removeEventListener('read', updateStatus); }; setEs(es); }, []); let search = (history, pathname, searchString) => { let location = {}; location.pathname = pathname; location.search = `?search=${searchString}`; history.push(location); }; let auth = (visitor) => { setVisitor(visitor); }; return ( <>
{ visitor.uid > 0 ? : }
} /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } /> } />
{ visitor.uid > 0 && }
); } App.propTypes = { match: ReactRouterPropTypes.match.isRequired };