From 78d3c6ceaa771f02979cc1a67c190c0ba1635514 Mon Sep 17 00:00:00 2001 From: Pranav Gawai Date: Sun, 29 Nov 2026 14:12:19 +0530 Subject: [PATCH] fix(mobile): stop switching when share handoff fails --- src/locales/en.json | 1 + src/mobile/shortcuts.dom.test.ts | 42 +++++++++++++++++++++++++++++++- src/mobile/shortcuts.ts | 21 ++++++++++++---- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/locales/en.json b/src/locales/en.json index 0cf7bdd985..8ad9e1ffc5 100644 --- a/src/locales/en.json +++ b/src/locales/en.json @@ -8467,6 +8467,7 @@ "mobile.select.hint": "Touch and hold to select, then drag the handles", "mobile.select.copyAll": "Copy all", "mobile.message.share": "Share", + "mobile.shortcut.handoffFailed": "Couldn't carry that action to the other place. Open the place first, then try again.", "mobile.link.open": "Open link", "mobile.link.copy": "Copy link", "mobile.link.share": "Share link", diff --git a/src/mobile/shortcuts.dom.test.ts b/src/mobile/shortcuts.dom.test.ts index 1cfb7c5086..3695ed756d 100644 --- a/src/mobile/shortcuts.dom.test.ts +++ b/src/mobile/shortcuts.dom.test.ts @@ -7,6 +7,7 @@ import { initialState, type AppState, type Bot } from "@/state/store"; import type { MobileConnectionRecord } from "../../shared/mobile-link"; import type { MobilePlatform } from "./platform"; import { getDraft } from "@/lib/drafts"; +import { installSystemDialogs } from "@/lib/system-dialog"; import { useOfferedComposerFiles } from "@/lib/composer-offers"; import { shareBot, shortcutAction, useShortcuts, type ShortcutAction, type ShortcutsBridge } from "./shortcuts"; @@ -23,7 +24,7 @@ beforeEach(() => { setBots = vi.fn(); forget = vi.fn(); bridge = { setBots, forget, onAction: (listener) => { fire = listener; return () => {}; } }; }); -afterEach(() => { act(() => root.unmount()); }); +afterEach(() => { act(() => root.unmount()); installSystemDialogs(null); }); function mount(state: Partial, connections = linked) { const dispatch = vi.fn(), switchTo = vi.fn(async () => {}), openChat = vi.fn(); @@ -117,6 +118,45 @@ describe("a share from another app", () => { expect(app.switchTo).toHaveBeenCalledWith("cloud"); expect(app.openChat).not.toHaveBeenCalled(); }); + + it("keeps this place open and explains when an image-only share cannot be retained", () => { + const alert = vi.fn(async () => "confirm" as const); + installSystemDialogs({ alert, form: vi.fn() }); + const app = mount({}); + vi.stubGlobal("sessionStorage", { + getItem: () => null, + setItem: () => { throw new DOMException("Quota exceeded", "QuotaExceededError"); }, + removeItem: () => {}, + }); + try { + act(() => fire({ kind: "share", place: "env-cloud", botId: "nova", text: "", images: [PIXEL] })); + expect(app.switchTo).not.toHaveBeenCalled(); + expect(app.dispatch).toHaveBeenCalledWith({ type: "error", message: "Couldn't carry that action to the other place. Open the place first, then try again." }); + expect(alert).toHaveBeenCalledWith({ title: "Couldn't carry that action to the other place. Open the place first, then try again.", confirmLabel: "OK", singleAction: true, tone: "neutral" }); + } finally { + vi.unstubAllGlobals(); + } + }); + + it("does not carry only the text when a shared image exceeds storage", () => { + const app = mount({}); + const saved: string[] = []; + vi.stubGlobal("sessionStorage", { + getItem: () => null, + setItem: (_key: string, value: string) => { + if (JSON.parse(value).images.length) throw new DOMException("Quota exceeded", "QuotaExceededError"); + saved.push(value); + }, + removeItem: () => {}, + }); + try { + act(() => fire({ kind: "share", place: "env-cloud", botId: "nova", text: "See this", images: [PIXEL] })); + expect(app.switchTo).not.toHaveBeenCalled(); + expect(saved).toEqual([]); + } finally { + vi.unstubAllGlobals(); + } + }); }); describe("an asked call", () => { diff --git a/src/mobile/shortcuts.ts b/src/mobile/shortcuts.ts index 888e478020..022b767d6b 100644 --- a/src/mobile/shortcuts.ts +++ b/src/mobile/shortcuts.ts @@ -2,6 +2,8 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { askForCall } from "@/lib/call"; import { offerComposerFiles } from "@/lib/composer-offers"; import { appendComposerDraft } from "@/lib/drafts"; +import { t } from "@/lib/i18n"; +import { systemDialogs } from "@/lib/system-dialog"; import type { Action, AppState } from "@/state/store"; import type { MobileConnectionRecord } from "../../shared/mobile-link"; import type { MobilePlatform } from "./platform"; @@ -126,7 +128,14 @@ export function useShortcuts({ platform, state, dispatch, place, connections, lo } if (action.place !== here) { const elsewhere = action === held ? undefined : linked.find((one) => one.environmentId === action.place); - if (elsewhere) { hold(action); void switchTo(elsewhere.id); } + if (elsewhere) { + if (hold(action)) void switchTo(elsewhere.id); + else { + const message = t("mobile.shortcut.handoffFailed"); + dispatch({ type: "error", message }); + void systemDialogs()?.alert({ title: message, confirmLabel: t("common.ok"), singleAction: true, tone: "neutral" }); + } + } return; } const bot = now.bots.find((one) => one.id === action.botId) ?? (action.kind === "share" ? shareBot(now.bots) : undefined); @@ -149,10 +158,12 @@ export function useShortcuts({ platform, state, dispatch, place, connections, lo } /** a run kept across the reload that switches places; one that still finds no place after it is dropped */ -function hold(action: ShortcutAction) { - try { sessionStorage.setItem(HELD_KEY, JSON.stringify(action)); } catch { - // pictures past the storage quota: the words still go - if (action.kind === "share" && action.text) try { sessionStorage.setItem(HELD_KEY, JSON.stringify({ ...action, images: [] })); } catch { /* storage unavailable */ } +function hold(action: ShortcutAction): boolean { + try { + sessionStorage.setItem(HELD_KEY, JSON.stringify(action)); + return true; + } catch { + return false; } }