Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(mobile): prevent repeated iOS file link action menus (#5561)
DavidShenXDee026b86c2de
diff --git a/apps/mobile/src/__tests__/showActionMenu.test.ts b/apps/mobile/src/__tests__/showActionMenu.test.tsnew file mode 100644index 0000000000..b1d45ab3ff--- /dev/null+++ b/apps/mobile/src/__tests__/showActionMenu.test.ts@@ -0,0 +1,129 @@+import { beforeEach, describe, expect, it, vi } from "vitest";+import { showActionMenu } from "@/platform/chrome/showActionMenu";++const native = vi.hoisted(() => ({+ os: "ios",+ show: vi.fn(),+ fallback: vi.fn(),+}));++vi.mock("react-native", () => ({+ Platform: { get OS() { return native.os; } },+ ActionSheetIOS: { showActionSheetWithOptions: native.fallback },+ Alert: { alert: vi.fn() },+}));+vi.mock("xdt-ios-action-sheet", () => ({+ iosBottomActionSheetAvailable: true,+ showIosBottomActionSheet: native.show,+}));++const request = {+ title: "preview.png",+ cancelLabel: "取消",+ items: [{ key: "open", label: "快速预览" }],+} as const;++function deferred<T>() {+ let resolve!: (value: T) => void;+ let reject!: (reason: Error) => void;+ const promise = new Promise<T>((res, rej) => { resolve = res; reject = rej; });+ return { promise, resolve, reject };+}++beforeEach(() => {+ native.os = "ios";+ native.show.mockReset();+ native.fallback.mockReset();+});++describe("iOS action menu presentation", () => {+ it("keeps one menu for repeated long-press callbacks and executes the action once", async () => {+ // The native promise settles after dismissal, not when an item starts closing it.+ const dismissal = deferred<number>();+ native.show.mockReturnValue(dismissal.promise);+ const action = vi.fn();+ const open = () => showActionMenu(request).then((result) => {+ if (result.kind === "action") action(result.key);+ return result;+ });+ const first = open();+ const repeated = [open(), open(), open()];+ // Let all requests finish even on the broken implementation (no hanging test).+ dismissal.resolve(0);+ const results = await Promise.all([first, ...repeated]);++ expect(native.show).toHaveBeenCalledTimes(1);+ expect(results).toEqual([+ { kind: "action", key: "open" },+ { kind: "cancel" }, { kind: "cancel" }, { kind: "cancel" },+ ]);+ expect(action).toHaveBeenCalledExactlyOnceWith("open");+ });++ it("does not replace an open menu with another target or replay ignored requests later", async () => {+ const dismissal = deferred<number>();+ native.show.mockReturnValueOnce(dismissal.promise).mockResolvedValue(0);+ const first = showActionMenu(request);+ const second = showActionMenu({ ...request, title: "another.png" });+ dismissal.resolve(1);+ expect(await first).toEqual({ kind: "cancel" });+ expect(await second).toEqual({ kind: "cancel" });+ expect(native.show).toHaveBeenCalledTimes(1);++ expect(await showActionMenu({ ...request, title: "next.png" }))+ .toEqual({ kind: "action", key: "open" });+ expect(native.show).toHaveBeenCalledTimes(2);+ expect(native.show.mock.calls[1][0].title).toBe("next.png");+ });++ it.each([0, 1, -1])("allows reopening after action/cancel/dismiss (%s)", async (index) => {+ native.show.mockResolvedValueOnce(index).mockResolvedValue(0);+ await showActionMenu(request);+ expect(await showActionMenu(request)).toEqual({ kind: "action", key: "open" });+ expect(native.show).toHaveBeenCalledTimes(2);+ });++ it("releases the menu after an asynchronous presentation failure", async () => {+ const presentation = deferred<number>();+ native.show.mockReturnValueOnce(presentation.promise).mockResolvedValue(0);+ const first = showActionMenu(request);+ const failure = expect(first).rejects.toThrow("No presenter");+ presentation.reject(new Error("No presenter"));+ await failure;+ expect(await showActionMenu(request)).toEqual({ kind: "action", key: "open" });+ });++ it("releases the menu when the native bridge throws synchronously", async () => {+ native.show.mockImplementationOnce(() => { throw new Error("Bridge unavailable"); })+ .mockResolvedValue(0);+ await expect(async () => showActionMenu(request)).rejects.toThrow("Bridge unavailable");+ expect(await showActionMenu(request)).toEqual({ kind: "action", key: "open" });+ });++ it("also suppresses duplicates in the ActionSheetIOS fallback", async () => {+ native.show.mockReturnValue(null);+ const callbacks: Array<(index: number) => void> = [];+ native.fallback.mockImplementation((_options, callback) => { callbacks.push(callback); });+ const first = showActionMenu(request);+ const repeated = showActionMenu(request);+ callbacks.forEach((callback) => callback(0));+ const results = await Promise.all([first, repeated]);+ expect(native.fallback).toHaveBeenCalledTimes(1);+ expect(results).toEqual([{ kind: "action", key: "open" }, { kind: "cancel" }]);++ const next = showActionMenu(request);+ callbacks.at(-1)!(1);+ expect(await next).toEqual({ kind: "cancel" });+ expect(native.fallback).toHaveBeenCalledTimes(2);+ });++ it("leaves Android on its existing in-page menu path", async () => {+ native.os = "android";+ expect(await showActionMenu(request)).toEqual({ kind: "cancel" });+ expect(native.show).not.toHaveBeenCalled();+ expect(native.fallback).not.toHaveBeenCalled();+ native.os = "ios";+ native.show.mockResolvedValue(0);+ expect(await showActionMenu(request)).toEqual({ kind: "action", key: "open" });+ });+});diff --git a/apps/mobile/src/platform/chrome/showActionMenu.ts b/apps/mobile/src/platform/chrome/showActionMenu.tsindex 25f4a59efb..c95ed41733 100644--- a/apps/mobile/src/platform/chrome/showActionMenu.ts+++ b/apps/mobile/src/platform/chrome/showActionMenu.ts@@ -11,6 +11,10 @@ import {type IosActionSheetSpec,} from "@/platform/chrome/actionMenuModel";+// 所有入口共用同一个原生 presenter。菜单关闭前忽略重复请求,也不把动作结果+// 共享给重复调用方,否则一次选择会执行多次预览/分享等副作用。+let actionMenuPresented = false;+/** iOS 无附着点的纯动作菜单走系统路径;其它端继续自绘。 */export function usesSystemActionMenu(): boolean {return Platform.OS === "ios";@@ -59,16 +63,21 @@ function presentIosActionSheet(spec: IosActionSheetSpec): Promise<number> {* 当前包尚未编进原生模块时回退 ActionSheetIOS。* 从用户手势回调里调用,不要从 useEffect 里调,避免 Strict Mode 弹两次。*/-export function showActionMenu<K extends string>(+export async function showActionMenu<K extends string>(request: ChromeActionMenuRequest<K>,): Promise<ChromeActionMenuResult<K>> {- if (Platform.OS !== "ios") {- return Promise.resolve({ kind: "cancel" });+ if (Platform.OS !== "ios" || actionMenuPresented) {+ return { kind: "cancel" };+ }+ actionMenuPresented = true;+ try {+ const spec = buildIosActionSheetSpec(request);+ // 原生 Sheet 的 promise 在关闭动画完成后才结算;异常也必须释放占用。+ const buttonIndex = await presentIosActionSheet(spec);+ return resolveIosActionSheetResult(spec, buttonIndex);+ } finally {+ actionMenuPresented = false;}- const spec = buildIosActionSheetSpec(request);- return presentIosActionSheet(spec).then((buttonIndex) =>- resolveIosActionSheetResult(spec, buttonIndex),- );}/** 确认框两端都用系统 Alert,这里只做统一入口。 */diff --git a/dependency-patches/[email protected] b/dependency-patches/[email protected]index ebe7fbaa06..3127eaddbc 100644--- a/dependency-patches/[email protected]+++ b/dependency-patches/[email protected]@@ -238,7 +238,18 @@ index d0c31ed20b4865679ec1397543fc74056e47b978..51594cc1c7587feb5dbc3ce00e745e65}// MARK: - UIGestureRecognizerDelegate-@@ -328,6 +422,230 @@ - (void)textViewDidChangeSelection:(UITextView *)textView+@@ -297,4 +391,10 @@+ - (void)handleLongPressIfNecessary:(UILongPressGestureRecognizer*)sender+ {++ // Long presses also report movement and release. Dispatch the action only++ // once, when recognition begins, without changing native text selection.++ if (sender.state != UIGestureRecognizerStateBegan) {++ return;++ }+++ const auto location = [self getLocationOfPress:sender];+ const auto child = [self getTouchChild:location];+@@ -328,6 +428,230 @@ - (void)textViewDidChangeSelection:(UITextView *)textViewstatic_cast<int>(selectedRange.location),static_cast<int>(selectedRange.location + selectedRange.length),});diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 0e4634b0fe..07d6f21ac6 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -32,7 +32,7 @@ patchedDependencies:hash: 272e3ded46880ee2f9eaff39327b8465de6df4072271de03caf73950e6e57822path: dependency-patches/harmonyos-sans-sc-webfont-splitted.patch- hash: d6eb1ea155bf2245746f79c5d2cc7c5abe165c34e55c464e04d641a78888cda0+ hash: 89f3c768724000c3f1284fa9381db2d983748684dddffa6f271313f415a27057path: dependency-patches/[email protected]hash: c4e335ad426e4028c24ae12915857a01c4eeb7e6e2ea00efdc07dbb7bffa56f0@@ -772,7 +772,7 @@ importers:version: 15.15.4([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])react-native-uitextview:specifier: 2.2.0- version: 2.2.0(patch_hash=d6eb1ea155bf2245746f79c5d2cc7c5abe165c34e55c464e04d641a78888cda0)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])+ version: 2.2.0(patch_hash=89f3c768724000c3f1284fa9381db2d983748684dddffa6f271313f415a27057)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected])react-native-web:specifier: ^0.21.2@@ -21156,7 +21156,7 @@ snapshots:react-native: 0.86.3(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected])warn-once: 0.1.1- [email protected](patch_hash=d6eb1ea155bf2245746f79c5d2cc7c5abe165c34e55c464e04d641a78888cda0)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected]):+ [email protected](patch_hash=89f3c768724000c3f1284fa9381db2d983748684dddffa6f271313f415a27057)([email protected](@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected]))([email protected]):dependencies:react: 19.2.3react-native: 0.86.3(@babel/[email protected])(@react-native/[email protected](@babel/[email protected]))(@types/[email protected])([email protected])