import { Suspense } from 'react'; import { cookies } from 'next/headers'; import { redirect } from 'next/navigation'; import Dashboard from "@/components/dashboard/dashboard"; import AnalyticsDashboard from '@/components/analytics/AnalyticsDashboard'; import StoreSelector from '@/components/analytics/StoreSelector'; import { getAnalyticsOverviewServer } from '@/lib/server-api'; import { fetchServer } from '@/lib/api'; import { performance } from 'perf_hooks'; import { Info, GitCommit, User, Zap, BarChart3 } from 'lucide-react'; import packageJson from '../../../package.json'; import { getGitInfo } from '@/lib/utils/git'; // Vendor type for consistency interface Vendor { _id: string; username: string; storeId: string; pgpKey: string; __v: number; } interface User { vendor: Vendor; } export default async function AnalyticsPage({ searchParams, }: { searchParams: Promise<{ storeId?: string }>; }) { const startTime = performance.now(); // Await searchParams as required by Next.js 15+ const resolvedSearchParams = await searchParams; // Check for storeId in query parameters (for staff users) const storeId = resolvedSearchParams?.storeId; // Check for storeId in cookies (alternative method for staff users) const cookieStore = await cookies(); const cookieStoreId = cookieStore.get('storeId')?.value; // Use query parameter first, then cookie, then undefined (for vendors) const finalStoreId = storeId || cookieStoreId; try { // Fetch user data and analytics data in parallel const [userResponse, initialData] = await Promise.all([ fetchServer("/auth/me"), getAnalyticsOverviewServer(finalStoreId) ]); // Get git info using the utility const gitInfo = getGitInfo(); const endTime = performance.now(); const generationTime = (endTime - startTime).toFixed(2); const panelVersion = packageJson.version; const commitHash = gitInfo.hash; const vendor = userResponse.vendor; return (
{/* Analytics Content */}

Loading analytics...

}>
v{panelVersion}
{vendor.username}
{commitHash}
Generated in {generationTime}ms
{process.env.NODE_ENV || 'development'}
); } catch (error) { console.error('Error fetching analytics data:', error); // If it's a 401/403 error, redirect to login if (error instanceof Error && error.message.includes('401')) { redirect('/login'); } // If it's a 400 error (missing storeId for staff), show store selector if (error instanceof Error && error.message.includes('400')) { return (

Analytics Dashboard

Please select a store to view analytics data.

); } // For other errors, show a fallback return (

Analytics Dashboard

Unable to load analytics data. Please try refreshing the page.

{finalStoreId && (

Accessing store: {finalStoreId}

)}
); } }