2025-04-16 13:41:55 +08:00
|
|
|
"use client"
|
|
|
|
|
2025-04-23 08:25:44 +08:00
|
|
|
import { useEffect, useState } from "react"
|
2025-04-21 00:08:17 +08:00
|
|
|
import { SidebarLeft } from "@/components/sidebar/sidebar-left"
|
2025-04-16 02:14:58 +08:00
|
|
|
import {
|
|
|
|
SidebarInset,
|
|
|
|
SidebarProvider,
|
|
|
|
} from "@/components/ui/sidebar"
|
2025-04-27 01:51:25 +08:00
|
|
|
// import { PricingAlert } from "@/components/billing/pricing-alert"
|
2025-04-23 16:20:10 +08:00
|
|
|
import { MaintenanceAlert } from "@/components/maintenance-alert"
|
2025-04-24 02:28:31 +08:00
|
|
|
import { useAccounts } from "@/hooks/use-accounts"
|
2025-04-25 00:29:32 +08:00
|
|
|
import { useAuth } from "@/components/AuthProvider"
|
|
|
|
import { useRouter } from "next/navigation"
|
|
|
|
import { Loader2 } from "lucide-react"
|
2025-04-26 06:57:01 +08:00
|
|
|
import { checkApiHealth } from "@/lib/api"
|
|
|
|
import { MaintenancePage } from "@/components/maintenance/maintenance-page"
|
2025-05-04 05:16:16 +08:00
|
|
|
import { DeleteOperationProvider } from "@/contexts/DeleteOperationContext"
|
|
|
|
import { StatusOverlay } from "@/components/ui/status-overlay"
|
2025-04-16 02:37:56 +08:00
|
|
|
|
2025-04-16 02:14:58 +08:00
|
|
|
interface DashboardLayoutProps {
|
|
|
|
children: React.ReactNode
|
2025-04-13 00:37:45 +08:00
|
|
|
}
|
|
|
|
|
2025-04-16 13:41:55 +08:00
|
|
|
export default function DashboardLayout({
|
2025-04-13 00:37:45 +08:00
|
|
|
children,
|
2025-04-16 02:14:58 +08:00
|
|
|
}: DashboardLayoutProps) {
|
2025-04-27 01:51:25 +08:00
|
|
|
// const [showPricingAlert, setShowPricingAlert] = useState(false)
|
2025-04-23 08:25:44 +08:00
|
|
|
const [showMaintenanceAlert, setShowMaintenanceAlert] = useState(false)
|
2025-04-26 06:57:01 +08:00
|
|
|
const [isApiHealthy, setIsApiHealthy] = useState(true)
|
|
|
|
const [isCheckingHealth, setIsCheckingHealth] = useState(true)
|
2025-04-24 02:28:31 +08:00
|
|
|
const { data: accounts } = useAccounts()
|
|
|
|
const personalAccount = accounts?.find(account => account.personal_account)
|
2025-04-25 00:29:32 +08:00
|
|
|
const { user, isLoading } = useAuth()
|
|
|
|
const router = useRouter()
|
2025-04-23 08:25:44 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
2025-04-27 01:51:25 +08:00
|
|
|
// setShowPricingAlert(false)
|
2025-04-24 12:30:08 +08:00
|
|
|
setShowMaintenanceAlert(false)
|
2025-04-23 08:25:44 +08:00
|
|
|
}, [])
|
|
|
|
|
2025-04-26 06:57:01 +08:00
|
|
|
// Check API health
|
|
|
|
useEffect(() => {
|
|
|
|
const checkHealth = async () => {
|
|
|
|
try {
|
|
|
|
const health = await checkApiHealth()
|
|
|
|
setIsApiHealthy(health.status === 'ok')
|
|
|
|
} catch (error) {
|
|
|
|
console.error('API health check failed:', error)
|
|
|
|
setIsApiHealthy(false)
|
|
|
|
} finally {
|
|
|
|
setIsCheckingHealth(false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkHealth()
|
|
|
|
// Check health every 30 seconds
|
|
|
|
const interval = setInterval(checkHealth, 30000)
|
|
|
|
return () => clearInterval(interval)
|
|
|
|
}, [])
|
|
|
|
|
2025-04-25 00:29:32 +08:00
|
|
|
// Check authentication status
|
|
|
|
useEffect(() => {
|
|
|
|
if (!isLoading && !user) {
|
|
|
|
router.push('/auth')
|
|
|
|
}
|
|
|
|
}, [user, isLoading, router])
|
|
|
|
|
2025-04-26 06:57:01 +08:00
|
|
|
// Show loading state while checking auth or health
|
|
|
|
if (isLoading || isCheckingHealth) {
|
2025-04-25 00:29:32 +08:00
|
|
|
return (
|
|
|
|
<div className="flex items-center justify-center min-h-screen">
|
|
|
|
<Loader2 className="h-8 w-8 animate-spin text-primary" />
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't render anything if not authenticated
|
|
|
|
if (!user) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
2025-04-26 06:57:01 +08:00
|
|
|
// Show maintenance page if API is not healthy
|
|
|
|
if (!isApiHealthy) {
|
|
|
|
return <MaintenancePage />
|
|
|
|
}
|
|
|
|
|
2025-04-12 08:04:40 +08:00
|
|
|
return (
|
2025-05-04 05:16:16 +08:00
|
|
|
<DeleteOperationProvider>
|
|
|
|
<SidebarProvider>
|
|
|
|
<SidebarLeft />
|
|
|
|
<SidebarInset>
|
|
|
|
<div className="bg-background">
|
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
</SidebarInset>
|
|
|
|
|
|
|
|
{/* <PricingAlert
|
|
|
|
open={showPricingAlert}
|
|
|
|
onOpenChange={setShowPricingAlert}
|
|
|
|
closeable={false}
|
|
|
|
accountId={personalAccount?.account_id}
|
|
|
|
/> */}
|
|
|
|
|
|
|
|
<MaintenanceAlert
|
|
|
|
open={showMaintenanceAlert}
|
|
|
|
onOpenChange={setShowMaintenanceAlert}
|
|
|
|
closeable={true}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{/* Status overlay for deletion operations */}
|
|
|
|
<StatusOverlay />
|
|
|
|
</SidebarProvider>
|
|
|
|
</DeleteOperationProvider>
|
2025-04-16 02:14:58 +08:00
|
|
|
)
|
2025-04-25 00:29:32 +08:00
|
|
|
}
|