Listen for folds in contract page

This commit is contained in:
James Grugett 2022-02-06 16:55:14 -06:00
parent 5aebd7eb41
commit 63b7f64683
3 changed files with 33 additions and 1 deletions

View File

@ -5,6 +5,7 @@ import {
getFollowedFolds, getFollowedFolds,
listenForFold, listenForFold,
listenForFolds, listenForFolds,
listenForFoldsWithTags,
listenForFollow, listenForFollow,
} from '../lib/firebase/folds' } from '../lib/firebase/folds'
@ -28,6 +29,16 @@ export const useFolds = () => {
return folds return folds
} }
export const useFoldsWithTags = (tags: string[] | undefined) => {
const [folds, setFolds] = useState<Fold[] | undefined>()
useEffect(() => {
if (tags && tags.length > 0) return listenForFoldsWithTags(tags, setFolds)
}, [tags])
return folds
}
export const useFollowingFold = (fold: Fold, user: User | null | undefined) => { export const useFollowingFold = (fold: Fold, user: User | null | undefined) => {
const [following, setFollowing] = useState<boolean | undefined>() const [following, setFollowing] = useState<boolean | undefined>()

View File

@ -154,6 +154,24 @@ export async function getFoldsByTags(tags: string[]) {
return _.sortBy(folds, (fold) => -1 * fold.followCount) return _.sortBy(folds, (fold) => -1 * fold.followCount)
} }
export function listenForFoldsWithTags(
tags: string[],
setFolds: (folds: Fold[]) => void
) {
const lowercaseTags = tags.map((tag) => tag.toLowerCase())
const q =
// TODO: split into multiple queries if tags.length > 10.
query(
foldCollection,
where('lowercaseTags', 'array-contains-any', lowercaseTags)
)
return listenForValues<Fold>(q, (folds) => {
const sorted = _.sortBy(folds, (fold) => -1 * fold.followCount)
setFolds(sorted)
})
}
export async function getFollowedFolds(userId: string) { export async function getFollowedFolds(userId: string) {
const snapshot = await getDocs( const snapshot = await getDocs(
query(collectionGroup(db, 'followers'), where('userId', '==', userId)) query(collectionGroup(db, 'followers'), where('userId', '==', userId))

View File

@ -25,6 +25,7 @@ import { Comment, listAllComments } from '../../lib/firebase/comments'
import Custom404 from '../404' import Custom404 from '../404'
import { getFoldsByTags } from '../../lib/firebase/folds' import { getFoldsByTags } from '../../lib/firebase/folds'
import { Fold } from '../../../common/fold' import { Fold } from '../../../common/fold'
import { useFoldsWithTags } from '../../hooks/use-fold'
export async function getStaticProps(props: { export async function getStaticProps(props: {
params: { username: string; contractSlug: string } params: { username: string; contractSlug: string }
@ -71,7 +72,9 @@ export default function ContractPage(props: {
const user = useUser() const user = useUser()
const contract = useContractWithPreload(props.slug, props.contract) const contract = useContractWithPreload(props.slug, props.contract)
const { bets, comments, folds } = props const { bets, comments } = props
const folds = useFoldsWithTags(contract?.tags) ?? props.folds
if (!contract) { if (!contract) {
return <Custom404 /> return <Custom404 />