4.2 KiB
4.2 KiB
Global UI Components and Providers
Purpose
Describe global layout, guards, and utility components/providers used throughout the frontend.
Code Locations (exact paths)
- App composition and routing:
frontend/src/App.tsx - Entry/providers:
frontend/src/main.tsx - Layout:
frontend/src/layout/AppLayout.tsx,frontend/src/layout/AppSidebar.tsx,frontend/src/layout/AppHeader.tsx(and related layout files) - Guards:
frontend/src/components/auth/ProtectedRoute.tsx,frontend/src/components/common/ModuleGuard.tsx - Global utilities:
frontend/src/components/common/ScrollToTop.tsx,frontend/src/components/common/GlobalErrorDisplay.tsx,frontend/src/components/common/LoadingStateMonitor.tsx,frontend/src/components/common/ErrorBoundary.tsx - Providers:
frontend/src/context/ThemeContext.tsx,frontend/src/context/HeaderMetricsContext.tsx,frontend/src/components/ui/toast/ToastContainer.tsx
High-Level Responsibilities
- Wrap the app with error handling, theming, metrics, toasts, and routing.
- Enforce authentication and module access at the route level.
- Provide global UI behaviors (scroll reset, error banner, loading monitor).
- Supply consistent layout (sidebar/header/content) for protected areas.
Detailed Behavior
- Providers (
main.tsx):ErrorBoundarywraps the entire app.ThemeProvidersupplies theme context.HeaderMetricsProvidersupplies header metrics context.ToastProviderexposes toast notifications.BrowserRouterprovides routing; renders<App />.
- Routing shell (
App.tsx):GlobalErrorDisplayrenders global errors;LoadingStateMonitortracks loading states.ScrollToTopresets scroll on route changes.- Public routes:
/signin,/signup. - Protected routes: wrapped in
ProtectedRoute→AppLayout;ModuleGuardused per-module. - Lazy-loaded module pages inside routes; ModuleGuard enforces module access flags.
- Guards:
ProtectedRoute: checksuseAuthStorefor authentication; redirects unauthenticated users to sign-in; logs out on failed refresh in App effect.ModuleGuard: gates module pages based on module enable settings/permissions.
- Layout:
AppLayoutcomposes sidebar/header/content;AppSidebaruses auth store for user/nav; header components provide top-level actions and metrics.
- Utilities:
ScrollToToplistens to route changes and scrolls to top.GlobalErrorDisplayshows global error banners.LoadingStateMonitortracks loading indicators globally.ToastProvidersupplies toast UI primitives for notifications.
Data Structures / Models Involved (no code)
- Context values from theme, header metrics, toast providers; auth state from
authStore; module enable settings fromsettingsStore.
Execution Flow
- App bootstrap wraps providers → routes render → ProtectedRoute checks auth → ModuleGuard checks module access → layout renders with sidebar/header → pages load lazily and fetch data.
Cross-Module Interactions
- Auth/module settings drive guards; toasts/errors/loading monitors are available to all pages; layout navigation links modules.
Error Handling
ErrorBoundarycatches render errors.GlobalErrorDisplaysurfaces application-level errors.ProtectedRoutelogs out on failed refresh in App effect; unauthorized users are redirected.
Tenancy Rules
- Enforced via backend auth and module enable settings; guards rely on auth/module settings to gate access.
Billing Rules
- None in UI components; billing info shown in pages that consume billing store/endpoints.
Background Tasks / Schedulers
- None; components react to store state and router changes.
Key Design Considerations
- Provider stack covers theme, metrics, toasts, error boundary, routing.
- Guards ensure unauthorized access is blocked at the route level.
- Global utilities avoid repeated boilerplate for scroll/error/loading behaviors.
How Developers Should Work With This Module
- Add new protected pages under
ProtectedRouteand wrap withModuleGuardwhen module-scoped. - Use existing toast/error/loading utilities instead of duplicating.
- Keep provider order consistent (ErrorBoundary → Theme/Header/Toast → Router → App).