import { BrowserRouter, Routes, Route, Navigate } from 'react-router-dom';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { Toaster } from 'react-hot-toast';
import { useEffect } from 'react';
import { useAuthStore } from '@/store/authStore';
import { useConfigStore } from '@/store/configStore';
import { MainLayout } from '@/components/layout';
import { LoadingOverlay } from '@/components/ui';
import { Login } from '@/pages/Login';
import { Dashboard } from '@/pages/Dashboard';
import { CustomersList } from '@/pages/customers';
import { ProjectsList } from '@/pages/projects';
import { TasksList } from '@/pages/tasks';
import { EquipmentList } from '@/pages/equipment';
import { RMAList } from '@/pages/rma';
import { SettingsDashboard } from '@/pages/settings';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
staleTime: 1000 * 60 * 5, // 5 minutes
retry: 1,
},
},
});
function ProtectedRoute({ children }: { children: React.ReactNode }) {
const { isAuthenticated, user, isLoading, fetchProfile } = useAuthStore();
const { fetchConfig, isLoaded: configLoaded } = useConfigStore();
useEffect(() => {
// Only fetch profile if we have a token but no user data
const token = localStorage.getItem('accessToken');
if (token && !user) {
fetchProfile();
}
}, [user, fetchProfile]);
useEffect(() => {
if (isAuthenticated && !configLoaded) {
fetchConfig();
}
}, [isAuthenticated, configLoaded, fetchConfig]);
// Show loading only when we're actively fetching
if (isLoading && !user) {
return ;
}
if (!isAuthenticated) {
return ;
}
return <>{children}>;
}
function AdminRoute({ children }: { children: React.ReactNode }) {
const { user } = useAuthStore();
if (user?.role.code !== 'ROOT' && user?.role.code !== 'ADMIN') {
return ;
}
return <>{children}>;
}
function AppRoutes() {
const { isAuthenticated } = useAuthStore();
return (
: }
/>
}
>
} />
} />
} />
} />
} />
} />
}
/>
} />
);
}
export function App() {
return (
);
}