Repository navigation
fix(mobile): t3 pair's app link asks to add the environment instead of opening NotFound #40675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
juliusmarminge
wants to merge
1
commit into
main
Choose a base branch
from
factory/issue-40666
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+140
−41
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
apps/mobile/src/features/connection/pairingAppLink.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| import { buildPairingAppLink } from "@t3tools/shared/remote"; | ||
| import { expect, it, vi } from "vite-plus/test"; | ||
|
|
||
| import type { ConfirmDialogRequest } from "../../components/ui/Dialog.types"; | ||
|
|
||
| const harness = vi.hoisted(() => ({ | ||
| confirm: vi.fn<(request: ConfirmDialogRequest) => void>(), | ||
| alert: vi.fn(), | ||
| pair: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("../../components/ui/Dialog", () => ({ | ||
| confirm: harness.confirm, | ||
| alert: harness.alert, | ||
| })); | ||
| vi.mock("../../state/use-remote-environment-registry", () => ({ | ||
| pairEnvironment: harness.pair, | ||
| })); | ||
|
|
||
| import { takePairingAppLink } from "./pairingAppLink"; | ||
|
|
||
| it("asks before pairing from the link t3 pair prints, and never routes it", async () => { | ||
| const pairingUrl = "https://example-host.test/pair#token=ABCD1234"; | ||
| const link = buildPairingAppLink(pairingUrl); | ||
| harness.pair.mockResolvedValue({ ok: true, error: null }); | ||
|
|
||
| expect(takePairingAppLink("t3code://threads")).toBe(false); | ||
| expect(takePairingAppLink(link)).toBe(true); | ||
| expect(harness.confirm).toHaveBeenCalledTimes(1); | ||
| expect(harness.confirm.mock.calls[0]![0].message).toContain("https://example-host.test"); | ||
| expect(harness.pair).not.toHaveBeenCalled(); | ||
|
|
||
| // The same link opened again while the prompt is up does not stack a second one. | ||
| expect(takePairingAppLink(link)).toBe(true); | ||
| expect(harness.confirm).toHaveBeenCalledTimes(1); | ||
|
|
||
| harness.confirm.mock.calls[0]![0].onConfirm(); | ||
| expect(harness.pair).toHaveBeenCalledWith(pairingUrl); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; | ||
| import { readPairingAppLink, resolveRemotePairingTarget } from "@t3tools/shared/remote"; | ||
| import { msg } from "gt-react-native"; | ||
|
|
||
| import * as Dialog from "../../components/ui/Dialog"; | ||
| import { m } from "../../i18n/messages"; | ||
| import { pairEnvironment } from "../../state/use-remote-environment-registry"; | ||
|
|
||
| // A link already asking or paired; a second open would only fail on the spent one-time token. | ||
| const takenPairingUrls = new Set<string>(); | ||
|
|
||
| function reportFailure(message: string | null) { | ||
| if (message !== null) Dialog.alert(m(msg("Connection failed")), message); | ||
| } | ||
|
|
||
| /** | ||
| * Takes the `t3code://pair?pairingUrl=…` link that `t3 pair` prints, so navigation never sees it. | ||
| * Any app can open the link, so the user confirms the host before its token is spent. | ||
| */ | ||
| export function takePairingAppLink(url: string): boolean { | ||
| const pairingUrl = readPairingAppLink(url); | ||
| if (pairingUrl === null) return false; | ||
| if (takenPairingUrls.has(pairingUrl)) return true; | ||
| // The origin keeps the scheme visible, so a plain HTTP backend is not mistaken for HTTPS. | ||
| let origin: string; | ||
| try { | ||
| origin = new URL(resolveRemotePairingTarget({ pairingUrl }).httpBaseUrl).origin; | ||
| } catch (error) { | ||
| reportFailure(errorDisplayMessage(error, m) ?? m(msg("Failed to pair with the environment."))); | ||
| return true; | ||
| } | ||
| takenPairingUrls.add(pairingUrl); | ||
| Dialog.confirm({ | ||
| title: m(msg("Add environment")), | ||
| message: m(msg("Add the environment at {origin}?"), { origin }), | ||
| confirmText: m(msg("Add")), | ||
| onCancel: () => takenPairingUrls.delete(pairingUrl), | ||
| onConfirm: () => { | ||
| void pairEnvironment(pairingUrl).then((outcome) => { | ||
| if (outcome.ok) return; | ||
| takenPairingUrls.delete(pairingUrl); | ||
| reportFailure(outcome.error); | ||
| }); | ||
| }, | ||
| }); | ||
| return true; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Release unconfirmed links when their alert is dismissed.
Dialog.confirmhandles only button presses:onCancelreleases the URL, andonConfirmkeeps it while pairing. On Android, an alert can close without a button press. A newer link can replace it, or the user can dismiss it with an outside tap or back press. When that happens, no callback runs, so the URL stays intakenPairingUrlswithout a visible prompt, an active pairing, or a successful pairing. Every later open of that URL then returnstruewithout confirmation or pairing, for the lifetime of the app process.Handle native dismissal for an unconfirmed link. Release that URL. Keep duplicate suppression while pairing is active and after successful pairing. Queuing a replaced confirmation is an alternative to releasing it. Choose one behavior, and do not change the behavior for the other paths.