Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): stabilize navigation screen options (#5199)

Tsai Yuan408f6ded8f5d
diff --git a/apps/mobile/app/(auth)/login.tsx b/apps/mobile/app/(auth)/login.tsx
index d88827a9c5..a8e21daaf9 100644
--- a/apps/mobile/app/(auth)/login.tsx
+++ b/apps/mobile/app/(auth)/login.tsx
@@ -1357,6 +1357,11 @@ export function LoginScreen({
const captchaChallengeOpen = auth.captchaChallenge !== null;
const deletionBubbleA11yHidden =
consentDialogOpen || realmConsentOpen || captchaChallengeOpen || handoffPhase !== 'done';
+ // Stack.Screen applies options from a layout effect; keep its object identity
+ // stable while this screen rerenders for auth or device-link state updates.
+ const loginStatusBarOptions = useMemo(() => ({
+ statusBarStyle: stageTheme === 'dark' ? 'light' : 'dark',
+ } as const), [stageTheme]);
return (
<MobileLoginHandoffStage
@@ -1368,11 +1373,7 @@ export function LoginScreen({
{!additionalAccount ? <SystemNavigationBack available={!!backAction} disabled={auth.isBusy}
label={loginText('back')} onPress={() => backAction?.()} /> : null}
{Platform.OS === 'ios' ? (
- <Stack.Screen
- options={{
- statusBarStyle: stageTheme === 'dark' ? 'light' : 'dark',
- }}
- />
+ <Stack.Screen options={loginStatusBarOptions} />
) : null}
{additionalAccount && onClose ? (
diff --git a/apps/mobile/src/__tests__/navigationOptionsStability.test.tsx b/apps/mobile/src/__tests__/navigationOptionsStability.test.tsx
new file mode 100644
index 0000000000..22fe7959c0
--- /dev/null
+++ b/apps/mobile/src/__tests__/navigationOptionsStability.test.tsx
@@ -0,0 +1,56 @@
+// @vitest-environment jsdom
+import { cleanup, render } from '@testing-library/react';
+import type { ReactNode } from 'react';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+
+const seenScreenOptions: unknown[] = [];
+
+function Screen({ options }: { options: unknown }) {
+ seenScreenOptions.push(options);
+ return null;
+}
+
+function Toolbar({ children }: { children?: ReactNode }) {
+ return <>{children}</>;
+}
+Toolbar.Button = () => null;
+
+vi.mock('expo-router', () => ({
+ Stack: { Screen, Toolbar },
+}));
+vi.mock('react-native', () => ({
+ Platform: { OS: 'ios' },
+}));
+vi.mock('@/platform/AdaptiveWindowContext', () => ({
+ useAdaptiveWindow: () => ({ barEdge: 'top' }),
+}));
+vi.mock('@/theme', () => ({
+ useTheme: () => ({ colors: { textPrimary: '#111111' } }),
+}));
+
+import { SystemNavigationBack } from '@/platform/chrome/SystemNavigationBack';
+
+beforeEach(() => {
+ seenScreenOptions.length = 0;
+});
+
+afterEach(() => cleanup());
+
+describe('navigation option stability', () => {
+ it('retains system-back options through unrelated rerenders and replaces them for option inputs', () => {
+ const view = render(
+ <SystemNavigationBack label="Back" onPress={() => undefined} />,
+ );
+ const initialOptions = seenScreenOptions.at(-1);
+
+ view.rerender(
+ <SystemNavigationBack label="Return" disabled onPress={() => undefined} />,
+ );
+ expect(seenScreenOptions.at(-1)).toBe(initialOptions);
+
+ view.rerender(
+ <SystemNavigationBack available={false} label="Return" disabled onPress={() => undefined} />,
+ );
+ expect(seenScreenOptions.at(-1)).not.toBe(initialOptions);
+ });
+});
diff --git a/apps/mobile/src/platform/chrome/SystemNavigationBack.tsx b/apps/mobile/src/platform/chrome/SystemNavigationBack.tsx
index f3112a4f74..ce99b63f57 100644
--- a/apps/mobile/src/platform/chrome/SystemNavigationBack.tsx
+++ b/apps/mobile/src/platform/chrome/SystemNavigationBack.tsx
@@ -1,4 +1,5 @@
import { Stack } from 'expo-router';
+import { useMemo } from 'react';
import { Platform } from 'react-native';
import { useAdaptiveWindow } from '../AdaptiveWindowContext';
import { useTheme } from '@/theme';
@@ -16,10 +17,19 @@ export function SystemNavigationBack({ onPress, label, disabled = false, close =
}) {
const enabled = useSystemNavigationBack();
const { colors } = useTheme();
+ // expo-router applies screen options in a layout effect. Keep this object
+ // stable across unrelated store updates, otherwise setOptions can feed a
+ // rerender back into this route indefinitely.
+ const screenOptions = useMemo(() => ({
+ headerShown: enabled && available,
+ headerTransparent: true,
+ headerShadowVisible: false,
+ headerBackVisible: false,
+ headerTitle: '',
+ headerTintColor: colors.textPrimary,
+ }), [available, colors.textPrimary, enabled]);
return <>
- <Stack.Screen options={{ headerShown: enabled && available, headerTransparent: true,
- headerShadowVisible: false, headerBackVisible: false, headerTitle: '',
- headerTintColor: colors.textPrimary }} />
+ <Stack.Screen options={screenOptions} />
{enabled && available ? <Stack.Toolbar placement="left">
<Stack.Toolbar.Button icon={close ? 'xmark' : 'chevron.backward'}
accessibilityLabel={label} disabled={disabled} onPress={onPress} />