|
| 1 | +import { env } from '@codebuff/common/env' |
| 2 | +import { headers } from 'next/headers' |
| 3 | +import Link from 'next/link' |
| 4 | + |
| 5 | +import type { ReferralCodeResponse } from '../../api/referrals/[code]/route' |
| 6 | +import type { Metadata } from 'next' |
| 7 | + |
| 8 | +import CardWithBeams from '@/components/card-with-beams' |
| 9 | +import { Button } from '@/components/ui/button' |
| 10 | +import { InstallInstructions } from '@/components/ui/install-instructions' |
| 11 | + |
| 12 | +export const generateMetadata = async ({ |
| 13 | + searchParams, |
| 14 | +}: { |
| 15 | + params: Promise<{ code: string }> |
| 16 | + searchParams: Promise<{ referrer?: string }> |
| 17 | +}): Promise<Metadata> => { |
| 18 | + const resolvedSearchParams = await searchParams |
| 19 | + const referrerName = resolvedSearchParams.referrer |
| 20 | + const title = referrerName |
| 21 | + ? `${referrerName} invited you to Codebuff!` |
| 22 | + : 'You were invited to Codebuff!' |
| 23 | + |
| 24 | + return { |
| 25 | + title, |
| 26 | + description: 'Install Codebuff and start building with AI in your terminal.', |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +export default async function ReferralPage({ |
| 31 | + params, |
| 32 | + searchParams, |
| 33 | +}: { |
| 34 | + params: Promise<{ code: string }> |
| 35 | + searchParams: Promise<{ referrer?: string }> |
| 36 | +}) { |
| 37 | + const { code } = await params |
| 38 | + const resolvedSearchParams = await searchParams |
| 39 | + const referrerParam = resolvedSearchParams.referrer |
| 40 | + |
| 41 | + let referrerName: string | null = null |
| 42 | + try { |
| 43 | + const baseUrl = env.NEXT_PUBLIC_CODEBUFF_APP_URL || 'http://localhost:3000' |
| 44 | + const headerList = await headers() |
| 45 | + const cookie = headerList.get('Cookie') ?? '' |
| 46 | + const response = await fetch(`${baseUrl}/api/referrals/${code}`, { |
| 47 | + headers: { Cookie: cookie }, |
| 48 | + }) |
| 49 | + |
| 50 | + if (!response.ok) { |
| 51 | + throw new Error('Failed to fetch referral data') |
| 52 | + } |
| 53 | + |
| 54 | + const referralData: ReferralCodeResponse = await response.json() |
| 55 | + referrerName = referralData.referrerName |
| 56 | + } catch { |
| 57 | + return ( |
| 58 | + <CardWithBeams |
| 59 | + title="Invalid Referral Link" |
| 60 | + description="This referral link is not valid or has expired." |
| 61 | + content={ |
| 62 | + <> |
| 63 | + <p className="text-center text-muted-foreground"> |
| 64 | + Please double-check the link you used or contact the person who |
| 65 | + shared it. |
| 66 | + </p> |
| 67 | + <div className="flex justify-center mt-4"> |
| 68 | + <Button asChild> |
| 69 | + <Link href="/">Go to Homepage</Link> |
| 70 | + </Button> |
| 71 | + </div> |
| 72 | + </> |
| 73 | + } |
| 74 | + /> |
| 75 | + ) |
| 76 | + } |
| 77 | + |
| 78 | + const displayName = referrerName || referrerParam || 'Someone' |
| 79 | + |
| 80 | + return ( |
| 81 | + <CardWithBeams |
| 82 | + title={`${displayName} invited you to Codebuff!`} |
| 83 | + description="Install Codebuff and start building with AI in your terminal." |
| 84 | + content={<InstallInstructions />} |
| 85 | + /> |
| 86 | + ) |
| 87 | +} |
0 commit comments