diff --git a/apps/mobile/src/components/ui/image/ProxiedImage.tsx b/apps/mobile/src/components/ui/image/ProxiedImage.tsx index 8941059f8..99df42d28 100644 --- a/apps/mobile/src/components/ui/image/ProxiedImage.tsx +++ b/apps/mobile/src/components/ui/image/ProxiedImage.tsx @@ -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, diff --git a/apps/mobile/src/lib/api-fetch.ts b/apps/mobile/src/lib/api-fetch.ts index a1646483d..3e8371bdd 100644 --- a/apps/mobile/src/lib/api-fetch.ts +++ b/apps/mobile/src/lib/api-fetch.ts @@ -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(env.VITE_API_URL, { +export const apiClient = hc(proxyEnv.VITE_API_URL, { fetch: async (input: any, options = {}) => apiFetch(input.toString(), options).catch((err) => { throw err diff --git a/apps/mobile/src/lib/auth.ts b/apps/mobile/src/lib/auth.ts index 77436c2e3..90b29a079 100644 --- a/apps/mobile/src/lib/auth.ts +++ b/apps/mobile/src/lib/auth.ts @@ -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(), { diff --git a/apps/mobile/src/lib/proxy-env.ts b/apps/mobile/src/lib/proxy-env.ts new file mode 100644 index 000000000..d12bb929f --- /dev/null +++ b/apps/mobile/src/lib/proxy-env.ts @@ -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 } diff --git a/apps/mobile/src/modules/debug/index.tsx b/apps/mobile/src/modules/debug/index.tsx index 77bc68779..4abd169fc 100644 --- a/apps/mobile/src/modules/debug/index.tsx +++ b/apps/mobile/src/modules/debug/index.tsx @@ -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 = () => { ) } + +export const EnvProfileIndicator = () => { + const envProfile = useEnvProfile() + + if (!__DEV__ && envProfile === "prod") return null + + return ( + + + {envProfile} + + + ) +} diff --git a/apps/mobile/src/modules/discover/search-tabs/SearchFeed.tsx b/apps/mobile/src/modules/discover/search-tabs/SearchFeed.tsx index 001cf7e4a..04de545c7 100644 --- a/apps/mobile/src/modules/discover/search-tabs/SearchFeed.tsx +++ b/apps/mobile/src/modules/discover/search-tabs/SearchFeed.tsx @@ -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>["data"][number] diff --git a/apps/mobile/src/screens/(headless)/debug.tsx b/apps/mobile/src/screens/(headless)/debug.tsx index a4809cee2..47a4dc620 100644 --- a/apps/mobile/src/screens/(headless)/debug.tsx +++ b/apps/mobile/src/screens/(headless)/debug.tsx @@ -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 ( + + Current Env Profile: {envProfile} +