diff --git a/src/locales/en.json b/src/locales/en.json index 0cf7bdd985..a381af8f7a 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.share.botUnavailable": "That bot is no longer available. Share again after opening the right bot.", "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..fc6ca1aa7e 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(); @@ -87,7 +88,7 @@ describe("a share from another app", () => { expect(shortcutAction({ kind: "share", text: " https://example.com ", images: [] })).toEqual({ kind: "share", text: "https://example.com", images: [] }); expect(shortcutAction({ kind: "share", place: "env-home", botId: "kiln", text: "", images: [PIXEL, "not base64!", 3] })) .toEqual({ kind: "share", place: "env-home", botId: "kiln", text: "", images: [PIXEL] }); - expect(shortcutAction({ kind: "share", place: "env-home", botId: "../x", text: "hi", images: [] })).toEqual({ kind: "share", text: "hi", images: [] }); + expect(shortcutAction({ kind: "share", place: "env-home", botId: "../x", text: "hi", images: [] })).toBeNull(); expect(shortcutAction({ kind: "share", text: " ", images: [] })).toBeNull(); }); @@ -117,6 +118,17 @@ describe("a share from another app", () => { expect(app.switchTo).toHaveBeenCalledWith("cloud"); expect(app.openChat).not.toHaveBeenCalled(); }); + + it("does not open another bot when the picked bot is no longer in this place", () => { + const alert = vi.fn(async () => "confirm" as const); + installSystemDialogs({ alert, form: vi.fn() }); + const app = mount({ bots: [kiln] }); + act(() => fire({ kind: "share", place: "env-home", botId: "removed", text: "private note", images: [] })); + expect(app.openChat).not.toHaveBeenCalled(); + expect(app.dispatch).toHaveBeenCalledWith({ type: "error", message: "That bot is no longer available. Share again after opening the right bot." }); + expect(alert).toHaveBeenCalledWith({ title: "That bot is no longer available. Share again after opening the right bot.", confirmLabel: "OK", singleAction: true, tone: "neutral" }); + expect(getDraft(localStorage, "bot:kiln:t-kiln")).toBe(""); + }); }); describe("an asked call", () => { diff --git a/src/mobile/shortcuts.ts b/src/mobile/shortcuts.ts index 888e478020..8b81b8ab77 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"; @@ -38,6 +40,7 @@ function shareAction(raw: Record): ShortcutAction | null { if (!words && !pictures.length) return null; const at = typeof place === "string" && place && place.length <= 300 ? place : undefined; const bot = typeof botId === "string" && ID.test(botId) ? botId : undefined; + if ((place !== undefined || botId !== undefined) && (!at || !bot)) return null; return { kind: "share", ...(at && bot ? { place: at, botId: bot } : {}), text: words, images: pictures }; } @@ -129,8 +132,15 @@ export function useShortcuts({ platform, state, dispatch, place, connections, lo if (elsewhere) { hold(action); void switchTo(elsewhere.id); } return; } - const bot = now.bots.find((one) => one.id === action.botId) ?? (action.kind === "share" ? shareBot(now.bots) : undefined); - if (!bot) return; + const bot = now.bots.find((one) => one.id === action.botId); + if (!bot) { + if (action.kind === "share") { + const message = t("mobile.share.botUnavailable"); + dispatch({ type: "error", message }); + void systemDialogs()?.alert({ title: message, confirmLabel: t("common.ok"), singleAction: true, tone: "neutral" }); + } + return; + } openChat(bot.id); if (action.kind === "share") placeShare(bot, action); else if (action.kind === "send") dispatch({ type: "send", botId: bot.id, text: action.text, threadId: bot.threadId });