/* eslint-disable react-hooks/exhaustive-deps */ import { ApolloClient, NormalizedCacheObject } from '@apollo/client'; import { useCallback, useEffect, useState } from 'react'; import { createApolloClient } from 'expo-schema/client'; import * as Font from 'expo-font'; import { initI18n } from 'i18n'; import { initSentry } from 'utils/sentry'; import { Platform } from 'react-native'; const webFontsList = [ 'https://assets.pivot.so/fonts/CIRCULARSTD-BOLD.woff2', 'https://assets.pivot.so/fonts/CIRCULARSTD-BOOK.woff2', 'https://assets.pivot.so/fonts/CIRCULARSTD-MEDIUM.woff2', ]; const mobileFonts = { 'CircularStd-Bold': require('assets/font/circular-std/CircularStd-Bold.ttf'), 'CircularStd-Book': require('assets/font/circular-std/CircularStd-Book.ttf'), 'CircularStd-Medium': require('assets/font/circular-std/CircularStd-Medium.ttf'), }; const webFonts = { 'CircularStd-Bold': webFontsList[0], 'CircularStd-Book': webFontsList[1], 'CircularStd-Medium': webFontsList[2], }; export const useAppConfiguration = (): AppConfiguration => { const fonts = Platform.select({ default: mobileFonts, web: webFonts, }); const [apolloClient, setApolloClient] = useState(); const [fontsLoaded, setFontLoaded] = useState(false); const [loading, setLoading] = useState(true); const loadFonts = async () => { try { await Promise.all(webFontsList.map(async (font) => await (await fetch(font)).blob)); await Font.loadAsync(fonts); setFontLoaded(true); } catch (error) { await Font.loadAsync(mobileFonts); setFontLoaded(true); throw new Error('Fonts loading error'); } }; const configureApplication = useCallback(async () => { // Init Sentry initSentry(); // Init i18n initI18n(); // Init Fonts loadFonts(); // Init Apollo Client const client = await createApolloClient(); setApolloClient(client); }, []); useEffect(() => { if (fontsLoaded && apolloClient) { setLoading(false); } }, [apolloClient, fontsLoaded]); useEffect(() => { configureApplication(); }, [configureApplication]); return { apolloClient: apolloClient!, loading }; }; type NormalizedApolloClient = ApolloClient; interface AppConfiguration { apolloClient: NormalizedApolloClient; loading: boolean; }