2026-02-18 15:31:01 +08:00
|
|
|
import { useEffect } from 'react';
|
2026-02-16 19:17:38 +08:00
|
|
|
import { Navigate, Outlet, Route, Routes, useLocation, useParams } from 'react-router-dom';
|
|
|
|
|
import { useAccount, useAccountComment, useSubplebbit } from '@plebbit/plebbit-react-hooks';
|
|
|
|
|
import useAccountsStore from '@plebbit/plebbit-react-hooks/dist/stores/accounts';
|
2024-12-24 17:15:51 +01:00
|
|
|
import { initSnow, removeSnow } from './lib/snow';
|
2025-06-04 16:37:17 +02:00
|
|
|
import { isAllView, isModView, isSubscriptionsView } from './lib/utils/view-utils';
|
2025-12-29 13:55:20 +01:00
|
|
|
import { preloadThemeAssets } from './lib/utils/preload-utils';
|
2025-03-05 12:01:48 +01:00
|
|
|
import useReplyModalStore from './stores/use-reply-modal-store';
|
2025-11-06 22:20:46 +01:00
|
|
|
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';
|
2024-05-31 15:16:21 +02:00
|
|
|
import useIsMobile from './hooks/use-is-mobile';
|
2024-08-29 14:18:20 +02:00
|
|
|
import useTheme from './hooks/use-theme';
|
2026-01-28 16:51:27 +08:00
|
|
|
import { useDirectories } from './hooks/use-directories';
|
2026-02-16 19:17:38 +08:00
|
|
|
import { useResolvedSubplebbitAddress } from './hooks/use-resolved-subplebbit-address';
|
2026-01-07 16:03:02 +01:00
|
|
|
import { getSubplebbitAddress, isPostRoute, isPendingPostRoute, isModQueueRoute } from './lib/utils/route-utils';
|
2024-03-27 15:10:58 +01:00
|
|
|
import styles from './app.module.css';
|
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';
|
2025-12-29 00:26:15 +01:00
|
|
|
import Rules from './views/rules';
|
2024-05-06 21:40:17 +02:00
|
|
|
import NotFound from './views/not-found';
|
2026-02-16 19:17:38 +08:00
|
|
|
import NotAllowed from './views/not-allowed';
|
2024-04-25 22:02:55 +02:00
|
|
|
import PendingPost from './views/pending-post';
|
2024-06-11 15:49:26 +02:00
|
|
|
import Post from './views/post';
|
2026-01-07 16:03:02 +01:00
|
|
|
import ModQueueView from './views/mod-queue';
|
2024-04-25 22:02:55 +02:00
|
|
|
import { DesktopBoardButtons, MobileBoardButtons } from './components/board-buttons';
|
2024-05-31 15:16:21 +02:00
|
|
|
import BoardHeader from './components/board-header';
|
2024-04-25 22:02:55 +02:00
|
|
|
import ChallengeModal from './components/challenge-modal';
|
2025-11-06 22:20:46 +01:00
|
|
|
import CreateBoardModal from './components/create-board-modal';
|
2025-12-08 22:40:16 +01:00
|
|
|
import FeedCacheContainer from './components/feed-cache-container';
|
2025-03-05 12:01:48 +01:00
|
|
|
import ReplyModal from './components/reply-modal';
|
2024-03-20 13:00:11 +01:00
|
|
|
import PostForm from './components/post-form';
|
2024-05-31 15:16:21 +02:00
|
|
|
import SubplebbitStats from './components/subplebbit-stats';
|
|
|
|
|
import TopBar from './components/topbar';
|
2025-11-07 18:20:11 +01:00
|
|
|
import TopbarEditModal from './components/topbar-edit-modal';
|
|
|
|
|
import DirectoryModal from './components/directory-modal';
|
|
|
|
|
import DisclaimerModal from './components/disclaimer-modal';
|
2025-03-05 12:06:27 +01:00
|
|
|
import SettingsModal from './components/settings-modal';
|
2024-02-27 17:58:08 +01:00
|
|
|
|
2025-12-29 13:55:20 +01:00
|
|
|
// Preload all theme assets (buttons, backgrounds) immediately on app load
|
|
|
|
|
// to prevent visible loading delays when switching themes
|
|
|
|
|
preloadThemeAssets();
|
|
|
|
|
|
2026-02-16 19:17:38 +08:00
|
|
|
const hasModQueueAccessRole = (role?: string): boolean => role === 'admin' || role === 'owner' || role === 'moderator';
|
|
|
|
|
|
2024-10-29 17:40:09 +01:00
|
|
|
const BoardLayout = () => {
|
2025-11-07 12:50:58 +01:00
|
|
|
const { accountCommentIndex, boardIdentifier } = useParams();
|
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());
|
2025-06-04 16:37:17 +02:00
|
|
|
const isInModView = isModView(location.pathname);
|
2026-01-28 16:51:27 +08:00
|
|
|
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 });
|
2025-11-06 22:20:46 +01:00
|
|
|
const { closeCreateBoardModal } = useCreateBoardModalStore();
|
2024-10-29 17:40:09 +01:00
|
|
|
|
2025-12-08 22:40:16 +01:00
|
|
|
const isOnPostRoute = isPostRoute(location.pathname);
|
|
|
|
|
const isOnPendingPostRoute = isPendingPostRoute(location.pathname);
|
2026-01-07 16:03:02 +01:00
|
|
|
const isOnModQueueRoute = isModQueueRoute(location.pathname);
|
2025-12-08 22:40:16 +01:00
|
|
|
|
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
|
|
|
|
2025-11-06 22:20:46 +01:00
|
|
|
// Close create board modal when navigating to a different page
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
closeCreateBoardModal();
|
|
|
|
|
}, [location.pathname, closeCreateBoardModal]);
|
|
|
|
|
|
2024-08-06 22:34:27 +02:00
|
|
|
// 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}`;
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2024-04-23 21:37:03 +02:00
|
|
|
return (
|
2024-05-05 15:55:24 +02:00
|
|
|
<div className={styles.boardLayout}>
|
2024-05-29 21:00:13 +02:00
|
|
|
<TopBar />
|
2025-11-06 22:20:46 +01:00
|
|
|
<CreateBoardModal />
|
2025-11-07 18:20:11 +01:00
|
|
|
<TopbarEditModal />
|
|
|
|
|
<DirectoryModal />
|
|
|
|
|
<DisclaimerModal />
|
2024-05-29 21:00:13 +02:00
|
|
|
<BoardHeader />
|
2024-06-04 10:30:01 +02:00
|
|
|
{isMobile
|
2026-01-25 16:29:15 +08:00
|
|
|
? (subplebbitAddress || isInAllView || isInModView || isInSubscriptionsView || pendingPost?.subplebbitAddress || isOnModQueueRoute) && (
|
2024-06-04 10:30:01 +02:00
|
|
|
<>
|
2025-12-06 15:46:34 +01:00
|
|
|
<PostForm key={key} />
|
2025-12-29 16:40:33 +01:00
|
|
|
<MobileBoardButtons />
|
2024-06-04 10:30:01 +02:00
|
|
|
</>
|
|
|
|
|
)
|
2026-01-25 16:29:15 +08:00
|
|
|
: (subplebbitAddress || isInAllView || isInModView || isInSubscriptionsView || pendingPost?.subplebbitAddress || isOnModQueueRoute) && (
|
2024-06-04 10:30:01 +02:00
|
|
|
<>
|
|
|
|
|
<PostForm key={key} />
|
2026-01-09 17:18:09 +01:00
|
|
|
{!(isInAllView || isInSubscriptionsView || isInModView) && !isOnModQueueRoute && <SubplebbitStats />}
|
2024-06-04 10:30:01 +02:00
|
|
|
<DesktopBoardButtons />
|
|
|
|
|
</>
|
|
|
|
|
)}
|
2025-12-08 22:40:16 +01:00
|
|
|
<FeedCacheContainer />
|
2026-01-07 16:03:02 +01:00
|
|
|
{(isOnPostRoute || isOnPendingPostRoute || isOnModQueueRoute) && <Outlet />}
|
2024-05-05 15:55:24 +02:00
|
|
|
</div>
|
2024-04-23 21:37:03 +02:00
|
|
|
);
|
|
|
|
|
};
|
2024-04-27 12:41:54 +02:00
|
|
|
|
2024-06-17 12:17:36 +02:00
|
|
|
const GlobalLayout = () => {
|
2024-08-26 17:39:27 +02:00
|
|
|
const [currentTheme] = useTheme();
|
2024-08-26 11:32:40 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-02-18 15:31:01 +08:00
|
|
|
if (currentTheme) {
|
|
|
|
|
document.body.classList.add(currentTheme);
|
2024-08-26 17:39:27 +02:00
|
|
|
return () => {
|
2026-02-18 15:31:01 +08:00
|
|
|
document.body.classList.remove(currentTheme);
|
2024-08-26 17:39:27 +02:00
|
|
|
};
|
|
|
|
|
}
|
2026-02-18 15:31:01 +08:00
|
|
|
}, [currentTheme]);
|
2024-06-17 12:17:36 +02:00
|
|
|
|
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
|
|
|
|
2025-03-05 12:06:27 +01:00
|
|
|
const location = useLocation();
|
|
|
|
|
const isInSettingsView = location.pathname.endsWith('/settings');
|
|
|
|
|
|
2024-06-17 12:17:36 +02:00
|
|
|
return (
|
2024-04-25 22:02:55 +02:00
|
|
|
<>
|
|
|
|
|
<ChallengeModal />
|
2025-03-05 12:01:48 +01:00
|
|
|
{activeCid && threadCid && subplebbitAddress && (
|
|
|
|
|
<ReplyModal
|
|
|
|
|
closeModal={closeModal}
|
|
|
|
|
parentCid={activeCid}
|
2025-12-28 11:53:02 +01:00
|
|
|
parentNumber={parentNumber}
|
2025-12-28 22:16:38 +01:00
|
|
|
threadNumber={threadNumber}
|
2025-03-05 12:01:48 +01:00
|
|
|
postCid={threadCid}
|
|
|
|
|
scrollY={scrollY}
|
|
|
|
|
showReplyModal={showReplyModal}
|
|
|
|
|
subplebbitAddress={subplebbitAddress}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
2025-03-05 12:06:27 +01:00
|
|
|
{isInSettingsView && <SettingsModal />}
|
2024-04-25 22:02:55 +02:00
|
|
|
<Outlet />
|
|
|
|
|
</>
|
|
|
|
|
);
|
2024-06-17 12:17:36 +02:00
|
|
|
};
|
2024-03-17 21:47:16 +01:00
|
|
|
|
2026-02-16 19:17:38 +08:00
|
|
|
const ModQueueRoute = () => {
|
|
|
|
|
const { boardIdentifier } = useParams();
|
|
|
|
|
const account = useAccount();
|
|
|
|
|
const accountAddress = account?.author?.address;
|
|
|
|
|
const subplebbitAddress = useResolvedSubplebbitAddress();
|
|
|
|
|
const subplebbit = useSubplebbit({ subplebbitAddress });
|
|
|
|
|
|
|
|
|
|
const accountSubplebbitAddresses = useAccountsStore(
|
|
|
|
|
(state) => {
|
|
|
|
|
const activeAccountId = state.activeAccountId;
|
|
|
|
|
const activeAccount = activeAccountId ? state.accounts[activeAccountId] : undefined;
|
|
|
|
|
const accountSubplebbits = activeAccount?.subplebbits || {};
|
|
|
|
|
return Object.keys(accountSubplebbits);
|
|
|
|
|
},
|
|
|
|
|
(prev, next) => {
|
|
|
|
|
if (prev.length !== next.length) return false;
|
|
|
|
|
return prev.every((val, idx) => val === next[idx]);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
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 />;
|
|
|
|
|
};
|
|
|
|
|
|
2025-11-03 22:06:18 +01:00
|
|
|
const App = () => (
|
|
|
|
|
<div className={styles.app}>
|
|
|
|
|
<Routes>
|
|
|
|
|
<Route element={<GlobalLayout />}>
|
|
|
|
|
<Route path='/' element={<Home />} />
|
|
|
|
|
<Route path='/faq' element={<FAQ />} />
|
2026-02-20 16:12:52 +08:00
|
|
|
<Route path='/rules/:boardIdentifier?' element={<Rules />} />
|
2025-11-03 22:06:18 +01:00
|
|
|
<Route element={<BoardLayout />}>
|
2026-02-20 20:16:11 +08:00
|
|
|
<Route path='/all/:timeFilterName/:pageNumber' element={null} />
|
2025-12-08 22:40:16 +01:00
|
|
|
<Route path='/all/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/all/:timeFilterName?/settings' element={null} />
|
|
|
|
|
<Route path='/all/catalog/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/all/catalog/:timeFilterName?/settings' element={null} />
|
|
|
|
|
|
2026-02-20 20:16:11 +08:00
|
|
|
<Route path='/subs/:timeFilterName/:pageNumber' element={null} />
|
2025-12-08 22:40:16 +01:00
|
|
|
<Route path='/subs/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/subs/:timeFilterName?/settings' element={null} />
|
|
|
|
|
<Route path='/subs/catalog/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/subs/catalog/:timeFilterName?/settings' element={null} />
|
|
|
|
|
|
2026-02-20 20:16:11 +08:00
|
|
|
<Route path='/mod/:timeFilterName/:pageNumber' element={null} />
|
2025-12-08 22:40:16 +01:00
|
|
|
<Route path='/mod/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/mod/:timeFilterName?/settings' element={null} />
|
|
|
|
|
<Route path='/mod/catalog/:timeFilterName?' element={null} />
|
|
|
|
|
<Route path='/mod/catalog/:timeFilterName?/settings' element={null} />
|
|
|
|
|
|
2026-02-16 19:17:38 +08:00
|
|
|
<Route path='/mod/modqueue' element={<ModQueueRoute />} />
|
|
|
|
|
<Route path='/mod/modqueue/settings' element={<ModQueueRoute />} />
|
2026-01-07 16:03:02 +01:00
|
|
|
|
2026-02-20 20:16:11 +08:00
|
|
|
<Route path='/:boardIdentifier/:pageNumber' element={null} />
|
2025-12-08 22:40:16 +01:00
|
|
|
<Route path='/:boardIdentifier' element={null} />
|
|
|
|
|
<Route path='/:boardIdentifier/settings' element={null} />
|
|
|
|
|
<Route path='/:boardIdentifier/catalog' element={null} />
|
|
|
|
|
<Route path='/:boardIdentifier/catalog/settings' element={null} />
|
|
|
|
|
|
2026-02-16 19:17:38 +08:00
|
|
|
<Route path='/:boardIdentifier/modqueue' element={<ModQueueRoute />} />
|
|
|
|
|
<Route path='/:boardIdentifier/modqueue/settings' element={<ModQueueRoute />} />
|
2026-01-07 16:03:02 +01:00
|
|
|
|
2025-11-07 12:50:58 +01:00
|
|
|
<Route path='/:boardIdentifier/thread/:commentCid' element={<Post />} />
|
|
|
|
|
<Route path='/:boardIdentifier/thread/:commentCid/settings' element={<Post />} />
|
2025-12-08 22:40:16 +01:00
|
|
|
|
|
|
|
|
<Route path='/pending/:accountCommentIndex' element={<PendingPost />} />
|
|
|
|
|
<Route path='/pending/:accountCommentIndex/settings' element={<PendingPost />} />
|
2024-03-18 15:04:30 +01:00
|
|
|
</Route>
|
2026-02-16 19:17:38 +08:00
|
|
|
<Route path='/not-allowed' element={<NotAllowed />} />
|
2025-11-03 22:06:18 +01:00
|
|
|
<Route path='/not-found' element={<NotFound />} />
|
2025-11-07 12:50:58 +01:00
|
|
|
<Route path='*' element={<NotFound />} />
|
2025-11-03 22:06:18 +01:00
|
|
|
</Route>
|
|
|
|
|
</Routes>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2024-02-27 17:58:08 +01:00
|
|
|
|
|
|
|
|
export default App;
|