feat(mobile): add environment profile management

- Introduce `proxyEnv` to dynamically manage environment configurations
- Add `EnvProfileIndicator` to show current environment profile
- Create `useEnvProfile` and `setEnvProfile` for runtime env switching
- Update imports and references to use `proxyEnv` instead of static `env`

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-03-05 23:21:29 +08:00
parent f1e1403b87
commit 5f1796d4a6
9 changed files with 89 additions and 16 deletions
@@ -1,13 +1,14 @@
import { env } from "@follow/shared/src/env.rn"
import { createBuildSafeHeaders } from "@follow/utils/src/headers"
import { getImageProxyUrl, IMAGE_PROXY_URL } from "@follow/utils/src/img-proxy"
import type { ImageProps } from "expo-image"
import { Image } from "expo-image"
import { forwardRef, useCallback, useMemo, useState } from "react"
const buildSafeHeaders = createBuildSafeHeaders(env.VITE_WEB_URL, [
import { proxyEnv } from "@/src/lib/proxy-env"
const buildSafeHeaders = createBuildSafeHeaders(proxyEnv.VITE_WEB_URL, [
IMAGE_PROXY_URL,
env.VITE_API_URL,
proxyEnv.VITE_API_URL,
])
export const ProxiedImage = forwardRef<
Image,
+3 -3
View File
@@ -1,17 +1,17 @@
/* eslint-disable no-console */
import type { AppType } from "@follow/shared"
import { env } from "@follow/shared/src/env.rn"
import { FetchError, ofetch } from "ofetch"
import { userActions } from "../store/user/store"
import { getCookie } from "./auth"
import { proxyEnv } from "./proxy-env"
const { hc } = require("hono/dist/cjs/client/client") as typeof import("hono/client")
export const apiFetch = ofetch.create({
retry: false,
baseURL: env.VITE_API_URL,
baseURL: proxyEnv.VITE_API_URL,
onRequest: async (ctx) => {
const { options, request } = ctx
if (__DEV__) {
@@ -47,7 +47,7 @@ export const apiFetch = ofetch.create({
},
})
export const apiClient = hc<AppType>(env.VITE_API_URL, {
export const apiClient = hc<AppType>(proxyEnv.VITE_API_URL, {
fetch: async (input: any, options = {}) =>
apiFetch(input.toString(), options).catch((err) => {
throw err
+2 -2
View File
@@ -1,5 +1,4 @@
import { expoClient } from "@better-auth/expo/client"
import { env } from "@follow/shared/src/env.rn"
import { useQuery } from "@tanstack/react-query"
import { twoFactorClient } from "better-auth/client/plugins"
import { createAuthClient } from "better-auth/react"
@@ -7,6 +6,7 @@ import type * as better_call from "better-call"
import * as SecureStore from "expo-secure-store"
import { whoamiQueryKey } from "../store/user/hooks"
import { proxyEnv } from "./proxy-env"
import { queryClient } from "./query-client"
const storagePrefix = "follow_auth"
@@ -14,7 +14,7 @@ export const cookieKey = `${storagePrefix}_cookie`
export const sessionTokenKey = "__Secure-better-auth.session_token"
const authClient = createAuthClient({
baseURL: `${env.VITE_API_URL}/better-auth`,
baseURL: `${proxyEnv.VITE_API_URL}/better-auth`,
plugins: [
twoFactorClient(),
{
+35
View File
@@ -0,0 +1,35 @@
import type { env } from "@follow/shared/src/env.rn"
import { envProfileMap } from "@follow/shared/src/env.rn"
import { createAtomHooks } from "@follow/utils"
import { reloadAppAsync } from "expo"
import { atomWithStorage } from "jotai/utils"
import { JotaiPersistSyncStorage } from "./jotai"
const [, , useEnvProfile, , getEnvProfile, _setEnvProfile] = createAtomHooks(
atomWithStorage(
"##Follow-Current-Env-Profile",
__DEV__ ? "dev" : "prod",
JotaiPersistSyncStorage,
{
getOnInit: true,
},
),
)
export const proxyEnv = new Proxy(
{},
{
get(target, prop) {
const profile = getEnvProfile() as keyof typeof envProfileMap
return envProfileMap[profile][prop as keyof (typeof envProfileMap)[typeof profile]]
},
},
) as any as typeof env
export const setEnvProfile = (profile: keyof typeof envProfileMap) => {
const currentProfile = getEnvProfile()
if (currentProfile === profile) return
_setEnvProfile(profile)
reloadAppAsync()
}
export { useEnvProfile }
+16 -1
View File
@@ -2,7 +2,7 @@ import { router } from "expo-router"
import { useAtom } from "jotai"
import { atomWithStorage } from "jotai/utils"
import { useMemo } from "react"
import { Dimensions } from "react-native"
import { Dimensions, Text, View } from "react-native"
import { Gesture, GestureDetector } from "react-native-gesture-handler"
import { runOnJS, useAnimatedStyle, useSharedValue, withSpring } from "react-native-reanimated"
import { useSafeAreaInsets } from "react-native-safe-area-context"
@@ -10,6 +10,7 @@ import { useSafeAreaInsets } from "react-native-safe-area-context"
import { ReAnimatedTouchableOpacity } from "@/src/components/common/AnimatedComponents"
import { BugCuteReIcon } from "@/src/icons/bug_cute_re"
import { JotaiPersistSyncStorage } from "@/src/lib/jotai"
import { useEnvProfile } from "@/src/lib/proxy-env"
export const DebugButton = () => {
const cachedPositionAtom = useMemo(
@@ -83,3 +84,17 @@ export const DebugButton = () => {
</GestureDetector>
)
}
export const EnvProfileIndicator = () => {
const envProfile = useEnvProfile()
if (!__DEV__ && envProfile === "prod") return null
return (
<View className="absolute bottom-0 left-16 items-center justify-center" pointerEvents="none">
<View className="bg-accent rounded p-1">
<Text className="text-xs uppercase text-white">{envProfile}</Text>
</View>
</View>
)
}
@@ -1,5 +1,4 @@
import { FeedViewType } from "@follow/constants"
import { env } from "@follow/shared/src/env.rn"
import { createBuildSafeHeaders } from "@follow/utils/src/headers"
import { IMAGE_PROXY_URL } from "@follow/utils/src/img-proxy"
import { useQuery } from "@tanstack/react-query"
@@ -15,15 +14,16 @@ import Animated, { FadeInUp } from "react-native-reanimated"
import { FeedIcon } from "@/src/components/ui/icon/feed-icon"
import { ItemPressable } from "@/src/components/ui/pressable/ItemPressable"
import { apiClient } from "@/src/lib/api-fetch"
import { proxyEnv } from "@/src/lib/proxy-env"
import { useSubscriptionByFeedId } from "@/src/store/subscription/hooks"
import { useSearchPageContext } from "../ctx"
import { BaseSearchPageFlatList, ItemSeparator, RenderScrollComponent } from "./__base"
import { useDataSkeleton } from "./hooks"
const buildSafeHeaders = createBuildSafeHeaders(env.VITE_WEB_URL, [
const buildSafeHeaders = createBuildSafeHeaders(proxyEnv.VITE_WEB_URL, [
IMAGE_PROXY_URL,
env.VITE_API_URL,
proxyEnv.VITE_API_URL,
])
type SearchResultItem = Awaited<ReturnType<typeof apiClient.discover.$post>>["data"][number]
@@ -1,3 +1,4 @@
import type { envProfileMap } from "@follow/shared/src/env.rn"
import { sleep } from "@follow/utils"
import { requireNativeModule } from "expo"
import * as Clipboard from "expo-clipboard"
@@ -20,10 +21,12 @@ import {
} from "react-native"
import { useSafeAreaInsets } from "react-native-safe-area-context"
import { Select } from "@/src/components/ui/form/Select"
import { getDbPath } from "@/src/database"
import { cookieKey, getCookie, sessionTokenKey, signOut } from "@/src/lib/auth"
import { loading } from "@/src/lib/loading"
import { quickLookImage } from "@/src/lib/native"
import { setEnvProfile, useEnvProfile } from "@/src/lib/proxy-env"
import { toast } from "@/src/lib/toast"
interface MenuSection {
@@ -38,6 +41,7 @@ interface MenuItem {
}
export default function DebugPanel() {
const insets = useSafeAreaInsets()
const envProfile = useEnvProfile()
const menuSections: MenuSection[] = [
{
@@ -153,6 +157,20 @@ export default function DebugPanel() {
return (
<ScrollView ref={ref} className="flex-1 bg-black" style={{ paddingTop: insets.top }}>
<View className="flex-row items-center justify-between px-8">
<Text className="text-2xl font-medium text-white">Current Env Profile: {envProfile}</Text>
<Select
options={[
{ label: "Dev", value: "dev" },
{ label: "Prod", value: "prod" },
{ label: "Staging", value: "staging" },
]}
value={envProfile}
onValueChange={(value) => {
setEnvProfile(value as keyof typeof envProfileMap)
}}
/>
</View>
{menuSections.map((section) => (
<View key={section.title}>
<Text className="mt-4 px-8 text-2xl font-medium text-white">{section.title}</Text>
+5 -1
View File
@@ -6,8 +6,9 @@ import { useColorScheme } from "nativewind"
import { useEffect } from "react"
import { useSheet } from "react-native-sheet-transitions"
import { FullWindowOverlay } from "../components/common/FullWindowOverlay"
import { useIntentHandler } from "../hooks/useIntentHandler"
import { DebugButton } from "../modules/debug"
import { DebugButton, EnvProfileIndicator } from "../modules/debug"
import { RootProviders } from "../providers"
import { usePrefetchSessionUser } from "../store/user/hooks"
import { getSystemBackgroundColor } from "../theme/utils"
@@ -38,6 +39,9 @@ export default function RootLayout() {
<AnimatedStack />
{__DEV__ && <DebugButton />}
<FullWindowOverlay>
<EnvProfileIndicator />
</FullWindowOverlay>
</RootProviders>
)
}
+3 -3
View File
@@ -3,7 +3,7 @@
*/
const profile = "prod"
const appEndpointMap = {
export const envProfileMap = {
prod: {
VITE_API_URL: "https://api.follow.is",
VITE_WEB_URL: "https://app.follow.is",
@@ -22,6 +22,6 @@ const appEndpointMap = {
}
export const env = {
VITE_WEB_URL: appEndpointMap[profile].VITE_WEB_URL,
VITE_API_URL: appEndpointMap[profile].VITE_API_URL,
VITE_WEB_URL: envProfileMap[profile].VITE_WEB_URL,
VITE_API_URL: envProfileMap[profile].VITE_API_URL,
}