|
1 | 1 | import Head from 'next/head' |
2 | | -import { useEffect } from 'react' |
| 2 | +import dynamic from 'next/dynamic' |
3 | 3 | import { Toaster } from 'react-hot-toast' |
4 | 4 | import { QueryClient, QueryClientProvider } from '@tanstack/react-query' |
5 | | -import { useOnAuthStateChange } from '@hooks/useOnAuthStateChange' |
6 | | -import { useCatchUserPresences } from '@hooks/useCatchUserPresences' |
7 | | -import { useInitialSteps } from '@hooks/useInitialSteps' |
8 | | -import { useBroadcastListner } from '@hooks/useBroadcastListner' |
9 | | -import useServiceWorker from '@hooks/useServiceWorker' |
10 | | -import { useHandleUserStatus } from '@hooks/useHanelUserStatus' |
11 | | -import { eventsHub } from '@services/eventsHub' |
12 | 5 | import GoogleAnalytics from '@components/GoogleAnalytics' |
13 | 6 | import NotificationPromptCard from '@components/NotificationPromptCard' |
14 | | -import { performMaintenanceCleanup } from '@db/messageComposerDB' |
15 | | -import { useEditorPreferences, applyEditorPreferences } from '@stores' |
16 | 7 |
|
17 | 8 | import '../styles/globals.scss' |
18 | 9 | import '../styles/styles.scss' |
19 | | -import { useRouter } from 'next/router' |
20 | 10 | import '@config' |
21 | 11 |
|
22 | | -// import { initializeApm } from '@utils/elasticApm' |
| 12 | +// Dynamically import router-dependent hooks (client-side only) |
| 13 | +// This prevents SSG errors on static pages like 404/500 |
| 14 | +const AppProviders = dynamic(() => import('@components/AppProviders'), { ssr: false }) |
23 | 15 |
|
24 | 16 | // Create a client |
25 | 17 | const queryClient = new QueryClient() |
@@ -47,106 +39,13 @@ const Header = () => { |
47 | 39 |
|
48 | 40 | export default function MyApp({ Component, pageProps }: any) { |
49 | 41 | const isMobileInitial = pageProps.isMobile || false |
50 | | - const { preferences, hydrated } = useEditorPreferences() |
51 | | - |
52 | | - const router = useRouter() |
53 | | - |
54 | | - useServiceWorker() |
55 | | - useOnAuthStateChange() |
56 | | - useCatchUserPresences() |
57 | | - // pinnedMessage, typingIndicator broadcaster |
58 | | - useBroadcastListner() |
59 | | - // service worker side |
60 | | - useHandleUserStatus() |
61 | | - useInitialSteps(isMobileInitial) |
62 | | - |
63 | | - // Apply editor preferences on hydration and changes |
64 | | - useEffect(() => { |
65 | | - if (hydrated) applyEditorPreferences(preferences) |
66 | | - }, [hydrated, preferences]) |
67 | | - |
68 | | - useEffect(() => { |
69 | | - eventsHub(router) |
70 | | - // initializeApm() |
71 | | - |
72 | | - // Run DB maintenance cleanup once per session (client-side only) |
73 | | - if (typeof window !== 'undefined') { |
74 | | - performMaintenanceCleanup().catch(() => { |
75 | | - // Silently fail - cleanup is best-effort |
76 | | - }) |
77 | | - |
78 | | - // iOS Safari keyboard viewport fix |
79 | | - // Two key behaviors to handle: |
80 | | - // 1. Height changes when keyboard opens/closes |
81 | | - // 2. iOS auto-scrolls when focusing elements in bottom half of screen |
82 | | - const doc = document.documentElement |
83 | | - let lastHeight = window.visualViewport?.height ?? window.innerHeight |
84 | | - let rafId: number | null = null |
85 | | - |
86 | | - // Set initial height |
87 | | - const vv = window.visualViewport |
88 | | - if (vv) { |
89 | | - doc.style.setProperty('--visual-viewport-height', `${vv.height}px`) |
90 | | - doc.style.setProperty('--vh', `${vv.height * 0.01}px`) |
91 | | - } |
92 | | - |
93 | | - // Update height immediately using rAF for smooth rendering |
94 | | - function handleViewportResize() { |
95 | | - if (rafId) cancelAnimationFrame(rafId) |
96 | | - |
97 | | - rafId = requestAnimationFrame(() => { |
98 | | - const vv = window.visualViewport |
99 | | - if (!vv) return |
100 | | - |
101 | | - const height = vv.height |
102 | | - |
103 | | - // Skip micro-updates (less than 50px change might be just toolbar hiding) |
104 | | - if (Math.abs(height - lastHeight) < 50) return |
105 | | - |
106 | | - lastHeight = height |
107 | | - doc.style.setProperty('--visual-viewport-height', `${height}px`) |
108 | | - doc.style.setProperty('--vh', `${height * 0.01}px`) |
109 | | - }) |
110 | | - } |
111 | | - |
112 | | - // CRITICAL: When iOS auto-scrolls to show focused element, reset scroll |
113 | | - // This prevents the "off-screen" issue when tapping bottom half |
114 | | - let scrollResetTimeout: ReturnType<typeof setTimeout> | null = null |
115 | | - |
116 | | - function handleViewportScroll() { |
117 | | - const vv = window.visualViewport |
118 | | - if (!vv || vv.offsetTop === 0) return |
119 | | - |
120 | | - // Clear any pending reset |
121 | | - if (scrollResetTimeout) clearTimeout(scrollResetTimeout) |
122 | | - |
123 | | - // Debounce the scroll reset to let iOS finish its animation |
124 | | - scrollResetTimeout = setTimeout(() => { |
125 | | - // Double-check offsetTop is still non-zero |
126 | | - if (window.visualViewport && window.visualViewport.offsetTop > 0) { |
127 | | - // Reset window scroll - our fixed container will realign |
128 | | - window.scrollTo(0, 0) |
129 | | - } |
130 | | - }, 100) |
131 | | - } |
132 | | - |
133 | | - window.visualViewport?.addEventListener('resize', handleViewportResize) |
134 | | - window.visualViewport?.addEventListener('scroll', handleViewportScroll) |
135 | | - |
136 | | - return () => { |
137 | | - if (rafId) cancelAnimationFrame(rafId) |
138 | | - if (scrollResetTimeout) clearTimeout(scrollResetTimeout) |
139 | | - window.visualViewport?.removeEventListener('resize', handleViewportResize) |
140 | | - window.visualViewport?.removeEventListener('scroll', handleViewportScroll) |
141 | | - } |
142 | | - } |
143 | | - }, []) |
144 | 42 |
|
145 | 43 | return ( |
146 | 44 | <div id="root"> |
147 | 45 | <Header /> |
148 | 46 | <GoogleAnalytics /> |
149 | 47 | <NotificationPromptCard /> |
| 48 | + <AppProviders isMobileInitial={isMobileInitial} /> |
150 | 49 | <QueryClientProvider client={queryClient}> |
151 | 50 | <Component {...pageProps} /> |
152 | 51 | </QueryClientProvider> |
|
0 commit comments