* refactor: remove mappers * chore: generate graphql types * lint: remove useless import * Remove preset-react-create-app from storybook addons * test: remove old tests * Upgrade storybook version * Remove sb preset-cra and add sb svgr loader * chore: remove figma image url from storybook --------- Co-authored-by: Charles Bochet <charles@twenty.com>
30 lines
912 B
TypeScript
30 lines
912 B
TypeScript
import { useEffect } from 'react';
|
|
import { useRecoilState } from 'recoil';
|
|
|
|
import { getUserIdFromToken } from '@/auth/services/AuthService';
|
|
import { currentUserState } from '@/auth/states/currentUserState';
|
|
import { isAuthenticatingState } from '@/auth/states/isAuthenticatingState';
|
|
import { useGetCurrentUserQuery } from '@/users/services';
|
|
|
|
type OwnProps = {
|
|
children: JSX.Element;
|
|
};
|
|
|
|
export function AuthProvider({ children }: OwnProps) {
|
|
const [, setCurrentUser] = useRecoilState(currentUserState);
|
|
const [, setIsAuthenticating] = useRecoilState(isAuthenticatingState);
|
|
|
|
const userIdFromToken = getUserIdFromToken();
|
|
|
|
const { data } = useGetCurrentUserQuery(userIdFromToken);
|
|
const user = data?.users?.[0];
|
|
useEffect(() => {
|
|
if (user) {
|
|
setCurrentUser(user);
|
|
setIsAuthenticating(false);
|
|
}
|
|
}, [user, setCurrentUser, setIsAuthenticating]);
|
|
|
|
return <>{children}</>;
|
|
}
|