From 4e137f4f042bd9e6b38c8181a6390313c8a1665f Mon Sep 17 00:00:00 2001 From: dafeiyupola <2368387344@qq.com> Date: Thu, 26 Nov 2026 22:00:12 +0800 Subject: [PATCH] fix(signup): let useClipboardCopy own the Copy link timer --- src/app/s/[slug]/commit-dialog.test.tsx | 25 +++++++++++++++ src/app/s/[slug]/commit-dialog.tsx | 41 +++++++++---------------- 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/app/s/[slug]/commit-dialog.test.tsx b/src/app/s/[slug]/commit-dialog.test.tsx index 25e61e36..894d75f5 100644 --- a/src/app/s/[slug]/commit-dialog.test.tsx +++ b/src/app/s/[slug]/commit-dialog.test.tsx @@ -756,6 +756,31 @@ describe(' sheet', () => { expect(status).toHaveClass('sr-only'); }); + // The two buttons used to keep two timers and clear neither, so a second + // copy 1.2 s later had its label wiped 0.8 s later by the first timer. + it('keeps Link copied for 2 seconds after the second copy, not the first', async () => { + const writeText = vi.fn(async () => {}); + stubClipboard(writeText); + await signUpWithEmail(''); + + fireEvent.click(screen.getByRole('button', { name: 'Copy link' })); + await waitFor( + () => expect(screen.getByRole('button', { name: 'Link copied' })).toBeInTheDocument(), + settle, + ); + + await new Promise((r) => setTimeout(r, 1200)); + fireEvent.click(screen.getByRole('button', { name: 'Link copied' })); + + await new Promise((r) => setTimeout(r, 1000)); + expect(screen.getByRole('button', { name: 'Link copied' })).toBeInTheDocument(); + + await waitFor( + () => expect(screen.getByRole('button', { name: 'Copy link' })).toBeInTheDocument(), + { timeout: 3000 }, + ); + }, 20_000); + it('says to copy the link by hand when the browser will not', async () => { const writeText = vi.fn(async () => { throw new Error('not allowed'); diff --git a/src/app/s/[slug]/commit-dialog.tsx b/src/app/s/[slug]/commit-dialog.tsx index 755561af..0c197075 100644 --- a/src/app/s/[slug]/commit-dialog.tsx +++ b/src/app/s/[slug]/commit-dialog.tsx @@ -2,6 +2,7 @@ import { Fragment, useEffect, useId, useMemo, useRef, useState, type ReactNode } from 'react'; import { useRouter } from 'next/navigation'; +import { useClipboardCopy } from '@/hooks/useClipboardCopy'; import { useLocale, useTranslations } from 'next-intl'; import { suggestEmail } from '@/lib/email-suggest'; import { buildIcs, calendarFileName } from '@/lib/ics'; @@ -154,9 +155,12 @@ export default function CommitDialog({ // The server then refuses a blank one, and the box says so from then on. const [emailNowRequired, setEmailNowRequired] = useState(false); const emailRequired = requireEmail || emailNowRequired; - const [shareCopied, setShareCopied] = useState(false); + // Both buttons copy the same link and only one is on screen at a time, so one + // hook covers them. It owns the "Link copied" window, and clears the previous + // timer on a second copy — the hand-written versions here did not. + const linkCopy = useClipboardCopy(); // What happened to the last Copy link, said in a live region. - const [copyResult, setCopyResult] = useState<'copied' | 'failed' | null>(null); + const [copyFailed, setCopyFailed] = useState(false); const triggerRef = useRef(null); const nameRef = useRef(null); const emailRef = useRef(null); @@ -327,8 +331,7 @@ export default function CommitDialog({ setError(null); setReported(null); if (wasSuccess) lastAttempt.current = null; - setShareCopied(false); - setCopyResult(null); + setCopyFailed(false); if (wasSuccess) router.refresh(); } @@ -374,32 +377,20 @@ export default function CommitDialog({ // user cancelled or share failed; fall through to copy } } - try { - await navigator.clipboard.writeText(success.editUrl); - setShareCopied(true); - setTimeout(() => setShareCopied(false), 2000); - } catch { - // last resort: do nothing — link is already on screen - } + // last resort: if the clipboard refuses, do nothing — the link is on screen + await linkCopy.copy(success.editUrl); } // Someone without an email gets Copy link, not Save your link: the link is how // they change or cancel, and a share sheet invites them to post it. async function handleCopy() { if (!success) return; - try { - await navigator.clipboard.writeText(success.editUrl); - setCopyResult('copied'); - // Back to Copy link, as Save your link does, unless a failure replaced it. - setTimeout(() => setCopyResult((r) => (r === 'copied' ? null : r)), 2000); - } catch { - // No clipboard over plain HTTP, or the browser refused. - setCopyResult('failed'); - } + // No clipboard over plain HTTP, or the browser refused: say so in the region. + setCopyFailed(!(await linkCopy.copy(success.editUrl))); } // Save your link and Copy link each say "Link copied" on themselves after a copy. - const linkCopied = success?.emailed ? shareCopied : copyResult === 'copied'; + const linkCopied = linkCopy.copied && !copyFailed; // The trigger's name and the sheet's: every row's button reads "Sign up". const signUpFor = t('signUpFor', { slot: actionName }); @@ -519,13 +510,9 @@ export default function CommitDialog({ // says what to do next, shows here on screen.

- {copyResult === 'copied' - ? t('copied') - : copyResult === 'failed' - ? t('copyFailed') - : null} + {copyFailed ? t('copyFailed') : linkCopied ? t('copied') : null}

)}