Starting to resolve the issue on prediction10

This commit is contained in:
Your Name 2026-10-27 10:49:08 +02:00
parent 415afe8ac8
commit a407d49f02
6 changed files with 387 additions and 93 deletions

View File

@ -0,0 +1,278 @@
/**
* Shared lazy page loaders + intentional prefetch (hover / idle).
* First navigation to a route otherwise pays for the network + parse of its chunk.
*/
import { lazy, type ComponentType, type LazyExoticComponent } from 'react';
type AnyModule = Record<string, unknown>;
type LazyPage = LazyExoticComponent<ComponentType<Record<string, never>>> & {
preload: () => Promise<unknown>;
};
const preloadCache = new Map<string, Promise<unknown>>();
function once(key: string, load: () => Promise<unknown>): Promise<unknown> {
let pending = preloadCache.get(key);
if (!pending) {
pending = load().catch((err) => {
preloadCache.delete(key);
throw err;
});
preloadCache.set(key, pending);
}
return pending;
}
function lazyNamed<T extends AnyModule, K extends keyof T>(
key: string,
loader: () => Promise<T>,
exportName: K
): LazyPage {
const preload = () =>
once(key, async () => {
const mod = await loader();
return mod[exportName];
});
const Comp = lazy(async () => {
const mod = await loader();
// Keep cache warm for subsequent prefetchRoute calls.
void once(key, async () => mod[exportName]);
return { default: mod[exportName] as ComponentType<Record<string, never>> };
}) as LazyPage;
Comp.preload = preload;
return Comp;
}
export const RegisterPage = lazyNamed('RegisterPage', () => import('../../features/auth/RegisterPage'), 'RegisterPage');
export const VerifyEmailPage = lazyNamed(
'VerifyEmailPage',
() => import('../../features/auth/VerifyEmailPage'),
'VerifyEmailPage'
);
export const ForgotPasswordPage = lazyNamed(
'ForgotPasswordPage',
() => import('../../features/auth/ForgotPasswordPage'),
'ForgotPasswordPage'
);
export const ResetPasswordPage = lazyNamed(
'ResetPasswordPage',
() => import('../../features/auth/ResetPasswordPage'),
'ResetPasswordPage'
);
export const FaqPublicPage = lazyNamed(
'FaqPublicPage',
() => import('../../features/faq/FaqPublicPage'),
'FaqPublicPage'
);
export const ShareResultVerifyPage = lazyNamed(
'ShareResultVerifyPage',
() => import('../../features/share-cards/ShareResultVerifyPage'),
'ShareResultVerifyPage'
);
export const HomePage = lazyNamed('HomePage', () => import('../../features/home/HomePage'), 'HomePage');
export const GamePlayRoute = lazyNamed(
'GamePlayRoute',
() => import('../../features/games/GamePlayRoute'),
'GamePlayRoute'
);
export const WalletPage = lazyNamed('WalletPage', () => import('../../features/wallet/WalletPage'), 'WalletPage');
export const ReferralPage = lazyNamed(
'ReferralPage',
() => import('../../features/referral/ReferralPage'),
'ReferralPage'
);
export const ProfilePage = lazyNamed(
'ProfilePage',
() => import('../../features/profile/ProfilePage'),
'ProfilePage'
);
export const FaqPage = lazyNamed('FaqPage', () => import('../../features/faq/FaqPage'), 'FaqPage');
export const SupportAdminPage = lazyNamed(
'SupportAdminPage',
() => import('../../features/support/SupportAdminPage'),
'SupportAdminPage'
);
export const FaqAdminPage = lazyNamed(
'FaqAdminPage',
() => import('../../features/faq/FaqAdminPage'),
'FaqAdminPage'
);
export const DashboardPage = lazyNamed(
'DashboardPage',
() => import('../../features/dashboard/DashboardPage'),
'DashboardPage'
);
export const OptionsPage = lazyNamed(
'OptionsPage',
() => import('../../features/options/OptionsPage'),
'OptionsPage'
);
export const UserManagementPage = lazyNamed(
'UserManagementPage',
() => import('../../features/users/UserManagementPage'),
'UserManagementPage'
);
export const ReportsHubPage = lazyNamed(
'ReportsHubPage',
() => import('../../features/reports/ReportsHubPage'),
'ReportsHubPage'
);
export const ReportDetailPage = lazyNamed(
'ReportDetailPage',
() => import('../../features/reports/ReportDetailPage'),
'ReportDetailPage'
);
export const SettingsPage = lazyNamed(
'SettingsPage',
() => import('../../features/settings/SettingsPage'),
'SettingsPage'
);
export const CatalogAdminPage = lazyNamed(
'CatalogAdminPage',
() => import('../../features/catalog/CatalogAdminPage'),
'CatalogAdminPage'
);
export const TreasuryAdminPage = lazyNamed(
'TreasuryAdminPage',
() => import('../../features/treasury/TreasuryAdminPage'),
'TreasuryAdminPage'
);
export const ApprovalsPage = lazyNamed(
'ApprovalsPage',
() => import('../../features/approvals/ApprovalsPage'),
'ApprovalsPage'
);
export const ApprovalPoliciesPage = lazyNamed(
'ApprovalPoliciesPage',
() => import('../../features/approvals/ApprovalPoliciesPage'),
'ApprovalPoliciesPage'
);
export const SecurityAdminPage = lazyNamed(
'SecurityAdminPage',
() => import('../../features/security/SecurityAdminPage'),
'SecurityAdminPage'
);
export const ReferralAdminPage = lazyNamed(
'ReferralAdminPage',
() => import('../../features/referral/ReferralAdminPage'),
'ReferralAdminPage'
);
export const ReferralRegularizationPage = lazyNamed(
'ReferralRegularizationPage',
() => import('../../features/referral/ReferralRegularizationPage'),
'ReferralRegularizationPage'
);
export const ReferralFraudDashboardPage = lazyNamed(
'ReferralFraudDashboardPage',
() => import('../../features/referral/ReferralFraudDashboardPage'),
'ReferralFraudDashboardPage'
);
export const TestWalletAdminPage = lazyNamed(
'TestWalletAdminPage',
() => import('../../features/test-wallet/TestWalletAdminPage'),
'TestWalletAdminPage'
);
export const ShareCardsAdminPage = lazyNamed(
'ShareCardsAdminPage',
() => import('../../features/share-cards/ShareCardsAdminPage'),
'ShareCardsAdminPage'
);
export const SoloEncouragementAdminPage = lazyNamed(
'SoloEncouragementAdminPage',
() => import('../../features/solo-encouragement/SoloEncouragementAdminPage'),
'SoloEncouragementAdminPage'
);
export const MarketingAdminPage = lazyNamed(
'MarketingAdminPage',
() => import('../../features/marketing/MarketingAdminPage'),
'MarketingAdminPage'
);
export const CountriesAdminPage = lazyNamed(
'CountriesAdminPage',
() => import('../../features/countries/CountriesAdminPage'),
'CountriesAdminPage'
);
export const NetworkStatusPage = lazyNamed(
'NetworkStatusPage',
() => import('../../features/network/NetworkStatusPage'),
'NetworkStatusPage'
);
export const RoleManagementPage = lazyNamed(
'RoleManagementPage',
() => import('../../features/rbac/RoleManagementPage'),
'RoleManagementPage'
);
export const AdminUsersPage = lazyNamed(
'AdminUsersPage',
() => import('../../features/rbac/AdminUsersPage'),
'AdminUsersPage'
);
export const AdminUserPermissionsPage = lazyNamed(
'AdminUserPermissionsPage',
() => import('../../features/rbac/AdminUserPermissionsPage'),
'AdminUserPermissionsPage'
);
/** Exact path → page preload. Longest match wins for nested paths. */
const PATH_PRELOAD: Array<{ path: string; preload: () => Promise<unknown> }> = [
{ path: '/register', preload: RegisterPage.preload },
{ path: '/verify-email', preload: VerifyEmailPage.preload },
{ path: '/forgot-password', preload: ForgotPasswordPage.preload },
{ path: '/reset-password', preload: ResetPasswordPage.preload },
{ path: '/faq', preload: FaqPublicPage.preload },
{ path: '/share/r', preload: ShareResultVerifyPage.preload },
{ path: '/home', preload: HomePage.preload },
{ path: '/games', preload: GamePlayRoute.preload },
{ path: '/wallet', preload: WalletPage.preload },
{ path: '/referral', preload: ReferralPage.preload },
{ path: '/profile', preload: ProfilePage.preload },
{ path: '/help/faq', preload: FaqPage.preload },
{ path: '/admin/support', preload: SupportAdminPage.preload },
{ path: '/admin/faq', preload: FaqAdminPage.preload },
{ path: '/dashboard', preload: DashboardPage.preload },
{ path: '/options', preload: OptionsPage.preload },
{ path: '/admin/users', preload: UserManagementPage.preload },
{ path: '/admin/reports', preload: ReportsHubPage.preload },
{ path: '/admin/settings', preload: SettingsPage.preload },
{ path: '/admin/catalog', preload: CatalogAdminPage.preload },
{ path: '/admin/treasury', preload: TreasuryAdminPage.preload },
{ path: '/admin/approvals', preload: ApprovalsPage.preload },
{ path: '/admin/approval-policies', preload: ApprovalPoliciesPage.preload },
{ path: '/admin/security', preload: SecurityAdminPage.preload },
{ path: '/admin/referral/regularization', preload: ReferralRegularizationPage.preload },
{ path: '/admin/referral/fraud', preload: ReferralFraudDashboardPage.preload },
{ path: '/admin/referral', preload: ReferralAdminPage.preload },
{ path: '/admin/test-wallet', preload: TestWalletAdminPage.preload },
{ path: '/admin/share-cards', preload: ShareCardsAdminPage.preload },
{ path: '/admin/solo-encouragement', preload: SoloEncouragementAdminPage.preload },
{ path: '/admin/marketing', preload: MarketingAdminPage.preload },
{ path: '/admin/countries', preload: CountriesAdminPage.preload },
{ path: '/admin/network', preload: NetworkStatusPage.preload },
{ path: '/admin/rbac', preload: RoleManagementPage.preload },
{ path: '/admin/admin-users', preload: AdminUsersPage.preload },
].sort((a, b) => b.path.length - a.path.length);
/** Warm the JS chunk for a destination before navigation. */
export function prefetchRoute(to: string): void {
const path = to.split('?')[0]?.split('#')[0] || to;
const match = PATH_PRELOAD.find(
(entry) => path === entry.path || path.startsWith(`${entry.path}/`)
);
if (!match) return;
void match.preload();
}
/** Prefetch game play shell + a specific implementation module. */
export function prefetchGamePlay(implementationKey?: string | null): void {
void GamePlayRoute.preload();
if (!implementationKey) return;
void import('../../features/games/registry').then((m) => {
m.preloadGameImplementation(implementationKey);
});
}

