fix: disable copy button while referral link is loading#177
Conversation
…#133) - Add loadingLink state to track when referral link is still loading - Disable Copy button when loadingLink is true or referralLink is empty - Show 'Loading...' text on Copy button during load - Add guard in copyLink handler to prevent copying empty string - Add disabled:opacity-50 and disabled:cursor-not-allowed styles - Add regression tests for loading, empty link, and successful copy
Greptile SummaryThis PR fixes a race condition where clicking Copy before the referral link had loaded would write an empty string to the clipboard. It introduces a
Confidence Score: 3/5The change is a net improvement over the original behaviour, but it introduces a scenario where the Copy button can become permanently disabled if the network request fails at the transport level. The The Important Files Changed
Sequence DiagramsequenceDiagram
participant Browser
participant ReferralsPage
participant API as /api/referrals/code
ReferralsPage->>ReferralsPage: "loadingLink = true (mount)"
ReferralsPage->>API: fetch("/api/referrals/code")
Note over ReferralsPage: Copy button: disabled + "Loading..."
alt Success (HTTP ok)
API-->>ReferralsPage: "{ link, code }"
ReferralsPage->>ReferralsPage: setReferralLink / setReferralCode
ReferralsPage->>ReferralsPage: setLoadingLink(false)
Note over ReferralsPage: Copy button: enabled + "Copy"
else HTTP error (non-ok)
API-->>ReferralsPage: "res.ok = false → returns null"
ReferralsPage->>ReferralsPage: setLoadingLink(false)
Note over ReferralsPage: Copy button: disabled (no link) + "Copy"
else Network rejection (throws)
API--xReferralsPage: fetch throws (offline / DNS)
Note over ReferralsPage: .then() never called
Note over ReferralsPage: loadingLink stays true forever
Note over ReferralsPage: Copy button: permanently disabled + "Loading..."
end
Browser->>ReferralsPage: User clicks Copy
alt referralLink is set
ReferralsPage->>Browser: navigator.clipboard.writeText(link)
ReferralsPage->>ReferralsPage: setCopied(true) → "Copied!"
else referralLink is empty
ReferralsPage->>ReferralsPage: early return (guard added by this PR)
end
|
Fix for #133 — Referral copy button copies empty link before invite link loads
Problem
On the Invite Friends dashboard, the referral link starts as an empty string until
/api/referrals/codereturns. The Copy button is enabled during that loading window, so a quick click can copy an empty string and show the user a successful copy state even though no invite link was copied.Changes
loadingLinkstate — starts astrue, set tofalseafterloadCode()completes (regardless of success/failure)loadingLinkis true orreferralLinkis empty (disabled={loadingLink || !referralLink})copyLink— early return ifreferralLinkis empty, preventing clipboard write of empty stringdisabled:opacity-50 disabled:cursor-not-allowedfor clear visual feedbackTesting
All 3 new tests pass:
Fixes #133