import React, { useState, useEffect, useReducer } from 'react'; import { Switch, Route, Link, useHistory } from 'react-router-dom'; import './semantic-ui/semantic.min.css'; import './light.css'; import './dark.css'; import { Container, Dropdown, Menu } from 'semantic-ui-react'; import Darkmode from 'darkmode-js'; import { isAdmin, requester } from './utils.js'; import { ManageScroll } from './ManageScroll.js'; import { Home } from './Home.js'; import { Account } from './Account.js'; import { Transactions, TransactionDetail } from './Transactions.js'; import { Paymaster } from './Paymaster.js'; import { Cards } from './Cards.js'; import { Training } from './Training.js'; import { AdminTransactions } from './AdminTransactions.js'; import { Admin } from './Admin.js'; import { Paste } from './Paste.js'; import { Sign } from './Sign.js'; import { Courses, CourseDetail } from './Courses.js'; import { ClassFeed, Classes, ClassDetail } from './Classes.js'; import { Members, MemberDetail } from './Members.js'; import { Charts } from './Charts.js'; import { Usage } from './Usage.js'; import { Auth } from './Auth.js'; import { Subscribe } from './PayPal.js'; import { PasswordReset, ConfirmReset } from './PasswordReset.js'; import { NotFound, PleaseLogin } from './Misc.js'; import { Debug } from './Debug.js'; import { StorageDetail } from './Storage.js'; import { Garden } from './Garden.js'; import { Footer } from './Footer.js'; import { LCARS1Display, LCARS2Display } from './Display.js'; const APP_VERSION = 5; // TODO: automate this function App() { const [token, setToken] = useState(localStorage.getItem('token', '')); const [user, setUser] = useState(JSON.parse(localStorage.getItem('user', 'false'))); const [refreshCount, refreshUser] = useReducer(x => x + 1, 0); const [yousure, setYousure] = useState(false); const history = useHistory(); function setTokenCache(x) { setToken(x); localStorage.setItem('token', x); } function setUserCache(x) { setUser(x); localStorage.setItem('user', JSON.stringify(x)); } useEffect(() => { requester('/user/', 'GET', token) .then(res => { setUserCache(res); }) .catch(err => { console.log(err); setUserCache(null); }); }, [token, refreshCount]); function logout() { if (yousure) { setTokenCache(''); setUserCache(null); setYousure(false); history.push('/'); window.scrollTo(0, 0); } else { setYousure(true); } } useEffect(() => { if (user) { const data = { id: user.member.id, username: user.username, path: history.location.pathname, }; requester('/ping/', 'POST', token, data) .then() .catch(err => { console.log(err); if (err.data && err.data.detail === 'Invalid token.') { logout(); // You Sure? logout(); } }); } }, [history.location]); useEffect(() => { const options = { bottom: '16px', right: '16px', buttonColorDark: '#666', buttonColorLight: '#aaa', label: '🌓', autoMatchOsTheme: false, } const darkmode = new Darkmode(options); darkmode.showWidget(); }, []); if (user && user?.app_version !== APP_VERSION) { setUserCache(false); window.location.reload(); } return (
{window.location.hostname !== 'my.protospace.ca' &&

~~~~~ Development site ~~~~~

}
{user && isAdmin(user) && } {user && isAdmin(user) && } {user && }
{user && user.member.set_details ? {user && isAdmin(user) && } {user && isAdmin(user) && } : }
) }; export default App;