-
Notifications
You must be signed in to change notification settings - Fork 49
Add affiliate tracking link copy fallback #157
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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); | ||
| } | ||
| } | ||
|
|
||
| export default function OfferDetailClient() { | ||
| const { slug } = useParams<{ slug: string }>(); | ||
| const router = useRouter(); | ||
|
|
@@ -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); | ||
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Each call to |
||
|
|
||
| if (loading) { | ||
| return ( | ||
| <main className="flex-1 container mx-auto px-4 py-8 max-w-5xl"> | ||
|
|
@@ -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> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
document.execCommand("copy")is deprecated and may silently returnfalseor throw in some browsers even when the selection succeeds. Addingtextarea.focus()before.select()improves reliability across browsers.