import React, { useState, useEffect } from 'react'; import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom'; import qs from 'qs'; import Icon from './ui/Icon'; import { Discover, Discussions, Blog, Tag, Home } from './ui/Feeds'; import { Friends, Readers } from './ui/Users'; import Settings from './ui/Settings'; import Contacts from './ui/Contacts'; import Chat from './ui/Chat'; import Post from './ui/Post'; import Thread from './ui/Thread'; import LoginButton from './ui/LoginButton'; import { UserLink } from './ui/UserInfo'; import SearchBox from './ui/SearchBox'; import cookies from 'react-cookies'; import { me } from './api'; export default function App() { 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 [eventSource, setEventSource] = useState({}); useEffect(() => { const { hash } = visitor; let es; if (hash) { me().then(visitor => auth(visitor)); if ('EventSource' in window) { const eventParams = { hash: hash }; let url = new URL(`https://juick.com/api/events?${qs.stringify(eventParams)}`); es = new EventSource(url); es.onopen = () => { console.log('online'); }; es.onerror = () => { es.removeEventListener('read', updateStatus); es.removeEventListener('msg', updateStatus); }; es.addEventListener('read', updateStatus); es.addEventListener('msg', updateStatus); setEventSource(es); } } return (() => { if (es && es.removeEventListener) { es.removeEventListener('read', updateStatus); es.removeEventListener('msg', updateStatus); } }); }, [visitor.hash]); 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 && }
); }