1
0
mirror of https://github.com/tooot-app/app synced 2025-06-05 22:19:13 +02:00

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

@@ -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,11 +19,13 @@ export interface Params {
const pushUseConnect = ({ mode, t, instances, dispatch }: Params) => {
return useEffect(() => {
const connect = async () => {
const expoToken = (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
const expoToken = isDevelopment
? 'DEVELOPMENT_TOKEN_1'
: (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
apiTooot({
method: 'get',

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,11 +13,13 @@ export const updateInstancePush = createAsyncThunk(
{ getState }
): Promise<Instance['push']['keys']['auth'] | undefined> => {
const state = getState() as RootState
const expoToken = (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
const expoToken = isDevelopment
? 'DEVELOPMENT_TOKEN_1'
: (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
if (disable) {
return await pushRegister(state, expoToken)

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,11 +20,13 @@ export const updateInstancePushDecode = createAsyncThunk(
return Promise.reject()
}
const expoToken = (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
const expoToken = isDevelopment
? 'DEVELOPMENT_TOKEN_1'
: (
await Notifications.getExpoPushTokenAsync({
experienceId: '@xmflsct/tooot'
})
).data
await apiTooot({
method: 'put',