Skip to content

Commit d5e337e

Browse files
t3dotggclaude
andcommitted
fix(web): capture the failed copy value per attempt so the reveal dialog cannot race
Co-Authored-By: Claude Fable 5 <[email protected]>
1 parent a494f9c commit d5e337e

1 file changed

Lines changed: 21 additions & 10 deletions

File tree

‎apps/web/src/components/settings/ConnectionsSettings.tsx‎

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,10 +593,10 @@ const PairingLinkListRow = memo(function PairingLinkListRow({
593593
: isLoopbackHostname(window.location.hostname)
594594
? null
595595
: currentOriginPairingUrl);
596-
// Last value a copy was attempted for. The clipboard-failure reveal dialog
597-
// must show exactly what failed to copy, not the row's default URL.
598-
const [lastCopyAttemptValue, setLastCopyAttemptValue] = useState<string | null>(null);
599-
const revealValue = lastCopyAttemptValue ?? shareablePairingUrl ?? pairingLink.credential;
596+
// Value of the copy attempt that last failed. The clipboard-failure reveal
597+
// dialog must show exactly what failed to copy, not the row's default URL.
598+
const [failedCopyValue, setFailedCopyValue] = useState<string | null>(null);
599+
const revealValue = failedCopyValue ?? shareablePairingUrl ?? pairingLink.credential;
600600
const isRevealValueUrl = revealValue !== pairingLink.credential;
601601
const isRevealValueHostedAppPairingUrl = isRevealValueUrl && isHostedAppPairingUrl(revealValue);
602602
// Never render a QR for a loopback URL, even in the manual-copy fallback.
@@ -607,8 +607,11 @@ const PairingLinkListRow = memo(function PairingLinkListRow({
607607
window.isSecureContext &&
608608
navigator.clipboard?.writeText != null;
609609

610-
const { copyToClipboard } = useCopyToClipboard<"code" | "hosted-link" | "link">({
611-
onCopy: (kind) => {
610+
const { copyToClipboard } = useCopyToClipboard<{
611+
value: string;
612+
kind: "code" | "hosted-link" | "link";
613+
}>({
614+
onCopy: ({ kind }) => {
612615
toastManager.add({
613616
type: "success",
614617
title:
@@ -625,7 +628,10 @@ const PairingLinkListRow = memo(function PairingLinkListRow({
625628
: "Paste it into another client to finish pairing.",
626629
});
627630
},
628-
onError: (error, kind) => {
631+
onError: (error, { value, kind }) => {
632+
// Captured per attempt so concurrent copies cannot make the dialog
633+
// reveal a different value than the one that failed.
634+
setFailedCopyValue(value);
629635
setIsRevealDialogOpen(true);
630636
toastManager.add(
631637
stackedThreadToast({
@@ -645,8 +651,7 @@ const PairingLinkListRow = memo(function PairingLinkListRow({
645651

646652
const copyPairingValue = useCallback(
647653
(value: string, kind: "code" | "hosted-link" | "link") => {
648-
setLastCopyAttemptValue(kind === "code" ? null : value);
649-
copyToClipboard(value, kind);
654+
copyToClipboard(value, { value, kind });
650655
},
651656
[copyToClipboard],
652657
);
@@ -712,7 +717,13 @@ const PairingLinkListRow = memo(function PairingLinkListRow({
712717
Share
713718
</Button>
714719
) : null}
715-
<Dialog open={isRevealDialogOpen} onOpenChange={setIsRevealDialogOpen}>
720+
<Dialog
721+
open={isRevealDialogOpen}
722+
onOpenChange={(open) => {
723+
setIsRevealDialogOpen(open);
724+
if (!open) setFailedCopyValue(null);
725+
}}
726+
>
716727
{canCopyToClipboard ? (
717728
shareablePairingUrl ? null : (
718729
<Button size="xs" variant="outline" onClick={handleCopyCode}>

0 commit comments

Comments
 (0)