diff --git a/apps/web/src/components/settings/AuthorizedClientsSettings.tsx b/apps/web/src/components/settings/AuthorizedClientsSettings.tsx index f308532748c9..d44bffdc0413 100644 --- a/apps/web/src/components/settings/AuthorizedClientsSettings.tsx +++ b/apps/web/src/components/settings/AuthorizedClientsSettings.tsx @@ -3,7 +3,7 @@ import { m as resolveMessage } from "~/i18n/messages"; import { managedCapabilityDenial } from "@t3tools/client-runtime/contract-labels"; import { AUTH_SCOPE_OPTIONS as PAIRING_SCOPE_OPTIONS } from "@t3tools/shared/authScopeOptions"; import { PlugIcon, PlusIcon, QrCodeIcon } from "lucide-react"; -import { type ReactNode, memo, useId, useState } from "react"; +import { type ReactNode, memo, useId, useRef, useState } from "react"; import { T, useGT, useMessages } from "gt-react"; import { AuthAccessWriteScope, @@ -614,6 +614,7 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead reportFailure: false, }); const [dialogOpen, setDialogOpen] = useState(false); + const pairingDialogSessionRef = useRef(0); const [pairingLabel, setPairingLabel] = useState(""); const [pairingScopes, setPairingScopes] = useState>([ ...AuthStandardClientScopes, @@ -623,6 +624,17 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead const relayAdminSelected = selectedScopes.includes(AuthRelayConfigureScope); const [isCreatingPairingLink, setIsCreatingPairingLink] = useState(false); + const handlePairingDialogOpenChange = (open: boolean) => { + setDialogOpen(open); + if (!open) { + pairingDialogSessionRef.current += 1; + setPairingLabel(""); + setPairingScopes( + AuthStandardClientScopes.filter((scope) => delegatableScopes.includes(scope)), + ); + } + }; + const handleCreatePairingLink = async () => { if ( selectedScopes.length === 0 || @@ -630,6 +642,7 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead !selectedScopes.every((scope) => readEnvironmentScope(environmentId, scope)) ) return; + const dialogSession = pairingDialogSessionRef.current; setIsCreatingPairingLink(true); const trimmedLabel = pairingLabel.trim(); const result = await createPairingCredential({ @@ -638,11 +651,9 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead }); if (result._tag === "Success") { onPairingLinkCreated(result.value); - setPairingLabel(""); - setPairingScopes( - AuthStandardClientScopes.filter((scope) => delegatableScopes.includes(scope)), - ); - setDialogOpen(false); + if (dialogSession === pairingDialogSessionRef.current) { + handlePairingDialogOpenChange(false); + } } else if (!isAtomCommandInterrupted(result)) { const error = squashAtomCommandFailure(result); const message = @@ -688,15 +699,7 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead ) : null} { - setDialogOpen(open); - if (!open) { - setPairingLabel(""); - setPairingScopes( - AuthStandardClientScopes.filter((scope) => delegatableScopes.includes(scope)), - ); - } - }} + onOpenChange={handlePairingDialogOpenChange} > {pairingDenial === undefined ? ( { + await t3.project("demo"); + const pairingToken = "**/api/auth/pairing-token"; + const requested = Promise.withResolvers(); + const release = Promise.withResolvers(); + await browser.route(pairingToken, async (route) => { + if (route.request.method !== "POST") return route.fallback(); + requested.resolve(); + await release.promise; + await route.continue(); + }); + + await t3.open("/settings/access"); + const authorizedClients = browser.locator("#authorized-clients"); + const open = authorizedClients.getByRole("button", "Create link", { exact: true }); + const dialog = screen.getByRole("dialog", "Create pairing link", { exact: true }); + // A closing popup keeps its role until its exit animation ends; only an open one has data-open. + const openDialog = browser + .locator('[data-slot="dialog-popup"][data-open]') + .getByRole("heading", "Create pairing link", { exact: true }); + const label = "Waiting tablet"; + try { + await open.tap(); + await dialog.getByPlaceholder("e.g. Living room iPad").fill(label); + await dialog.getByRole("button", "Create link", { exact: true }).tap(); + await requested.promise; + await browser.keyboard.press("Escape"); + await dialog.waitFor({ state: "hidden" }); + await open.tap(); + await openDialog.waitFor(); + + release.resolve(); + await authorizedClients.getByText(label, { exact: true }).waitFor(); + await app.screenshot("after-first-link-finishes"); + await expect(openDialog).toBeVisible(); + } finally { + release.resolve(); + await browser.unroute(pairingToken); + } + }, +);