|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Check, Copy, MessageCircle, QrCode, Share2, Trophy } from "lucide-react"; |
| 4 | +import Link from "next/link"; |
| 5 | +import { useState } from "react"; |
| 6 | +import { toast } from "sonner"; |
| 7 | +import { BrandedQrModal } from "@/components/common/branded-qr-modal"; |
| 8 | +import { Button } from "@/components/ui/button"; |
| 9 | +import { LP_ACTION_REWARDS } from "@/constants/gamification"; |
| 10 | +import { useProfile } from "@/hooks/use-profile"; |
| 11 | +import { trackShare } from "@/lib/analytics/ga4"; |
| 12 | +import { inviteUrl } from "@/lib/referrals/invite"; |
| 13 | +import { cn } from "@/lib/utils"; |
| 14 | + |
| 15 | +const REWARD = LP_ACTION_REWARDS.REFERRAL_SIGNUP; |
| 16 | + |
| 17 | +/** The signed-in student's personal invite link, with one-tap ways to send it. */ |
| 18 | +export function InviteCard({ className }: { className?: string }) { |
| 19 | + const { profile } = useProfile(); |
| 20 | + const [copied, setCopied] = useState(false); |
| 21 | + const [qrOpen, setQrOpen] = useState(false); |
| 22 | + |
| 23 | + if (!profile?.username) return null; |
| 24 | + |
| 25 | + const link = inviteUrl(profile.username); |
| 26 | + const invites = profile.referralCount ?? 0; |
| 27 | + const message = `Join me on CampusLoop, the app only for students of our college. ${link}`; |
| 28 | + |
| 29 | + async function copy() { |
| 30 | + try { |
| 31 | + await navigator.clipboard.writeText(link); |
| 32 | + setCopied(true); |
| 33 | + toast.success("Invite link copied"); |
| 34 | + trackShare("copy_link", "invite", profile?.username); |
| 35 | + setTimeout(() => setCopied(false), 2000); |
| 36 | + } catch { |
| 37 | + toast.error("Could not copy. Press and hold the link to copy it."); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + async function share() { |
| 42 | + if (typeof navigator.share !== "function") return copy(); |
| 43 | + try { |
| 44 | + await navigator.share({ title: "CampusLoop", text: message, url: link }); |
| 45 | + trackShare("native", "invite", profile?.username); |
| 46 | + } catch { |
| 47 | + // The student closed the share sheet. |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return ( |
| 52 | + <section |
| 53 | + className={cn("space-y-3 rounded-lg border border-border bg-surface p-4", className)} |
| 54 | + aria-labelledby="invite-title" |
| 55 | + > |
| 56 | + <div className="flex items-start justify-between gap-3"> |
| 57 | + <div className="space-y-1"> |
| 58 | + <h2 id="invite-title" className="text-lg text-fg"> |
| 59 | + Invite your friends |
| 60 | + </h2> |
| 61 | + <p className="text-sm text-fg-muted"> |
| 62 | + You get <span className="font-semibold text-reward-text">+{REWARD} LP</span> for every friend who |
| 63 | + joins with their college email. |
| 64 | + </p> |
| 65 | + </div> |
| 66 | + <p className="shrink-0 text-right"> |
| 67 | + <span className="block text-2xl font-bold tabular-nums text-fg">{invites}</span> |
| 68 | + <span className="text-xs text-fg-muted">{invites === 1 ? "friend joined" : "friends joined"}</span> |
| 69 | + </p> |
| 70 | + </div> |
| 71 | + |
| 72 | + <button |
| 73 | + type="button" |
| 74 | + onClick={copy} |
| 75 | + className={cn( |
| 76 | + "flex w-full items-center justify-between gap-3 rounded-md border border-border", |
| 77 | + "bg-surface-muted px-3 py-2.5 text-left" |
| 78 | + )} |
| 79 | + aria-label="Copy your invite link" |
| 80 | + > |
| 81 | + <span className="min-w-0 truncate text-sm font-medium text-fg">{link.replace("https://", "")}</span> |
| 82 | + {copied ? ( |
| 83 | + <Check className="size-4 shrink-0 text-success" aria-hidden /> |
| 84 | + ) : ( |
| 85 | + <Copy className="size-4 shrink-0 text-fg-muted" aria-hidden /> |
| 86 | + )} |
| 87 | + </button> |
| 88 | + |
| 89 | + <div className="grid grid-cols-2 gap-2 sm:grid-cols-4"> |
| 90 | + <Button asChild variant="success" size="sm"> |
| 91 | + <a |
| 92 | + href={`https://wa.me/?text=${encodeURIComponent(message)}`} |
| 93 | + target="_blank" |
| 94 | + rel="noopener noreferrer" |
| 95 | + onClick={() => trackShare("whatsapp", "invite", profile.username)} |
| 96 | + > |
| 97 | + <MessageCircle aria-hidden /> WhatsApp |
| 98 | + </a> |
| 99 | + </Button> |
| 100 | + <Button variant="secondary" size="sm" onClick={share}> |
| 101 | + <Share2 aria-hidden /> Share |
| 102 | + </Button> |
| 103 | + <Button variant="secondary" size="sm" onClick={() => setQrOpen(true)}> |
| 104 | + <QrCode aria-hidden /> QR code |
| 105 | + </Button> |
| 106 | + <Button asChild variant="outline" size="sm"> |
| 107 | + <Link href="/invites"> |
| 108 | + <Trophy aria-hidden /> Top inviters |
| 109 | + </Link> |
| 110 | + </Button> |
| 111 | + </div> |
| 112 | + |
| 113 | + <BrandedQrModal |
| 114 | + isOpen={qrOpen} |
| 115 | + onClose={() => setQrOpen(false)} |
| 116 | + title={`Join ${profile.displayName} on CampusLoop`} |
| 117 | + subtitle={profile.institution?.name ?? "Your campus. Only your campus."} |
| 118 | + shortUrl={link} |
| 119 | + category="profile" |
| 120 | + avatarUrl={profile.avatarUrl} |
| 121 | + /> |
| 122 | + </section> |
| 123 | + ); |
| 124 | +} |
0 commit comments