2025-06-09 15:50:15 -07:00
|
|
|
"use client";
|
|
|
|
|
|
2026-01-26 23:32:30 -08:00
|
|
|
import { useEffect, useState } from "react";
|
2026-03-31 18:39:45 -07:00
|
|
|
import { USER_QUERY_KEY } from "@/atoms/user/user-query.atoms";
|
2026-01-25 16:15:25 +05:30
|
|
|
import { useGlobalLoadingEffect } from "@/hooks/use-global-loading";
|
2026-04-07 03:10:06 -07:00
|
|
|
import { ensureTokensFromElectron, getBearerToken, redirectToLogin } from "@/lib/auth-utils";
|
2026-03-31 18:39:45 -07:00
|
|
|
import { queryClient } from "@/lib/query-client/client";
|
2025-06-09 15:50:15 -07:00
|
|
|
|
|
|
|
|
interface DashboardLayoutProps {
|
2025-07-27 10:05:37 -07:00
|
|
|
children: React.ReactNode;
|
2025-06-09 15:50:15 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function DashboardLayout({ children }: DashboardLayoutProps) {
|
2025-07-27 10:05:37 -07:00
|
|
|
const [isCheckingAuth, setIsCheckingAuth] = useState(true);
|
2025-06-09 15:50:15 -07:00
|
|
|
|
2026-01-25 16:15:25 +05:30
|
|
|
// Use the global loading screen - spinner animation won't reset
|
2026-01-27 15:28:30 +05:30
|
|
|
useGlobalLoadingEffect(isCheckingAuth);
|
2026-01-25 16:15:25 +05:30
|
|
|
|
2025-07-27 10:05:37 -07:00
|
|
|
useEffect(() => {
|
2026-04-06 23:02:25 -07:00
|
|
|
async function checkAuth() {
|
|
|
|
|
let token = getBearerToken();
|
|
|
|
|
if (!token) {
|
|
|
|
|
const synced = await ensureTokensFromElectron();
|
|
|
|
|
if (synced) token = getBearerToken();
|
|
|
|
|
}
|
|
|
|
|
if (!token) {
|
|
|
|
|
redirectToLogin();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
queryClient.invalidateQueries({ queryKey: [...USER_QUERY_KEY] });
|
|
|
|
|
setIsCheckingAuth(false);
|
2025-07-27 10:05:37 -07:00
|
|
|
}
|
2026-04-06 23:02:25 -07:00
|
|
|
checkAuth();
|
2025-12-02 01:24:09 -08:00
|
|
|
}, []);
|
2025-06-09 15:50:15 -07:00
|
|
|
|
2026-01-25 16:15:25 +05:30
|
|
|
// Return null while loading - the global provider handles the loading UI
|
2025-10-10 00:50:29 -07:00
|
|
|
if (isCheckingAuth) {
|
2026-01-25 16:15:25 +05:30
|
|
|
return null;
|
2025-07-27 10:05:37 -07:00
|
|
|
}
|
2025-06-09 15:50:15 -07:00
|
|
|
|
2025-11-03 22:34:37 -08:00
|
|
|
return (
|
2025-11-11 04:02:04 +02:00
|
|
|
<div className="h-full flex flex-col ">
|
|
|
|
|
<div className="flex-1 min-h-0">{children}</div>
|
|
|
|
|
</div>
|
2025-11-03 22:34:37 -08:00
|
|
|
);
|
2025-07-27 10:05:37 -07:00
|
|
|
}
|