Files
5chan/src/app.tsx
T

177 lines
6.8 KiB
TypeScript
Raw Normal View History

2024-08-26 17:39:27 +02:00
import { useEffect, useState } from 'react';
import { Outlet, Route, Routes, useLocation, useNavigate, useParams } from 'react-router-dom';
2024-10-29 17:40:09 +01:00
import { useAccountComment, useAccountComments } from '@plebbit/plebbit-react-hooks';
2024-12-24 17:13:12 +01:00
import { initSnow, removeSnow, shouldShowSnow } from './lib/snow';
2024-06-17 15:39:49 +02:00
import { isAllView, isSubscriptionsView } from './lib/utils/view-utils';
import useIsMobile from './hooks/use-is-mobile';
import useTheme from './hooks/use-theme';
2024-10-29 17:40:09 +01:00
import useTimeFilter from './hooks/use-time-filter';
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-07-02 13:32:50 +02:00
import FAQ from './views/faq';
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';
2024-12-24 17:13:12 +01:00
import useSpecialThemeStore from './stores/use-special-theme-store';
2024-02-27 17:58:08 +01:00
const ValidateRouteParams = () => {
2024-10-29 17:40:09 +01:00
const { accountCommentIndex, timeFilterName } = useParams();
const { timeFilterNames, lastVisitTimeFilterName } = useTimeFilter();
const { accountComments } = useAccountComments();
2024-10-29 17:40:09 +01:00
const isValidAccountCommentIndex =
!accountCommentIndex ||
(!isNaN(parseInt(accountCommentIndex)) &&
parseInt(accountCommentIndex) >= 0 &&
accountComments?.length > 0 &&
parseInt(accountCommentIndex) < accountComments.length);
2024-05-06 21:40:17 +02:00
const isDynamicTimeFilter = (filter: string) => /^\d+[hdwmy]$/.test(filter);
2024-10-29 17:40:09 +01:00
const isTimeFilterNameValid =
!timeFilterName || timeFilterNames.includes(timeFilterName as any) || timeFilterName === lastVisitTimeFilterName || isDynamicTimeFilter(timeFilterName);
if (!isValidAccountCommentIndex || !isTimeFilterNameValid) {
2024-05-06 21:40:17 +02:00
return <NotFound />;
}
2024-10-29 17:40:09 +01:00
return <Outlet />;
};
const BoardLayout = () => {
const { accountCommentIndex, subplebbitAddress } = useParams();
const location = useLocation();
const isMobile = useIsMobile();
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
const pendingPost = useAccountComment({ commentIndex: accountCommentIndex ? parseInt(accountCommentIndex) : undefined });
2024-12-24 17:13:12 +01:00
// Christmas theme
const { isEnabled: isSpecialEnabled } = useSpecialThemeStore();
useEffect(() => {
if (isSpecialEnabled) {
initSnow({ flakeCount: 150 });
}
return () => {
removeSnow();
};
}, [isSpecialEnabled]);
// force rerender of post form when navigating between pages, except when opening settings modal in current view
const key = location.pathname.endsWith('/settings')
? `${subplebbitAddress}-${location.pathname.replace(/\/settings$/, '')}`
: `${subplebbitAddress}-${location.pathname}`;
return (
<div className={styles.boardLayout}>
<TopBar />
<BoardHeader />
{isMobile
? (subplebbitAddress || isInAllView || isInSubscriptionsView || pendingPost?.subplebbitAddress) && (
<>
<MobileBoardButtons />
<PostForm key={key} />
</>
)
: (subplebbitAddress || isInAllView || isInSubscriptionsView || pendingPost?.subplebbitAddress) && (
<>
<PostForm key={key} />
{!(isInAllView || isInSubscriptionsView) && <SubplebbitStats />}
<DesktopBoardButtons />
</>
)}
<Outlet />
</div>
);
};
const GlobalLayout = () => {
2024-08-26 17:39:27 +02:00
const [theme, setTheme] = useState('');
const [currentTheme] = useTheme();
useEffect(() => {
2024-08-26 17:39:27 +02:00
if (currentTheme !== theme) {
setTheme(currentTheme);
}
}, [currentTheme, theme]);
useEffect(() => {
if (theme) {
document.body.classList.add(theme);
return () => {
document.body.classList.remove(theme);
};
}
}, [theme]);
return (
2024-04-25 22:02:55 +02:00
<>
<ChallengeModal />
<Outlet />
</>
);
};
2024-03-17 21:47:16 +01:00
const App = () => {
// react router doesn't handle the %23 hash correctly, so we need to replace it with #
const navigate = useNavigate();
const location = useLocation();
useEffect(() => {
const currentPath = location.pathname + location.hash;
if (currentPath.includes('%23')) {
const correctedPath = currentPath.replace('%23', '#');
navigate(correctedPath, { replace: true });
}
}, [location, navigate]);
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 />} />
2024-07-02 13:32:50 +02:00
<Route path='/faq' element={<FAQ />} />
<Route path='*' element={<NotFound />} />
<Route element={<ValidateRouteParams />}>
2024-10-29 17:40:09 +01:00
<Route element={<BoardLayout />}>
<Route path='/p/:subplebbitAddress' element={<Board />} />
<Route path='/p/:subplebbitAddress/settings' element={<Board />} />
<Route path='/p/:subplebbitAddress/catalog' element={<Catalog />} />
<Route path='/p/:subplebbitAddress/catalog/settings' element={<Catalog />} />
2024-07-02 13:32:50 +02:00
2024-10-29 17:40:09 +01: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
2024-10-29 17:40:09 +01: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-04-25 22:02:55 +02:00
2024-10-29 17:40:09 +01: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-10-29 17:40:09 +01:00
<Route path='/profile/:accountCommentIndex' element={<PendingPost />} />
<Route path='/profile/:accountCommentIndex/settings' element={<PendingPost />} />
</Route>
2024-04-25 22:02:55 +02:00
</Route>
2024-03-18 15:04:30 +01:00
</Route>
2024-02-27 17:58:08 +01:00
</Routes>
</div>
);
};
export default App;