mirror of
https://github.com/zimplexing/OrionTV.git
synced 2026-03-12 15:17:29 +08:00
fix(init): resolve startup error message timing issue
Fix race condition where 'please check network or server address' error was shown on first startup even when API was properly configured. - Add isLoadingServerConfig state to track server config fetch status - Modify authStore to wait for server config loading before showing errors - Ensure loadSettings completes fully before triggering login checks - Only show network error when config fetch actually fails, not during loading
This commit is contained in:
@@ -13,7 +13,6 @@ import useAuthStore from "@/stores/authStore";
|
|||||||
import { useUpdateStore, initUpdateStore } from "@/stores/updateStore";
|
import { useUpdateStore, initUpdateStore } from "@/stores/updateStore";
|
||||||
import { UpdateModal } from "@/components/UpdateModal";
|
import { UpdateModal } from "@/components/UpdateModal";
|
||||||
import { UPDATE_CONFIG } from "@/constants/UpdateConfig";
|
import { UPDATE_CONFIG } from "@/constants/UpdateConfig";
|
||||||
import MobileBottomNavigation from "@/components/MobileBottomNavigation";
|
|
||||||
import { useResponsiveLayout } from "@/hooks/useResponsiveLayout";
|
import { useResponsiveLayout } from "@/hooks/useResponsiveLayout";
|
||||||
|
|
||||||
// Prevent the splash screen from auto-hiding before asset loading is complete.
|
// Prevent the splash screen from auto-hiding before asset loading is complete.
|
||||||
@@ -31,7 +30,10 @@ export default function RootLayout() {
|
|||||||
const responsiveConfig = useResponsiveLayout();
|
const responsiveConfig = useResponsiveLayout();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadSettings();
|
const initializeApp = async () => {
|
||||||
|
await loadSettings();
|
||||||
|
};
|
||||||
|
initializeApp();
|
||||||
initUpdateStore(); // 初始化更新存储
|
initUpdateStore(); // 初始化更新存储
|
||||||
}, [loadSettings]);
|
}, [loadSettings]);
|
||||||
|
|
||||||
@@ -73,8 +75,6 @@ export default function RootLayout() {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isMobile = responsiveConfig.deviceType === 'mobile';
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
|
<ThemeProvider value={colorScheme === "dark" ? DarkTheme : DefaultTheme}>
|
||||||
<View style={styles.container}>
|
<View style={styles.container}>
|
||||||
@@ -88,7 +88,6 @@ export default function RootLayout() {
|
|||||||
<Stack.Screen name="favorites" options={{ headerShown: false }} />
|
<Stack.Screen name="favorites" options={{ headerShown: false }} />
|
||||||
<Stack.Screen name="+not-found" />
|
<Stack.Screen name="+not-found" />
|
||||||
</Stack>
|
</Stack>
|
||||||
{isMobile && <MobileBottomNavigation colorScheme={colorScheme} />}
|
|
||||||
</View>
|
</View>
|
||||||
<Toast />
|
<Toast />
|
||||||
<LoginModal />
|
<LoginModal />
|
||||||
|
|||||||
@@ -24,10 +24,34 @@ const useAuthStore = create<AuthState>((set) => ({
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const serverConfig = useSettingsStore.getState().serverConfig;
|
// Wait for server config to be loaded if it's currently loading
|
||||||
|
const settingsState = useSettingsStore.getState();
|
||||||
|
let serverConfig = settingsState.serverConfig;
|
||||||
|
|
||||||
|
// If server config is loading, wait a bit for it to complete
|
||||||
|
if (settingsState.isLoadingServerConfig) {
|
||||||
|
// Wait up to 3 seconds for server config to load
|
||||||
|
const maxWaitTime = 3000;
|
||||||
|
const checkInterval = 100;
|
||||||
|
let waitTime = 0;
|
||||||
|
|
||||||
|
while (waitTime < maxWaitTime) {
|
||||||
|
await new Promise(resolve => setTimeout(resolve, checkInterval));
|
||||||
|
waitTime += checkInterval;
|
||||||
|
const currentState = useSettingsStore.getState();
|
||||||
|
if (!currentState.isLoadingServerConfig) {
|
||||||
|
serverConfig = currentState.serverConfig;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (!serverConfig?.StorageType) {
|
if (!serverConfig?.StorageType) {
|
||||||
Toast.show({ type: "error", text1: "请检查网络或者服务器地址是否可用" });
|
// Only show error if we're not loading and have tried to fetch the config
|
||||||
return
|
if (!settingsState.isLoadingServerConfig) {
|
||||||
|
Toast.show({ type: "error", text1: "请检查网络或者服务器地址是否可用" });
|
||||||
|
}
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const cookies = await Cookies.get(api.baseURL);
|
const cookies = await Cookies.get(api.baseURL);
|
||||||
if (serverConfig && serverConfig.StorageType === "localstorage" && !cookies.auth) {
|
if (serverConfig && serverConfig.StorageType === "localstorage" && !cookies.auth) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ interface SettingsState {
|
|||||||
};
|
};
|
||||||
isModalVisible: boolean;
|
isModalVisible: boolean;
|
||||||
serverConfig: ServerConfig | null;
|
serverConfig: ServerConfig | null;
|
||||||
|
isLoadingServerConfig: boolean;
|
||||||
loadSettings: () => Promise<void>;
|
loadSettings: () => Promise<void>;
|
||||||
fetchServerConfig: () => Promise<void>;
|
fetchServerConfig: () => Promise<void>;
|
||||||
setApiBaseUrl: (url: string) => void;
|
setApiBaseUrl: (url: string) => void;
|
||||||
@@ -33,6 +34,7 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
|||||||
remoteInputEnabled: false,
|
remoteInputEnabled: false,
|
||||||
isModalVisible: false,
|
isModalVisible: false,
|
||||||
serverConfig: null,
|
serverConfig: null,
|
||||||
|
isLoadingServerConfig: false,
|
||||||
videoSource: {
|
videoSource: {
|
||||||
enabledAll: true,
|
enabledAll: true,
|
||||||
sources: {},
|
sources: {},
|
||||||
@@ -48,10 +50,13 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
|||||||
sources: {},
|
sources: {},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
api.setBaseUrl(settings.apiBaseUrl);
|
if (settings.apiBaseUrl) {
|
||||||
await get().fetchServerConfig();
|
api.setBaseUrl(settings.apiBaseUrl);
|
||||||
|
await get().fetchServerConfig();
|
||||||
|
}
|
||||||
},
|
},
|
||||||
fetchServerConfig: async () => {
|
fetchServerConfig: async () => {
|
||||||
|
set({ isLoadingServerConfig: true });
|
||||||
try {
|
try {
|
||||||
const config = await api.getServerConfig();
|
const config = await api.getServerConfig();
|
||||||
if (config) {
|
if (config) {
|
||||||
@@ -61,6 +66,8 @@ export const useSettingsStore = create<SettingsState>((set, get) => ({
|
|||||||
} catch (error) {
|
} catch (error) {
|
||||||
set({ serverConfig: null });
|
set({ serverConfig: null });
|
||||||
console.info("Failed to fetch server config:", error);
|
console.info("Failed to fetch server config:", error);
|
||||||
|
} finally {
|
||||||
|
set({ isLoadingServerConfig: false });
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setApiBaseUrl: (url) => set({ apiBaseUrl: url }),
|
setApiBaseUrl: (url) => set({ apiBaseUrl: url }),
|
||||||
|
|||||||
Reference in New Issue
Block a user