Files
TheGame/packages/web/lib/hooks.ts
The Lone Rōnin 6d6d75e6e4 Cross Browser Gradient for App Drawer (#294)
* Cross Browser Compatibility for Gradient with new hover animation

* [MyMeta] Use responsive units on the header and footer (#305)

* fixing menu drawer to display on Chromium

* switching footer layout to responsive units

* build failing b/c player is undefined

* refactoring bottom nav to use fewer explicit sizes

* fixed link issues and removed box from focused menu items ala.

* `Error serializing `.guild` returned from `getStaticProps` in "/guild/[guildname]"`

* codegen was removed, but some references linger

* switching profile options to icons to save space

* simplifying & making links clickable

* accidentally duplicated a parameter

* adding Chakra icons to web app

* removing version smudge from icons lib

* simplifying clumsy ternary statement

* lint cares that imports are in alphabetical order

* switching drawer images to next.js component (re: #294)

* satiating the linter

* moving BoxedNextImage to deign-system

* Fix server side rendering issues with backdrop filter

* boxing next.js images

* these only look broken in Chrome's mobile emulator 🍄

* making the serialization fix precisely correct 🎷

Co-authored-by: Will Holcomb <dys@dhappy.org>
Co-authored-by: Pacien Boisson <pakokrew@gmail.com>
2021-02-16 11:20:41 +04:00

47 lines
1.3 KiB
TypeScript

import { Web3Context } from 'contexts/Web3Context';
import { useGetMeQuery } from 'graphql/autogen/types';
import { useRouter } from 'next/router';
import React, { 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 };
};
export const useMounted = () => {
// https://www.joshwcomeau.com/react/the-perils-of-rehydration/
const [hasMounted, setHasMounted] = React.useState(false);
React.useEffect(() => {
setHasMounted(true);
}, []);
return hasMounted;
};