spaceport/webclient/src/App.js

373 lines
9.0 KiB
JavaScript
Raw Normal View History

2022-07-13 04:49:10 +00:00
import React, { useState, useEffect, useReducer } from 'react';
2022-07-14 21:58:50 +00:00
import { Switch, Route, Link, useHistory } from 'react-router-dom';
import './semantic-ui/semantic.min.css';
import './light.css';
2020-10-10 21:57:25 +00:00
import './dark.css';
2022-07-13 04:49:10 +00:00
import { Container, Dropdown, Menu } from 'semantic-ui-react';
2020-04-05 22:00:33 +00:00
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';
2020-01-10 00:29:52 +00:00
import { Training } from './Training.js';
import { AdminTransactions } from './AdminTransactions.js';
import { Admin } from './Admin.js';
2020-02-17 03:28:17 +00:00
import { Paste } from './Paste.js';
2021-12-08 22:02:30 +00:00
import { Sign } from './Sign.js';
2020-01-10 06:24:37 +00:00
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';
2020-09-18 05:04:00 +00:00
import { Auth } from './Auth.js';
import { Subscribe } from './PayPal.js';
2020-06-20 03:50:09 +00:00
import { PasswordReset, ConfirmReset } from './PasswordReset.js';
import { NotFound, PleaseLogin } from './Misc.js';
2022-04-10 07:11:53 +00:00
import { Debug } from './Debug.js';
2023-06-03 18:56:01 +00:00
import { Storage, StorageDetail, ClaimShelf } from './Storage.js';
2022-05-17 02:38:20 +00:00
import { Garden } from './Garden.js';
2020-01-11 08:56:31 +00:00
import { Footer } from './Footer.js';
2023-04-05 04:54:49 +00:00
import { LCARS1Display, LCARS2Display } from './Display.js';
2020-01-09 09:15:01 +00:00
2023-06-01 23:03:35 +00:00
const APP_VERSION = 6; // TODO: automate this
2022-01-21 22:48:51 +00:00
2020-01-06 23:32:29 +00:00
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);
2020-01-21 23:45:54 +00:00
const [yousure, setYousure] = useState(false);
const history = useHistory();
2020-01-09 09:04:32 +00:00
function setTokenCache(x) {
setToken(x);
localStorage.setItem('token', x);
}
2020-01-09 09:04:32 +00:00
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]);
2020-01-09 09:04:32 +00:00
function logout() {
2020-01-21 23:45:54 +00:00
if (yousure) {
setTokenCache('');
setUserCache(null);
2020-01-21 23:45:54 +00:00
setYousure(false);
history.push('/');
2020-01-23 03:32:54 +00:00
window.scrollTo(0, 0);
2020-01-21 23:45:54 +00:00
} else {
setYousure(true);
}
}
useEffect(() => {
2020-08-03 03:26:20 +00:00
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]);
2020-04-05 22:00:33 +00:00
useEffect(() => {
const options = {
bottom: '16px',
right: '16px',
2020-04-16 05:04:48 +00:00
buttonColorDark: '#666',
buttonColorLight: '#aaa',
2023-02-18 02:56:48 +00:00
label: '🌓',
2023-02-18 02:55:04 +00:00
autoMatchOsTheme: false,
2020-04-05 22:00:33 +00:00
}
const darkmode = new Darkmode(options);
darkmode.showWidget();
}, []);
2022-01-21 22:48:51 +00:00
if (user && user?.app_version !== APP_VERSION) {
setUserCache(false);
window.location.reload();
}
2020-01-07 02:10:25 +00:00
return (
<div>
<ManageScroll />
2020-01-11 08:56:31 +00:00
<div className='content-wrap'>
<div className='content-wrap-inside'>
<Switch>
<Route exact path='/classfeed'>
<ClassFeed />
</Route>
<Route exact path='/usage/:name'>
2022-05-10 01:22:45 +00:00
<Usage token={token} />
</Route>
2022-08-09 07:21:40 +00:00
<Route exact path='/display/lcars1'>
<LCARS1Display token={token} />
</Route>
2023-04-05 04:54:49 +00:00
<Route exact path='/display/lcars2'>
<LCARS2Display token={token} />
</Route>
<Route path='/'>
<Container>
<div className='hero'>
<Link to='/'>
<img src='/logo-long.svg' className='logo-long' />
</Link>
</div>
{window.location.hostname !== 'my.protospace.ca' &&
<p style={{ background: 'yellow' }}>~~~~~ Development site ~~~~~</p>
}
</Container>
<Menu>
<Container>
<Menu.Item
2022-04-05 20:36:46 +00:00
icon='home'
as={Link}
to='/'
/>
2021-12-08 22:02:30 +00:00
<Dropdown item text='Member' id='ps-menu'>
<Dropdown.Menu>
<Dropdown.Item
content='Account'
as={Link}
to='/account'
/>
<Dropdown.Item
content='Transactions'
as={Link}
to='/transactions'
/>
<Dropdown.Item
content='Paymaster'
as={Link}
to='/paymaster'
/>
<Dropdown.Item
content='Training'
as={Link}
to='/training'
/>
<Dropdown.Item
content='Cards / Access'
as={Link}
to='/cards'
/>
</Dropdown.Menu>
</Dropdown>
<Dropdown item text='Space' id='ps-menu'>
<Dropdown.Menu>
<Dropdown.Item
content='Member List'
as={Link}
to='/members'
/>
<Dropdown.Item
content='Classes'
as={Link}
to='/classes'
/>
<Dropdown.Item
content='Utilities'
as={Link}
to='/utils'
/>
<Dropdown.Item
content='Charts'
as={Link}
to='/charts'
/>
{user && isAdmin(user) && <Dropdown.Item
content='Admin'
as={Link}
to='/admin'
/>}
{user && isAdmin(user) && <Dropdown.Item
content='Transactions'
as={Link}
to='/admintrans'
/>}
</Dropdown.Menu>
</Dropdown>
{user && <Menu.Menu position='right'>
<Menu.Item
2022-05-09 22:36:11 +00:00
content={yousure ? 'Log out?' : ''}
onClick={logout}
2022-04-05 20:36:46 +00:00
icon='sign out'
/>
<Menu.Item fitted content='' />
</Menu.Menu>}
</Container>
</Menu>
<Route exact path='/'>
<Home token={token} setTokenCache={setTokenCache} user={user} refreshUser={refreshUser} />
</Route>
<div className='topPadding'>
2020-02-17 03:28:17 +00:00
<Switch>
2023-05-30 19:35:07 +00:00
<Route path='/storage/:id'>
<StorageDetail token={token} user={user} />
</Route>
2023-06-03 18:56:01 +00:00
<Route path='/storage'>
<Storage token={token} user={user} />
</Route>
2022-04-10 07:11:53 +00:00
<Route path='/debug'>
<Debug token={token} user={user} />
</Route>
<Route path='/password/reset/confirm/:uid/:token'>
<ConfirmReset />
2020-02-17 03:28:17 +00:00
</Route>
<Route path='/password/reset'>
<PasswordReset />
2020-02-17 03:28:17 +00:00
</Route>
2020-01-10 00:29:52 +00:00
<Route path='/utils'>
<Paste token={token} />
</Route>
<Route path='/sign'>
<Sign token={token} />
2020-02-17 03:28:17 +00:00
</Route>
2020-01-10 06:24:37 +00:00
<Route path='/charts'>
<Charts />
2020-02-17 03:28:17 +00:00
</Route>
2020-01-10 06:24:37 +00:00
<Route path='/auth'>
<Auth user={user} />
2020-02-17 03:28:17 +00:00
</Route>
<Route path='/subscribe'>
<Subscribe />
</Route>
<Route exact path='/classes'>
<Classes token={token} user={user} refreshUser={refreshUser} />
2020-02-17 03:28:17 +00:00
</Route>
2022-05-17 02:38:20 +00:00
<Route path='/garden'>
<Garden />
</Route>
{user && user.member.set_details ?
<Switch>
2023-05-30 23:44:05 +00:00
<Route path='/claimshelf'>
<ClaimShelf token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/account'>
<Account token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/transactions/:id'>
<TransactionDetail token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/transactions'>
<Transactions user={user} />
</Route>
<Route path='/paymaster'>
2022-08-23 02:54:19 +00:00
<Paymaster token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/cards'>
<Cards token={token} user={user} />
</Route>
<Route path='/training'>
<Training user={user} />
</Route>
<Route path='/courses/:id'>
<CourseDetail token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/courses'>
<Courses token={token} user={user} />
</Route>
<Route path='/classes/:id'>
<ClassDetail token={token} user={user} refreshUser={refreshUser} />
</Route>
<Route path='/members/:id'>
<MemberDetail token={token} user={user} />
</Route>
<Route path='/members'>
<Members token={token} user={user} />
</Route>
{user && isAdmin(user) &&
<Route path='/admin'>
<Admin token={token} user={user} />
</Route>
}
{user && isAdmin(user) &&
<Route path='/admintrans'>
<AdminTransactions token={token} user={user} />
</Route>
}
<Route path='/:page'>
<NotFound />
</Route>
</Switch>
:
<Route path='/:page'>
<PleaseLogin />
2020-02-17 03:28:17 +00:00
</Route>
}
</Switch>
</div>
</Route>
</Switch>
2020-01-11 08:56:31 +00:00
</div>
</div>
2020-01-09 09:04:32 +00:00
2020-01-11 08:56:31 +00:00
<Footer />
</div>
2020-01-09 07:38:49 +00:00
)
};
2020-01-06 23:32:29 +00:00
export default App;