1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
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 Header from './ui/Header';
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 [es, setEs] = useState({});
useEffect(() => {
const { hash } = visitor;
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)}`);
let 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);
setEs(es);
}
}
return (() => {
if (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 (
<Router>
<>
<Header>
<div id="header_wrapper">
{
visitor.uid > 0 ?
<UserLink user={visitor} />
: <div id="logo">
<Link to="/">Juick</Link>
</div>
}
<div id="search" className="desktop">
<SearchBox pathname="/discover" onSearch={search} />
</div>
<nav id="global">
{visitor.uid > 0 ?
<Link to={{ pathname: '/' }}>
<Icon name="ei-bell" size="s" /><span className="desktop">Discuss</span>
{
visitor.unreadCount &&
<span className="badge">{visitor.unreadCount}</span>
}
</Link>
:
<Link to='/?media=1' rel="nofollow">
<Icon name="ei-camera" size="s" />
<span className="desktop">Photos</span>
</Link>
}
<Link to={{ pathname: '/discover' }} rel="nofollow">
<Icon name="ei-search" size="s" />
<span className="desktop">Discover</span>
</Link>
{visitor.uid > 0 ?
<Link to={{ pathname: '/post' }}>
<Icon name="ei-pencil" size="s" />
<span className="desktop">Post</span>
</Link>
:
<LoginButton title="Login" onAuth={auth} />
}
</nav>
</div>
</Header>
<section id="content">
<Switch>
<Route exact path="/" render={(props) => <Discussions visitor={visitor} {...props} />} />
<Route exact path="/home" render={(props) => <Home visitor={visitor} {...props} />} />
<Route exact path="/discover" render={(props) =>
<Discover visitor={visitor} {...props} />
} />
<Route exact path="/settings" render={(props) =>
<Settings visitor={visitor} {...props} onChange={auth} />
} />
<Route exact path="/post" render={(props) => <Post visitor={visitor} {...props} />} />
<Route exact path="/pm" render={(props) => <Contacts visitor={visitor} {...props} />} />
<Route exact path="/pm/:user" render={(props) => <Chat connection={es} visitor={visitor} {...props} />} />
<Route exact path="/:user/friends" render={(props) => <Friends visitor={visitor} {...props} />} />
<Route exact path="/:user/readers" render={(props) => <Readers visitor={visitor} {...props} />} />
<Route exact path="/:user" render={(props) => <Blog visitor={visitor} {...props} />} />
<Route exact path="/tag/:tag" render={(props) => <Tag visitor={visitor} {...props} />} />
<Route exact path="/:user/:mid" render={(props) => <Thread connection={es} visitor={visitor} {...props} />} />
</Switch>
</section>
{
visitor.uid > 0 &&
<aside id="sidebar">
<Link to="/?show=my">
<Icon name="ei-clock" size="s" />
<span className="desktop">My feed</span>
</Link>
<Link to="/pm">
<Icon name="ei-envelope" size="s" />
<span className="desktop">Messages</span>
</Link>
<Link to="/?show=discuss">
<Icon name="ei-bell" size="s" />
<span className="desktop">Discussions</span>
</Link>
<Link to="/settings" rel="nofollow">
<Icon name="ei-gear" size="s" />
<span className="desktop">Settings</span>
</Link>
</aside>
}
</>
</Router>
);
}
|