manifold/web/hooks/use-user.ts
Marshall Polaris 47f10301c8
Change lodash stuff so that it can be tree-shaken out of build (#233)
* Set common package.json sideEffects: false

* Configure SWC to modularize lodash imports

* Import specific lodash functions instead of _

* Add an eslint rule to avoid full lodash import
2022-05-22 01:36:05 -07:00

38 lines
865 B
TypeScript

import { useEffect, useState } from 'react'
import { PrivateUser } from 'common/user'
import {
listenForLogin,
listenForPrivateUser,
listenForUser,
User,
} from 'web/lib/firebase/users'
import { useStateCheckEquality } from './use-state-check-equality'
export const useUser = () => {
const [user, setUser] = useStateCheckEquality<User | null | undefined>(
undefined
)
useEffect(() => listenForLogin(setUser), [setUser])
const userId = user?.id
useEffect(() => {
if (userId) return listenForUser(userId, setUser)
}, [userId, setUser])
return user
}
export const usePrivateUser = (userId?: string) => {
const [privateUser, setPrivateUser] = useState<
PrivateUser | null | undefined
>(undefined)
useEffect(() => {
if (userId) return listenForPrivateUser(userId, setPrivateUser)
}, [userId])
return privateUser
}