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()
}, [])
useEffect(() => {
AppState.addEventListener('change', appStateEffect)
const appStateListener = AppState.addEventListener('change', appStateEffect)
return () => {
AppState.removeEventListener('change', appStateEffect)
appStateListener.remove()
}
}, [])

View File

@ -1,3 +1,4 @@
import { mapEnvironment } from '@utils/checkEnvironment'
import axios from 'axios'
import chalk from 'chalk'
import Constants from 'expo-constants'
@ -16,7 +17,11 @@ export type Params = {
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>({
method,

View File

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

View File

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

View File

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

View File

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

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 navigationRef from '@helpers/navigationRef'
import { Dispatch } from '@reduxjs/toolkit'
import { isDevelopment } from '@utils/checkEnvironment'
import { disableAllPushes, Instance } from '@utils/slices/instancesSlice'
import * as Notifications from 'expo-notifications'
import { useEffect } from 'react'
@ -18,7 +19,9 @@ export interface Params {
const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => {
return useEffect(() => {
const connect = async () => {
const expoToken = (
const expoToken = isDevelopment
? 'DEVELOPMENT_TOKEN_1'
: (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})

View File

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

View File

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