manifold/web/hooks/use-user.ts

35 lines
766 B
TypeScript
Raw Normal View History

import { useEffect, useState } from 'react'
import { PrivateUser } from '../../common/user'
import {
listenForLogin,
listenForPrivateUser,
listenForUser,
User,
} from '../lib/firebase/users'
2021-12-09 22:05:55 +00:00
export const useUser = () => {
const [user, setUser] = useState<User | null | undefined>(undefined)
useEffect(() => listenForLogin(setUser), [])
const userId = user?.id
useEffect(() => {
if (userId) return listenForUser(userId, setUser)
}, [userId])
2021-12-09 23:23:21 +00:00
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
}