aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/App.js
blob: 0764281aaca2ad0f1ff779557e1957b461164fb3 (plain) (blame)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import React from 'react';
import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom';
import * as 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 Avatar from './components/Avatar';
import Header from './components/Header';
import SearchBox from './components/SearchBox';
import NavigationIcon from './components/NavigationIcon';

import cookies from 'react-cookies';

import { me } from './api';

export default class App extends React.Component {
  constructor(props) {
    super(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}`);
    }
    this.state = {
      visitor: {
        uid: Number(cookies.load('_juick_uid')),
        hash: cookies.load('hash')
      },
      appMarginLeft: 'inherit'
    };
    this.pm = React.createRef();
    this.thread = React.createRef();
    this.sidebar = React.createRef();
  }

  initES = () => {
    if (!('EventSource' in window)) {
      return;
    }
    const params = { hash: this.state.visitor.hash };
    let url = new URL(`https://api.juick.com/events?${qs.stringify(params)}`);
    this.es = new EventSource(url);
    this.es.onopen = () => {
      console.log('online');
    };
    this.es.addEventListener('msg', msg => {
      try {
        var jsonMsg = JSON.parse(msg.data);
        console.log('data: ' + msg.data);
        // refresh server visitor state (unread counters)
        me().then(visitor => {
          this.setState({
            visitor: visitor
          });
        });
        if (jsonMsg.service) {
          return;
        }
        if (!jsonMsg.mid) {
          this.pm.current.onMessage(jsonMsg);
        }
        if (jsonMsg.rid && this.thread.current) {
          this.thread.current.onReply(jsonMsg);
        }
      } catch (err) {
        console.log(err);
      }
    });
  }

  componentDidMount() {
    const { hash } = this.state.visitor;
    this.initES();
    if (hash) {
      me().then(visitor => this.auth(visitor));
    }
  }
  search = (history, pathname, searchString) => {
    let location = {};
    location.pathname = pathname;
    location.search = `?search=${searchString}`;
    history.push(location);
  }
  toggleSidebar = () => {
    let width = this.sidebar.current.style.width;
    this.sidebar.current.style.width = width === '248px' ? '0' : '248px';
    this.setState({
      appMarginLeft: this.state.appMarginLeft === 'inherit' ? '250px' : 'inherit'
    });
  }
  render() {
    const user = this.state.visitor;
    return (
      <Router>
        <>
          <Header style={{ marginLeft: this.state.appMarginLeft }}>
            <div id="header_wrapper">
              <NavigationIcon onToggle={this.toggleSidebar} />
              <div id="logo" className="desktop"><Link to="/">Juick</Link></div>
              <div id="search" className="desktop">
                <SearchBox pathname="/discover" onSearch={this.search} {...this.props} />
              </div>
              <nav id="global">
                {user.uid > 0 ?
                  <Link to={{ pathname: '/' }}>
                    <Icon name="ei-bell" size="s" /><span className="desktop">Discuss</span>
                    {
                      user.unreadCount &&
                      <span className="badge">{user.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>

                {user.uid > 0 ?
                  <Link to={{ pathname: '/post' }}>
                    <Icon name="ei-pencil" size="s" />
                    <span className="desktop">Post</span>
                  </Link>
                  :
                  <LoginButton title="Login" onAuth={this.auth} />
                }
              </nav>
            </div>
          </Header>
          <div id="wrapper" style={{ marginLeft: this.state.appMarginLeft }}>
            <section id="content">
              <Switch>
                <Route exact path="/" render={(props) => <Discussions visitor={user} {...props} />} />
                <Route exact path="/home" render={(props) => <Home visitor={user} {...props} />} />
                <Route exact path="/discover" render={(props) =>
                  <Discover visitor={user} {...props} />
                } />
                <Route exact path="/settings" render={(props) =>
                  <Settings visitor={user} {...props} />
                } />
                <Route exact path="/post" render={(props) => <Post visitor={user} {...props} />} />
                <Route exact path="/pm" render={(props) => <Contacts visitor={user} {...props} />} />
                <Route exact path="/pm/:user" render={(props) => <Chat ref={this.pm} visitor={user} {...props} />} />
                <Route exact path="/:user/friends" render={(props) => <Friends visitor={user} {...props} />} />
                <Route exact path="/:user/readers" render={(props) => <Readers visitor={user} {...props} />} />
                <Route exact path="/:user" render={(props) => <Blog key={props.match.params.user} visitor={user} {...props} />} />
                <Route exact path="/tag/:tag" render={(props) => <Tag visitor={user} {...props} />} />
                <Route exact path="/:user/:mid" render={(props) => <Thread ref={this.thread} visitor={user} {...props} />} />
              </Switch>
            </section>
            <aside id="sidebar" ref={this.sidebar}>
              <Avatar user={user} />
              <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>
          </div>
          <div id="footer" style={{ marginLeft: this.state.appMarginLeft }}>
            <div id="footer-right"> &middot;
                <a href="/help/contacts" rel="nofollow">Contacts</a> &middot;
                <a href="/help/tos" rel="nofollow">TOS</a>
            </div>
            <div id="footer-left">juick.com &copy; 2008-2018
                <br />
            </div>
          </div>
        </>
      </Router>
    );
  }
  auth = (visitor) => {
    cookies.save('_juick_uid', visitor.uid, { path: '/' });
    this.setState({
      visitor: visitor
    });
  }
}