Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 14 additions & 2 deletions src/mobile/shortcuts.dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -23,7 +24,7 @@ beforeEach(() => {
setBots = vi.fn<ShortcutsBridge["setBots"]>(); forget = vi.fn<ShortcutsBridge["forget"]>();
bridge = { setBots, forget, onAction: (listener) => { fire = listener; return () => {}; } };
});
afterEach(() => { act(() => root.unmount()); });
afterEach(() => { act(() => root.unmount()); installSystemDialogs(null); });

function mount(state: Partial<AppState>, connections = linked) {
const dispatch = vi.fn(), switchTo = vi.fn(async () => {}), openChat = vi.fn();
Expand Down Expand Up @@ -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();
});

Expand Down Expand Up @@ -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", () => {
Expand Down
14 changes: 12 additions & 2 deletions src/mobile/shortcuts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -38,6 +40,7 @@ function shareAction(raw: Record<string, unknown>): 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 };
}

Expand Down Expand Up @@ -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 });
Expand Down
Loading