View File

@ -2,100 +2,52 @@
* Client-side route configuration for the web admin console.
* Heavy feature pages are lazy-loaded so a hard refresh only parses the shell + active route.
*/
import { lazy, Suspense, type ComponentType, type ReactNode } from 'react';
import { Suspense, type ReactNode } from 'react';
import { createBrowserRouter, Navigate } from 'react-router-dom';
import { Box, CircularProgress } from '@mui/material';
import { AppShell } from '../../shared/layout/AppShell';
import { RequireAuth } from '../../shared/auth/RequireAuth';
import { LoginPage } from '../../features/auth/LoginPage';
import { SetupPage } from '../../features/auth/SetupPage';
function lazyNamed<T extends Record<string, unknown>, K extends keyof T>(
loader: () => Promise<T>,
exportName: K
) {
return lazy(async () => {
const mod = await loader();
return { default: mod[exportName] as ComponentType<Record<string, never>> };
});
}
const RegisterPage = lazyNamed(() => import('../../features/auth/RegisterPage'), 'RegisterPage');
const VerifyEmailPage = lazyNamed(() => import('../../features/auth/VerifyEmailPage'), 'VerifyEmailPage');
const ForgotPasswordPage = lazyNamed(
() => import('../../features/auth/ForgotPasswordPage'),
'ForgotPasswordPage'
);
const ResetPasswordPage = lazyNamed(() => import('../../features/auth/ResetPasswordPage'), 'ResetPasswordPage');
const FaqPublicPage = lazyNamed(() => import('../../features/faq/FaqPublicPage'), 'FaqPublicPage');
const ShareResultVerifyPage = lazyNamed(
() => import('../../features/share-cards/ShareResultVerifyPage'),
'ShareResultVerifyPage'
);
const HomePage = lazyNamed(() => import('../../features/home/HomePage'), 'HomePage');
const GamePlayRoute = lazyNamed(() => import('../../features/games/GamePlayRoute'), 'GamePlayRoute');
const WalletPage = lazyNamed(() => import('../../features/wallet/WalletPage'), 'WalletPage');
const ReferralPage = lazyNamed(() => import('../../features/referral/ReferralPage'), 'ReferralPage');
const ProfilePage = lazyNamed(() => import('../../features/profile/ProfilePage'), 'ProfilePage');
const FaqPage = lazyNamed(() => import('../../features/faq/FaqPage'), 'FaqPage');
const SupportAdminPage = lazyNamed(() => import('../../features/support/SupportAdminPage'), 'SupportAdminPage');
const FaqAdminPage = lazyNamed(() => import('../../features/faq/FaqAdminPage'), 'FaqAdminPage');
const DashboardPage = lazyNamed(() => import('../../features/dashboard/DashboardPage'), 'DashboardPage');
const OptionsPage = lazyNamed(() => import('../../features/options/OptionsPage'), 'OptionsPage');
const UserManagementPage = lazyNamed(
() => import('../../features/users/UserManagementPage'),
'UserManagementPage'
);
const ReportsHubPage = lazyNamed(() => import('../../features/reports/ReportsHubPage'), 'ReportsHubPage');
const ReportDetailPage = lazyNamed(() => import('../../features/reports/ReportDetailPage'), 'ReportDetailPage');
const SettingsPage = lazyNamed(() => import('../../features/settings/SettingsPage'), 'SettingsPage');
const CatalogAdminPage = lazyNamed(() => import('../../features/catalog/CatalogAdminPage'), 'CatalogAdminPage');
const TreasuryAdminPage = lazyNamed(() => import('../../features/treasury/TreasuryAdminPage'), 'TreasuryAdminPage');
const ApprovalsPage = lazyNamed(() => import('../../features/approvals/ApprovalsPage'), 'ApprovalsPage');
const ApprovalPoliciesPage = lazyNamed(
() => import('../../features/approvals/ApprovalPoliciesPage'),
'ApprovalPoliciesPage'
);
const SecurityAdminPage = lazyNamed(() => import('../../features/security/SecurityAdminPage'), 'SecurityAdminPage');
const ReferralAdminPage = lazyNamed(() => import('../../features/referral/ReferralAdminPage'), 'ReferralAdminPage');
const ReferralRegularizationPage = lazyNamed(
() => import('../../features/referral/ReferralRegularizationPage'),
'ReferralRegularizationPage'
);
const ReferralFraudDashboardPage = lazyNamed(
() => import('../../features/referral/ReferralFraudDashboardPage'),
'ReferralFraudDashboardPage'
);
const TestWalletAdminPage = lazyNamed(
() => import('../../features/test-wallet/TestWalletAdminPage'),
'TestWalletAdminPage'
);
const ShareCardsAdminPage = lazyNamed(
() => import('../../features/share-cards/ShareCardsAdminPage'),
'ShareCardsAdminPage'
);
const SoloEncouragementAdminPage = lazyNamed(
() => import('../../features/solo-encouragement/SoloEncouragementAdminPage'),
'SoloEncouragementAdminPage'
);
const MarketingAdminPage = lazyNamed(
() => import('../../features/marketing/MarketingAdminPage'),
'MarketingAdminPage'
);
const CountriesAdminPage = lazyNamed(
() => import('../../features/countries/CountriesAdminPage'),
'CountriesAdminPage'
);
const NetworkStatusPage = lazyNamed(() => import('../../features/network/NetworkStatusPage'), 'NetworkStatusPage');
const RoleManagementPage = lazyNamed(() => import('../../features/rbac/RoleManagementPage'), 'RoleManagementPage');
const AdminUsersPage = lazyNamed(() => import('../../features/rbac/AdminUsersPage'), 'AdminUsersPage');
const AdminUserPermissionsPage = lazyNamed(
() => import('../../features/rbac/AdminUserPermissionsPage'),
'AdminUserPermissionsPage'
);
import {
RegisterPage,
VerifyEmailPage,
ForgotPasswordPage,
ResetPasswordPage,
FaqPublicPage,
ShareResultVerifyPage,
HomePage,
GamePlayRoute,
WalletPage,
ReferralPage,
ProfilePage,
FaqPage,
SupportAdminPage,
FaqAdminPage,
DashboardPage,
OptionsPage,
UserManagementPage,
ReportsHubPage,
ReportDetailPage,
SettingsPage,
CatalogAdminPage,
TreasuryAdminPage,
ApprovalsPage,
ApprovalPoliciesPage,
SecurityAdminPage,
ReferralAdminPage,
ReferralRegularizationPage,
ReferralFraudDashboardPage,
TestWalletAdminPage,
ShareCardsAdminPage,
SoloEncouragementAdminPage,
MarketingAdminPage,
CountriesAdminPage,
NetworkStatusPage,
RoleManagementPage,
AdminUsersPage,
AdminUserPermissionsPage,
} from './routePrefetch';
function RouteFallback() {
return (
@ -437,5 +389,4 @@ export const router = createBrowserRouter([
},
],
},
{ path: '*', element: <Navigate to="/home" replace /> },
]);

