twenty/front/src/App.tsx

92 lines
2.6 KiB
TypeScript
Raw Normal View History

import React, { useEffect, useState } from 'react';
import { Navigate, Route, Routes } from 'react-router-dom';
2023-06-05 23:20:55 +03:00
import { ThemeProvider } from '@emotion/react';
import { browserPrefersDarkMode } from '@/utils/utils';
import { RequireAuth } from './modules/auth/components/RequireAuth';
import { getUserIdFromToken } from './modules/auth/services/AuthService';
import { AppLayout } from './modules/ui/layout/AppLayout';
2023-06-05 23:20:55 +03:00
import { darkTheme, lightTheme } from './modules/ui/layout/styles/themes';
import { mapToUser, User } from './modules/users/interfaces/user.interface';
import { useGetCurrentUserQuery } from './modules/users/services';
2023-04-20 19:46:27 +03:00
import AuthCallback from './pages/auth/Callback';
import { Login } from './pages/auth/Login';
import { Companies } from './pages/companies/Companies';
import { Opportunities } from './pages/opportunities/Opportunities';
import { People } from './pages/people/People';
2022-12-01 17:58:08 +03:00
2023-06-05 23:20:55 +03:00
type AppProps = {
themeEnabled?: boolean;
};
export function App({ themeEnabled = true }: AppProps) {
const [user, setUser] = useState<User | undefined>(undefined);
2023-01-27 14:12:04 +03:00
const userIdFromToken = getUserIdFromToken();
const { data } = useGetCurrentUserQuery(userIdFromToken);
useEffect(() => {
if (data?.users[0]) {
setUser(mapToUser(data?.users[0]));
}
}, [data]);
2023-04-09 17:43:43 +03:00
2023-06-05 23:20:55 +03:00
const defaultTheme = browserPrefersDarkMode() ? darkTheme : lightTheme;
const app = (
<>
{
<AppLayout user={user}>
<Routes>
2023-04-21 16:36:10 +03:00
<Route
path="/"
element={
<RequireAuth>
<Navigate to="/people" replace />
</RequireAuth>
}
/>
<Route
path="/people"
element={
<RequireAuth>
<People />
</RequireAuth>
}
/>
<Route
path="/companies"
element={
<RequireAuth>
<Companies />
</RequireAuth>
}
/>
<Route
path="/opportunities"
element={
<RequireAuth>
<Opportunities />
</RequireAuth>
}
/>
2023-04-20 19:46:27 +03:00
<Route path="/auth/callback" element={<AuthCallback />} />
<Route path="/auth/login" element={<Login />} />
</Routes>
</AppLayout>
}
</>
2022-12-01 17:58:08 +03:00
);
2023-06-05 23:20:55 +03:00
return (
<>
{themeEnabled ? (
<ThemeProvider theme={defaultTheme}>{app}</ThemeProvider>
) : (
app
)}
</>
);
2022-12-01 17:58:08 +03:00
}