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 .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,7 @@ jobs:
app/tests/hand-back-row-layout.test.tsx
app/tests/home-holds-still.test.tsx
app/tests/home-no-answer.test.tsx
app/tests/home-screen-connect-browser.test.tsx
app/tests/long-names-layout.test.tsx
app/tests/long-turn-reopen.test.tsx
app/tests/member-rows-long-names.test.tsx
Expand Down
67 changes: 45 additions & 22 deletions app/src/components/plugins/connect-in-new-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@ import { ConsentHandoffDialog } from "@/components/plugins/consent-handoff";
import { desktopShell } from "@/lib/desktop/shell";
import { embedConfig } from "@/lib/embed/config";
import { failureOf } from "@/lib/load-failure";
import { holdConsentTab, releaseConsentTab } from "@/lib/plugins/consent-tab";
import { markConnectReturn } from "@/lib/plugins/connect-return";
import {
forgetConnectReturn,
markConnectReturn,
} from "@/lib/plugins/connect-return";
import {
brokeredConsentFor,
type ConsentHandoff,
consentHandoffAddress,
storeConsentHandoff,
} from "@/lib/plugins/consent-handoff";
import {
holdConsentTab,
releaseConsentTab,
signsInInThisWindow,
} from "@/lib/plugins/consent-tab";
import { finishInBackground } from "@/lib/plugins/finish-on-return";
import {
connectAccountMutationOptions,
Expand Down Expand Up @@ -155,6 +162,8 @@ export function useConnectInNewTab(
* guarantee by a different route.
*/
const claimTab = (): Window | null => {
// A Home Screen web app has no tabs: its sign-in goes in this window (`signsInInThisWindow`).
if (signsInInThisWindow()) return null;
const tab = window.open("", "_blank");
if (tab) tab.opener = null;
// Embedded, the press then asks for the page that closes this tab (lib/plugins/consent-tab.ts).
Expand Down Expand Up @@ -183,10 +192,25 @@ export function useConnectInNewTab(
}
const tab = claimTab();
handle.current = tab;
markConnectReturn(tab, serverId, here());
markConnectReturn(noteTab(tab), serverId, here());
void during(ask(serverId, tab));
};

/*
* WHERE THE NOTE GOES: the claimed tab, or, in a Home Screen web app, this window, which is where
* the sign-in will come back to (`signsInInThisWindow`).
*/
const noteTab = (tab: Window | null) =>
signsInInThisWindow() ? window : tab;

/** The press ended with nothing to sign in to: the claimed tab goes, and so does this window's note. */
const endUnused = (tab: Window | null) => {
tab?.close();
releaseConsentTab(tab);
handle.current = null;
if (signsInInThisWindow()) forgetConnectReturn();
};

/**
* The same, for a press whose `first` is what names the app: adding an app from Browse apps
* answers the id of the row it made, and that row is what the sign-in is for.
Expand All @@ -200,18 +224,14 @@ export function useConnectInNewTab(
const tab = claimTab();
handle.current = tab;
const from = here();
const unused = () => {
tab?.close();
releaseConsentTab(tab);
handle.current = null;
};
const unused = () => endUnused(tab);
return during(
first().then((serverId) => {
if (serverId === null) {
unused();
return;
}
markConnectReturn(tab, serverId, from);
markConnectReturn(noteTab(tab), serverId, from);
return ask(serverId, tab);
}, unused),
);
Expand All @@ -228,9 +248,7 @@ export function useConnectInNewTab(
try {
start = await connect.mutateAsync(serverId);
} catch {
tab?.close();
releaseConsentTab(tab);
handle.current = null;
endUnused(tab);
return;
}
/*
Expand All @@ -241,9 +259,7 @@ export function useConnectInNewTab(
* against it.
*/
if (start !== null && "connected" in start) {
tab?.close();
releaseConsentTab(tab);
handle.current = null;
endUnused(tab);
void invalidateAppConnection(queryClient);
return;
}
Expand All @@ -253,9 +269,7 @@ export function useConnectInNewTab(
* that has the form, beats a button that silently does nothing.
*/
if (start === null) {
tab?.close();
releaseConsentTab(tab);
handle.current = null;
endUnused(tab);
setNeedsKey(true);
return;
}
Expand All @@ -278,8 +292,11 @@ export function useConnectInNewTab(
* browser, and the browser has none of this window's storage, so the step's own page
* (`/connecting`) would open nowhere. The stand-in is held until the countdown ends or Continue
* now is pressed, and then pointed at the sign-in exactly as it was before the step.
*
* The same in a Home Screen web app, which has no tab for the page: the dialog, then this window
* goes to the sign-in, as Connections' own Connect does.
*/
if (consent && desktopShell()) {
if (consent && (desktopShell() || signsInInThisWindow())) {
held.current = tab;
setHandoff({ ...consent, url: authorizationUrl });
return;
Expand All @@ -295,6 +312,14 @@ export function useConnectInNewTab(

/** Send the claimed tab to `destination`, and watch for the person to come back from it. */
const leaveFor = (tab: Window | null, destination: string) => {
/*
* A Home Screen web app goes there itself, and comes back to the app's page, which goes back to
* where Connect was pressed once the account is connected (lib/plugins/connect-return.ts).
*/
if (signsInInThisWindow()) {
window.location.assign(destination);
return;
}
releaseConsentTab(tab);
setWaiting(true);
if (tab) tab.location.replace(destination);
Expand All @@ -311,10 +336,8 @@ export function useConnectInNewTab(
// Cancel opens nothing: the held tab goes, and nothing is left waiting for it.
const cancelHandoff = () => {
setHandoff(null);
held.current?.close();
releaseConsentTab(held.current);
endUnused(held.current);
held.current = null;
handle.current = null;
};
const consentStep = createElement(ConsentHandoffDialog, {
handoff,
Expand Down
9 changes: 8 additions & 1 deletion app/src/lib/plugins/confirm-on-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { QueryClient } from "@tanstack/react-query";
import { appSetupQueryOptions, appsQueryOptions } from "@/lib/apps/queries";
import { desktopShell } from "@/lib/desktop/shell";
import { embedConfig } from "@/lib/embed/config";
import { signsInInThisWindow } from "./consent-tab";
import { fromDirectory } from "./provider-vocabulary";
import { pluginKeys } from "./queries";

Expand Down Expand Up @@ -46,7 +47,13 @@ export function confirmOnReturn(
serverId: string,
confirm: () => Promise<unknown>,
): void {
if (!desktopShell() && embedConfig() === undefined) return;
/*
* A Home Screen web app's sign-in goes in its own window and comes back to the page that confirms
* it (`signsInInThisWindow`). Should iOS show the sign-in over the web app instead, the web app has
* the focus again once the person is done, and confirms it then, as here.
*/
if (!desktopShell() && embedConfig() === undefined && !signsInInThisWindow())
return;
pending.set(serverId, { confirm, until: Date.now() + EMBEDDED_WAIT_MS });
client = queryClient;
listen();
Expand Down
22 changes: 21 additions & 1 deletion app/src/lib/plugins/connect-return.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } from "@tanstack/react-query";
import { useRouter } from "@tanstack/react-router";
import { useEffect, useState } from "react";
import { signsInInThisWindow } from "./consent-tab";
import { connectionsQueryOptions, personalConnections } from "./queries";

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

/**
* Drop the note this window wrote for itself, for a press that ended with no sign-in to come back
* from (refused, a key form, or connected at once). Only a Home Screen web app writes one for its own
* window (`signsInInThisWindow`), and left there it would send a later visit to the app's page back
* to that conversation.
*/
export function forgetConnectReturn(): void {
try {
sessionStorage.removeItem(KEY);
} catch {
// No storage, so no note.
}
}

/** The note this tab was given, dropped as it is read so it can be acted on once. */
function takeNote(): Note | null {
try {
Expand Down Expand Up @@ -99,7 +118,8 @@ export function useBackAfterConnect(app: string): void {
const path = note?.path;
useEffect(() => {
if (!connected || !path) return;
window.close();
// A Home Screen web app's sign-in ran in its one window, which is never closed: it goes back.
if (!signsInInThisWindow()) window.close();
if (!window.closed) router.history.replace(path);
}, [connected, path, router]);
}
27 changes: 27 additions & 0 deletions app/src/lib/plugins/consent-tab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import type { QueryClient } from "@tanstack/react-query";
import { useEffect, useMemo } from "react";
import { appSetupQueryOptions } from "@/lib/apps/queries";
import { deploymentCapabilitiesQueryOptions } from "@/lib/deployment/queries";
import { desktopShell } from "@/lib/desktop/shell";
import { embedConfig } from "@/lib/embed/config";
import { pluginKeys } from "@/lib/plugins/queries";
import { onAppleMobile, openedFromHomeScreen } from "@/lib/web-push/browser";

/**
* WHERE AN APP'S SIGN-IN OPENS, EMBEDDED IN ANOTHER APP'S PAGE.
Expand Down Expand Up @@ -177,6 +179,31 @@ export async function connectsInWindow(
);
}

/**
* OPENED FROM AN IPHONE'S OR IPAD'S HOME SCREEN, AN APP'S SIGN-IN GOES IN THIS WINDOW.
*
* A web app on the Home Screen has no tabs. A tab claimed on the press (`useConnectInNewTab`) opened
* as a sheet over the app showing a blank page: the sign-in showed only once the person pressed Open
* in Safari, and then finished in Safari, whose cookies and storage the web app doesn't share. A press
* that was refused (an app that can't be connected here) left that blank sheet over the card saying
* why, and Open in Safari had nothing to open.
*
* So no tab is claimed there. This window goes to the sign-in, as Connections does in any browser, and
* the vendor brings the person back to OpenBot in this same window, still signed in, where the app's
* page goes back to where Connect was pressed once the account is connected
* (lib/plugins/connect-return.ts).
*
* Embedded in another app's page, or in OpenBot Desktop's window, each keeps its own way.
*/
export function signsInInThisWindow(): boolean {
return (
embedConfig() === undefined &&
!desktopShell() &&
onAppleMobile() &&
openedFromHomeScreen()
);
}

/** Read every plugin list again, as a screen does once the person is back from a sign-in. */
export function readConnectionsAgain(queryClient: QueryClient): Promise<void> {
return queryClient.invalidateQueries({ queryKey: pluginKeys.all });
Expand Down
Loading
Loading