Skip to content

Commit 1eae857

Browse files
kalvinnchaunpub1ntr8jjcqq6gt06q5avvqttfjgpwshmra22pcmcagdnukw4ja4nqqsa9g54
andauthored
feat(desktop): streamline nostr identity pairing (#1974)
Signed-off-by: npub1ntr8jjcqq6gt06q5avvqttfjgpwshmra22pcmcagdnukw4ja4nqqsa9g54 <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1ntr8jjcqq6gt06q5avvqttfjgpwshmra22pcmcagdnukw4ja4nqqsa9g54 <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@sprout-oss.stage.blox.sqprod.co>
1 parent 74f3f29 commit 1eae857

4 files changed

Lines changed: 323 additions & 103 deletions

File tree

‎desktop/src/features/profile/ui/NostrBindConsentDialog.tsx‎

Lines changed: 177 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as DialogPrimitive from "@radix-ui/react-dialog";
22
import { openUrl } from "@tauri-apps/plugin-opener";
3+
import { ChevronDown } from "lucide-react";
34
import { AnimatePresence, motion, useReducedMotion } from "motion/react";
45
import * as React from "react";
56
import { toast } from "sonner";
@@ -88,6 +89,11 @@ function normalizeVerificationCode(value: string): string[] {
8889
.map((character) => character.trim());
8990
}
9091

92+
function isNostrBindRequestExpired(payload: NostrBindDeepLinkPayload): boolean {
93+
const expiry = new Date(payload.expiresAt).getTime();
94+
return Number.isNaN(expiry) || expiry <= Date.now();
95+
}
96+
9197
function formatError(error: unknown): string {
9298
if (error instanceof Error) {
9399
return error.message;
@@ -135,6 +141,74 @@ async function returnSignedResponseToBrowser(
135141
}
136142
}
137143

144+
function SignedResponseControls({
145+
copyFailed,
146+
copyLabel,
147+
onCopy,
148+
signedResponse,
149+
}: {
150+
copyFailed: boolean;
151+
copyLabel: string;
152+
onCopy: () => void;
153+
signedResponse: string;
154+
}) {
155+
return (
156+
<div className="space-y-4" data-testid="nostr-bind-manual-fallback-content">
157+
<pre
158+
className="max-h-56 w-full overflow-auto rounded-2xl border border-border/70 bg-muted/60 p-4 text-left shadow-xs"
159+
data-testid="nostr-bind-signed-response"
160+
>
161+
<code className="whitespace-pre-wrap break-all font-mono text-xs leading-5 text-foreground">
162+
{signedResponse}
163+
</code>
164+
</pre>
165+
166+
{copyFailed ? (
167+
<p className="w-full rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-left text-sm text-destructive">
168+
{COPY_FAILURE_MESSAGE}
169+
</p>
170+
) : null}
171+
172+
<Button
173+
aria-label={copyLabel}
174+
className="h-10 w-full"
175+
data-testid="nostr-bind-copy-response"
176+
onClick={onCopy}
177+
type="button"
178+
>
179+
<span aria-live="polite" className="sr-only">
180+
{copyLabel}
181+
</span>
182+
<span
183+
aria-hidden="true"
184+
className="inline-grid h-5 place-items-center overflow-hidden"
185+
>
186+
<span
187+
className={cn(
188+
COPY_BUTTON_LABEL_CLASS,
189+
copyLabel === "Copy response"
190+
? "translate-y-0 opacity-100"
191+
: "-translate-y-0.5 opacity-0",
192+
)}
193+
>
194+
Copy response
195+
</span>
196+
<span
197+
className={cn(
198+
COPY_BUTTON_LABEL_CLASS,
199+
copyLabel === "Copied"
200+
? "translate-y-0 opacity-100"
201+
: "translate-y-0.5 opacity-0",
202+
)}
203+
>
204+
Copied
205+
</span>
206+
</span>
207+
</Button>
208+
</div>
209+
);
210+
}
211+
138212
export function NostrBindConsentDialog() {
139213
const isPreview = isNostrBindPreviewEnabled();
140214
const [payload, setPayload] = React.useState<NostrBindDeepLinkPayload | null>(
@@ -154,12 +228,15 @@ export function NostrBindConsentDialog() {
154228
const [hasCodeMismatch, setHasCodeMismatch] = React.useState(false);
155229
const [copyFailed, setCopyFailed] = React.useState(false);
156230
const [error, setError] = React.useState<string | null>(null);
231+
const [isManualFallbackOpen, setIsManualFallbackOpen] = React.useState(false);
157232
const codeInputRefs = React.useRef<Array<HTMLInputElement | null>>([]);
158233
const codeShakeRef = React.useRef<HTMLDivElement | null>(null);
159234
const codeShakeAnimationRef = React.useRef<Animation | null>(null);
160235
const copiedTimerRef = React.useRef<ReturnType<typeof setTimeout> | null>(
161236
null,
162237
);
238+
const autoSignAttemptRef = React.useRef<string | null>(null);
239+
const activeSignAttemptRef = React.useRef<symbol | null>(null);
163240
const systemColorScheme = useSystemColorScheme();
164241
const shouldReduceMotion = useReducedMotion();
165242
const enteredVerificationCode = verificationCode.join("");
@@ -204,13 +281,17 @@ export function NostrBindConsentDialog() {
204281

205282
const unlistenPromise = listenForNostrBindDeepLinks((nextPayload) => {
206283
clearCopiedState();
284+
autoSignAttemptRef.current = null;
285+
activeSignAttemptRef.current = null;
207286
setPayload(nextPayload);
208287
setIdentity(null);
288+
setIsSigning(false);
209289
setSignedResponse(null);
210290
setVerificationCode(createEmptyVerificationCode());
211291
setHasCodeMismatch(false);
212292
setCopyFailed(false);
213293
setError(null);
294+
setIsManualFallbackOpen(false);
214295
getIdentity()
215296
.then(setIdentity)
216297
.catch((error) => {
@@ -225,22 +306,19 @@ export function NostrBindConsentDialog() {
225306
};
226307
}, [clearCopiedState, isPreview]);
227308

228-
const isExpired = React.useMemo(() => {
229-
if (!payload) {
230-
return false;
231-
}
232-
const expiry = new Date(payload.expiresAt).getTime();
233-
return Number.isNaN(expiry) || expiry <= Date.now();
234-
}, [payload]);
309+
const isExpired = payload !== null && isNostrBindRequestExpired(payload);
235310

236311
const resetDialog = React.useCallback(() => {
237312
clearCopiedState();
313+
autoSignAttemptRef.current = null;
314+
activeSignAttemptRef.current = null;
238315
setPayload(null);
239316
setSignedResponse(null);
240317
setVerificationCode(createEmptyVerificationCode());
241318
setHasCodeMismatch(false);
242319
setCopyFailed(false);
243320
setError(null);
321+
setIsManualFallbackOpen(false);
244322
setIdentity(null);
245323
setIsSigning(false);
246324
}, [clearCopiedState]);
@@ -297,6 +375,7 @@ export function NostrBindConsentDialog() {
297375
(index: number, value: string) => {
298376
const nextDigits = value.replace(/\D/g, "");
299377
const next = [...verificationCode];
378+
autoSignAttemptRef.current = null;
300379

301380
if (!nextDigits) {
302381
next[index] = "";
@@ -363,6 +442,7 @@ export function NostrBindConsentDialog() {
363442
}
364443

365444
event.preventDefault();
445+
autoSignAttemptRef.current = null;
366446
const next = normalizeVerificationCode(pastedCode);
367447
setVerificationCode(next);
368448
if (
@@ -402,6 +482,7 @@ export function NostrBindConsentDialog() {
402482

403483
if (event.key === "Backspace") {
404484
event.preventDefault();
485+
autoSignAttemptRef.current = null;
405486
const targetIndex = verificationCode[index]
406487
? index
407488
: Math.max(index - 1, 0);
@@ -429,10 +510,10 @@ export function NostrBindConsentDialog() {
429510
);
430511

431512
const handleSign = React.useCallback(async () => {
432-
if (!payload) {
513+
if (activeSignAttemptRef.current || !payload) {
433514
return;
434515
}
435-
if (isExpired) {
516+
if (isNostrBindRequestExpired(payload)) {
436517
setError(EXPIRED_LINK_MESSAGE);
437518
return;
438519
}
@@ -447,6 +528,8 @@ export function NostrBindConsentDialog() {
447528
return;
448529
}
449530

531+
const attempt = Symbol("nostr-bind-sign");
532+
activeSignAttemptRef.current = attempt;
450533
setIsSigning(true);
451534
clearCopiedState();
452535
setError(null);
@@ -461,21 +544,35 @@ export function NostrBindConsentDialog() {
461544
origin: payload.origin,
462545
expiresAt: payload.expiresAt,
463546
});
547+
if (activeSignAttemptRef.current !== attempt) {
548+
return;
549+
}
550+
464551
setSignedResponse(signed);
465552
if (payload.returnMode === "browser_fragment_v1" && payload.callbackUrl) {
466-
setError(
467-
await returnSignedResponseToBrowser(payload.callbackUrl, signed),
553+
const callbackError = await returnSignedResponseToBrowser(
554+
payload.callbackUrl,
555+
signed,
468556
);
557+
if (activeSignAttemptRef.current !== attempt) {
558+
return;
559+
}
560+
setError(callbackError);
561+
setIsManualFallbackOpen(callbackError !== null);
469562
}
470563
} catch (error) {
471-
setError(formatError(error) || "Failed to sign binding response.");
564+
if (activeSignAttemptRef.current === attempt) {
565+
setError(formatError(error) || "Failed to sign binding response.");
566+
}
472567
} finally {
473-
setIsSigning(false);
568+
if (activeSignAttemptRef.current === attempt) {
569+
activeSignAttemptRef.current = null;
570+
setIsSigning(false);
571+
}
474572
}
475573
}, [
476574
clearCopiedState,
477575
enteredVerificationCode,
478-
isExpired,
479576
isPreview,
480577
isVerificationCodeComplete,
481578
isVerificationCodeValid,
@@ -484,6 +581,35 @@ export function NostrBindConsentDialog() {
484581
verificationCode,
485582
]);
486583

584+
React.useEffect(() => {
585+
if (
586+
!payload ||
587+
identity === null ||
588+
isExpired ||
589+
isSigning ||
590+
signedResponse !== null ||
591+
!isVerificationCodeValid
592+
) {
593+
return;
594+
}
595+
596+
const attemptKey = `${payload.challengeId}:${enteredVerificationCode}`;
597+
if (autoSignAttemptRef.current === attemptKey) {
598+
return;
599+
}
600+
autoSignAttemptRef.current = attemptKey;
601+
void handleSign();
602+
}, [
603+
enteredVerificationCode,
604+
handleSign,
605+
identity,
606+
isExpired,
607+
isSigning,
608+
isVerificationCodeValid,
609+
payload,
610+
signedResponse,
611+
]);
612+
487613
const handleCopyAgain = React.useCallback(async () => {
488614
if (!signedResponse) {
489615
return;
@@ -548,7 +674,7 @@ export function NostrBindConsentDialog() {
548674
id="nostr-bind-description"
549675
>
550676
{payload.returnMode === "browser_fragment_v1"
551-
? "Buzz opened your browser to finish verification. If it did not open, copy the response below."
677+
? "Buzz opened your browser to finish verification."
552678
: "Copy the response below, then paste it into the Buzz website to finish verification."}
553679
</DialogPrimitive.Description>
554680

@@ -558,65 +684,50 @@ export function NostrBindConsentDialog() {
558684
</p>
559685
) : null}
560686

561-
<pre
562-
className="mt-10 max-h-56 w-full overflow-auto rounded-2xl border border-border/70 bg-muted/60 p-4 text-left shadow-xs"
563-
data-testid="nostr-bind-signed-response"
564-
>
565-
<code className="whitespace-pre-wrap break-all font-mono text-xs leading-5 text-foreground">
566-
{signedResponse}
567-
</code>
568-
</pre>
569-
570-
{copyFailed ? (
571-
<p className="mt-4 w-full rounded-md border border-destructive/30 bg-destructive/10 px-3 py-2 text-left text-sm text-destructive">
572-
{COPY_FAILURE_MESSAGE}
573-
</p>
574-
) : null}
687+
{payload.returnMode === "browser_fragment_v1" ? (
688+
<details
689+
className="group mt-10 w-full overflow-hidden rounded-2xl border border-border/70 bg-muted/30 text-left shadow-xs"
690+
data-testid="nostr-bind-manual-fallback"
691+
onToggle={(event) =>
692+
setIsManualFallbackOpen(event.currentTarget.open)
693+
}
694+
open={isManualFallbackOpen}
695+
>
696+
<summary className="flex cursor-pointer list-none items-center justify-between gap-4 px-4 py-3 text-sm font-medium transition-colors hover:bg-muted/40 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring [&::-webkit-details-marker]:hidden">
697+
<span>Pairing didn’t finish automatically?</span>
698+
<ChevronDown className="h-4 w-4 shrink-0 text-muted-foreground transition-transform duration-150 group-open:rotate-180" />
699+
</summary>
700+
<div className="space-y-4 border-t border-border/55 p-4">
701+
<p className="text-sm leading-6 text-muted-foreground">
702+
Copy this response and paste it into the pairing page.
703+
</p>
704+
<SignedResponseControls
705+
copyFailed={copyFailed}
706+
copyLabel={finishCopyButtonLabel}
707+
onCopy={handleCopyAgain}
708+
signedResponse={signedResponse}
709+
/>
710+
</div>
711+
</details>
712+
) : (
713+
<div className="mt-10 w-full">
714+
<SignedResponseControls
715+
copyFailed={copyFailed}
716+
copyLabel={finishCopyButtonLabel}
717+
onCopy={handleCopyAgain}
718+
signedResponse={signedResponse}
719+
/>
720+
</div>
721+
)}
575722

576723
<div className="mt-8 flex w-full flex-col gap-3">
577-
<Button
578-
aria-label={finishCopyButtonLabel}
579-
className="h-10 w-full"
580-
data-testid="nostr-bind-copy-response"
581-
onClick={handleCopyAgain}
582-
type="button"
583-
>
584-
<span aria-live="polite" className="sr-only">
585-
{finishCopyButtonLabel}
586-
</span>
587-
<span
588-
aria-hidden="true"
589-
className="inline-grid h-5 place-items-center overflow-hidden"
590-
>
591-
<span
592-
className={cn(
593-
COPY_BUTTON_LABEL_CLASS,
594-
finishCopyButtonLabel === "Copy response"
595-
? "translate-y-0 opacity-100"
596-
: "-translate-y-0.5 opacity-0",
597-
)}
598-
>
599-
Copy response
600-
</span>
601-
<span
602-
className={cn(
603-
COPY_BUTTON_LABEL_CLASS,
604-
finishCopyButtonLabel === "Copied"
605-
? "translate-y-0 opacity-100"
606-
: "translate-y-0.5 opacity-0",
607-
)}
608-
>
609-
Copied
610-
</span>
611-
</span>
612-
</Button>
613724
<Button
614725
className="h-10 w-full text-muted-foreground hover:text-accent-foreground"
615726
onClick={() => handleOpenChange(false)}
616727
type="button"
617728
variant="ghost"
618729
>
619-
Close
730+
Continue
620731
</Button>
621732
</div>
622733
</OnboardingSlideTransition>

0 commit comments

Comments
 (0)