2026-04-23 15:33:11 +03:30
|
|
|
'use client';
|
|
|
|
|
|
2026-04-29 21:55:46 +03:30
|
|
|
import { memo, useEffect } from 'react';
|
2026-04-23 15:33:11 +03:30
|
|
|
import { useRouter } from 'next/navigation';
|
|
|
|
|
import { useAuth } from '@/lib/hooks/useAuth';
|
|
|
|
|
import Sidebar from '@/components/ui/Sidebar';
|
2026-04-29 01:22:53 +03:30
|
|
|
import { ThemeToggle } from '@/components/ui/ThemeToggle';
|
2026-04-29 21:55:46 +03:30
|
|
|
import { DashboardAccountMenu } from '@/components/ui/DashboardAccountMenu';
|
2026-04-23 15:33:11 +03:30
|
|
|
|
|
|
|
|
export default function DashboardLayout({ children }: { children: React.ReactNode }) {
|
2026-04-29 21:55:46 +03:30
|
|
|
const { user, currentOrganization, isAuthReady } = useAuth();
|
2026-04-23 15:33:11 +03:30
|
|
|
const router = useRouter();
|
|
|
|
|
|
|
|
|
|
// ✅ AUTH GUARD (runs once per navigation group)
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!isAuthReady) return;
|
|
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
|
router.replace('/login');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!currentOrganization) {
|
|
|
|
|
router.replace('/select-organization');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}, [isAuthReady, user, currentOrganization, router]);
|
|
|
|
|
|
|
|
|
|
// ✅ LOADING ONLY FOR INITIAL LOAD
|
|
|
|
|
if (!isAuthReady) {
|
|
|
|
|
return (
|
2026-04-29 01:22:53 +03:30
|
|
|
<div className="h-screen flex items-center justify-center app-web-bg">
|
2026-04-23 15:33:11 +03:30
|
|
|
Loading app...
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!user || !currentOrganization) {
|
|
|
|
|
return (
|
2026-04-29 01:22:53 +03:30
|
|
|
<div className="h-screen flex items-center justify-center app-web-bg">
|
2026-04-23 15:33:11 +03:30
|
|
|
Loading workspace...
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
2026-04-29 01:22:53 +03:30
|
|
|
<div className="flex h-screen app-web-bg text-text-primary">
|
2026-04-23 15:33:11 +03:30
|
|
|
<Sidebar />
|
|
|
|
|
|
|
|
|
|
<div className="flex-1 flex flex-col">
|
2026-04-29 21:55:46 +03:30
|
|
|
<DashboardHeader organizationName={currentOrganization.name} />
|
2026-04-23 15:33:11 +03:30
|
|
|
|
|
|
|
|
<main className="p-6 flex-1 overflow-y-auto">
|
2026-04-29 01:22:53 +03:30
|
|
|
<div className="surface-panel p-6 min-h-full">
|
|
|
|
|
{children}
|
|
|
|
|
</div>
|
2026-04-23 15:33:11 +03:30
|
|
|
</main>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
2026-04-29 14:02:42 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const DashboardHeader = memo(function DashboardHeader({
|
|
|
|
|
organizationName,
|
|
|
|
|
}: {
|
|
|
|
|
organizationName: string;
|
|
|
|
|
}) {
|
|
|
|
|
return (
|
2026-04-29 21:55:46 +03:30
|
|
|
<header className="h-[71px] flex justify-between items-center gap-4 px-6 border-b border-border/70 backdrop-blur-sm">
|
|
|
|
|
<h2 className="text-lg font-medium truncate min-w-0">{organizationName}</h2>
|
2026-04-29 14:02:42 +03:30
|
|
|
|
2026-04-29 21:55:46 +03:30
|
|
|
<div className="flex items-center gap-3 shrink-0">
|
2026-04-29 14:02:42 +03:30
|
|
|
<ThemeToggle />
|
2026-04-29 21:55:46 +03:30
|
|
|
<DashboardAccountMenu />
|
2026-04-29 14:02:42 +03:30
|
|
|
</div>
|
|
|
|
|
</header>
|
|
|
|
|
);
|
|
|
|
|
});
|