mirror of
https://github.com/MetaFam/TheGame.git
synced 2026-02-08 04:55:02 -05:00
* upgraded storybook dependencies * upgraded web dependencies * updated timezone selector * upgrade chakra in metamaps * upgraded react-dnd in metamaps * upgraded framer-motion * fixed types in metamaps * upgraded eslint * upgraded lerna, husky and graphql * upgraded node version * removed metamaps package * fixed all eslint issues * ran yarn format to prettier format all files * updated lint-staged & husky scripts * add executable perms to pre-push scripts * updated yarn.lock * fixed eslint and moved chakra icons to ds * fixed emotion errors * removed extra useContext * update yarn.lock * upgraded more packages * removed unnecessary .huskyrc.json * lint fix
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import { Web3Context, Web3ContextType } from 'contexts/Web3Context';
|
|
import { useGetMeQuery } from 'graphql/autogen/types';
|
|
import { MeType } from 'graphql/types';
|
|
import { useRouter } from 'next/router';
|
|
import React, { useContext, useEffect } from 'react';
|
|
|
|
export const useWeb3 = (): Web3ContextType => useContext(Web3Context);
|
|
|
|
type UseUserOpts = {
|
|
redirectTo?: string;
|
|
redirectIfFound?: boolean;
|
|
};
|
|
|
|
export const useUser = ({ redirectTo, redirectIfFound }: UseUserOpts = {}): {
|
|
user: MeType | null;
|
|
fetching: boolean;
|
|
} => {
|
|
const { authToken } = useWeb3();
|
|
const router = useRouter();
|
|
|
|
const [{ data, error, fetching }] = useGetMeQuery({
|
|
pause: !authToken,
|
|
});
|
|
const me = data?.me[0];
|
|
const user = error || !authToken || !me ? null : me;
|
|
|
|
useEffect(() => {
|
|
if (!redirectTo) 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, fetching };
|
|
};
|
|
|
|
export const useMounted = (): boolean => {
|
|
// https://www.joshwcomeau.com/react/the-perils-of-rehydration/
|
|
const [hasMounted, setHasMounted] = React.useState(false);
|
|
React.useEffect(() => {
|
|
setHasMounted(true);
|
|
}, []);
|
|
return hasMounted;
|
|
};
|