Skip to content
Merged
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
50 changes: 47 additions & 3 deletions src/app/affiliates/[slug]/OfferDetailClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Link from "next/link";
import { Button } from "@/components/ui/button";
import { Badge } from "@/components/ui/badge";
import { MarkdownContent } from "@/components/ui/MarkdownContent";
import { ArrowLeft, Users, TrendingUp, Calendar, ExternalLink, Zap } from "lucide-react";
import { ArrowLeft, Users, TrendingUp, Calendar, ExternalLink, Zap, Copy, Check } from "lucide-react";
import { Avatar, AvatarImage, AvatarFallback } from "@/components/ui/avatar";

interface AffiliateOffer {
Expand Down Expand Up @@ -40,6 +40,31 @@ function formatSats(sats: number): string {
return sats.toLocaleString();
}

async function copyText(text: string): Promise<boolean> {
try {
if (navigator.clipboard?.writeText) {
await navigator.clipboard.writeText(text);
return true;
}
} catch {
// Fall through to the textarea fallback below.
}

const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();

try {
return document.execCommand("copy");
} finally {
document.body.removeChild(textarea);
}
Comment on lines +53 to +65

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 document.execCommand("copy") is deprecated and may silently return false or throw in some browsers even when the selection succeeds. Adding textarea.focus() before .select() improves reliability across browsers.

Suggested change
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy");
} finally {
document.body.removeChild(textarea);
}
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "");
textarea.style.position = "fixed";
textarea.style.top = "0";
textarea.style.opacity = "0";
document.body.appendChild(textarea);
textarea.focus();
textarea.select();
try {
return document.execCommand("copy");
} finally {
document.body.removeChild(textarea);
}

}

export default function OfferDetailClient() {
const { slug } = useParams<{ slug: string }>();
const router = useRouter();
Expand All @@ -48,6 +73,7 @@ export default function OfferDetailClient() {
const [applying, setApplying] = useState(false);
const [applied, setApplied] = useState(false);
const [trackingUrl, setTrackingUrl] = useState("");
const [trackingCopied, setTrackingCopied] = useState(false);
const [error, setError] = useState("");
const [currentUserId, setCurrentUserId] = useState<string | null>(null);
const [btcUsd, setBtcUsd] = useState<number | null>(null);
Expand Down Expand Up @@ -113,6 +139,19 @@ export default function OfferDetailClient() {
setApplying(false);
}

async function handleCopyTrackingUrl() {
setError("");
setTrackingCopied(false);

if (await copyText(trackingUrl)) {
setTrackingCopied(true);
window.setTimeout(() => setTrackingCopied(false), 2000);
return;
}

setError("Unable to copy tracking link. Select the link and copy it manually.");
}
Comment on lines +142 to +153

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Stale timeout on rapid re-clicks or unmount

Each call to handleCopyTrackingUrl schedules a new setTimeout without cancelling the previous one. If the user clicks Copy twice within 2 seconds, the first timeout fires and resets the "Copied" state prematurely — before 2 seconds have elapsed since the second click. Additionally, if the component unmounts while the timer is pending, the timeout still fires and calls setTrackingCopied(false) on the stale closure. Storing the timer ID in a useRef and clearing it at the top of the handler (and in a useEffect cleanup) would prevent both issues.


if (loading) {
return (
<main className="flex-1 container mx-auto px-4 py-8 max-w-5xl">
Expand Down Expand Up @@ -338,9 +377,14 @@ export default function OfferDetailClient() {
<Button
size="sm"
variant="outline"
onClick={() => navigator.clipboard.writeText(trackingUrl)}
onClick={handleCopyTrackingUrl}
>
Copy
{trackingCopied ? (
<Check className="h-4 w-4" />
) : (
<Copy className="h-4 w-4" />
)}
{trackingCopied ? "Copied" : "Copy"}
</Button>
</div>
</div>
Expand Down
Loading