2022-08-12 20:41:00 +00:00
|
|
|
import { useContext } from 'react'
|
2022-06-08 03:24:18 +00:00
|
|
|
import { useFirestoreDocumentData } from '@react-query-firebase/firestore'
|
|
|
|
import { QueryClient } from 'react-query'
|
|
|
|
|
2022-08-12 20:41:00 +00:00
|
|
|
import { doc, DocumentData } from 'firebase/firestore'
|
|
|
|
import { getUser, User, users } from 'web/lib/firebase/users'
|
2022-07-21 07:38:26 +00:00
|
|
|
import { AuthContext } from 'web/components/auth-context'
|
2021-12-09 22:05:55 +00:00
|
|
|
|
|
|
|
export const useUser = () => {
|
2022-08-12 20:41:00 +00:00
|
|
|
const authUser = useContext(AuthContext)
|
|
|
|
return authUser ? authUser.user : authUser
|
2021-12-09 23:23:21 +00:00
|
|
|
}
|
2022-02-04 03:04:56 +00:00
|
|
|
|
2022-08-12 20:41:00 +00:00
|
|
|
export const usePrivateUser = () => {
|
|
|
|
const authUser = useContext(AuthContext)
|
|
|
|
return authUser ? authUser.privateUser : authUser
|
2022-02-04 03:04:56 +00:00
|
|
|
}
|
2022-06-08 03:24:18 +00:00
|
|
|
|
2022-07-14 17:02:46 +00:00
|
|
|
export const useUserById = (userId = '_') => {
|
2022-06-08 03:24:18 +00:00
|
|
|
const result = useFirestoreDocumentData<DocumentData, User>(
|
|
|
|
['users', userId],
|
2022-06-29 19:21:40 +00:00
|
|
|
doc(users, userId),
|
2022-06-08 03:24:18 +00:00
|
|
|
{ subscribe: true, includeMetadataChanges: true }
|
|
|
|
)
|
|
|
|
|
2022-07-14 17:03:29 +00:00
|
|
|
if (userId === '_') return undefined
|
|
|
|
|
2022-06-08 03:24:18 +00:00
|
|
|
return result.isLoading ? undefined : result.data
|
|
|
|
}
|
|
|
|
|
|
|
|
const queryClient = new QueryClient()
|
|
|
|
|
|
|
|
export const prefetchUser = (userId: string) => {
|
2022-06-08 04:06:14 +00:00
|
|
|
queryClient.prefetchQuery(['users', userId], () => getUser(userId))
|
2022-06-08 03:24:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const prefetchUsers = (userIds: string[]) => {
|
2022-06-08 04:06:14 +00:00
|
|
|
userIds.forEach(prefetchUser)
|
2022-06-08 03:24:18 +00:00
|
|
|
}
|