|
| 1 | +import { CREDITS_REFERRAL_BONUS } from '@codebuff/common/old-constants' |
1 | 2 | import { WEBSITE_URL } from '@codebuff/sdk' |
2 | | -import React from 'react' |
| 3 | +import { useQuery } from '@tanstack/react-query' |
| 4 | +import React, { useState } from 'react' |
3 | 5 |
|
4 | 6 | import { BottomBanner } from './bottom-banner' |
| 7 | +import { Button } from './button' |
5 | 8 | import { useChatStore } from '../state/chat-store' |
| 9 | +import { useTheme } from '../hooks/use-theme' |
| 10 | +import { useTimeout } from '../hooks/use-timeout' |
| 11 | +import { getAuthToken } from '../utils/auth' |
| 12 | +import { getApiClient } from '../utils/codebuff-api' |
| 13 | +import { copyTextToClipboard } from '../utils/clipboard' |
| 14 | +import { BORDER_CHARS } from '../utils/ui-constants' |
| 15 | + |
| 16 | +interface ReferralData { |
| 17 | + referralCode: string |
| 18 | + referrals: { id: string }[] |
| 19 | + referralLimit: number |
| 20 | +} |
6 | 21 |
|
7 | 22 | export const ReferralBanner = () => { |
8 | 23 | const setInputMode = useChatStore((state) => state.setInputMode) |
| 24 | + const theme = useTheme() |
| 25 | + const [isHovered, setIsHovered] = useState(false) |
| 26 | + const [isCopied, setIsCopied] = useState(false) |
| 27 | + const { setTimeout } = useTimeout() |
| 28 | + const authToken = getAuthToken() |
| 29 | + |
| 30 | + const { data: referralData } = useQuery({ |
| 31 | + queryKey: ['referrals'], |
| 32 | + queryFn: async () => { |
| 33 | + const client = getApiClient() |
| 34 | + const response = await client.get<ReferralData>('/api/referrals', { |
| 35 | + includeCookie: true, |
| 36 | + }) |
| 37 | + if (!response.ok) { |
| 38 | + throw new Error(`Failed to fetch referral data: ${response.status}`) |
| 39 | + } |
| 40 | + return response.data! |
| 41 | + }, |
| 42 | + enabled: !!authToken, |
| 43 | + staleTime: 5 * 60 * 1000, |
| 44 | + retry: false, |
| 45 | + }) |
| 46 | + |
| 47 | + const referralCode = referralData?.referralCode ?? null |
| 48 | + const referralLink = referralCode ? `${WEBSITE_URL}/referrals/${referralCode}` : null |
| 49 | + const referralCount = referralData?.referrals.length ?? null |
| 50 | + const referralLimit = referralData?.referralLimit ?? null |
9 | 51 |
|
10 | | - const referralUrl = `${WEBSITE_URL}/referrals` |
| 52 | + const handleCopy = async () => { |
| 53 | + if (!referralLink) return |
| 54 | + try { |
| 55 | + await copyTextToClipboard(referralLink, { suppressGlobalMessage: true }) |
| 56 | + setIsCopied(true) |
| 57 | + setTimeout('reset-copied', () => setIsCopied(false), 2000) |
| 58 | + } catch { |
| 59 | + // Error is already logged and displayed by copyTextToClipboard |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + const copyLabel = isCopied ? '✔ Copied!' : '⎘ Copy referral link' |
11 | 64 |
|
12 | 65 | return ( |
13 | 66 | <BottomBanner |
14 | | - borderColorKey="warning" |
15 | | - text={`Refer your friends: ${referralUrl}`} |
| 67 | + borderColorKey="primary" |
| 68 | + border={['top', 'bottom', 'left', 'right']} |
16 | 69 | onClose={() => setInputMode('default')} |
17 | | - /> |
| 70 | + > |
| 71 | + <box style={{ flexDirection: 'column', gap: 0, flexGrow: 1, marginRight: 3 }}> |
| 72 | + <text style={{ fg: theme.foreground }}> |
| 73 | + {`Share this link with friends and you'll both earn ${CREDITS_REFERRAL_BONUS} credits`} |
| 74 | + </text> |
| 75 | + |
| 76 | + {referralCount !== null && referralLimit !== null && ( |
| 77 | + <text style={{ fg: theme.muted }}> |
| 78 | + {`You've referred ${referralCount}/${referralLimit} people`} |
| 79 | + </text> |
| 80 | + )} |
| 81 | + |
| 82 | + {referralLink ? ( |
| 83 | + <box style={{ flexDirection: 'column', gap: 0 }}> |
| 84 | + <text style={{ fg: theme.muted }}>{referralLink}</text> |
| 85 | + <box style={{ flexDirection: 'row', paddingTop: 0 }}> |
| 86 | + <Button |
| 87 | + onClick={handleCopy} |
| 88 | + onMouseOver={() => setIsHovered(true)} |
| 89 | + onMouseOut={() => setIsHovered(false)} |
| 90 | + style={{ |
| 91 | + paddingLeft: 1, |
| 92 | + paddingRight: 1, |
| 93 | + borderStyle: 'single', |
| 94 | + borderColor: isCopied |
| 95 | + ? 'green' |
| 96 | + : isHovered |
| 97 | + ? theme.foreground |
| 98 | + : theme.primary, |
| 99 | + customBorderChars: BORDER_CHARS, |
| 100 | + }} |
| 101 | + > |
| 102 | + <text |
| 103 | + style={{ |
| 104 | + fg: isCopied |
| 105 | + ? 'green' |
| 106 | + : isHovered |
| 107 | + ? theme.foreground |
| 108 | + : theme.primary, |
| 109 | + }} |
| 110 | + > |
| 111 | + {copyLabel} |
| 112 | + </text> |
| 113 | + </Button> |
| 114 | + </box> |
| 115 | + </box> |
| 116 | + ) : ( |
| 117 | + <text style={{ fg: theme.muted }}>Loading referral link...</text> |
| 118 | + )} |
| 119 | + </box> |
| 120 | + </BottomBanner> |
18 | 121 | ) |
19 | 122 | } |
0 commit comments