Files
5chan/src/app.tsx
T

344 lines
14 KiB
TypeScript
Raw Normal View History

import { lazy, Suspense, useEffect } from 'react';
import { Navigate, Outlet, Route, Routes, useLocation, useParams } from 'react-router-dom';
import { useAccount, useAccountComment, useSubplebbit } from '@bitsocialnet/bitsocial-react-hooks';
2024-12-24 17:15:51 +01:00
import { initSnow, removeSnow } from './lib/snow';
import { isAllView, isCatalogView, isModView, isSubscriptionsView } from './lib/utils/view-utils';
import { preloadThemeAssets } from './lib/utils/preload-utils';
2025-03-05 12:01:48 +01:00
import useReplyModalStore from './stores/use-reply-modal-store';
import useCreateBoardModalStore from './stores/use-create-board-modal-store';
2025-03-05 12:01:48 +01:00
import useSpecialThemeStore from './stores/use-special-theme-store';
import useIsMobile from './hooks/use-is-mobile';
import { useAccountSubplebbitAddresses } from './hooks/use-account-subplebbit-addresses';
import useTheme from './hooks/use-theme';
import { useDirectories } from './hooks/use-directories';
import { useResolvedSubplebbitAddress } from './hooks/use-resolved-subplebbit-address';
import {
getBoardPath,
getSubplebbitAddress,
isBoardModRoute,
isDirectoryBoard,
isLegacyBoardModQueueRoute,
isPostRoute,
isPendingPostRoute,
isModQueueRoute,
isValidBoardModRoute,
isValidModRoute,
} from './lib/utils/route-utils';
import styles from './app.module.css';
import { DesktopBoardButtons, MobileBoardButtons } from './components/board-buttons';
import Board from './views/board';
import Blotter from './views/blotter';
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';
import ModQueueView from './views/mod-queue';
import NotAllowed from './views/not-allowed';
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';
import Rules from './views/rules';
import BoardHeader from './components/board-header';
import FeedCacheContainer from './components/feed-cache-container';
2024-03-20 13:00:11 +01:00
import PostForm from './components/post-form';
import BoardBlotter from './components/board-blotter';
import BoardsBar from './components/boards-bar';
const AccountDataEditor = lazy(() => import('./views/account-data-editor'));
const BoardsBarEditModal = lazy(() => import('./components/boards-bar-edit-modal'));
const CreateBoardModal = lazy(() => import('./components/create-board-modal'));
const ChallengeModal = lazy(() => import('./components/challenge-modal'));
const DirectoryModal = lazy(() => import('./components/directory-modal'));
const DisclaimerModal = lazy(() => import('./components/disclaimer-modal'));
const ReplyModal = lazy(() => import('./components/reply-modal'));
const SettingsModal = lazy(() => import('./components/settings-modal'));
2024-02-27 17:58:08 +01:00
// Preload all theme assets (buttons, backgrounds) immediately on app load
// to prevent visible loading delays when switching themes
preloadThemeAssets();
const hasModQueueAccessRole = (role?: string): boolean => role === 'admin' || role === 'owner' || role === 'moderator';
2024-10-29 17:40:09 +01:00
const BoardLayout = () => {
const params = useParams();
const { accountCommentIndex, boardIdentifier, pageNumber } = params;
2024-10-29 17:40:09 +01:00
const location = useLocation();
const isMobile = useIsMobile();
const isInAllView = isAllView(location.pathname);
const isInSubscriptionsView = isSubscriptionsView(location.pathname, useParams());
const isInModView = isModView(location.pathname);
const directories = useDirectories();
const subplebbitAddress = boardIdentifier ? getSubplebbitAddress(boardIdentifier, directories) : undefined;
2024-10-29 17:40:09 +01:00
const pendingPost = useAccountComment({ commentIndex: accountCommentIndex ? parseInt(accountCommentIndex) : undefined });
const { closeCreateBoardModal } = useCreateBoardModalStore();
const isOnPostRoute = isPostRoute(location.pathname);
const isOnPendingPostRoute = isPendingPostRoute(location.pathname);
2026-01-07 16:03:02 +01:00
const isOnModQueueRoute = isModQueueRoute(location.pathname);
const shouldRenderOutlet = isOnPostRoute || isOnPendingPostRoute || isOnModQueueRoute;
const isInCatalogView = isCatalogView(location.pathname, params);
2024-12-24 17:13:12 +01:00
// Christmas theme
const { isEnabled: isSpecialEnabled } = useSpecialThemeStore();
useEffect(() => {
2024-12-25 17:11:14 +01:00
if (isSpecialEnabled && !isMobile) {
2024-12-24 17:13:12 +01:00
initSnow({ flakeCount: 150 });
}
return () => {
removeSnow();
};
2024-12-25 17:11:56 +01:00
}, [isSpecialEnabled, isMobile]);
2024-12-24 17:13:12 +01:00
// Close create board modal when navigating to a different page
useEffect(() => {
closeCreateBoardModal();
}, [location.pathname, closeCreateBoardModal]);
// 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}`;
if (pageNumber === '1') {
return <Navigate to='/not-found' replace />;
}
// Invalid /mod/ paths (e.g. /mod/modqueue, /mod/asdoijasd) -> not-found
if (location.pathname.startsWith('/mod/') && !isValidModRoute(location.pathname)) {
return <Navigate to='/not-found' replace />;
}
if (isLegacyBoardModQueueRoute(location.pathname)) {
return <Navigate to='/not-found' replace />;
}
// Invalid board-scoped mod paths (e.g. /biz/mod, /biz/mod/asdoijasd) -> not-found
if (isBoardModRoute(location.pathname) && !isValidBoardModRoute(location.pathname)) {
return <Navigate to='/not-found' replace />;
}
// Normalize address URLs to directory codes: /anime-and-manga.eth/thread/xxx -> /a/thread/xxx
if (boardIdentifier && !isDirectoryBoard(boardIdentifier, directories)) {
const canonicalBoardIdentifier = getBoardPath(boardIdentifier, directories);
if (canonicalBoardIdentifier !== boardIdentifier) {
const canonicalPath = location.pathname.replace(`/${boardIdentifier}`, `/${canonicalBoardIdentifier}`);
return <Navigate to={canonicalPath + (location.search || '')} replace />;
}
}
return (
<div className={styles.boardLayout}>
<BoardsBar />
<Suspense fallback={null}>
<CreateBoardModal />
</Suspense>
<Suspense fallback={null}>
<BoardsBarEditModal />
</Suspense>
<Suspense fallback={null}>
<DirectoryModal />
</Suspense>
<Suspense fallback={null}>
<DisclaimerModal />
</Suspense>
<BoardHeader />
{isMobile
? (subplebbitAddress || isInAllView || isInModView || isInSubscriptionsView || pendingPost?.subplebbitAddress || isOnModQueueRoute) &&
(isInCatalogView ? (
<>
<PostForm key={key} />
2025-12-29 16:40:33 +01:00
<MobileBoardButtons />
</>
) : (
<>
<MobileBoardButtons />
<PostForm key={key} />
</>
))
: (subplebbitAddress || isInAllView || isInModView || isInSubscriptionsView || pendingPost?.subplebbitAddress || isOnModQueueRoute) && (
<>
<PostForm key={key} />
{!(isInAllView || isInSubscriptionsView || isInModView) && !isOnModQueueRoute && <BoardBlotter />}
<DesktopBoardButtons />
</>
)}
<FeedCacheContainer />
{shouldRenderOutlet && <Outlet />}
</div>
);
};
const GlobalLayout = () => {
useTheme();
2025-12-28 22:16:38 +01:00
const { activeCid, parentNumber, threadNumber, threadCid, subplebbitAddress, closeModal, showReplyModal, scrollY } = useReplyModalStore();
2025-03-05 12:01:48 +01:00
const location = useLocation();
const isInSettingsView = location.pathname.endsWith('/settings');
return (
2024-04-25 22:02:55 +02:00
<>
<Suspense fallback={null}>
<ChallengeModal />
</Suspense>
2025-03-05 12:01:48 +01:00
{activeCid && threadCid && subplebbitAddress && (
<Suspense fallback={null}>
<ReplyModal
closeModal={closeModal}
parentCid={activeCid}
parentNumber={parentNumber}
threadNumber={threadNumber}
postCid={threadCid}
scrollY={scrollY}
showReplyModal={showReplyModal}
subplebbitAddress={subplebbitAddress}
/>
</Suspense>
2025-03-05 12:01:48 +01:00
)}
{isInSettingsView && (
<Suspense fallback={null}>
<SettingsModal />
</Suspense>
)}
2024-04-25 22:02:55 +02:00
<Outlet />
</>
);
};
2024-03-17 21:47:16 +01:00
/** Wraps Board with viewType/boardIdentifier derived from current route. Used when infinite scroll is OFF. */
const BoardFeedRoute = () => {
const location = useLocation();
const params = useParams();
const viewType: 'all' | 'subs' | 'mod' | 'board' = isAllView(location.pathname)
? 'all'
: isSubscriptionsView(location.pathname, params)
? 'subs'
: isModView(location.pathname)
? 'mod'
: 'board';
return <Board viewType={viewType} boardIdentifier={params.boardIdentifier} />;
};
/** Wraps Catalog with viewType/boardIdentifier derived from current route. Used when infinite scroll is OFF. */
const CatalogFeedRoute = () => {
const location = useLocation();
const params = useParams();
const viewType: 'all' | 'subs' | 'mod' | 'board' = isAllView(location.pathname)
? 'all'
: isSubscriptionsView(location.pathname, params)
? 'subs'
: isModView(location.pathname)
? 'mod'
: 'board';
return <Catalog viewType={viewType} boardIdentifier={params.boardIdentifier} />;
};
const ModQueueRoute = () => {
const { boardIdentifier } = useParams();
const account = useAccount();
const accountAddress = account?.author?.address;
const subplebbitAddress = useResolvedSubplebbitAddress();
const subplebbit = useSubplebbit({ subplebbitAddress });
const accountSubplebbitAddresses = useAccountSubplebbitAddresses();
if (!account) {
return null;
}
if (!accountAddress) {
return <Navigate to='/not-allowed' replace />;
}
if (!boardIdentifier) {
return accountSubplebbitAddresses.length > 0 ? <ModQueueView /> : <Navigate to='/not-allowed' replace />;
}
// Wait for board role metadata before enforcing access to avoid false redirects during initial load.
const boardState = subplebbit?.state;
const isBoardLoading = !subplebbit || !boardState || (boardState !== 'succeeded' && boardState !== 'failed');
if (isBoardLoading) {
return null;
}
const accountRole = subplebbit?.roles?.[accountAddress]?.role;
return hasModQueueAccessRole(accountRole) ? <ModQueueView /> : <Navigate to='/not-allowed' replace />;
};
const App = () => {
// Feed routes are always rendered by FeedCacheContainer (Virtuoso used for all modes)
const boardFeedElement = null;
const catalogFeedElement = null;
return (
<div className={styles.app}>
<Routes>
<Route element={<GlobalLayout />}>
<Route path='/' element={<Home />} />
<Route path='/faq' element={<FAQ />} />
<Route path='/rules/:boardIdentifier?' element={<Rules />} />
<Route path='/blotter' element={<Blotter />} />
<Route
path='/settings/account-data'
element={
<Suspense fallback={null}>
<AccountDataEditor />
</Suspense>
}
/>
<Route element={<BoardLayout />}>
{/* Canonical multiboard routes (no time filter) */}
<Route path='/all' element={boardFeedElement} />
<Route path='/all/settings' element={boardFeedElement} />
<Route path='/all/catalog' element={catalogFeedElement} />
<Route path='/all/catalog/settings' element={catalogFeedElement} />
<Route path='/subs' element={boardFeedElement} />
<Route path='/subs/settings' element={boardFeedElement} />
<Route path='/subs/catalog' element={catalogFeedElement} />
<Route path='/subs/catalog/settings' element={catalogFeedElement} />
<Route path='/mod' element={boardFeedElement} />
<Route path='/mod/settings' element={boardFeedElement} />
<Route path='/mod/catalog' element={catalogFeedElement} />
<Route path='/mod/catalog/settings' element={catalogFeedElement} />
2026-01-07 16:03:02 +01:00
<Route path='/mod/queue' element={<ModQueueRoute />} />
<Route path='/mod/queue/settings' element={<ModQueueRoute />} />
{/* Invalid subpaths: old URLs and unknown paths -> not-found */}
<Route path='/mod/modqueue' element={<Navigate to='/not-found' replace />} />
<Route path='/mod/modqueue/settings' element={<Navigate to='/not-found' replace />} />
<Route path='/all/*' element={<Navigate to='/not-found' replace />} />
<Route path='/subs/*' element={<Navigate to='/not-found' replace />} />
<Route path='/mod/*' element={<Navigate to='/not-found' replace />} />
<Route path='/:boardIdentifier/:pageNumber' element={boardFeedElement} />
<Route path='/:boardIdentifier/:pageNumber/settings' element={boardFeedElement} />
<Route path='/:boardIdentifier' element={boardFeedElement} />
<Route path='/:boardIdentifier/settings' element={boardFeedElement} />
<Route path='/:boardIdentifier/catalog' element={catalogFeedElement} />
<Route path='/:boardIdentifier/catalog/settings' element={catalogFeedElement} />
2026-01-07 16:03:02 +01:00
<Route path='/:boardIdentifier/mod/queue' element={<ModQueueRoute />} />
<Route path='/:boardIdentifier/mod/queue/settings' element={<ModQueueRoute />} />
<Route path='/:boardIdentifier/modqueue' element={<Navigate to='/not-found' replace />} />
<Route path='/:boardIdentifier/modqueue/settings' element={<Navigate to='/not-found' replace />} />
<Route path='/:boardIdentifier/mod' element={<Navigate to='/not-found' replace />} />
<Route path='/:boardIdentifier/mod/*' element={<Navigate to='/not-found' replace />} />
<Route path='/:boardIdentifier/thread/:commentCid' element={<Post />} />
<Route path='/:boardIdentifier/thread/:commentCid/settings' element={<Post />} />
<Route path='/pending/:accountCommentIndex' element={<PendingPost />} />
<Route path='/pending/:accountCommentIndex/settings' element={<PendingPost />} />
</Route>
<Route path='/not-allowed' element={<NotAllowed />} />
<Route path='/not-found' element={<NotFound />} />
<Route path='*' element={<NotFound />} />
2024-03-18 15:04:30 +01:00
</Route>
</Routes>
</div>
);
};
2024-02-27 17:58:08 +01:00
export default App;