Files
5chan/src/app.tsx
T

139 lines
5.7 KiB
TypeScript
Raw Normal View History

import { Outlet, Route, Routes, useLocation, useParams } from 'react-router-dom';
import { isAllView, isHomeView, isNotFoundView, isSubscriptionsView } from './lib/utils/view-utils';
import useIsMobile from './hooks/use-is-mobile';
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';
import Post from './views/post';
2024-04-25 22:02:55 +02:00
import { DesktopBoardButtons, MobileBoardButtons } from './components/board-buttons';
import BoardHeader from './components/board-header';
2024-04-25 22:02:55 +02:00
import ChallengeModal from './components/challenge-modal';
2024-03-20 13:00:11 +01:00
import PostForm from './components/post-form';
import SubplebbitStats from './components/subplebbit-stats';
import TopBar from './components/topbar';
import { useEffect } from 'react';
import { nsfwTags } from './views/home/home';
import useDefaultSubplebbits from './hooks/use-default-subplebbits';
import useThemeStore from './stores/use-theme-store';
import { timeFilterNames } from './hooks/use-time-filter';
2024-02-27 17:58:08 +01:00
const BoardLayout = () => {
const { accountCommentIndex, subplebbitAddress, timeFilterName } = useParams();
const location = useLocation();
const isMobile = useIsMobile();
const isInAllView = isAllView(location.pathname, useParams());
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
2024-05-06 21:40:17 +02:00
const isValidAccountCommentIndex = !accountCommentIndex || (!isNaN(parseInt(accountCommentIndex)) && parseInt(accountCommentIndex) >= 0);
if (!isValidAccountCommentIndex || (timeFilterName && !timeFilterNames.includes(timeFilterName))) {
2024-05-06 21:40:17 +02:00
return <NotFound />;
}
// force rerender of post form when navigating between pages
const key = `${subplebbitAddress}-${location.pathname}`;
return (
<div className={styles.boardLayout}>
<TopBar />
<BoardHeader />
{isMobile
2024-06-08 11:41:21 +02:00
? (subplebbitAddress || isInAllView || isInSubscriptionsView) && (
<>
<PostForm key={key} />
<MobileBoardButtons />
</>
)
2024-06-08 11:41:21 +02:00
: (subplebbitAddress || isInAllView || isInSubscriptionsView) && (
<>
<PostForm key={key} />
{!(isInAllView || isInSubscriptionsView) && <SubplebbitStats />}
<DesktopBoardButtons />
</>
)}
<Outlet />
</div>
);
};
const GlobalLayout = () => {
const location = useLocation();
const { subplebbitAddress } = useParams<{ subplebbitAddress: string }>();
const getTheme = useThemeStore((state) => state.getTheme);
const setTheme = useThemeStore((state) => state.setTheme);
const subplebbits = useDefaultSubplebbits();
const isInHomeView = isHomeView(location.pathname);
const isInNotFoundView = isNotFoundView(location.pathname, useParams());
2024-02-28 21:18:37 +01:00
useEffect(() => {
let theme = 'yotsuba-b';
2024-02-28 21:18:37 +01:00
if (isInHomeView || isInNotFoundView) {
theme = 'yotsuba';
} else if (subplebbitAddress) {
theme = getTheme(subplebbitAddress);
const subplebbit = subplebbits.find((s) => s.address === subplebbitAddress);
if (subplebbit && subplebbit.tags && subplebbit.tags.some((tag) => nsfwTags.includes(tag)) && theme === 'yotsuba-b') {
theme = 'yotsuba';
setTheme(subplebbitAddress, 'yotsuba');
}
}
document.body.classList.remove('yotsuba', 'yotsuba-b', 'futaba', 'burichan', 'tomorrow', 'photon');
document.body.classList.add(theme);
}, [location.pathname, subplebbitAddress, getTheme, setTheme, subplebbits, isInHomeView]);
return (
2024-04-25 22:02:55 +02:00
<>
<ChallengeModal />
<Outlet />
</>
);
};
2024-03-17 21:47:16 +01:00
const App = () => {
2024-02-27 17:58:08 +01:00
return (
<div className={styles.app}>
2024-02-27 17:58:08 +01:00
<Routes>
<Route element={<GlobalLayout />}>
2024-04-25 22:02:55 +02:00
<Route path='/' element={<Home />} />
<Route element={<BoardLayout />}>
<Route path='/p/:subplebbitAddress' element={<Board />} />
2024-05-09 16:04:24 +02:00
<Route path='/p/:subplebbitAddress/settings' element={<Board />} />
2024-04-25 22:02:55 +02:00
<Route path='/p/:subplebbitAddress/catalog' element={<Catalog />} />
2024-05-09 16:04:24 +02:00
<Route path='/p/:subplebbitAddress/catalog/settings' element={<Catalog />} />
2024-04-25 22:02:55 +02:00
<Route path='/p/:subplebbitAddress/c/:commentCid' element={<Post />} />
<Route path='/p/:subplebbitAddress/c/:commentCid/settings' element={<Post />} />
<Route path='/p/:subplebbitAddress/description' element={<Post />} />
<Route path='/p/:subplebbitAddress/description/settings' element={<Post />} />
<Route path='/p/:subplebbitAddress/rules' element={<Post />} />
<Route path='/p/:subplebbitAddress/rules/settings' element={<Post />} />
2024-04-25 22:02:55 +02:00
<Route path='/p/all/:timeFilterName?' element={<Board />} />
<Route path='/p/all/:timeFilterName?/settings' element={<Board />} />
<Route path='/p/all/description' element={<Post />} />
<Route path='/p/all/catalog/:timeFilterName?' element={<Catalog />} />
<Route path='/p/all/catalog/:timeFilterName?/settings' element={<Catalog />} />
2024-05-17 17:25:25 +02:00
<Route path='/p/subscriptions/:timeFilterName?' element={<Board />} />
<Route path='/p/subscriptions/:timeFilterName?/settings' element={<Board />} />
<Route path='/p/subscriptions/catalog/:timeFilterName?' element={<Catalog />} />
<Route path='/p/subscriptions/catalog/:timeFilterName?/settings' element={<Catalog />} />
2024-05-17 17:25:25 +02:00
2024-04-25 22:02:55 +02:00
<Route path='/profile/:accountCommentIndex' element={<PendingPost />} />
2024-05-09 16:04:24 +02:00
<Route path='/profile/:accountCommentIndex/settings' element={<PendingPost />} />
2024-04-25 22:02:55 +02:00
</Route>
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;