Use proper environment mapping

This commit is contained in:
Zhiyuan Zheng 2022-01-02 22:28:33 +01:00
parent 22af0bf828
commit 013d55aee2
10 changed files with 99 additions and 40 deletions

View File

@ -41,10 +41,10 @@ const App: React.FC = () => {
Notifications.dismissAllNotificationsAsync() Notifications.dismissAllNotificationsAsync()
}, []) }, [])
useEffect(() => { useEffect(() => {
AppState.addEventListener('change', appStateEffect) const appStateListener = AppState.addEventListener('change', appStateEffect)
return () => { return () => {
AppState.removeEventListener('change', appStateEffect) appStateListener.remove()
} }
}, []) }, [])

View File

@ -1,3 +1,4 @@
import { mapEnvironment } from '@utils/checkEnvironment'
import axios from 'axios' import axios from 'axios'
import chalk from 'chalk' import chalk from 'chalk'
import Constants from 'expo-constants' import Constants from 'expo-constants'
@ -16,7 +17,11 @@ export type Params = {
sentry?: boolean sentry?: boolean
} }
export const TOOOT_API_DOMAIN = __DEV__ ? 'testapi.tooot.app' : 'api.tooot.app' export const TOOOT_API_DOMAIN = mapEnvironment({
release: 'api.tooot.app',
candidate: 'api-candidate.tooot.app',
development: 'api-development.tooot.app'
})
const apiTooot = async <T = unknown>({ const apiTooot = async <T = unknown>({
method, method,

View File

@ -2,6 +2,7 @@ import analytics from '@components/analytics'
import Button from '@components/Button' import Button from '@components/Button'
import Icon from '@components/Icon' import Icon from '@components/Icon'
import { MenuContainer, MenuRow } from '@components/Menu' import { MenuContainer, MenuRow } from '@components/Menu'
import { isDevelopment } from '@utils/checkEnvironment'
import { updateInstancePush } from '@utils/slices/instances/updatePush' import { updateInstancePush } from '@utils/slices/instances/updatePush'
import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert' import { updateInstancePushAlert } from '@utils/slices/instances/updatePushAlert'
import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode' import { updateInstancePushDecode } from '@utils/slices/instances/updatePushDecode'
@ -45,11 +46,16 @@ const TabMePush: React.FC = () => {
setPushCanAskAgain(settings.canAskAgain) setPushCanAskAgain(settings.canAskAgain)
} }
useEffect(() => { useEffect(() => {
Notifications.getExpoPushTokenAsync({ if (isDevelopment) {
experienceId: '@xmflsct/tooot' setPushAvailable(true)
}) } else {
.then(data => setPushAvailable(!!data)) Notifications.getExpoPushTokenAsync({
.catch(() => setPushAvailable(false)) experienceId: '@xmflsct/tooot'
})
.then(data => setPushAvailable(!!data))
.catch(() => setPushAvailable(false))
}
checkPush() checkPush()
const subscription = AppState.addEventListener('change', checkPush) const subscription = AppState.addEventListener('change', checkPush)
return () => { return () => {
@ -106,7 +112,7 @@ const TabMePush: React.FC = () => {
return ( return (
<ScrollView> <ScrollView>
{pushAvailable !== false ? ( {pushAvailable === true ? (
<> <>
{pushEnabled === false ? ( {pushEnabled === false ? (
<MenuContainer> <MenuContainer>

View File

@ -1,4 +1,4 @@
import * as Updates from 'expo-updates' import { isDevelopment } from '@utils/checkEnvironment'
import React from 'react' import React from 'react'
import { ScrollView } from 'react-native-gesture-handler' import { ScrollView } from 'react-native-gesture-handler'
import SettingsAnalytics from './Settings/Analytics' import SettingsAnalytics from './Settings/Analytics'
@ -13,12 +13,7 @@ const TabMeSettings: React.FC = () => {
<SettingsTooot /> <SettingsTooot />
<SettingsAnalytics /> <SettingsAnalytics />
{__DEV__ || {isDevelopment ? <SettingsDev /> : null}
['development'].some(channel =>
Updates.releaseChannel.includes(channel)
) ? (
<SettingsDev />
) : null}
</ScrollView> </ScrollView>
) )
} }

View File

@ -4,7 +4,6 @@ import { MenuContainer, MenuRow } from '@components/Menu'
import { useNavigation } from '@react-navigation/native' import { useNavigation } from '@react-navigation/native'
import { StyleConstants } from '@utils/styles/constants' import { StyleConstants } from '@utils/styles/constants'
import { useTheme } from '@utils/styles/ThemeManager' import { useTheme } from '@utils/styles/ThemeManager'
import * as Updates from 'expo-updates'
import * as Linking from 'expo-linking' import * as Linking from 'expo-linking'
import * as StoreReview from 'expo-store-review' import * as StoreReview from 'expo-store-review'
import * as WebBrowser from 'expo-web-browser' import * as WebBrowser from 'expo-web-browser'
@ -12,6 +11,7 @@ import React from 'react'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import { useSelector } from 'react-redux' import { useSelector } from 'react-redux'
import { getInstanceActive } from '@utils/slices/instancesSlice' import { getInstanceActive } from '@utils/slices/instancesSlice'
import { isCandidate, isDevelopment } from '@utils/checkEnvironment'
const SettingsTooot: React.FC = () => { const SettingsTooot: React.FC = () => {
const instanceActive = useSelector(getInstanceActive) const instanceActive = useSelector(getInstanceActive)
@ -36,10 +36,7 @@ const SettingsTooot: React.FC = () => {
Linking.openURL('https://www.buymeacoffee.com/xmflsct') Linking.openURL('https://www.buymeacoffee.com/xmflsct')
}} }}
/> />
{__DEV__ || {isDevelopment || isCandidate ? (
['release', 'development'].some(channel =>
Updates.releaseChannel?.includes(channel)
) ? (
<MenuRow <MenuRow
title={t('me.settings.review.heading')} title={t('me.settings.review.heading')}
content={ content={

View File

@ -1,5 +1,5 @@
import { isDevelopment } from '@utils/checkEnvironment'
import Constants from 'expo-constants' import Constants from 'expo-constants'
import * as Updates from 'expo-updates'
import * as Sentry from 'sentry-expo' import * as Sentry from 'sentry-expo'
import log from './log' import log from './log'
@ -8,9 +8,7 @@ const sentry = () => {
Sentry.init({ Sentry.init({
dsn: Constants.manifest?.extra?.sentryDSN, dsn: Constants.manifest?.extra?.sentryDSN,
enableInExpoDevelopment: false, enableInExpoDevelopment: false,
debug: debug: isDevelopment
__DEV__ ||
['development'].some(channel => Updates.releaseChannel.includes(channel))
}) })
} }

View File

@ -0,0 +1,49 @@
import * as Updates from 'expo-updates'
const mapEnvironment = <T = unknown>({
release,
candidate,
development
}: {
release: T
candidate?: T
development?: T
}): T => {
if (isDevelopment) {
if (development) {
return development
} else {
throw new Error('Development environment but no development handler')
}
}
if (isCandidate) {
if (candidate) {
return candidate
} else {
throw new Error('Candidate environment but no candidate handler')
}
}
if (isRelease) {
return release
}
throw new Error(
`Environment not set. Please set the environment in the Expo project settings.`
)
}
const isDevelopment =
__DEV__ ||
['development'].some(channel => Updates.releaseChannel.includes(channel))
const isCandidate = ['candidate'].some(channel =>
Updates.releaseChannel.includes(channel)
)
const isRelease = ['release'].some(channel =>
Updates.releaseChannel.includes(channel)
)
export { mapEnvironment, isDevelopment, isCandidate, isRelease }

View File

@ -3,6 +3,7 @@ import apiTooot from '@api/tooot'
import { displayMessage } from '@components/Message' import { displayMessage } from '@components/Message'
import navigationRef from '@helpers/navigationRef' import navigationRef from '@helpers/navigationRef'
import { Dispatch } from '@reduxjs/toolkit' import { Dispatch } from '@reduxjs/toolkit'
import { isDevelopment } from '@utils/checkEnvironment'
import { disableAllPushes, Instance } from '@utils/slices/instancesSlice' import { disableAllPushes, Instance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import { useEffect } from 'react' import { useEffect } from 'react'
@ -18,11 +19,13 @@ export interface Params {
const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => { const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => {
return useEffect(() => { return useEffect(() => {
const connect = async () => { const connect = async () => {
const expoToken = ( const expoToken = isDevelopment
await Notifications.getExpoPushTokenAsync({ ? 'DEVELOPMENT_TOKEN_1'
experienceId: '@xmflsct/tooot' : (
}) await Notifications.getExpoPushTokenAsync({
).data experienceId: '@xmflsct/tooot'
})
).data
apiTooot({ apiTooot({
method: 'get', method: 'get',

View File

@ -1,5 +1,6 @@
import { createAsyncThunk } from '@reduxjs/toolkit' import { createAsyncThunk } from '@reduxjs/toolkit'
import { RootState } from '@root/store' import { RootState } from '@root/store'
import { isDevelopment } from '@utils/checkEnvironment'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import { Instance } from '../instancesSlice' import { Instance } from '../instancesSlice'
import pushRegister from './push/register' import pushRegister from './push/register'
@ -12,11 +13,13 @@ export const updateInstancePush = createAsyncThunk(
{ getState } { getState }
): Promise<Instance['push']['keys']['auth'] | undefined> => { ): Promise<Instance['push']['keys']['auth'] | undefined> => {
const state = getState() as RootState const state = getState() as RootState
const expoToken = ( const expoToken = isDevelopment
await Notifications.getExpoPushTokenAsync({ ? 'DEVELOPMENT_TOKEN_1'
experienceId: '@xmflsct/tooot' : (
}) await Notifications.getExpoPushTokenAsync({
).data experienceId: '@xmflsct/tooot'
})
).data
if (disable) { if (disable) {
return await pushRegister(state, expoToken) return await pushRegister(state, expoToken)

View File

@ -2,6 +2,7 @@ import apiTooot from '@api/tooot'
import { createAsyncThunk } from '@reduxjs/toolkit' import { createAsyncThunk } from '@reduxjs/toolkit'
import i18n from '@root/i18n/i18n' import i18n from '@root/i18n/i18n'
import { RootState } from '@root/store' import { RootState } from '@root/store'
import { isDevelopment } from '@utils/checkEnvironment'
import * as Notifications from 'expo-notifications' import * as Notifications from 'expo-notifications'
import { Platform } from 'react-native' import { Platform } from 'react-native'
import { getInstance, Instance } from '../instancesSlice' import { getInstance, Instance } from '../instancesSlice'
@ -19,11 +20,13 @@ export const updateInstancePushDecode = createAsyncThunk(
return Promise.reject() return Promise.reject()
} }
const expoToken = ( const expoToken = isDevelopment
await Notifications.getExpoPushTokenAsync({ ? 'DEVELOPMENT_TOKEN_1'
experienceId: '@xmflsct/tooot' : (
}) await Notifications.getExpoPushTokenAsync({
).data experienceId: '@xmflsct/tooot'
})
).data
await apiTooot({ await apiTooot({
method: 'put', method: 'put',