|
| 1 | +import { useQuery } from "@tanstack/react-query"; |
| 2 | +import { useRouter } from "@tanstack/react-router"; |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { connectionsQueryOptions, personalConnections } from "./queries"; |
| 5 | + |
| 6 | +/** |
| 7 | + * BACK TO WHERE CONNECT WAS PRESSED, once the sign-in a new tab opened is done. |
| 8 | + * |
| 9 | + * A Bot's card in a conversation, and an app's row on a Bot's page, start a sign-in in a new tab |
| 10 | + * (`useConnectInNewTab`), so the person never leaves what they were in the middle of. The sign-in |
| 11 | + * comes back to the app's page under Connections, because that is where the connect route returns |
| 12 | + * a browser to. Reported: after connecting Gmail from a conversation, the person was left on |
| 13 | + * Gmail's page in the new tab, when what they expected was the Bot they had been talking to, which |
| 14 | + * by then was already carrying on in the first tab. |
| 15 | + * |
| 16 | + * So the tab is told, before it leaves, where the press was made: written into the new tab's own |
| 17 | + * session storage while it is still OpenBot's blank page, which the vendor's pages cannot touch and |
| 18 | + * which is still there when the tab comes back to OpenBot. On the app's page, once the account is |
| 19 | + * listed as connected, the tab closes, which puts the person back on the tab they pressed Connect |
| 20 | + * in. Where the browser will not let it close, it goes to that page instead. |
| 21 | + * |
| 22 | + * ONLY THE PAGE THE SIGN-IN LANDS ON acts on it, and only once: the note is read and dropped on |
| 23 | + * arrival, so somebody who stays in that tab and moves on is never taken anywhere later. A sign-in |
| 24 | + * that failed comes back saying so (`?status=failed`), and then the person stays where the failure |
| 25 | + * is said. Storage refused (a private window, a policy) means no note: the page works as before. |
| 26 | + */ |
| 27 | + |
| 28 | +const KEY = "openbot:connect-return"; |
| 29 | + |
| 30 | +type Note = { app: string; path: string }; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tell the tab a sign-in is about to open in where to come back to. `tab` is the blank tab claimed |
| 34 | + * on the click, still OpenBot's origin, so its session storage is this site's, in that tab. |
| 35 | + */ |
| 36 | +export function markConnectReturn( |
| 37 | + tab: Window | null, |
| 38 | + app: string, |
| 39 | + path: string, |
| 40 | +): void { |
| 41 | + if (!tab) return; |
| 42 | + try { |
| 43 | + tab.sessionStorage.setItem(KEY, JSON.stringify({ app, path })); |
| 44 | + } catch { |
| 45 | + // No storage: the tab stays where the sign-in lands, as it did before. See above. |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +/** The note this tab was given, dropped as it is read so it can be acted on once. */ |
| 50 | +function takeNote(): Note | null { |
| 51 | + try { |
| 52 | + const text = sessionStorage.getItem(KEY); |
| 53 | + if (text === null) return null; |
| 54 | + sessionStorage.removeItem(KEY); |
| 55 | + const note = JSON.parse(text) as Partial<Note>; |
| 56 | + return typeof note.app === "string" && |
| 57 | + typeof note.path === "string" && |
| 58 | + // Only a path in OpenBot itself, never an address somewhere else. |
| 59 | + note.path.startsWith("/") && |
| 60 | + !note.path.startsWith("//") |
| 61 | + ? { app: note.app, path: note.path } |
| 62 | + : null; |
| 63 | + } catch { |
| 64 | + return null; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/** Whether the address the sign-in came back to says it failed. */ |
| 69 | +function cameBackFailed(): boolean { |
| 70 | + const search = new URLSearchParams(window.location.search); |
| 71 | + return ( |
| 72 | + search.get("status") === "failed" || search.get("connected") === "failed" |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +/** |
| 77 | + * On an app's page under Connections: when this tab was opened for this app's sign-in, close it |
| 78 | + * once the account is connected, or go back to where Connect was pressed. See above. |
| 79 | + */ |
| 80 | +export function useBackAfterConnect(app: string): void { |
| 81 | + const [note] = useState(() => { |
| 82 | + const taken = takeNote(); |
| 83 | + return taken && !cameBackFailed() ? taken : null; |
| 84 | + }); |
| 85 | + const waiting = note !== null && note.app === app; |
| 86 | + const connections = useQuery({ |
| 87 | + ...connectionsQueryOptions(), |
| 88 | + enabled: waiting, |
| 89 | + // The page confirms the account on arrival; until it is listed, ask again. |
| 90 | + refetchInterval: waiting ? 2_000 : false, |
| 91 | + }); |
| 92 | + // A withdrawn credential leaves its row behind, so a row alone is not connected. |
| 93 | + const connected = |
| 94 | + waiting && |
| 95 | + personalConnections(connections.data?.connections ?? []).some( |
| 96 | + (row) => row.serverId === app && row.revoked !== true, |
| 97 | + ); |
| 98 | + const router = useRouter(); |
| 99 | + const path = note?.path; |
| 100 | + useEffect(() => { |
| 101 | + if (!connected || !path) return; |
| 102 | + window.close(); |
| 103 | + if (!window.closed) router.history.replace(path); |
| 104 | + }, [connected, path, router]); |
| 105 | +} |
0 commit comments