Skip to content

Commit 9ed62a6

Browse files
committed
fix: tell user when they're on their own referral page
1 parent 887d5dc commit 9ed62a6

File tree

3 files changed

+113
-39
lines changed

3 files changed

+113
-39
lines changed

web/src/app/referrals/[code]/page.tsx

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { authOptions } from '../../api/auth/[...nextauth]/auth-options'
1010
import CardWithBeams from '@/components/card-with-beams'
1111
import { Button } from '@/components/ui/button'
1212
import { ReferralClientWrapper } from '@/components/referral/referral-client-wrapper'
13+
import { GitHubSignInButton } from '@/components/referral/github-signin-button'
1314

1415
export const generateMetadata = async ({
1516
params,
@@ -78,29 +79,6 @@ export default async function ReferralPage({
7879
)
7980
}
8081

81-
// If it's the same user trying to use their own referral code (only check if logged in)
82-
if (session && referralData.isSameUser) {
83-
return (
84-
<CardWithBeams
85-
title="That's Your Own Referral Code!"
86-
description="You can't use your own referral code to get bonus credits."
87-
content={
88-
<>
89-
<p className="text-center text-muted-foreground">
90-
Share your referral link with friends to earn credits when they
91-
sign up!
92-
</p>
93-
<div className="flex justify-center mt-4">
94-
<Button asChild>
95-
<Link href="/profile?tab=referrals">View Your Referrals</Link>
96-
</Button>
97-
</div>
98-
</>
99-
}
100-
/>
101-
)
102-
}
103-
10482
// If referrer has maxed out referrals
10583
if (referralData.status.reason) {
10684
return (
@@ -142,26 +120,35 @@ export default async function ReferralPage({
142120
Welcome! Complete your referral to get bonus credits.
143121
</p>
144122

145-
<div className="flex flex-col sm:flex-row gap-3 justify-center">
146-
{session ? (
147-
<Button asChild size="lg">
123+
<div className="flex justify-center">
124+
{!session && (
125+
<GitHubSignInButton
126+
referralCode={code}
127+
referrerName={referrerName}
128+
/>
129+
)}
130+
</div>
131+
132+
{/* Complete Referral button positioned at bottom right for logged-in users */}
133+
{session && !referralData.isSameUser && (
134+
<div className="flex justify-end mt-6">
135+
<Button asChild>
148136
<Link href={`/onboard?referral_code=${code}`}>
149137
Complete Referral
150138
</Link>
151139
</Button>
152-
) : (
153-
<Button asChild size="lg">
154-
<Link
155-
href={`/login?referral=${code}${referrerName ? `&referrer=${encodeURIComponent(referrerName)}` : ''}`}
156-
>
157-
Sign Up & Get Bonus Credits
158-
</Link>
159-
</Button>
160-
)}
161-
<Button variant="outline" asChild size="lg">
162-
<Link href="/">Learn More</Link>
163-
</Button>
164-
</div>
140+
</div>
141+
)}
142+
143+
{/* Show warning if user is viewing their own referral link */}
144+
{session && referralData.isSameUser && (
145+
<div className="mt-6 p-3 bg-red-800 border border-red-900 rounded-md">
146+
<p className="text-white text-sm font-medium">
147+
⚠️ This is your own referral link. You will not be able to
148+
redeem your own code.
149+
</p>
150+
</div>
151+
)}
165152
</div>
166153
</>
167154
}

web/src/components/navbar/user-dropdown.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ export const UserDropdown = ({ session: { user } }: { session: Session }) => {
4848
<DropdownMenuItem onClick={() => router.push('/profile?tab=api-keys')}>
4949
<Key className="mr-2 size-4" /> <span>API Keys</span>
5050
</DropdownMenuItem>
51+
<DropdownMenuItem onClick={() => router.push('/profile?tab=referrals')}>
52+
<Gift className="mr-2 size-4" /> <span>Refer Friends</span>
53+
</DropdownMenuItem>
5154
<DropdownMenuSeparator />
5255
<DropdownMenuItem
5356
onClick={() => {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
'use client'
2+
3+
import { signIn } from 'next-auth/react'
4+
import { useTransition } from 'react'
5+
import posthog from 'posthog-js'
6+
7+
import { Button } from '@/components/ui/button'
8+
import { Icons } from '@/components/icons'
9+
import { toast } from '@/components/ui/use-toast'
10+
import { sleep } from '@codebuff/common/util/promise'
11+
12+
interface GitHubSignInButtonProps {
13+
referralCode: string
14+
referrerName?: string
15+
}
16+
17+
export const GitHubSignInButton = ({
18+
referralCode,
19+
referrerName,
20+
}: GitHubSignInButtonProps) => {
21+
const [isPending, startTransition] = useTransition()
22+
23+
const handleSignIn = () => {
24+
startTransition(async () => {
25+
// Store referral code in localStorage for fallback
26+
localStorage.setItem('referral_code', referralCode)
27+
if (referrerName) {
28+
localStorage.setItem('referrer_name', referrerName)
29+
}
30+
31+
// Create callback URL that includes referral information
32+
const callbackUrl = `${window.location.origin}/onboard?referral_code=${referralCode}${referrerName ? `&referrer=${encodeURIComponent(referrerName)}` : ''}`
33+
34+
console.log('🔵 GitHubSignInButton: Starting GitHub sign-in', {
35+
referralCode,
36+
referrerName,
37+
callbackUrl,
38+
})
39+
40+
posthog.capture('auth.referral_github_login_started', {
41+
referralCode,
42+
referrerName,
43+
callbackUrl,
44+
})
45+
46+
try {
47+
const result = await signIn('github', { callbackUrl })
48+
console.log('🔵 GitHubSignInButton: signIn result:', result)
49+
} catch (error) {
50+
console.error('🔵 GitHubSignInButton: signIn failed:', error)
51+
toast({
52+
title: 'Sign in failed',
53+
description:
54+
'Please try again or contact support if the problem persists.',
55+
})
56+
return
57+
}
58+
59+
await sleep(10000).then(() => {
60+
toast({
61+
title: 'Uh-oh this is taking a while...',
62+
description: 'Would you mind trying again?',
63+
})
64+
})
65+
})
66+
}
67+
68+
return (
69+
<Button
70+
onClick={handleSignIn}
71+
disabled={isPending}
72+
size="lg"
73+
className="flex items-center gap-2"
74+
>
75+
{isPending && <Icons.loader className="mr-2 size-4 animate-spin" />}
76+
<img
77+
src="https://s2.googleusercontent.com/s2/favicons?domain=github.com"
78+
className="rounded-full w-4 h-4"
79+
alt="GitHub logo"
80+
/>
81+
Login with GitHub
82+
</Button>
83+
)
84+
}

0 commit comments

Comments
 (0)