From a407d49f022da4989f1b9951cbf3efd63ea2bbba Mon Sep 17 00:00:00 2001 From: Your Name Date: Tue, 27 Oct 2026 10:49:08 +0200 Subject: [PATCH] Starting to resolve the issue on prediction10 --- prediqo_web/src/app/router/routePrefetch.ts | 278 ++++++++++++++++++ prediqo_web/src/app/router/router.tsx | 129 +++----- prediqo_web/src/features/games/registry.ts | 37 ++- .../src/features/home/components/GameCard.tsx | 4 + .../src/features/options/OptionsPage.tsx | 28 +- prediqo_web/src/shared/layout/AppShell.tsx | 4 + 6 files changed, 387 insertions(+), 93 deletions(-) create mode 100644 prediqo_web/src/app/router/routePrefetch.ts diff --git a/prediqo_web/src/app/router/routePrefetch.ts b/prediqo_web/src/app/router/routePrefetch.ts new file mode 100644 index 00000000..5d197fcd --- /dev/null +++ b/prediqo_web/src/app/router/routePrefetch.ts @@ -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; + +type LazyPage = LazyExoticComponent>> & { + preload: () => Promise; +}; + +const preloadCache = new Map>(); + +function once(key: string, load: () => Promise): Promise { + let pending = preloadCache.get(key); + if (!pending) { + pending = load().catch((err) => { + preloadCache.delete(key); + throw err; + }); + preloadCache.set(key, pending); + } + return pending; +} + +function lazyNamed( + key: string, + loader: () => Promise, + 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> }; + }) 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 }> = [ + { 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); + }); +} diff --git a/prediqo_web/src/app/router/router.tsx b/prediqo_web/src/app/router/router.tsx index 5e00b732..d4eefa3e 100644 --- a/prediqo_web/src/app/router/router.tsx +++ b/prediqo_web/src/app/router/router.tsx @@ -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, K extends keyof T>( - loader: () => Promise, - exportName: K -) { - return lazy(async () => { - const mod = await loader(); - return { default: mod[exportName] as ComponentType> }; - }); -} - -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: }, ]); diff --git a/prediqo_web/src/features/games/registry.ts b/prediqo_web/src/features/games/registry.ts index c07e5289..155824da 100644 --- a/prediqo_web/src/features/games/registry.ts +++ b/prediqo_web/src/features/games/registry.ts @@ -2,15 +2,48 @@ import { lazy, type ComponentType, type LazyExoticComponent } from 'react'; import type { GamePlayEntry } from './types'; +type LazyGamePage = LazyExoticComponent & { + preload: () => Promise; +}; + +const gamePreloadCache = new Map>(); + +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> = { - 'candle-predict': lazy(() => +const PAGES_BY_IMPLEMENTATION: Record = { + '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; diff --git a/prediqo_web/src/features/home/components/GameCard.tsx b/prediqo_web/src/features/home/components/GameCard.tsx index ad402d57..b0d88735 100644 --- a/prediqo_web/src/features/home/components/GameCard.tsx +++ b/prediqo_web/src/features/home/components/GameCard.tsx @@ -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) { playable && onPlay?.(game)} + onMouseEnter={() => playable && prefetchGamePlay(game.implementationKey)} + onFocus={() => playable && prefetchGamePlay(game.implementationKey)} + onTouchStart={() => playable && prefetchGamePlay(game.implementationKey)} sx={{ height: '100%', alignItems: 'stretch' }} > { + 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 ( @@ -265,7 +283,13 @@ export function OptionsPage() { const Icon = card.icon; return ( - nav(card.path)} sx={{ height: '100%' }}> + nav(card.path)} + onMouseEnter={() => prefetchRoute(card.path)} + onFocus={() => prefetchRoute(card.path)} + onTouchStart={() => prefetchRoute(card.path)} + sx={{ height: '100%' }} + > setMobileOpen(false)} + onMouseEnter={() => prefetchRoute(it.to)} + onFocus={() => prefetchRoute(it.to)} + onTouchStart={() => prefetchRoute(it.to)} sx={{ borderRadius: 2, mb: 0.5 }} > {it.icon}