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
33 changes: 18 additions & 15 deletions apps/web/src/components/settings/AuthorizedClientsSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<ReadonlyArray<AuthGrantScope>>([
...AuthStandardClientScopes,
Expand All @@ -623,13 +624,25 @@ 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 ||
!readEnvironmentScope(environmentId, AuthAccessWriteScope) ||
!selectedScopes.every((scope) => readEnvironmentScope(environmentId, scope))
)
return;
const dialogSession = pairingDialogSessionRef.current;
setIsCreatingPairingLink(true);
const trimmedLabel = pairingLabel.trim();
const result = await createPairingCredential({
Expand All @@ -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 =
Expand Down Expand Up @@ -688,15 +699,7 @@ export const AuthorizedClientsHeaderAction = memo(function AuthorizedClientsHead
) : null}
<Dialog
open={dialogOpen && pairingDenial === undefined}
onOpenChange={(open) => {
setDialogOpen(open);
if (!open) {
setPairingLabel("");
setPairingScopes(
AuthStandardClientScopes.filter((scope) => delegatableScopes.includes(scope)),
);
}
}}
onOpenChange={handlePairingDialogOpenChange}
>
{pairingDenial === undefined ? (
<DialogTrigger
Expand Down
48 changes: 48 additions & 0 deletions scripts/scenarios/pairing-completion-keeps-reopened-dialog.e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { expect } from "e2e";

import { test, WEB } from "./t3.ts";

test(
"Finishing a pairing link leaves a reopened Create link dialog open",
WEB,
async ({ t3, app, browser, screen }) => {
await t3.project("demo");
const pairingToken = "**/api/auth/pairing-token";
const requested = Promise.withResolvers<void>();
const release = Promise.withResolvers<void>();
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);
}
},
);
Loading