Files
5chan/src/app.tsx
T

92 lines
3.2 KiB
TypeScript
Raw Normal View History

import { useEffect } from 'react';
import { Outlet, Route, Routes, useLocation, useParams } from 'react-router-dom';
import { isHomeView } from './lib/utils/view-utils';
2024-02-28 21:18:37 +01:00
import useTheme from './hooks/use-theme';
import styles from './app.module.css';
2024-04-25 22:02:55 +02:00
import Board from './views/board';
2024-04-12 14:28:06 +02:00
import Catalog from './views/catalog';
2024-03-20 13:00:11 +01:00
import Home from './views/home';
2024-05-06 21:40:17 +02:00
import NotFound from './views/not-found';
2024-04-25 22:02:55 +02:00
import PendingPost from './views/pending-post';
2024-04-02 17:00:36 +02:00
import PostPage from './views/post-page';
2024-03-20 22:06:35 +01:00
import Settings from './views/settings';
2024-03-19 21:12:57 +01:00
import BoardBanner from './components/board-banner';
2024-04-25 22:02:55 +02:00
import { DesktopBoardButtons, MobileBoardButtons } from './components/board-buttons';
import BoardNav from './components/board-nav';
import ChallengeModal from './components/challenge-modal';
import SubplebbitStats from './components/subplebbit-stats';
2024-03-20 13:00:11 +01:00
import PostForm from './components/post-form';
2024-02-27 17:58:08 +01:00
const BoardLayout = () => {
2024-05-06 21:40:17 +02:00
const { accountCommentIndex, commentCid, subplebbitAddress } = useParams();
const location = useLocation();
2024-05-06 21:40:17 +02:00
const isValidAccountCommentIndex = !accountCommentIndex || (!isNaN(parseInt(accountCommentIndex)) && parseInt(accountCommentIndex) >= 0);
const isValidCommentCid = !commentCid || /^Qm[a-zA-Z0-9]{44}$/.test(commentCid);
const isValidSubplebbitAddress = !subplebbitAddress || subplebbitAddress.includes('.') || /^12D3K[a-zA-Z0-9]{44}$/.test(subplebbitAddress);
2024-05-06 21:40:17 +02:00
if (!isValidAccountCommentIndex || !isValidCommentCid || !isValidSubplebbitAddress) {
return <NotFound />;
}
// force rerender of post form when navigating between pages
const key = `${subplebbitAddress}-${location.pathname}`;
return (
<div className={styles.boardLayout}>
<BoardNav />
<BoardBanner />
<MobileBoardButtons />
<PostForm key={key} />
<SubplebbitStats />
<DesktopBoardButtons />
<Outlet />
</div>
);
};
2024-02-27 17:58:08 +01:00
const App = () => {
const location = useLocation();
const isInHomeView = isHomeView(location.pathname);
2024-04-10 13:16:07 +02:00
const [theme] = useTheme();
2024-02-28 21:18:37 +01:00
useEffect(() => {
document.body.classList.forEach((className) => document.body.classList.remove(className));
const classToAdd = isInHomeView ? 'yotsuba' : theme;
document.body.classList.add(classToAdd);
}, [theme, isInHomeView]);
2024-02-28 21:18:37 +01:00
2024-04-25 22:02:55 +02:00
const globalLayout = (
<>
<ChallengeModal />
<Outlet />
</>
);
2024-03-17 21:47:16 +01:00
2024-02-27 17:58:08 +01:00
return (
<div className={styles.app}>
2024-02-27 17:58:08 +01:00
<Routes>
2024-04-25 22:02:55 +02:00
<Route element={globalLayout}>
<Route path='/' element={<Home />} />
<Route element={<BoardLayout />}>
<Route path='/p/:subplebbitAddress' element={<Board />} />
<Route path='/p/:subplebbitAddress/catalog' element={<Catalog />} />
<Route path='/p/:subplebbitAddress/c/:commentCid' element={<PostPage />} />
<Route path='/p/:subplebbitAddress/description' element={<PostPage />} />
<Route path='/p/:subplebbitAddress/rules' element={<PostPage />} />
<Route path='/profile/:accountCommentIndex' element={<PendingPost />} />
</Route>
<Route path='/settings' element={<Settings />} />
2024-05-06 21:40:17 +02:00
<Route path='*' element={<NotFound />} />
2024-03-18 15:04:30 +01:00
</Route>
2024-02-27 17:58:08 +01:00
</Routes>
</div>
);
};
export default App;