manifold/web/hooks/use-contracts.ts

55 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-01-05 06:32:52 +00:00
import _ from 'lodash'
import { useEffect, useState } from 'react'
2022-01-05 06:32:52 +00:00
import {
Contract,
listenForContracts,
listenForHotContracts,
2022-01-05 06:32:52 +00:00
} from '../lib/firebase/contracts'
import { listenForTaggedContracts } from '../lib/firebase/folds'
export const useContracts = () => {
const [contracts, setContracts] = useState<Contract[] | undefined>()
useEffect(() => {
return listenForContracts(setContracts)
}, [])
return contracts
}
2022-01-05 06:32:52 +00:00
2022-02-05 21:56:42 +00:00
export const useUpdatedContracts = (initialContracts: Contract[]) => {
const [contracts, setContracts] = useState(initialContracts)
useEffect(() => {
return listenForContracts((newContracts) => {
const contractMap = _.fromPairs(newContracts.map((c) => [c.id, c]))
setContracts(initialContracts.map((c) => contractMap[c.id]))
})
}, [initialContracts])
return contracts
}
export const useTaggedContracts = (tags: string[] | undefined) => {
const [contracts, setContracts] = useState<Contract[] | undefined>(
tags && tags.length === 0 ? [] : undefined
)
const tagsKey = tags?.map((tag) => tag.toLowerCase()).join(',') ?? ''
useEffect(() => {
if (!tags || tags.length === 0) return
return listenForTaggedContracts(tags, setContracts)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [tagsKey])
return contracts
}
2022-01-05 06:32:52 +00:00
export const useHotContracts = () => {
const [hotContracts, setHotContracts] = useState<Contract[] | undefined>()
2022-01-05 06:32:52 +00:00
useEffect(() => listenForHotContracts(setHotContracts), [])
2022-01-05 06:32:52 +00:00
return hotContracts
2022-01-05 06:32:52 +00:00
}