@@ -4,65 +4,91 @@ import { useEffect, useState } from 'react' |
4 | 4 | import { useAuth } from '../providers/AuthProvider' |
5 | 5 | import { checkPremiumAccess } from '../../lib/firebase/user' |
6 | 6 | import Link from 'next/link' |
| 7 | +import { LoadingSpinner } from '../ui/LoadingSpinner' |
7 | 8 |
|
8 | 9 | interface PremiumContentProps { |
9 | 10 | children: React.ReactNode |
10 | 11 | } |
11 | 12 |
|
12 | 13 | export function PremiumContent({ children }: PremiumContentProps) { |
13 | | - const { user } = useAuth() |
| 14 | + const { user, loading: authLoading } = useAuth() |
14 | 15 | const [hasPremiumAccess, setHasPremiumAccess] = useState<boolean | null>(null) |
| 16 | + const [isLoading, setIsLoading] = useState(true) |
| 17 | + const [checkAttempts, setCheckAttempts] = useState(0) |
| 18 | + const MAX_CHECK_ATTEMPTS = 3 |
15 | 19 |
|
16 | 20 | useEffect(() => { |
17 | | - let mounted = true; |
| 21 | + let mounted = true |
| 22 | + let timeoutId: NodeJS.Timeout |
18 | 23 |
|
19 | 24 | async function checkAccess() { |
20 | | - if (user) { |
21 | | - try { |
22 | | - const hasAccess = await checkPremiumAccess(user.uid) |
23 | | - if (mounted) { |
24 | | - setHasPremiumAccess(hasAccess) |
25 | | - } |
26 | | - } catch (error) { |
27 | | - console.error('Error checking premium access:', error) |
28 | | - if (mounted) { |
29 | | - setHasPremiumAccess(false) |
30 | | - } |
31 | | - } |
32 | | - } else { |
| 25 | + if (authLoading) { |
| 26 | + return // Wait for auth to complete |
| 27 | + } |
| 28 | + |
| 29 | + if (!user) { |
33 | 30 | if (mounted) { |
34 | 31 | setHasPremiumAccess(false) |
| 32 | + setIsLoading(false) |
| 33 | + } |
| 34 | + return |
| 35 | + } |
| 36 | + |
| 37 | + try { |
| 38 | + const hasAccess = await checkPremiumAccess(user.uid) |
| 39 | + if (mounted) { |
| 40 | + setHasPremiumAccess(hasAccess) |
| 41 | + setIsLoading(false) |
| 42 | + } |
| 43 | + } catch (error) { |
| 44 | + console.error('Error checking premium access:', error) |
| 45 | + if (mounted) { |
| 46 | + if (checkAttempts >= MAX_CHECK_ATTEMPTS) { |
| 47 | + setHasPremiumAccess(false) |
| 48 | + setIsLoading(false) |
| 49 | + } else { |
| 50 | + timeoutId = setTimeout(() => { |
| 51 | + setCheckAttempts(prev => prev + 1) |
| 52 | + }, Math.min(1000 * Math.pow(2, checkAttempts), 5000)) |
| 53 | + } |
35 | 54 | } |
36 | 55 | } |
37 | 56 | } |
38 | 57 |
|
39 | 58 | checkAccess() |
40 | 59 |
|
41 | | - // Set up an interval to periodically check access |
42 | | - const intervalId = setInterval(checkAccess, 5000) |
43 | | - |
44 | 60 | return () => { |
45 | 61 | mounted = false |
46 | | - clearInterval(intervalId) |
| 62 | + if (timeoutId) clearTimeout(timeoutId) |
47 | 63 | } |
48 | | - }, [user]) |
| 64 | + }, [user, authLoading, checkAttempts]) |
49 | 65 |
|
50 | | - if (hasPremiumAccess === null) { |
51 | | - return <div className="animate-pulse">Loading...</div> |
| 66 | + // Show loading state while either auth is loading or premium check is in progress |
| 67 | + if (authLoading || isLoading) { |
| 68 | + return ( |
| 69 | + <div className="flex justify-center items-center p-8"> |
| 70 | + <LoadingSpinner |
| 71 | + size="lg" |
| 72 | + label="Loading premium content..." |
| 73 | + color="primary" |
| 74 | + /> |
| 75 | + </div> |
| 76 | + ) |
52 | 77 | } |
53 | 78 |
|
54 | | - if (!hasPremiumAccess) { |
| 79 | + // Only show upgrade UI when we're certain user is not premium |
| 80 | + if (hasPremiumAccess === false) { |
55 | 81 | return ( |
56 | | - <div className="p-6 bg-yellow-50 border border-yellow-200 rounded-lg"> |
57 | | - <h3 className="text-lg font-semibold text-yellow-800 mb-2"> |
| 82 | + <div className="p-6 bg-yellow-50 dark:bg-yellow-900/20 border border-yellow-200 dark:border-yellow-800 rounded-lg shadow-sm"> |
| 83 | + <h3 className="text-lg font-semibold text-yellow-800 dark:text-yellow-200 mb-2"> |
58 | 84 | Premium Content |
59 | 85 | </h3> |
60 | | - <p className="text-yellow-700 mb-4"> |
| 86 | + <p className="text-yellow-700 dark:text-yellow-300 mb-4"> |
61 | 87 | This content is available exclusively to premium subscribers. |
62 | 88 | </p> |
63 | 89 | <Link |
64 | 90 | href="/pricing" |
65 | | - className="inline-block px-4 py-2 bg-yellow-600 text-white rounded hover:bg-yellow-700 transition-colors" |
| 91 | + className="inline-block px-4 py-2 bg-yellow-600 text-white rounded-md hover:bg-yellow-700 transition-colors duration-200 focus:outline-none focus:ring-2 focus:ring-yellow-500 focus:ring-offset-2 dark:focus:ring-offset-gray-900" |
66 | 92 | > |
67 | 93 | Upgrade to Premium |
68 | 94 | </Link> |
|