2025-12-03 21:56:50 +00:00
|
|
|
import { Suspense, lazy, type ComponentType } from 'react';
|
|
|
|
|
import { LoadingSpinner } from './loading-spinner';
|
|
|
|
|
|
2025-12-17 14:15:45 +00:00
|
|
|
// Composant de fallback pour les erreurs de chargement
|
|
|
|
|
function ErrorFallback({ pageName }: { pageName: string }) {
|
|
|
|
|
return (
|
|
|
|
|
<div className="container mx-auto px-4 py-8">
|
|
|
|
|
<div className="max-w-2xl mx-auto">
|
|
|
|
|
<h1 className="text-2xl font-bold mb-4">{pageName}</h1>
|
|
|
|
|
<div className="bg-yellow-50 border border-yellow-200 text-yellow-700 px-4 py-3 rounded">
|
|
|
|
|
<p>Failed to load {pageName}. Please refresh the page.</p>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-03 21:56:50 +00:00
|
|
|
interface LazyComponentProps {
|
|
|
|
|
fallback?: React.ReactNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function createLazyComponent<T extends ComponentType<any>>(
|
|
|
|
|
importFunc: () => Promise<{ default: T }>,
|
2025-12-13 02:34:34 +00:00
|
|
|
fallback?: React.ReactNode,
|
2025-12-03 21:56:50 +00:00
|
|
|
) {
|
|
|
|
|
const LazyComponent = lazy(importFunc);
|
|
|
|
|
|
|
|
|
|
return function WrappedLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
props: React.ComponentProps<T> & LazyComponentProps,
|
2025-12-03 21:56:50 +00:00
|
|
|
) {
|
2025-12-17 14:15:45 +00:00
|
|
|
// Extraire fallback des props pour ne pas le passer au composant lazy
|
|
|
|
|
const { fallback: _fallback, ...componentProps } = props;
|
2025-12-03 21:56:50 +00:00
|
|
|
return (
|
|
|
|
|
<Suspense fallback={fallback || <LoadingSpinner />}>
|
2025-12-17 14:15:45 +00:00
|
|
|
{/* @ts-expect-error - LazyComponent props are compatible but TypeScript can't infer it */}
|
|
|
|
|
<LazyComponent {...componentProps} />
|
2025-12-03 21:56:50 +00:00
|
|
|
</Suspense>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Composants lazy communs
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyDashboard = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/DashboardPage').then((m) => ({ default: m.DashboardPage })),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-17 13:07:35 +00:00
|
|
|
export const LazyChat = createLazyComponent(() =>
|
|
|
|
|
import('@/features/chat/pages/ChatPage').then((m) => ({
|
|
|
|
|
default: m.ChatPage,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyLibrary = createLazyComponent(
|
2025-12-17 14:15:45 +00:00
|
|
|
() =>
|
|
|
|
|
import('@/features/library/pages/LibraryPage')
|
|
|
|
|
.then((m) => ({ default: m.default }))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error('Failed to load LibraryPage:', err);
|
|
|
|
|
return { default: () => <ErrorFallback pageName="Library" /> };
|
|
|
|
|
}) as Promise<{ default: ComponentType<any> }>,
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyProfile = createLazyComponent(() =>
|
2025-12-17 14:15:45 +00:00
|
|
|
import('@/pages/ProfilePage')
|
|
|
|
|
.then((m) => ({ default: m.ProfilePage }))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error('Failed to load ProfilePage:', err);
|
|
|
|
|
return { default: () => <ErrorFallback pageName="Profile" /> };
|
|
|
|
|
}) as Promise<{ default: ComponentType<any> }>,
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-17 13:07:35 +00:00
|
|
|
export const LazySettings = createLazyComponent(() =>
|
2025-12-17 14:15:45 +00:00
|
|
|
import('@/features/settings/pages/SettingsPage')
|
|
|
|
|
.then((m) => ({
|
|
|
|
|
default: m.SettingsPage,
|
|
|
|
|
}))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error('Failed to load SettingsPage:', err);
|
|
|
|
|
return { default: () => <ErrorFallback pageName="Settings" /> };
|
|
|
|
|
}) as Promise<{ default: ComponentType<any> }>,
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyLogin = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/LoginPage').then((m) => ({ default: m.LoginPage })),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyRegister = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/RegisterPage').then((m) => ({ default: m.RegisterPage })),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyForgotPassword = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/auth/pages/ForgotPasswordPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyVerifyEmail = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/auth/pages/VerifyEmailPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyResetPassword = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/auth/pages/ResetPasswordPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazySessions = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/auth/pages/SessionsPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyNotFound = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/error/pages/NotFoundPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
|
|
|
|
export const LazyServerError = createLazyComponent(
|
2025-12-13 02:34:34 +00:00
|
|
|
() => import('@/features/error/pages/ServerErrorPage'),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyUserProfile = createLazyComponent(() =>
|
|
|
|
|
import('@/features/profile/pages/UserProfilePage').then((m) => ({
|
|
|
|
|
default: m.UserProfilePage,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyRoles = createLazyComponent(() =>
|
|
|
|
|
import('@/features/roles/pages/RolesPage').then((m) => ({
|
|
|
|
|
default: m.RolesPage,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyTrackDetail = createLazyComponent(() =>
|
|
|
|
|
import('@/features/tracks/pages/TrackDetailPage').then((m) => ({
|
|
|
|
|
default: m.TrackDetailPage,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-13 02:34:34 +00:00
|
|
|
export const LazyPlaylistRoutes = createLazyComponent(() =>
|
|
|
|
|
import('@/features/playlists/routes').then((m) => ({
|
|
|
|
|
default: m.PlaylistRoutes,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-24 12:19:54 +00:00
|
|
|
export const LazySearch = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/SearchPage').then((m) => ({
|
|
|
|
|
default: m.SearchPage,
|
|
|
|
|
})),
|
|
|
|
|
);
|
2025-12-24 12:22:31 +00:00
|
|
|
export const LazyNotifications = createLazyComponent(() =>
|
|
|
|
|
import('@/features/notifications/pages/NotificationsPage').then((m) => ({
|
|
|
|
|
default: m.NotificationsPage,
|
|
|
|
|
})),
|
|
|
|
|
);
|
2025-12-17 13:07:35 +00:00
|
|
|
export const LazyMarketplace = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/marketplace/MarketplaceHome').then((m) => ({
|
|
|
|
|
default: m.MarketplaceHome,
|
|
|
|
|
})),
|
2025-12-03 21:56:50 +00:00
|
|
|
);
|
2025-12-25 10:25:06 +00:00
|
|
|
export const LazyAnalytics = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/AnalyticsPage').then((m) => ({
|
|
|
|
|
default: m.AnalyticsPage,
|
|
|
|
|
})),
|
|
|
|
|
);
|
2025-12-25 10:27:17 +00:00
|
|
|
export const LazyWebhooks = createLazyComponent(() =>
|
|
|
|
|
import('@/pages/WebhooksPage').then((m) => ({
|
|
|
|
|
default: m.WebhooksPage,
|
|
|
|
|
})),
|
|
|
|
|
);
|