Skip to content

Commit 045bf79

Browse files
authored
From the Home Screen, an app's sign-in opens in OpenBot itself (#2902)
* From the Home Screen, an app's sign-in opens in OpenBot itself Opened from an iPhone's or iPad's Home Screen, a card's Connect claimed a new tab on the click, which a Home Screen web app doesn't have: iOS showed a blank page over the app, and the sign-in only went on in Safari, whose cookies and storage the web app doesn't share. A refused press (Gusto, which this OpenBot can't connect) left that blank page over the card saying why. There, a connect press now claims no tab. OpenBot's step before a Composio sign-in is the dialog Connections already uses, this window goes to the sign-in, and the vendor brings it back to the app's page, which goes back to where Connect was pressed once the account is connected. A press with nothing to sign in to opens nothing. In a browser tab, embedded, and in OpenBot Desktop's window, nothing changes. Test: home-screen-connect-browser, in Chromium and WebKit at a phone's width, red on main. * Home Screen connect test: style the page before pressing, and let the step go on by itself
1 parent 51deec9 commit 045bf79

8 files changed

Lines changed: 742 additions & 24 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,7 @@ jobs:
734734
app/tests/hand-back-row-layout.test.tsx
735735
app/tests/home-holds-still.test.tsx
736736
app/tests/home-no-answer.test.tsx
737+
app/tests/home-screen-connect-browser.test.tsx
737738
app/tests/long-names-layout.test.tsx
738739
app/tests/long-turn-reopen.test.tsx
739740
app/tests/member-rows-long-names.test.tsx

‎app/src/components/plugins/connect-in-new-tab.ts‎

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@ import { ConsentHandoffDialog } from "@/components/plugins/consent-handoff";
44
import { desktopShell } from "@/lib/desktop/shell";
55
import { embedConfig } from "@/lib/embed/config";
66
import { failureOf } from "@/lib/load-failure";
7-
import { holdConsentTab, releaseConsentTab } from "@/lib/plugins/consent-tab";
8-
import { markConnectReturn } from "@/lib/plugins/connect-return";
7+
import {
8+
forgetConnectReturn,
9+
markConnectReturn,
10+
} from "@/lib/plugins/connect-return";
911
import {
1012
brokeredConsentFor,
1113
type ConsentHandoff,
1214
consentHandoffAddress,
1315
storeConsentHandoff,
1416
} from "@/lib/plugins/consent-handoff";
17+
import {
18+
holdConsentTab,
19+
releaseConsentTab,
20+
signsInInThisWindow,
21+
} from "@/lib/plugins/consent-tab";
1522
import { finishInBackground } from "@/lib/plugins/finish-on-return";
1623
import {
1724
connectAccountMutationOptions,
@@ -155,6 +162,8 @@ export function useConnectInNewTab(
155162
* guarantee by a different route.
156163
*/
157164
const claimTab = (): Window | null => {
165+
// A Home Screen web app has no tabs: its sign-in goes in this window (`signsInInThisWindow`).
166+
if (signsInInThisWindow()) return null;
158167
const tab = window.open("", "_blank");
159168
if (tab) tab.opener = null;
160169
// Embedded, the press then asks for the page that closes this tab (lib/plugins/consent-tab.ts).
@@ -183,10 +192,25 @@ export function useConnectInNewTab(
183192
}
184193
const tab = claimTab();
185194
handle.current = tab;
186-
markConnectReturn(tab, serverId, here());
195+
markConnectReturn(noteTab(tab), serverId, here());
187196
void during(ask(serverId, tab));
188197
};
189198

199+
/*
200+
* WHERE THE NOTE GOES: the claimed tab, or, in a Home Screen web app, this window, which is where
201+
* the sign-in will come back to (`signsInInThisWindow`).
202+
*/
203+
const noteTab = (tab: Window | null) =>
204+
signsInInThisWindow() ? window : tab;
205+
206+
/** The press ended with nothing to sign in to: the claimed tab goes, and so does this window's note. */
207+
const endUnused = (tab: Window | null) => {
208+
tab?.close();
209+
releaseConsentTab(tab);
210+
handle.current = null;
211+
if (signsInInThisWindow()) forgetConnectReturn();
212+
};
213+
190214
/**
191215
* The same, for a press whose `first` is what names the app: adding an app from Browse apps
192216
* answers the id of the row it made, and that row is what the sign-in is for.
@@ -200,18 +224,14 @@ export function useConnectInNewTab(
200224
const tab = claimTab();
201225
handle.current = tab;
202226
const from = here();
203-
const unused = () => {
204-
tab?.close();
205-
releaseConsentTab(tab);
206-
handle.current = null;
207-
};
227+
const unused = () => endUnused(tab);
208228
return during(
209229
first().then((serverId) => {
210230
if (serverId === null) {
211231
unused();
212232
return;
213233
}
214-
markConnectReturn(tab, serverId, from);
234+
markConnectReturn(noteTab(tab), serverId, from);
215235
return ask(serverId, tab);
216236
}, unused),
217237
);
@@ -228,9 +248,7 @@ export function useConnectInNewTab(
228248
try {
229249
start = await connect.mutateAsync(serverId);
230250
} catch {
231-
tab?.close();
232-
releaseConsentTab(tab);
233-
handle.current = null;
251+
endUnused(tab);
234252
return;
235253
}
236254
/*
@@ -241,9 +259,7 @@ export function useConnectInNewTab(
241259
* against it.
242260
*/
243261
if (start !== null && "connected" in start) {
244-
tab?.close();
245-
releaseConsentTab(tab);
246-
handle.current = null;
262+
endUnused(tab);
247263
void invalidateAppConnection(queryClient);
248264
return;
249265
}
@@ -253,9 +269,7 @@ export function useConnectInNewTab(
253269
* that has the form, beats a button that silently does nothing.
254270
*/
255271
if (start === null) {
256-
tab?.close();
257-
releaseConsentTab(tab);
258-
handle.current = null;
272+
endUnused(tab);
259273
setNeedsKey(true);
260274
return;
261275
}
@@ -278,8 +292,11 @@ export function useConnectInNewTab(
278292
* browser, and the browser has none of this window's storage, so the step's own page
279293
* (`/connecting`) would open nowhere. The stand-in is held until the countdown ends or Continue
280294
* now is pressed, and then pointed at the sign-in exactly as it was before the step.
295+
*
296+
* The same in a Home Screen web app, which has no tab for the page: the dialog, then this window
297+
* goes to the sign-in, as Connections' own Connect does.
281298
*/
282-
if (consent && desktopShell()) {
299+
if (consent && (desktopShell() || signsInInThisWindow())) {
283300
held.current = tab;
284301
setHandoff({ ...consent, url: authorizationUrl });
285302
return;
@@ -295,6 +312,14 @@ export function useConnectInNewTab(
295312

296313
/** Send the claimed tab to `destination`, and watch for the person to come back from it. */
297314
const leaveFor = (tab: Window | null, destination: string) => {
315+
/*
316+
* A Home Screen web app goes there itself, and comes back to the app's page, which goes back to
317+
* where Connect was pressed once the account is connected (lib/plugins/connect-return.ts).
318+
*/
319+
if (signsInInThisWindow()) {
320+
window.location.assign(destination);
321+
return;
322+
}
298323
releaseConsentTab(tab);
299324
setWaiting(true);
300325
if (tab) tab.location.replace(destination);
@@ -311,10 +336,8 @@ export function useConnectInNewTab(
311336
// Cancel opens nothing: the held tab goes, and nothing is left waiting for it.
312337
const cancelHandoff = () => {
313338
setHandoff(null);
314-
held.current?.close();
315-
releaseConsentTab(held.current);
339+
endUnused(held.current);
316340
held.current = null;
317-
handle.current = null;
318341
};
319342
const consentStep = createElement(ConsentHandoffDialog, {
320343
handoff,

‎app/src/lib/plugins/confirm-on-return.ts‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { QueryClient } from "@tanstack/react-query";
22
import { appSetupQueryOptions, appsQueryOptions } from "@/lib/apps/queries";
33
import { desktopShell } from "@/lib/desktop/shell";
44
import { embedConfig } from "@/lib/embed/config";
5+
import { signsInInThisWindow } from "./consent-tab";
56
import { fromDirectory } from "./provider-vocabulary";
67
import { pluginKeys } from "./queries";
78

@@ -46,7 +47,13 @@ export function confirmOnReturn(
4647
serverId: string,
4748
confirm: () => Promise<unknown>,
4849
): void {
49-
if (!desktopShell() && embedConfig() === undefined) return;
50+
/*
51+
* A Home Screen web app's sign-in goes in its own window and comes back to the page that confirms
52+
* it (`signsInInThisWindow`). Should iOS show the sign-in over the web app instead, the web app has
53+
* the focus again once the person is done, and confirms it then, as here.
54+
*/
55+
if (!desktopShell() && embedConfig() === undefined && !signsInInThisWindow())
56+
return;
5057
pending.set(serverId, { confirm, until: Date.now() + EMBEDDED_WAIT_MS });
5158
client = queryClient;
5259
listen();

‎app/src/lib/plugins/connect-return.ts‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { useQuery } from "@tanstack/react-query";
22
import { useRouter } from "@tanstack/react-router";
33
import { useEffect, useState } from "react";
4+
import { signsInInThisWindow } from "./consent-tab";
45
import { connectionsQueryOptions, personalConnections } from "./queries";
56

67
/**
@@ -19,6 +20,10 @@ import { connectionsQueryOptions, personalConnections } from "./queries";
1920
* listed as connected, the tab closes, which puts the person back on the tab they pressed Connect
2021
* in. Where the browser will not let it close, it goes to that page instead.
2122
*
23+
* A HOME SCREEN WEB APP HAS NO NEW TAB: its sign-in goes in its one window (`signsInInThisWindow`),
24+
* so the note is written into that window's own session storage instead, which is still there when
25+
* the vendor brings it back, and the app's page goes back to where Connect was pressed.
26+
*
2227
* ONLY THE PAGE THE SIGN-IN LANDS ON acts on it, and only once: the note is read and dropped on
2328
* arrival, so somebody who stays in that tab and moves on is never taken anywhere later. A sign-in
2429
* that failed comes back saying so (`?status=failed`), and then the person stays where the failure
@@ -46,6 +51,20 @@ export function markConnectReturn(
4651
}
4752
}
4853

54+
/**
55+
* Drop the note this window wrote for itself, for a press that ended with no sign-in to come back
56+
* from (refused, a key form, or connected at once). Only a Home Screen web app writes one for its own
57+
* window (`signsInInThisWindow`), and left there it would send a later visit to the app's page back
58+
* to that conversation.
59+
*/
60+
export function forgetConnectReturn(): void {
61+
try {
62+
sessionStorage.removeItem(KEY);
63+
} catch {
64+
// No storage, so no note.
65+
}
66+
}
67+
4968
/** The note this tab was given, dropped as it is read so it can be acted on once. */
5069
function takeNote(): Note | null {
5170
try {
@@ -99,7 +118,8 @@ export function useBackAfterConnect(app: string): void {
99118
const path = note?.path;
100119
useEffect(() => {
101120
if (!connected || !path) return;
102-
window.close();
121+
// A Home Screen web app's sign-in ran in its one window, which is never closed: it goes back.
122+
if (!signsInInThisWindow()) window.close();
103123
if (!window.closed) router.history.replace(path);
104124
}, [connected, path, router]);
105125
}

‎app/src/lib/plugins/consent-tab.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ import type { QueryClient } from "@tanstack/react-query";
22
import { useEffect, useMemo } from "react";
33
import { appSetupQueryOptions } from "@/lib/apps/queries";
44
import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries";
5+
import { desktopShell } from "@/lib/desktop/shell";
56
import { embedConfig } from "@/lib/embed/config";
67
import { pluginKeys } from "@/lib/plugins/queries";
8+
import { onAppleMobile, openedFromHomeScreen } from "@/lib/web-push/browser";
79

810
/**
911
* WHERE AN APP'S SIGN-IN OPENS, EMBEDDED IN ANOTHER APP'S PAGE.
@@ -177,6 +179,31 @@ export async function connectsInWindow(
177179
);
178180
}
179181

182+
/**
183+
* OPENED FROM AN IPHONE'S OR IPAD'S HOME SCREEN, AN APP'S SIGN-IN GOES IN THIS WINDOW.
184+
*
185+
* A web app on the Home Screen has no tabs. A tab claimed on the press (`useConnectInNewTab`) opened
186+
* as a sheet over the app showing a blank page: the sign-in showed only once the person pressed Open
187+
* in Safari, and then finished in Safari, whose cookies and storage the web app doesn't share. A press
188+
* that was refused (an app that can't be connected here) left that blank sheet over the card saying
189+
* why, and Open in Safari had nothing to open.
190+
*
191+
* So no tab is claimed there. This window goes to the sign-in, as Connections does in any browser, and
192+
* the vendor brings the person back to OpenBot in this same window, still signed in, where the app's
193+
* page goes back to where Connect was pressed once the account is connected
194+
* (lib/plugins/connect-return.ts).
195+
*
196+
* Embedded in another app's page, or in OpenBot Desktop's window, each keeps its own way.
197+
*/
198+
export function signsInInThisWindow(): boolean {
199+
return (
200+
embedConfig() === undefined &&
201+
!desktopShell() &&
202+
onAppleMobile() &&
203+
openedFromHomeScreen()
204+
);
205+
}
206+
180207
/** Read every plugin list again, as a screen does once the person is back from a sign-in. */
181208
export function readConnectionsAgain(queryClient: QueryClient): Promise<void> {
182209
return queryClient.invalidateQueries({ queryKey: pluginKeys.all });

0 commit comments

Comments
 (0)