mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-02-09 05:25:15 -05:00
* fixed update token on address change * fetching already set profile data in setup flow * select skills colors * showing more data on player tile * rename variables
38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { Web3Context } from 'contexts/Web3Context';
|
|
import { useGetMeQuery } from 'graphql/autogen/types';
|
|
import { useRouter } from 'next/router';
|
|
import { useContext, useEffect } from 'react';
|
|
|
|
export const useWeb3 = () => useContext(Web3Context);
|
|
|
|
type UseUserOpts = {
|
|
redirectTo?: string;
|
|
redirectIfFound?: boolean;
|
|
};
|
|
|
|
export const useUser = ({ redirectTo, redirectIfFound }: UseUserOpts = {}) => {
|
|
const { authToken } = useWeb3();
|
|
const router = useRouter();
|
|
|
|
const [{ data, error, fetching }] = useGetMeQuery({
|
|
pause: !authToken,
|
|
});
|
|
|
|
const user = data?.me[0];
|
|
|
|
useEffect(() => {
|
|
if (!redirectTo || !user) return;
|
|
|
|
if (
|
|
// If redirectTo is set, redirect if the user was not found.
|
|
(redirectTo && !redirectIfFound && !user) ||
|
|
// If redirectIfFound is also set, redirect if the user was found
|
|
(redirectIfFound && user)
|
|
) {
|
|
router.push(redirectTo);
|
|
}
|
|
}, [router, user, redirectIfFound, redirectTo]);
|
|
|
|
return { user: error ? null : user, fetching };
|
|
};
|