GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Refactor PremiumContent component to improve loading and access checks · aidrivencoder/master-git-github@f215437

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src/components
    • premium
      • PremiumContent.tsx
    • ui
      • LoadingSpinner.tsx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/components/premium/PremiumContent.tsx‎

Copy file name to clipboardExpand all lines: src/components/premium/PremiumContent.tsx
+53-27Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,65 +4,91 @@ import { useEffect, useState } from 'react'
44
import { useAuth } from '../providers/AuthProvider'
55
import { checkPremiumAccess } from '../../lib/firebase/user'
66
import Link from 'next/link'
7+
import { LoadingSpinner } from '../ui/LoadingSpinner'
78

89
interface PremiumContentProps {
910
children: React.ReactNode
1011
}
1112

1213
export function PremiumContent({ children }: PremiumContentProps) {
13-
const { user } = useAuth()
14+
const { user, loading: authLoading } = useAuth()
1415
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
1519

1620
useEffect(() => {
17-
let mounted = true;
21+
let mounted = true
22+
let timeoutId: NodeJS.Timeout
1823

1924
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) {
3330
if (mounted) {
3431
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+
}
3554
}
3655
}
3756
}
3857

3958
checkAccess()
4059

41-
// Set up an interval to periodically check access
42-
const intervalId = setInterval(checkAccess, 5000)
43-
4460
return () => {
4561
mounted = false
46-
clearInterval(intervalId)
62+
if (timeoutId) clearTimeout(timeoutId)
4763
}
48-
}, [user])
64+
}, [user, authLoading, checkAttempts])
4965

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+
)
5277
}
5378

54-
if (!hasPremiumAccess) {
79+
// Only show upgrade UI when we're certain user is not premium
80+
if (hasPremiumAccess === false) {
5581
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">
5884
Premium Content
5985
</h3>
60-
<p className="text-yellow-700 mb-4">
86+
<p className="text-yellow-700 dark:text-yellow-300 mb-4">
6187
This content is available exclusively to premium subscribers.
6288
</p>
6389
<Link
6490
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"
6692
>
6793
Upgrade to Premium
6894
</Link>
Collapse file

‎src/components/ui/LoadingSpinner.tsx‎

Copy file name to clipboard
+31-8Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
'use client'
22

33
interface LoadingSpinnerProps {
4-
size?: 'sm' | 'md' | 'lg'
4+
size?: 'xs' | 'sm' | 'md' | 'lg' | 'xl'
5+
label?: string
6+
className?: string
7+
color?: 'primary' | 'secondary' | 'accent'
58
}
69

7-
export function LoadingSpinner({ size = 'md' }: LoadingSpinnerProps) {
10+
export function LoadingSpinner({
11+
size = 'md',
12+
label,
13+
className = '',
14+
color = 'primary'
15+
}: LoadingSpinnerProps) {
816
const sizeClasses = {
9-
sm: 'w-4 h-4',
10-
md: 'w-8 h-8',
11-
lg: 'w-12 h-12'
17+
xs: 'w-3 h-3 border-2',
18+
sm: 'w-4 h-4 border-2',
19+
md: 'w-8 h-8 border-3',
20+
lg: 'w-12 h-12 border-4',
21+
xl: 'w-16 h-16 border-4'
22+
}
23+
24+
const colorClasses = {
25+
primary: 'border-primary-200 border-t-primary-600 dark:border-primary-800 dark:border-t-primary-400',
26+
secondary: 'border-gray-200 border-t-gray-600 dark:border-gray-700 dark:border-t-gray-300',
27+
accent: 'border-yellow-200 border-t-yellow-600 dark:border-yellow-800 dark:border-t-yellow-400'
1228
}
1329

1430
return (
15-
<div className="flex justify-center">
31+
<div className={`flex flex-col items-center gap-2 ${className}`}>
1632
<div
17-
className={`${sizeClasses[size]} border-4 border-primary-200 border-t-primary-600 rounded-full animate-spin dark:border-primary-800 dark:border-t-primary-400`}
33+
className={`${sizeClasses[size]} ${colorClasses[color]} rounded-full animate-spin`}
34+
role="status"
35+
aria-label={label || 'Loading'}
1836
/>
37+
{label && (
38+
<span className="text-sm text-gray-600 dark:text-gray-400">
39+
{label}
40+
</span>
41+
)}
1942
</div>
2043
)
21-
}
44+
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.