View File

@ -2,15 +2,48 @@ import { lazy, type ComponentType, type LazyExoticComponent } from 'react';
import type { GamePlayEntry } from './types';
type LazyGamePage = LazyExoticComponent<ComponentType> & {
preload: () => Promise<unknown>;
};
const gamePreloadCache = new Map<string, Promise<unknown>>();
function lazyGame(
key: string,
loader: () => Promise<{ default: ComponentType }>
): LazyGamePage {
const preload = () => {
let pending = gamePreloadCache.get(key);
if (!pending) {
pending = loader().catch((err) => {
gamePreloadCache.delete(key);
throw err;
});
gamePreloadCache.set(key, pending);
}
return pending;
};
const Comp = lazy(loader) as LazyGamePage;
Comp.preload = preload;
return Comp;
}
/** Web UI modules keyed by backend implementation_key. */
const PAGES_BY_IMPLEMENTATION: Record<string, LazyExoticComponent<ComponentType>> = {
'candle-predict': lazy(() =>
const PAGES_BY_IMPLEMENTATION: Record<string, LazyGamePage> = {
'candle-predict': lazyGame('candle-predict', () =>
import('../candle-predict/CandlePredictPage').then((m) => ({
default: m.CandlePredictPage,
}))
),
};
/** Warm a game page chunk before the user navigates into it. */
export function preloadGameImplementation(implementationKey: string): void {
const page = PAGES_BY_IMPLEMENTATION[implementationKey];
if (!page) return;
void page.preload();
}
export function hasWebImplementation(implementationKey: string | null | undefined): boolean {
if (!implementationKey) return false;
return implementationKey in PAGES_BY_IMPLEMENTATION;

View File

@ -7,6 +7,7 @@ import PlayArrowRoundedIcon from '@mui/icons-material/PlayArrowRounded';
import type { SvgIconComponent } from '@mui/icons-material';
import { useTranslation } from 'react-i18next';
import type { TFunction } from 'i18next';
import { prefetchGamePlay } from '../../../app/router/routePrefetch';
import type { Game, GameStatus } from '../types';
function statusMeta(status: GameStatus, t: TFunction) {
@ -66,6 +67,9 @@ export function GameCard({ game, onPlay }: Props) {
<CardActionArea
disabled={!playable}
onClick={() => playable && onPlay?.(game)}
onMouseEnter={() => playable && prefetchGamePlay(game.implementationKey)}
onFocus={() => playable && prefetchGamePlay(game.implementationKey)}
onTouchStart={() => playable && prefetchGamePlay(game.implementationKey)}
sx={{ height: '100%', alignItems: 'stretch' }}
>
<Box

View File

@ -2,7 +2,7 @@
* Admin options hub page.
* Presents navigation cards to user management and application settings.
*/
import type { ElementType } from 'react';
import { useEffect, type ElementType } from 'react';
import { Box, Card, CardActionArea, CardContent, Chip, Stack, Typography } from '@mui/material';
import PeopleOutlineRoundedIcon from '@mui/icons-material/PeopleOutlineRounded';
import SettingsOutlinedIcon from '@mui/icons-material/SettingsOutlined';
@ -26,6 +26,7 @@ import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useAuth } from '../../shared/auth/AuthContext';
import { OPTIONS_CARD_PERMISSIONS } from '../../shared/auth/permissions';
import { prefetchRoute } from '../../app/router/routePrefetch';
type OptionCardKey =
| 'users'
@ -244,6 +245,23 @@ export function OptionsPage() {
return !perm || can(perm);
});
// Warm destination chunks while the hub is idle so the first card click is instant.
useEffect(() => {
const paths = OPTION_CARDS.filter((card) => {
const perm = OPTIONS_CARD_PERMISSIONS[card.key];
return !perm || can(perm);
}).map((c) => c.path);
const run = () => {
for (const path of paths) prefetchRoute(path);
};
if (typeof window.requestIdleCallback === 'function') {
const id = window.requestIdleCallback(run, { timeout: 1500 });
return () => window.cancelIdleCallback(id);
}
const id = window.setTimeout(run, 200);
return () => window.clearTimeout(id);
}, [can]);
return (
<Box sx={{ minWidth: 0 }}>
<Stack spacing={0.75}>
@ -265,7 +283,13 @@ export function OptionsPage() {
const Icon = card.icon;
return (
<Card key={card.key} sx={{ gridColumn: { xs: '1 / -1', md: 'span 6' } }}>
<CardActionArea onClick={() => nav(card.path)} sx={{ height: '100%' }}>
<CardActionArea
onClick={() => nav(card.path)}
onMouseEnter={() => prefetchRoute(card.path)}
onFocus={() => prefetchRoute(card.path)}
onTouchStart={() => prefetchRoute(card.path)}
sx={{ height: '100%' }}
>
<CardContent sx={{ p: 2.5 }}>
<Stack direction="row" spacing={1.5} sx={{ alignItems: 'flex-start' }}>
<Box

View File

@ -44,6 +44,7 @@ import {
} from '@mui/material';
import { useTheme } from '@mui/material/styles';
import { Link as RouterLink, Outlet, useLocation } from 'react-router-dom';
import { prefetchRoute } from '../../app/router/routePrefetch';
import { useAuth } from '../auth/AuthContext';
import { ADMIN_PAGE_PERMISSIONS } from '../auth/permissions';
import { useAppPresenceHeartbeat } from '../hooks/useAppPresenceHeartbeat';
@ -215,6 +216,9 @@ export function AppShell() {
to={it.to}
selected={selected}
onClick={() => setMobileOpen(false)}
onMouseEnter={() => prefetchRoute(it.to)}
onFocus={() => prefetchRoute(it.to)}
onTouchStart={() => prefetchRoute(it.to)}
sx={{ borderRadius: 2, mb: 0.5 }}
>
<ListItemIcon sx={{ minWidth: 40 }}>{it.icon}</ListItemIcon>