suna/frontend/src/app/layout.tsx

77 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-03-30 14:48:57 +08:00
'use client';
import { useEffect, useState } from 'react';
import './globals.css';
import { Inter } from 'next/font/google';
import { MainNav } from '@/components/layout/main-nav';
import { AuthProvider } from '@/context/auth-context';
import { Toaster } from 'sonner';
import { toast } from 'sonner';
2025-03-31 06:22:00 +08:00
import { createClient } from '@/utils/supabase/client';
2025-03-30 14:48:57 +08:00
const inter = Inter({ subsets: ['latin'] });
export default function RootLayout({
children,
}: {
children: React.ReactNode;
}) {
const [apiConnected, setApiConnected] = useState<boolean | null>(null);
useEffect(() => {
async function checkApiConnection() {
try {
2025-03-31 06:22:00 +08:00
const supabase = createClient();
const { data: { session } } = await supabase.auth.getSession();
const token = session?.access_token;
2025-03-30 14:48:57 +08:00
const headers: HeadersInit = {
'Content-Type': 'application/json',
};
// Add auth token if available
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
// Try to access an authenticated endpoint
const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/health-check`, {
method: 'GET',
headers,
});
setApiConnected(response.ok);
if (!response.ok) {
console.error('Backend API is not responding:', response.status, response.statusText);
toast.error('Cannot connect to backend API. Some features may not work.');
}
} catch (error) {
console.error('Error connecting to backend API:', error);
setApiConnected(false);
toast.error('Cannot connect to backend API. Some features may not work.');
}
}
checkApiConnection();
}, []);
return (
2025-03-31 06:55:26 +08:00
<html lang="en" className="h-full">
<body className={`${inter.className} h-full flex flex-col`}>
2025-03-30 14:48:57 +08:00
<AuthProvider>
<MainNav />
{apiConnected === false && (
2025-03-31 06:55:26 +08:00
<div className="bg-yellow-100 border-l-4 border-yellow-500 text-yellow-700 p-2 text-sm" role="alert">
2025-03-30 14:48:57 +08:00
<p className="font-bold">Warning</p>
<p>Could not connect to backend API. Agent and thread features may not work properly.</p>
</div>
)}
2025-03-31 06:55:26 +08:00
<main className="flex-1 overflow-hidden">{children}</main>
2025-03-30 14:48:57 +08:00
<Toaster position="top-right" />
</AuthProvider>
</body>
</html>
);
}