| import { scheduleAfterFirstPaint } from '@/utils/after-paint'; |
|
|
| let vercelAnalyticsScheduled = false; |
| let dashboardFontsScheduled = false; |
|
|
| export interface DashboardFontContext { |
| variant?: string | null; |
| lang?: string | null; |
| dir?: string | null; |
| } |
|
|
| export type DashboardFontFamily = 'nunito' | 'tajawal'; |
|
|
| |
| |
| |
| export function dashboardFontFamilies(context: DashboardFontContext = {}): DashboardFontFamily[] { |
| const variant = (context.variant || 'full').toLowerCase(); |
| const lang = (context.lang || 'en').split('-')[0]?.toLowerCase() || 'en'; |
| const dir = (context.dir || '').toLowerCase(); |
| const families: DashboardFontFamily[] = []; |
|
|
| if (variant === 'happy') families.push('nunito'); |
| if (dir === 'rtl' || lang === 'ar') families.push('tajawal'); |
|
|
| return families; |
| } |
|
|
| |
| |
| |
| |
| const DASHBOARD_FONT_LOADERS: Record<DashboardFontFamily, () => Promise<unknown>> = { |
| nunito: () => Promise.all([ |
| import('@fontsource/nunito/400.css'), |
| import('@fontsource/nunito/600.css'), |
| import('@fontsource/nunito/700.css'), |
| import('@fontsource/nunito/400-italic.css'), |
| ]), |
| tajawal: () => Promise.all([ |
| import('@fontsource/tajawal/400.css'), |
| import('@fontsource/tajawal/500.css'), |
| import('@fontsource/tajawal/700.css'), |
| ]), |
| }; |
|
|
| function getBuildVariant(): string { |
| try { |
| return import.meta.env.VITE_VARIANT || 'full'; |
| } catch { |
| return 'full'; |
| } |
| } |
|
|
| let dashboardFontsLoaded = false; |
|
|
| function loadDeferredDashboardFonts(): void { |
| if (typeof document === 'undefined' || dashboardFontsLoaded) return; |
|
|
| const root = document.documentElement; |
| const families = dashboardFontFamilies({ |
| variant: root.dataset.variant || getBuildVariant(), |
| lang: root.lang || 'en', |
| dir: root.dir || '', |
| }); |
| if (families.length === 0) return; |
|
|
| dashboardFontsLoaded = true; |
| void Promise.all(families.map((family) => DASHBOARD_FONT_LOADERS[family]())).catch(() => { |
| |
| }); |
| } |
|
|
| export function initDeferredDashboardFonts(): void { |
| if (dashboardFontsScheduled) return; |
| dashboardFontsScheduled = true; |
| scheduleAfterFirstPaint(loadDeferredDashboardFonts, 3000); |
| } |
|
|
| export function initVercelAnalytics(): void { |
| if (vercelAnalyticsScheduled || typeof window === 'undefined') return; |
| vercelAnalyticsScheduled = true; |
| scheduleAfterFirstPaint(() => { |
| void import('@vercel/analytics') |
| .then(({ inject }) => { |
| inject({ |
| beforeSend: (event) => (Math.random() > 0.1 ? null : event), |
| }); |
| }) |
| .catch(() => { |
| |
| }); |
| }, 3000); |
| } |
|
|