2021-12-17 04:17:33 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-02-04 03:04:56 +00:00
|
|
|
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 = () => {
|
2021-12-17 04:09:38 +00:00
|
|
|
const [user, setUser] = useState<User | null | undefined>(undefined)
|
|
|
|
|
2021-12-17 04:17:33 +00:00
|
|
|
useEffect(() => listenForLogin(setUser), [])
|
2021-12-15 00:45:13 +00:00
|
|
|
|
|
|
|
const userId = user?.id
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (userId) return listenForUser(userId, setUser)
|
|
|
|
}, [userId])
|
|
|
|
|
2021-12-09 23:23:21 +00:00
|
|
|
return user
|
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
|
|
|
export const usePrivateUser = (userId?: string) => {
|
|
|
|
const [privateUser, setPrivateUser] = useState<
|
|
|
|
PrivateUser | null | undefined
|
|
|
|
>(undefined)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (userId) return listenForPrivateUser(userId, setPrivateUser)
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
return privateUser
|
|
|
|
}
|