-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
168 lines (145 loc) · 5.27 KB
/
Copy pathApp.tsx
File metadata and controls
168 lines (145 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import {
ApolloClient,
ApolloLink,
ApolloProvider,
createHttpLink,
from,
InMemoryCache,
useApolloClient,
} from "@apollo/client"
import { setContext } from "@apollo/client/link/context"
import * as Notifications from "expo-notifications"
import { getItemAsync } from "expo-secure-store"
import * as SplashScreen from "expo-splash-screen"
import { StatusBar } from "expo-status-bar"
import { useFonts } from "expo-font"
import { useEffect } from "react"
import { FONT_SOURCE } from "./constants/Fonts"
import { GestureHandlerRootView } from "react-native-gesture-handler"
import { SafeAreaProvider } from "react-native-safe-area-context"
import { Provider } from "react-redux"
import ErrorBoundary from "./components/ErrorBoundary"
import Colors from "./constants/Colors"
import Url from "./constants/Url"
import Navigation, { navigationRef } from "./navigation"
import ThemeContextProvider from "./utils/context/ThemeContext"
import { ScrollYContextProvider } from "./utils/context/ScrollYContext"
import useDeeplinking from "./utils/hooks/useDeeplinking"
import useNotifications from "./utils/hooks/useNotifications"
import useQuickActions from "./utils/hooks/useQuickActions"
import useUser, { STORE_KEY } from "./utils/hooks/useUser"
import { useActivityManager } from "./utils/hooks/useActivityManager"
import { store } from "./utils/redux"
import "./features/timeline/utils/geofenceTask"
import "./utils/widget/widgetBackgroundFetch"
import useWidgets from "./utils/widget/hooks/useWidgets"
import { setLogVerbosity } from "@apollo/client"
import { BottomSheetModalProvider } from "@gorhom/bottom-sheet"
import { SearchMenuProvider } from "./contexts/SearchMenuContext"
import { AiChatProvider } from "./contexts/AiChatContext"
import { KeyboardProvider } from "react-native-keyboard-controller"
setLogVerbosity("error")
SplashScreen.preventAutoHideAsync()
SplashScreen.setOptions({
duration: 500,
fade: true,
})
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldPlaySound: false,
shouldSetBadge: false,
shouldShowAlert: true,
shouldShowBanner: true,
shouldShowList: true,
priority: Notifications.AndroidNotificationPriority.HIGH,
}),
})
const withToken = setContext(async () => {
try {
const stored = await getItemAsync(STORE_KEY)
const user = stored ? JSON.parse(stored) : null
const token = user?.token || ""
return { token }
} catch (error) {
return { token: "" }
}
})
const authMiddleware = new ApolloLink((op, forw) => {
const { token } = op.getContext()
op.setContext(() => ({
headers: {
authentication: token || "",
},
}))
return forw(op)
})
const httpLink = createHttpLink({
uri: Url.API + "/graphql",
})
const link = from([withToken, authMiddleware.concat(httpLink)])
const cache = new InMemoryCache()
const apolloClient = new ApolloClient({
cache,
link,
})
function AppContent() {
const { isAuthenticated, loadUser, isLoading, removeUser } = useUser()
const client = useApolloClient()
const { sendTokenToServer } = useNotifications(navigationRef as any)
useQuickActions(navigationRef as any)
useActivityManager()
useWidgets()
useEffect(() => {
loadUser()
}, [])
useEffect(() => {
if (isAuthenticated) {
sendTokenToServer().catch(async (err) => {
const cause = err?.cause
if (cause?.extensions?.response?.statusCode === 403) {
await client.resetStore()
await removeUser()
}
})
}
}, [isAuthenticated])
const linking = useDeeplinking()
return (
<>
<StatusBar />
<Navigation isAuthenticated={isAuthenticated} isLoading={isLoading} linking={linking} />
</>
)
}
function App() {
const [fontsLoaded] = useFonts(FONT_SOURCE)
useEffect(() => {
if (fontsLoaded) SplashScreen.hideAsync()
}, [fontsLoaded])
return (
<SafeAreaProvider style={{ flex: 1, backgroundColor: Colors.primary }}>
<ErrorBoundary>
<KeyboardProvider>
<GestureHandlerRootView style={{ flex: 1 }}>
<BottomSheetModalProvider>
<ThemeContextProvider>
<ScrollYContextProvider>
<SearchMenuProvider>
<ApolloProvider client={apolloClient}>
<Provider store={store}>
<AiChatProvider>
<AppContent />
</AiChatProvider>
</Provider>
</ApolloProvider>
</SearchMenuProvider>
</ScrollYContextProvider>
</ThemeContextProvider>
</BottomSheetModalProvider>
</GestureHandlerRootView>
</KeyboardProvider>
</ErrorBoundary>
</SafeAreaProvider>
)
}
export default App