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): refresh update check message on language change (#624) (#632)

qsoyq76f9791a2920
diff --git a/apps/mobile/app/settings.tsx b/apps/mobile/app/settings.tsx
index 3a8b52a9cc..e6182c0901 100644
--- a/apps/mobile/app/settings.tsx
+++ b/apps/mobile/app/settings.tsx
@@ -65,7 +65,11 @@ import {
} from '@/notifications/pushNotifications';
import { buildMobileUpdateInfoRows, currentMobileOtaVersion } from '@/settings/updateInfo';
import { shouldCheckBundleUpdate } from '@/update/bundleUpdate';
-import { runManualUpdateCheck } from '@/update/manualUpdateCheck';
+import {
+ manualUpdateCheckMessage,
+ runManualUpdateCheck,
+ type ManualUpdateCheckOutcome,
+} from '@/update/manualUpdateCheck';
import { useBundleUpdatePrompt } from '@/update/useBundleUpdatePrompt';
import { useCanaryChannelGate } from '@/update/useCanaryChannelGate';
import { useTheme, useThemedStyles, type ThemeColors } from '@/theme';
@@ -94,7 +98,7 @@ export default function SettingsScreen() {
useState(false);
const [debugExpanded, setDebugExpanded] = useState(false);
const [updatePhase, setUpdatePhase] = useState<UpdatePhase>('idle');
- const [updateMessage, setUpdateMessage] = useState<string | null>(null);
+ const [updateOutcome, setUpdateOutcome] = useState<ManualUpdateCheckOutcome | null>(null);
const [pushEnabled, setPushEnabled] = useState(false);
const [pushBusy, setPushBusy] = useState(false);
const [pushMessage, setPushMessage] = useState<string | null>(null);
@@ -159,6 +163,14 @@ export default function SettingsScreen() {
isTestFlightBuild: IS_TESTFLIGHT_BUILD,
});
const updateCheckEnabled = bundleCheckEnabled || updatesEnabled;
+ // 保存未翻译的结果,语言切换触发重渲染时用当前 t() 重新生成提示。
+ const updateMessage = useMemo(
+ () => updateOutcome && manualUpdateCheckMessage(updateOutcome, {
+ isTestFlightBuild: IS_TESTFLIGHT_BUILD,
+ t,
+ }),
+ [t, updateOutcome],
+ );
const aboutSection = overview.sections.find((section) => section.id === 'about');
const debugSection = overview.sections.find((section) => section.id === 'debug');
@@ -228,7 +240,7 @@ export default function SettingsScreen() {
// 审核模式:入口按钮已隐藏,这里再挡一层(状态由代码保证,不依赖 UI 层记得隐藏)。
if (REVIEW_MODE || !updateCheckEnabled || updateCheckInFlightRef.current) return;
updateCheckInFlightRef.current = true;
- setUpdateMessage(null);
+ setUpdateOutcome(null);
try {
const outcome = await runManualUpdateCheck({
checkBundleUpdate: bundleCheckEnabled ? checkBundleUpdate : undefined,
@@ -238,31 +250,19 @@ export default function SettingsScreen() {
reload: () => Updates.reloadAsync(),
onPhase: (phase) => setUpdatePhase(phase),
});
+ setUpdateOutcome(outcome);
if (outcome.kind === 'bundle-update-available') {
setUpdatePhase('idle');
- setUpdateMessage(t('settings.version.bundleUpdateFound'));
} else if (outcome.kind === 'up-to-date') {
setUpdatePhase('uptodate');
- setUpdateMessage(t(
- IS_TESTFLIGHT_BUILD
- ? 'settings.version.testFlightNoContentUpdate'
- : 'settings.version.upToDate',
- ));
} else if (outcome.kind === 'ota-unavailable') {
setUpdatePhase('uptodate');
- setUpdateMessage(t(
- IS_TESTFLIGHT_BUILD
- ? 'settings.version.testFlightContentUpdateUnavailable'
- : 'settings.version.bundleUpToDateNoOta',
- ));
} else if (outcome.kind === 'reloading') {
setUpdatePhase('downloading');
- setUpdateMessage(t('settings.version.downloadedRestarting'));
} else if (outcome.kind === 'busy') {
setUpdatePhase('idle');
} else {
setUpdatePhase('error');
- setUpdateMessage(outcome.message);
}
} finally {
updateCheckInFlightRef.current = false;
diff --git a/apps/mobile/src/__tests__/mobileSettings.test.ts b/apps/mobile/src/__tests__/mobileSettings.test.ts
index 14b5266c13..3c1240ec90 100644
--- a/apps/mobile/src/__tests__/mobileSettings.test.ts
+++ b/apps/mobile/src/__tests__/mobileSettings.test.ts
@@ -211,10 +211,13 @@ describe('mobile settings overview', () => {
expect(source).toContain('isTestFlightBuild: IS_TESTFLIGHT_BUILD');
expect(source).toContain('const updateCheckEnabled = bundleCheckEnabled || updatesEnabled');
expect(source).toContain('checkBundleUpdate: bundleCheckEnabled ? checkBundleUpdate : undefined');
+ expect(source).toContain('const [updateOutcome, setUpdateOutcome] = useState<ManualUpdateCheckOutcome | null>(null);');
+ expect(source).toContain('manualUpdateCheckMessage(updateOutcome, {');
+ expect(source).toContain('setUpdateOutcome(outcome);');
+ expect(source).not.toContain('const [updateMessage, setUpdateMessage]');
+ expect(source).not.toContain('setUpdateMessage(');
expect(source).toContain("'settings.version.testFlightCheckAction'");
expect(source).toContain("'settings.version.testFlightCheckingAccessibility'");
- expect(source).toContain("'settings.version.testFlightNoContentUpdate'");
- expect(source).toContain("'settings.version.testFlightContentUpdateUnavailable'");
expect(source).toContain("testID=\"settings.testFlightUpdateHint\"");
expect(source).toContain("{t('settings.version.testFlightUpdateManaged')}");
expect(source).toContain("{t('settings.version.bundleVersion', { version: appVersion })}");
diff --git a/apps/mobile/src/update/manualUpdateCheck.test.ts b/apps/mobile/src/update/manualUpdateCheck.test.ts
index 2313f09118..a01f44733b 100644
--- a/apps/mobile/src/update/manualUpdateCheck.test.ts
+++ b/apps/mobile/src/update/manualUpdateCheck.test.ts
@@ -1,6 +1,7 @@
import { beforeAll, describe, expect, it, vi } from 'vitest';
import { i18n } from '@/i18n';
import {
+ manualUpdateCheckMessage,
runManualUpdateCheck,
type BundleUpdateCheckOutcome,
type ManualUpdateCheckDeps,
@@ -66,7 +67,7 @@ describe('runManualUpdateCheck', () => {
const input = deps({ checkBundleUpdate: bundleCheck('error') });
await expect(runManualUpdateCheck(input)).resolves.toEqual({
kind: 'error',
- message: '无法检查整包更新,请稍后重试',
+ reason: 'bundle-check',
});
expect(input.checkOtaUpdate).not.toHaveBeenCalled();
});
@@ -85,4 +86,32 @@ describe('runManualUpdateCheck', () => {
await expect(runManualUpdateCheck(input)).resolves.toEqual({ kind: 'ota-unavailable' });
expect(input.checkOtaUpdate).not.toHaveBeenCalled();
});
+
+ it('keeps OTA failure details unlocalized until the settings page renders them', async () => {
+ const input = deps({
+ checkOtaUpdate: vi.fn(async () => {
+ throw new Error('offline');
+ }),
+ });
+
+ await expect(runManualUpdateCheck(input)).resolves.toEqual({
+ kind: 'error',
+ reason: 'ota-check',
+ detail: 'offline',
+ });
+ });
+});
+
+describe('manualUpdateCheckMessage', () => {
+ it('uses the current language for an already completed TestFlight check', async () => {
+ const outcome = { kind: 'up-to-date' } as const;
+
+ await i18n.changeLanguage('zh-CN');
+ expect(manualUpdateCheckMessage(outcome, { isTestFlightBuild: true, t: i18n.t }))
+ .toBe('当前没有可用的内容更新。新测试版本请在 TestFlight 中查看。');
+
+ await i18n.changeLanguage('en');
+ expect(manualUpdateCheckMessage(outcome, { isTestFlightBuild: true, t: i18n.t }))
+ .toBe('No content updates are available. Check TestFlight for new test builds.');
+ });
});
diff --git a/apps/mobile/src/update/manualUpdateCheck.ts b/apps/mobile/src/update/manualUpdateCheck.ts
index ac1322c6c9..c6cc0ebe11 100644
--- a/apps/mobile/src/update/manualUpdateCheck.ts
+++ b/apps/mobile/src/update/manualUpdateCheck.ts
@@ -1,4 +1,4 @@
-import { i18n } from '@/i18n';
+import type { TFunction } from 'i18next';
/** 手动整包检查结果,供统一流程决定是否继续检查 JS 热更新。 */
export type BundleUpdateCheckOutcome =
@@ -18,7 +18,7 @@ export type ManualUpdateCheckOutcome =
| { kind: 'ota-unavailable' }
| { kind: 'reloading' }
| { kind: 'busy' }
- | { kind: 'error'; message: string };
+ | { kind: 'error'; reason: 'bundle-check' | 'ota-check'; detail?: string };
/** 统一更新检查所需的外部能力,由设置页注入真实 Expo / 整包更新实现。 */
export interface ManualUpdateCheckDeps {
@@ -50,10 +50,10 @@ export async function runManualUpdateCheck({
try {
bundleOutcome = await checkBundleUpdate();
} catch {
- return { kind: 'error', message: i18n.t('settings.version.bundleCheckFailed') };
+ return { kind: 'error', reason: 'bundle-check' };
}
if (bundleOutcome === 'update-available') return { kind: 'bundle-update-available' };
- if (bundleOutcome === 'error') return { kind: 'error', message: i18n.t('settings.version.bundleCheckFailed') };
+ if (bundleOutcome === 'error') return { kind: 'error', reason: 'bundle-check' };
if (bundleOutcome === 'busy') return { kind: 'busy' };
}
@@ -70,9 +70,39 @@ export async function runManualUpdateCheck({
const detail = error instanceof Error ? error.message.trim() : String(error).trim();
return {
kind: 'error',
- message: detail
- ? i18n.t('settings.version.checkFailedDetail', { detail })
- : i18n.t('settings.version.checkFailed'),
+ reason: 'ota-check',
+ ...(detail ? { detail } : {}),
};
}
}
+
+/**
+ * 在设置页渲染更新检查结果。结果本身只保存语义和动态详情,避免语言切换后保留旧语言的字符串。
+ */
+export function manualUpdateCheckMessage(
+ outcome: ManualUpdateCheckOutcome,
+ options: { isTestFlightBuild: boolean; t: TFunction },
+): string | null {
+ const { isTestFlightBuild, t } = options;
+ switch (outcome.kind) {
+ case 'bundle-update-available':
+ return t('settings.version.bundleUpdateFound');
+ case 'up-to-date':
+ return t(isTestFlightBuild
+ ? 'settings.version.testFlightNoContentUpdate'
+ : 'settings.version.upToDate');
+ case 'ota-unavailable':
+ return t(isTestFlightBuild
+ ? 'settings.version.testFlightContentUpdateUnavailable'
+ : 'settings.version.bundleUpToDateNoOta');
+ case 'reloading':
+ return t('settings.version.downloadedRestarting');
+ case 'error':
+ if (outcome.reason === 'bundle-check') return t('settings.version.bundleCheckFailed');
+ return outcome.detail
+ ? t('settings.version.checkFailedDetail', { detail: outcome.detail })
+ : t('settings.version.checkFailed');
+ case 'busy':
+ return null;
+ }
+}