2022-08-12 20:41:00 +00:00
|
|
|
import { useContext } from 'react'
|
2022-10-11 05:32:55 +00:00
|
|
|
import {
|
|
|
|
useFirestoreDocumentData,
|
|
|
|
useFirestoreQueryData,
|
|
|
|
} from '@react-query-firebase/firestore'
|
|
|
|
import { useQuery, useQueryClient } from 'react-query'
|
2022-06-08 03:24:18 +00:00
|
|
|
|
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'
|
2022-10-11 05:32:55 +00:00
|
|
|
import { ContractMetrics } from 'common/calculate-metrics'
|
|
|
|
import { getUserContractMetricsQuery } from 'web/lib/firebase/contract-metrics'
|
|
|
|
import { getContractFromId } from 'web/lib/firebase/contracts'
|
|
|
|
import { buildArray, filterDefined } from 'common/util/array'
|
|
|
|
import { CPMMBinaryContract } from 'common/contract'
|
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
|
|
|
|
}
|
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export const usePrefetchUser = (userId: string) => {
|
|
|
|
return usePrefetchUsers([userId])[0]
|
2022-06-08 03:24:18 +00:00
|
|
|
}
|
|
|
|
|
2022-09-02 02:38:09 +00:00
|
|
|
export const usePrefetchUsers = (userIds: string[]) => {
|
|
|
|
const queryClient = useQueryClient()
|
|
|
|
return userIds.map((userId) =>
|
|
|
|
queryClient.prefetchQuery(['users', userId], () => getUser(userId))
|
|
|
|
)
|
2022-06-08 03:24:18 +00:00
|
|
|
}
|
2022-10-11 05:32:55 +00:00
|
|
|
|
2022-10-13 18:49:43 +00:00
|
|
|
export const useUserContractMetricsByProfit = (userId: string, count = 50) => {
|
2022-10-11 05:32:55 +00:00
|
|
|
const positiveResult = useFirestoreQueryData<ContractMetrics>(
|
|
|
|
['contract-metrics-descending', userId, count],
|
|
|
|
getUserContractMetricsQuery(userId, count, 'desc')
|
|
|
|
)
|
|
|
|
const negativeResult = useFirestoreQueryData<ContractMetrics>(
|
|
|
|
['contract-metrics-ascending', userId, count],
|
|
|
|
getUserContractMetricsQuery(userId, count, 'asc')
|
|
|
|
)
|
|
|
|
|
|
|
|
const metrics = buildArray(positiveResult.data, negativeResult.data)
|
|
|
|
const contractIds = metrics.map((m) => m.contractId)
|
|
|
|
|
|
|
|
const contractResult = useQuery(['contracts', contractIds], () =>
|
|
|
|
Promise.all(contractIds.map(getContractFromId))
|
|
|
|
)
|
|
|
|
const contracts = contractResult.data
|
|
|
|
|
|
|
|
if (!positiveResult.data || !negativeResult.data || !contracts)
|
|
|
|
return undefined
|
|
|
|
|
2022-10-13 18:49:43 +00:00
|
|
|
const filteredContracts = filterDefined(contracts).filter(
|
|
|
|
(c) => !c.isResolved
|
|
|
|
) as CPMMBinaryContract[]
|
|
|
|
const filteredMetrics = metrics
|
|
|
|
.filter((m) => m.from && Math.abs(m.from.day.profit) >= 0.5)
|
|
|
|
.filter((m) => filteredContracts.find((c) => c.id === m.contractId))
|
|
|
|
|
2022-10-11 05:32:55 +00:00
|
|
|
return { contracts: filteredContracts, metrics: filteredMetrics }
|
|
|
|
}
|
|
|
|
|
|
|
|
export const useUserContractMetrics = (userId = '_', contractId: string) => {
|
|
|
|
const result = useFirestoreDocumentData<DocumentData, ContractMetrics>(
|
|
|
|
['user-contract-metrics', userId, contractId],
|
|
|
|
doc(users, userId, 'contract-metrics', contractId)
|
|
|
|
)
|
|
|
|
|
|
|
|
if (userId === '_') return undefined
|
|
|
|
|
|
|
|
return result.data
|
|
|
|
}
|