-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.tsx
423 lines (400 loc) · 21.4 KB
/
App.tsx
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
import React, {useEffect, useState} from "react"
import {Switch, Route, Redirect, useHistory, useLocation} from "react-router-dom"
import {Context, ThemeContext, HideNavbarContext, HideSidebarContext, HideSortbarContext, HasNotificationContext, TabletContext, ActiveGroupContext,
HideTitlebarContext, EnableDragContext, ActiveDropdownContext, FilterDropActiveContext, MobileScrollingContext, EmojisContext, PostsContext, RestrictTypeContext,
SidebarHoverContext, SessionContext, SessionFlagContext, UserImgContext, UserImgPostContext, MobileContext, SelectionModeContext, ActiveFavgroupContext} from "./Context"
import favicon from "./assets/icons/favicon.png"
import permissions from "./structures/Permissions"
import PostsPage from "./pages/PostsPage"
import CommentsPage from "./pages/CommentsPage"
import ArtistsPage from "./pages/ArtistsPage"
import CharactersPage from "./pages/CharactersPage"
import SeriesPage from "./pages/SeriesPage"
import TagsPage from "./pages/TagsPage"
import PostPage from "./pages/PostPage"
import UploadPage from "./pages/UploadPage"
import $404Page from "./pages/404Page"
import $403Page from "./pages/403Page"
import $401Page from "./pages/401Page"
import HelpPage from "./pages/HelpPage"
import TermsPage from "./pages/TermsPage"
import LoginPage from "./pages/LoginPage"
import SignUpPage from "./pages/SignUpPage"
import $2FAPage from "./pages/2FAPage"
import $2FAEnablePage from "./pages/2FAEnablePage"
import ContactPage from "./pages/ContactPage"
import CopyrightRemovalPage from "./pages/CopyrightRemovalPage"
import UserProfilePage from "./pages/UserProfilePage"
import ChangeUsernamePage from "./pages/ChangeUsernamePage"
import ChangeEmailPage from "./pages/ChangeEmailPage"
import ChangeEmailSuccessPage from "./pages/ChangeEmailSuccessPage"
import VerifyEmailPage from "./pages/VerifyEmailPage"
import VerifyEmailSuccessPage from "./pages/VerifyEmailSuccessPage"
import ChangePasswordPage from "./pages/ChangePasswordPage"
import ResetPasswordPage from "./pages/ResetPasswordPage"
import ForgotPasswordPage from "./pages/ForgotPasswordPage"
import EditPostPage from "./pages/EditPostPage"
import UserPage from "./pages/UserPage"
import TagPage from "./pages/TagPage"
import GroupPage from "./pages/GroupPage"
import FavgroupPage from "./pages/FavgroupPage"
import TagHistoryPage from "./pages/TagHistoryPage"
import PostHistoryPage from "./pages/PostHistoryPage"
import GroupHistoryPage from "./pages/GroupHistoryPage"
import HistoryPage from "./pages/HistoryPage"
import UnverifiedPostPage from "./pages/UnverifiedPostPage"
import functions from "./structures/Functions"
import ModQueuePage from "./pages/ModQueuePage"
import EditUnverifiedPostPage from "./pages/EditUnverifiedPostPage"
import SetAvatarPage from "./pages/SetAvatarPage"
import TranslationHistoryPage from "./pages/TranslationHistoryPage"
import ForumPage from "./pages/ForumPage"
import ThreadPage from "./pages/ThreadPage"
import GroupsPage from "./pages/GroupsPage"
import BulkUploadPage from "./pages/BulkUploadPage"
import MailPage from "./pages/MailPage"
import MessagePage from "./pages/MessagePage"
import LoginHistoryPage from "./pages/LoginHistoryPage"
import IPBlacklistPage from "./pages/IPBlacklistPage"
import NewsBannerPage from "./pages/NewsBannerPage"
import PremiumPage from "./pages/PremiumPage"
import PremiumRequiredDialog from "./dialogs/PremiumRequiredDialog"
import PremiumSuccessPage from "./pages/PremiumSuccessPage"
import DragAndDrop from "./components/DragAndDrop"
import AudioPlayer from "./components/AudioPlayer"
import ActionBanner from "./components/ActionBanner"
import NewsBanner from "./components/NewsBanner"
import "./index.less"
require.context("./assets/icons", true)
let destroy2FATimeout = null as any
const App: React.FunctionComponent = (props) => {
const [loaded, setLoaded] = useState(false)
const [theme, setTheme] = useState("purple")
const [hideSortbar, setHideSortbar] = useState(false)
const [hideSidebar, setHideSidebar] = useState(false)
const [hideNavbar, setHideNavbar] = useState(false)
const [hideTitlebar, setHideTitlebar] = useState(false)
const [enableDrag, setEnableDrag] = useState(false)
const [activeDropdown, setActiveDropdown] = useState("none")
const [filterDropActive, setFilterDropActive] = useState(false)
const [sidebarHover, setSidebarHover] = useState(false)
const [session, setSession] = useState({}) as any
const [sessionFlag, setSessionFlag] = useState(false)
const [userImg, setUserImg] = useState("")
const [userImgPost, setUserImgPost] = useState("")
const [mobile, setMobile] = useState(false)
const [tablet, setTablet] = useState(false)
const [mobileScrolling, setMobileScrolling] = useState(false)
const [selectionMode, setSelectionMode] = useState(false)
const [hasNotification, setHasNotification] = useState(false)
const [emojis, setEmojis] = useState([])
const [restrictType, setRestrictType] = useState("all")
const [posts, setPosts] = useState([]) as any
const [activeGroup, setActiveGroup] = useState(null) as any
const [activeFavgroup, setActiveFavgroup] = useState(null) as any
const history = useHistory()
const location = useLocation()
const getSessionCookie = async () => {
const cookie = await functions.get("/api/user/session", null, session, setSessionFlag)
setSession(cookie)
if (cookie.username && !permissions.isPremium(cookie)) {
await functions.post("/api/user/upscaledimages", {reset: true}, session, setSessionFlag)
}
}
const cacheEmojis = async () => {
const emojis = await functions.emojisCache(session, setSessionFlag)
setEmojis(emojis)
}
useEffect(() => {
const savedActiveGroup = localStorage.getItem("activeGroup")
const savedActiveFavgroup = localStorage.getItem("activeFavgroup")
const savedRestrict = localStorage.getItem("restrict")
if (savedRestrict) setRestrictType(savedRestrict)
const onDOMLoaded = () => {
setLoaded(true)
getSessionCookie()
functions.clearCache()
cacheEmojis()
if (savedActiveGroup) setActiveGroup(savedActiveGroup)
if (savedActiveFavgroup) setActiveFavgroup(JSON.parse(savedActiveFavgroup))
}
window.addEventListener("load", onDOMLoaded)
return () => {
window.removeEventListener("load", onDOMLoaded)
}
}, [])
useEffect(() => {
if (activeGroup) {
localStorage.setItem("activeGroup", activeGroup)
} else {
localStorage.removeItem("activeGroup")
}
if (activeFavgroup) {
localStorage.setItem("activeFavgroup", JSON.stringify(activeFavgroup))
} else {
localStorage.removeItem("activeFavgroup")
}
}, [activeGroup, activeFavgroup])
const getImg = () => {
if (session.username) {
return session.image ? functions.getTagLink("pfp", session.image) : favicon
} else {
return ""
}
}
const updatePfp = async () => {
await functions.refreshCache(getImg())
setUserImg(getImg())
if (session.imagePost) setUserImgPost(session.imagePost)
}
useEffect(() => {
if (!session.username) return
const events = new EventSource("/api/notifications", {withCredentials: true})
events.onmessage = (event: any) => {
if (event.data === "new message!") setHasNotification(true)
}
return () => {
events.close()
}
}, [session])
useEffect(() => {
if (!session.cookie) return
updatePfp()
}, [session, theme])
const destroy2FA = async () => {
try {
await functions.post("/api/2fa/delete", null, session, setSessionFlag)
setSessionFlag(true)
} catch {
// ignore
}
}
useEffect(() => {
if (!location.pathname.includes("/post")) {
setActiveGroup(null)
setActiveFavgroup(null)
}
if (session.username && !session.emailVerified) {
if (location.pathname !== "/verify-email") {
history.push("/verify-email")
}
}
clearTimeout(destroy2FATimeout)
destroy2FATimeout = setTimeout(() => {
if (!session.username && session.email) {
if (location.pathname !== "/2fa") {
destroy2FA()
}
}
}, 1000)
}, [location, session])
useEffect(() => {
if (sessionFlag) {
setSessionFlag(false)
getSessionCookie()
updatePfp()
}
}, [sessionFlag])
useEffect(() => {
functions.preventDragging()
})
useEffect(() => {
setActiveGroup(null)
setActiveFavgroup(null)
}, [posts])
useEffect(() => {
const handleScroll = () => {
if (filterDropActive || selectionMode) {
setMobileScrolling(false)
return setHideSortbar(false)
}
if (functions.scrolledToTop()) return setHideSortbar(false)
if (sidebarHover) return setHideSortbar(true)
setActiveDropdown("none")
return setHideSortbar(true)
}
const handleMouseMove = (event: any) => {
if (filterDropActive || selectionMode) {
setMobileScrolling(false)
return setHideSortbar(false)
}
if (activeDropdown !== "none") return setHideSortbar(false)
if (functions.scrolledToTop()) return setHideSortbar(false)
if (sidebarHover) return setHideSortbar(true)
const sortbar = document.querySelector(".sortbar")
const amt = hideTitlebar ? (sortbar ? (functions.navbarHeight() + functions.sortbarHeight()) : functions.navbarHeight()) : (functions.titlebarHeight() + functions.navbarHeight() + functions.sortbarHeight())
if (event.clientY < amt) return setHideSortbar(false)
return setHideSortbar(true)
}
window.addEventListener("scroll", handleScroll)
window.addEventListener("mousemove", handleMouseMove)
return () => {
window.removeEventListener("scroll", handleScroll)
window.removeEventListener("mousemove", handleMouseMove)
}
}, [selectionMode, filterDropActive, hideTitlebar, activeDropdown, sidebarHover])
useEffect(() => {
if (mobile) return functions.dragScroll(false)
functions.dragScroll(enableDrag)
}, [enableDrag, history])
useEffect(() => {
if (!theme || theme === "purple") {
document.documentElement.style.setProperty("--background", "#09071c")
document.documentElement.style.setProperty("--titlebarBG", "#090420")
document.documentElement.style.setProperty("--navbarBG", "#0b0322")
} else if (theme === "purple-light") {
document.documentElement.style.setProperty("--background", "#ffffff")
document.documentElement.style.setProperty("--titlebarBG", "#dfdfff")
document.documentElement.style.setProperty("--navbarBG", "#dbddff")
} else if (theme === "magenta") {
document.documentElement.style.setProperty("--background", "#17040e")
document.documentElement.style.setProperty("--titlebarBG", "#1c0511")
document.documentElement.style.setProperty("--navbarBG", "#1e0514")
} else if (theme === "magenta-light") {
document.documentElement.style.setProperty("--background", "#ffffff")
document.documentElement.style.setProperty("--titlebarBG", "#ffe2f2")
document.documentElement.style.setProperty("--navbarBG", "#ffe0f1")
}
functions.changeFavicon(theme)
}, [theme])
useEffect(() => {
const resize = () => {
const isMobile = window.matchMedia("(max-width: 500px)").matches
const isTablet = window.matchMedia("(min-width: 501px) and (max-width: 1200px)").matches
if (isMobile) {
setMobile(true)
setTablet(false)
} else if (isTablet) {
setTablet(true)
setMobile(false)
} else {
setMobile(false)
setTablet(false)
}
}
resize()
window.addEventListener("resize", resize)
document.documentElement.style.visibility = "visible"
return () => {
window.removeEventListener("resize", resize)
}
}, [])
return (
<div className={`app ${!loaded ? "stop-transitions" : ""}`}>
<ActiveFavgroupContext.Provider value={{activeFavgroup, setActiveFavgroup}}>
<ActiveGroupContext.Provider value={{activeGroup, setActiveGroup}}>
<PostsContext.Provider value={{posts, setPosts}}>
<EmojisContext.Provider value={{emojis, setEmojis}}>
<RestrictTypeContext.Provider value={{restrictType, setRestrictType}}>
<HasNotificationContext.Provider value={{hasNotification, setHasNotification}}>
<SelectionModeContext.Provider value={{selectionMode, setSelectionMode}}>
<MobileScrollingContext.Provider value={{mobileScrolling, setMobileScrolling}}>
<TabletContext.Provider value={{tablet, setTablet}}>
<MobileContext.Provider value={{mobile, setMobile}}>
<UserImgPostContext.Provider value={{userImgPost, setUserImgPost}}>
<UserImgContext.Provider value={{userImg, setUserImg}}>
<SessionFlagContext.Provider value={{sessionFlag, setSessionFlag}}>
<SessionContext.Provider value={{session, setSession}}>
<SidebarHoverContext.Provider value={{sidebarHover, setSidebarHover}}>
<FilterDropActiveContext.Provider value={{filterDropActive, setFilterDropActive}}>
<ActiveDropdownContext.Provider value={{activeDropdown, setActiveDropdown}}>
<EnableDragContext.Provider value={{enableDrag, setEnableDrag}}>
<HideSortbarContext.Provider value={{hideSortbar, setHideSortbar}}>
<HideNavbarContext.Provider value={{hideNavbar, setHideNavbar}}>
<HideTitlebarContext.Provider value={{hideTitlebar, setHideTitlebar}}>
<HideSidebarContext.Provider value={{hideSidebar, setHideSidebar}}>
<ThemeContext.Provider value={{theme, setTheme}}>
<Context>
<DragAndDrop/>
<NewsBanner/>
<ActionBanner/>
<PremiumRequiredDialog/>
<Switch>
<Route exact path={["/", "/posts", "/home"]}><PostsPage/></Route>
<Route exact path="/profile"><UserProfilePage/></Route>
<Route exact path="/upload"><UploadPage/></Route>
<Route exact path="/bulk-upload"><BulkUploadPage/></Route>
<Route exact path="/tags"><TagsPage/></Route>
<Route exact path="/series"><SeriesPage/></Route>
<Route exact path="/characters"><CharactersPage/></Route>
<Route exact path="/artists"><ArtistsPage/></Route>
<Route exact path="/comments"><CommentsPage/></Route>
<Route exact path="/groups"><GroupsPage/></Route>
<Route exact path="/history"><HistoryPage/></Route>
<Route exact path="/premium"><PremiumPage/></Route>
<Route exact path="/user/:username" render={(props) => <UserPage {...props}/>}></Route>
<Route exact path="/tag/history/:tag" render={(props) => <TagHistoryPage {...props}/>}></Route>
<Route exact path="/user/:username/tag/history" render={(props) => <TagHistoryPage {...props}/>}></Route>
<Route exact path="/tag/:tag" render={(props) => <TagPage {...props}/>}></Route>
<Route exact path="/group/:group" render={(props) => <GroupPage {...props}/>}></Route>
<Route exact path="/group/history/:group" render={(props) => <GroupHistoryPage {...props}/>}></Route>
<Route exact path="/user/:username/group/history" render={(props) => <GroupHistoryPage {...props}/>}></Route>
<Route exact path="/favgroup/:username/:favgroup" render={(props) => <FavgroupPage {...props}/>}></Route>
<Route exact path="/translation/history/:id/:order" render={(props) => <TranslationHistoryPage {...props}/>}></Route>
<Route exact path="/user/:username/translation/history" render={(props) => <TranslationHistoryPage {...props}/>}></Route>
<Route exact path="/post/history/:id" render={(props) => <PostHistoryPage {...props}/>}></Route>
<Route exact path="/user/:username/post/history" render={(props) => <PostHistoryPage {...props}/>}></Route>
<Route exact path="/post/:id" render={(props) => <PostPage {...props}/>}></Route>
<Route exact path="/unverified/post/:id" render={(props) => <UnverifiedPostPage {...props}/>}></Route>
<Route exact path="/edit-post/:id" render={(props) => <EditPostPage {...props}/>}></Route>
<Route exact path="/unverified/edit-post/:id" render={(props) => <EditUnverifiedPostPage {...props}/>}></Route>
<Route exact path="/set-avatar/:id" render={(props) => <SetAvatarPage {...props}/>}></Route>
<Route exact path="/help"><HelpPage/></Route>
<Route exact path="/forum"><ForumPage/></Route>
<Route exact path="/thread/:id" render={(props) => <ThreadPage {...props}/>}></Route>
<Route exact path="/mail"><MailPage/></Route>
<Route exact path="/message/:id" render={(props) => <MessagePage {...props}/>}></Route>
<Route exact path="/change-username"><ChangeUsernamePage/></Route>
<Route exact path="/change-email"><ChangeEmailPage/></Route>
<Route exact path="/change-email-success"><ChangeEmailSuccessPage/></Route>
<Route exact path="/verify-email"><VerifyEmailPage/></Route>
<Route exact path="/verify-email-success"><VerifyEmailSuccessPage/></Route>
<Route exact path="/premium-success"><PremiumSuccessPage/></Route>
<Route exact path="/reset-password"><ResetPasswordPage/></Route>
<Route exact path="/change-password"><ChangePasswordPage/></Route>
<Route exact path="/forgot-password"><ForgotPasswordPage/></Route>
<Route exact path={["/signup", "/register"]}><SignUpPage/></Route>
<Route exact path="/login"><LoginPage/></Route>
<Route exact path="/2fa"><$2FAPage/></Route>
<Route exact path="/enable-2fa"><$2FAEnablePage/></Route>
<Route exact path="/login-history"><LoginHistoryPage/></Route>
<Route exact path="/contact"><ContactPage/></Route>
<Route exact path="/copyright-removal"><CopyrightRemovalPage/></Route>
<Route exact path="/mod-queue"><ModQueuePage/></Route>
<Route exact path={["/privacy", "/privacypolicy"]}><Redirect to="/terms#privacy"/></Route>
<Route exact path={["/terms", "termsofservice"]}><TermsPage/></Route>
<Route exact path="/news-banner"><NewsBannerPage/></Route>
<Route exact path="/ip-blacklist"><IPBlacklistPage/></Route>
<Route exact path="/401"><$401Page/></Route>
<Route exact path="/403"><$403Page/></Route>
<Route path="*"><$404Page/></Route>
</Switch>
<AudioPlayer/>
</Context>
</ThemeContext.Provider>
</HideSidebarContext.Provider>
</HideTitlebarContext.Provider>
</HideNavbarContext.Provider>
</HideSortbarContext.Provider>
</EnableDragContext.Provider>
</ActiveDropdownContext.Provider>
</FilterDropActiveContext.Provider>
</SidebarHoverContext.Provider>
</SessionContext.Provider>
</SessionFlagContext.Provider>
</UserImgContext.Provider>
</UserImgPostContext.Provider>
</MobileContext.Provider>
</TabletContext.Provider>
</MobileScrollingContext.Provider>
</SelectionModeContext.Provider>
</HasNotificationContext.Provider>
</RestrictTypeContext.Provider>
</EmojisContext.Provider>
</PostsContext.Provider>
</ActiveGroupContext.Provider>
</ActiveFavgroupContext.Provider>
</div>
)
}
export default App