|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { DubEmbed } from '@dub/embed-react'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | + |
| 6 | +export const DubReferral = () => { |
| 7 | + const [publicToken, setPublicToken] = useState(''); |
| 8 | + const [error, setError] = useState<string | null>(null); |
| 9 | + const [isLoading, setIsLoading] = useState(true); |
| 10 | + |
| 11 | + useEffect(() => { |
| 12 | + const fetchPublicToken = async () => { |
| 13 | + try { |
| 14 | + setIsLoading(true); |
| 15 | + setError(null); |
| 16 | + |
| 17 | + const response = await fetch('/api/dub/embed-token'); |
| 18 | + |
| 19 | + if (!response.ok) { |
| 20 | + throw new Error(`Failed to fetch token: ${response.status} ${response.statusText}`); |
| 21 | + } |
| 22 | + |
| 23 | + const data = await response.json(); |
| 24 | + |
| 25 | + if (!data.publicToken) { |
| 26 | + throw new Error('No public token received from server'); |
| 27 | + } |
| 28 | + |
| 29 | + setPublicToken(data.publicToken); |
| 30 | + } catch (err) { |
| 31 | + console.error('Error fetching public token:', err); |
| 32 | + setError(err instanceof Error ? err.message : 'An unexpected error occurred'); |
| 33 | + } finally { |
| 34 | + setIsLoading(false); |
| 35 | + } |
| 36 | + }; |
| 37 | + |
| 38 | + fetchPublicToken(); |
| 39 | + }, []); |
| 40 | + |
| 41 | + if (isLoading) { |
| 42 | + return <div>Loading...</div>; |
| 43 | + } |
| 44 | + |
| 45 | + if (error) { |
| 46 | + return ( |
| 47 | + <div className="flex min-h-[400px] items-center justify-center"> |
| 48 | + <div className="w-full max-w-md rounded-md border border-destructive/50 bg-destructive/10 p-6 text-center"> |
| 49 | + <h3 className="mb-2 text-sm font-medium text-destructive">Oops, something went wrong</h3> |
| 50 | + <p className="text-xs text-destructive/80">{error}</p> |
| 51 | + <p className="mt-4 text-xs text-muted-foreground"> |
| 52 | + Please refresh the page and try again. If the problem persists,{' '} |
| 53 | + <a |
| 54 | + href="https://discord.gg/compai" |
| 55 | + target="_blank" |
| 56 | + rel="noopener noreferrer" |
| 57 | + className="underline underline-offset-2 hover:text-foreground transition-colors" |
| 58 | + > |
| 59 | + contact us |
| 60 | + </a> |
| 61 | + . |
| 62 | + </p> |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + if (!publicToken) { |
| 69 | + return ( |
| 70 | + <div className="rounded-md border border-muted bg-muted/50 p-4"> |
| 71 | + <p className="text-sm text-muted-foreground"> |
| 72 | + No token available. Please try refreshing the page. |
| 73 | + </p> |
| 74 | + </div> |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + return <DubEmbed data="referrals" token={publicToken} />; |
| 79 | +}; |
0 commit comments