aboutsummaryrefslogtreecommitdiff
path: root/vnext/src/App.js
blob: f20d8db35a74e05daf230f91d1b5b4129c784806 (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
import { useState, useEffect, useRef, Fragment, useCallback } from 'react'
import { Route, Link, Routes } from 'react-router-dom'

import svg4everybody from 'svg4everybody'

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 Header from './ui/Header'
import Post from './ui/Post'
import Thread from './ui/Thread'

import { loginUrl, me, trends } from './api'
import { useVisitor } from './ui/VisitorContext'
import Avatar from './ui/Avatar'
import { Toaster } from 'react-hot-toast'

/**
 * 
 * @param {import('react').PropsWithChildren<{}> & {
 * footer: string
 * }} props props
 */
export default function App({ footer }) {

  let contentRef = useRef(null)

  const [allTrends, setAllTrends] = useState([])

  const [visitor, setVisitor] = useVisitor()

  useEffect(() => {
    svg4everybody()
    /*
    if (params['hash']) {
      setCookie('hash', params['hash'], { path: '/' })
      let retpath = params['retpath'] || `${window.location.protocol}//${window.location.host}${window.location.pathname}`
      window.history.replaceState({}, document.title, retpath)
    }*/
  }, [])

  let updateStatus = useCallback(() => {
    // refresh server visitor state (unread counters)
    me().then(visitor => {
      setVisitor(visitor)
    }).catch(console.error)
  }, [setVisitor])

  const [eventSource, setEventSource] = /** @param EventSource? */ useState({})

  useEffect(() => {
    let es
    if (visitor) {
      if ('EventSource' in window) {
        es = new EventSource('/api/events', {
          withCredentials: true
        })
        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, updateStatus])

  useEffect(() => {
    const getTrends = async () => {
      setAllTrends(await trends())
    }
    getTrends().catch(console.error)
  }, [])

  return (
    <>
      <Header />
      <Toaster />
      {
        <aside id="sidebar">
          <div id="sidebar_wrapper">
            {
              <nav id="global">
                {
                  visitor ?
                    <>
                      <div id="ctitle">
                        <Avatar user={visitor} />
                      </div>
                      <Link to={{ pathname: '/post' }}>
                        <Icon name="ei-pencil" size="s" />
                        <span className="desktop">Post</span>
                      </Link>
                      <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="/settings" rel="nofollow">
                        <Icon name="ei-gear" size="s" />
                        <span className="desktop">Settings</span>
                      </Link>
                    </> :
                    <a href={`/login?retpath=${window.location.href}`}>
                      <Icon name="ei-user" size="s" />
                      <span className="desktop">Login</span>
                    </a>
                }
                <Link to={{ pathname: '/discover' }} rel="nofollow">
                  <Icon name="ei-search" size="s" />
                  <span className="desktop">Discover</span>
                </Link>
                <Link to="/?show=discuss">
                  <Icon name="ei-bell" size="s" />
                  <span className="desktop">Discussions</span>
                </Link>
                <Link to='/?media=1' rel="nofollow">
                  <Icon name="ei-camera" size="s" />
                  <span className="desktop">Photos</span>
                </Link>
              </nav>
            }
            <div className="tags desktop">
              <h4>Trends</h4>
              {allTrends.map((it, index) => (
                <Fragment key={it.tag}>
                  {index > 0 && ' '}
                  <Link to={`/tag/${it.tag}`}>#{it.tag}</Link>
                </Fragment>
              ))}
            </div>
            <div id="footer" className="desktop">
              <div id="footer-left">&copy; 2008-2023, Juick team
                {footer && (<><br />Sponsors: <span dangerouslySetInnerHTML={{ __html: footer }}></span></>)}</div>
              <div id="footer-right">
                &nbsp;&middot;&nbsp;<Link to="/help/contacts" rel="nofollow">Contacts</Link>
                &nbsp;&middot;&nbsp;<Link to="/help/tos" rel="nofollow">TOS</Link>
              </div>
            </div>
          </div>
        </aside>
      }
      <section id="content" ref={contentRef}>
        <Routes>
          <Route exact path="/" element={<Discussions />} />
          <Route exact path="/home" element={<Home />} />
          <Route exact path="/discover" element={<Discover />} />
          <Route exact path="/settings" element={<Settings onChange={updateStatus} />} />
          <Route exact path="/post" element={<Post />} />
          <Route exact path="/pm" element={<Contacts />} />
          <Route exact path="/pm/:user" element={<Chat connection={eventSource} />} />
          <Route exact path="/:user/friends" element={<Friends />} />
          <Route exact path="/:user/readers" element={<Readers />} />
          <Route exact path="/:user" element={<Blog />} />
          <Route exact path="/tag/:tag" element={<Tag />} />
          <Route exact path="/:user/:mid" element={<Thread connection={eventSource} />} />
        </Routes>
      </section>
    </>
  )
}