Repository navigation
Expand file tree
/
Copy pathpage.js
More file actions
218 lines (188 loc) · 6.86 KB
/
Copy pathpage.js
File metadata and controls
218 lines (188 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
'use client';
import { useState, useEffect } from 'react';
import dynamic from 'next/dynamic';
import { supabase } from '@/lib/supabase';
import Navbar from '@/components/Navbar';
import Hero from '@/components/Hero';
import DigitalProducts from '@/components/DigitalProducts';
import ProjectStore from '@/components/ProjectStore';
import CustomServices from '@/components/CustomServices';
import Footer from '@/components/Footer';
import SmoothScroll from '@/components/SmoothScroll';
import FloatingContactButton from '@/components/FloatingContactButton';
import StarField from '@/components/StarField';
import { useProducts } from '@/hooks/useProducts';
const DemoModal = dynamic(() => import('@/components/DemoModal'));
const InquiryModal = dynamic(() => import('@/components/InquiryModal'));
const LoginScreen = dynamic(() => import('@/components/LoginScreen'));
export default function HomePage() {
const [session, setSession] = useState(null);
const [authLoading, setAuthLoading] = useState(true);
const { products, loading: productsLoading, error: productsError, refetch: refetchProducts } = useProducts();
const [demoProject, setDemoProject] = useState(null);
const [inquiryConfig, setInquiryConfig] = useState(null);
const [showLogin, setShowLogin] = useState(false);
const [loginMessage, setLoginMessage] = useState('');
// 0. Auto-redirect port 3000 -> port 8000 fallback
useEffect(() => {
if (typeof window !== 'undefined' && window.location.port === '3000') {
window.location.replace(window.location.href.replace(':3000', ':8000'));
}
const params = new URLSearchParams(window.location.search);
if (params.get('login') === '1') {
setShowLogin(true);
params.delete('login');
const query = params.toString();
window.history.replaceState({}, document.title, `${window.location.pathname}${query ? `?${query}` : ''}`);
}
}, []);
// Auth
useEffect(() => {
if (!supabase) {
setAuthLoading(false);
return undefined;
}
const handleInitialAuth = async () => {
if (typeof window !== 'undefined' && window.location.hash) {
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');
const refreshToken = params.get('refresh_token');
if (accessToken) {
try {
setAuthLoading(true);
const { data, error } = await supabase.auth.setSession({
access_token: accessToken,
refresh_token: refreshToken || ''
});
if (error) throw error;
if (data?.session) {
setSession(data.session);
setShowLogin(false);
}
} catch (err) {
console.error('Error parsing hash session:', err.message);
} finally {
window.history.replaceState(
{},
document.title,
window.location.pathname + window.location.search
);
}
}
}
const [{ data: { session: currentSession } }, { data: { user: authenticatedUser } }] = await Promise.all([
supabase.auth.getSession(),
supabase.auth.getUser(),
]);
setSession(authenticatedUser ? currentSession : null);
setAuthLoading(false);
};
handleInitialAuth();
const redirectSignedInClient = async (session) => {
const searchParams = new URLSearchParams(window.location.search);
if (searchParams.get('quick_contact') === 'verified') return;
const nextPath = searchParams.get('next');
if (nextPath?.startsWith('/') && !nextPath.startsWith('//')) {
window.location.assign(nextPath);
return;
}
const { data: clientAccount } = await supabase
.from('client_accounts')
.select('portal_enabled')
.eq('user_id', session.user.id)
.maybeSingle();
if (clientAccount?.portal_enabled) window.location.assign('/users');
};
const { data: { subscription } } = supabase.auth.onAuthStateChange((event, session) => {
setSession(session);
if (session) {
setShowLogin(false);
if (event === 'SIGNED_IN') {
setTimeout(() => { void redirectSignedInClient(session); }, 0);
}
}
setAuthLoading(false);
});
return () => subscription.unsubscribe();
}, []);
// Project inquiries are public; authentication is reserved for the user portal.
const handleInquiryRequest = (config) => {
setInquiryConfig(config);
};
// Auth Loading State
if (authLoading) {
return (
<div className="min-h-screen flex items-center justify-center bg-canvas">
<div className="flex flex-col items-center gap-4">
<div className="w-16 h-16 border border-slate-200 dark:border-white/10 bg-accent-soft animate-spin"></div>
<span className="text-xl font-bold text-foreground tracking-widest font-display">
LOADING
</span>
</div>
</div>
);
}
return (
<SmoothScroll>
<div className="relative min-h-screen overflow-hidden bg-canvas text-foreground font-sans antialiased selection:bg-accent-soft selection:text-foreground">
<StarField />
<div className="relative z-10">
{/* Header (Pass session state and trigger callbacks) */}
<Navbar
session={session}
onOpenLogin={() => setShowLogin(true)}
/>
{/* Hero Section */}
<Hero
onOpenDemo={setDemoProject}
onOpenInquiry={handleInquiryRequest}
/>
{/* BuildInByte-owned digital product catalog */}
<DigitalProducts />
{/* Software Inventory Grid */}
<ProjectStore
customProjects={products}
isLoading={productsLoading}
loadError={productsError}
onRetry={refetchProducts}
onOpenDemo={setDemoProject}
onOpenInquiry={handleInquiryRequest}
/>
{/* Custom Services & Student Collective Model */}
<CustomServices
onOpenInquiry={handleInquiryRequest}
/>
{/* Footer */}
<Footer />
{/* Modals */}
{demoProject && (
<DemoModal
project={demoProject}
onClose={() => setDemoProject(null)}
onOpenInquiry={handleInquiryRequest}
/>
)}
{inquiryConfig && (
<InquiryModal
config={inquiryConfig}
onClose={() => setInquiryConfig(null)}
/>
)}
{/* Overlay Login Panel */}
{showLogin && (
<LoginScreen
onClose={() => {
setShowLogin(false);
setLoginMessage('');
}}
message={loginMessage}
/>
)}
{/* Floating Contact Button */}
<FloatingContactButton />
</div>
</div>
</SmoothScroll>
);
}