Listen to followed folds. Follow button retreives from local storage.

This commit is contained in:
James Grugett 2022-02-20 23:12:35 -06:00
parent 62b1966b21
commit 207a12df55
5 changed files with 47 additions and 12 deletions

View File

@ -1,6 +1,6 @@
import clsx from 'clsx' import clsx from 'clsx'
import { Fold } from '../../common/fold' import { Fold } from '../../common/fold'
import { useFollowingFold } from '../hooks/use-fold' import { useFollowedFolds } from '../hooks/use-fold'
import { useUser } from '../hooks/use-user' import { useUser } from '../hooks/use-user'
import { followFold, unfollowFold } from '../lib/firebase/folds' import { followFold, unfollowFold } from '../lib/firebase/folds'
@ -8,7 +8,11 @@ export function FollowFoldButton(props: { fold: Fold; className?: string }) {
const { fold, className } = props const { fold, className } = props
const user = useUser() const user = useUser()
const following = useFollowingFold(fold, user)
const followedFoldIds = useFollowedFolds(user)
const following = followedFoldIds
? followedFoldIds.includes(fold.id)
: undefined
const onFollow = () => { const onFollow = () => {
if (user) followFold(fold.id, user.id) if (user) followFold(fold.id, user.id)

View File

@ -1,4 +1,5 @@
import _ from 'lodash' import _ from 'lodash'
import { useRef } from 'react'
import { Fold } from '../../common/fold' import { Fold } from '../../common/fold'
import { User } from '../../common/user' import { User } from '../../common/user'
@ -50,8 +51,11 @@ export const useActiveContracts = (
) )
) )
const followedFoldSlugs = // Save the initial followed fold slugs.
followedFoldIds === undefined ? undefined : followedFolds.map((f) => f.slug) const followedFoldSlugsRef = useRef<string[] | undefined>()
if (followedFoldIds && !followedFoldSlugsRef.current)
followedFoldSlugsRef.current = followedFolds.map((f) => f.slug)
const initialFollowedFoldSlugs = followedFoldSlugsRef.current
const tagSet = new Set( const tagSet = new Set(
_.flatten(followedFolds.map((fold) => fold.lowercaseTags)) _.flatten(followedFolds.map((fold) => fold.lowercaseTags))
@ -97,5 +101,10 @@ export const useActiveContracts = (
(contract) => commentsByContract[contract.id] ?? [] (contract) => commentsByContract[contract.id] ?? []
) )
return { activeContracts, activeBets, activeComments, followedFoldSlugs } return {
activeContracts,
activeBets,
activeComments,
initialFollowedFoldSlugs,
}
} }

View File

@ -2,11 +2,11 @@ import { useEffect, useState } from 'react'
import { Fold } from '../../common/fold' import { Fold } from '../../common/fold'
import { User } from '../../common/user' import { User } from '../../common/user'
import { import {
getFollowedFolds,
listenForFold, listenForFold,
listenForFolds, listenForFolds,
listenForFoldsWithTags, listenForFoldsWithTags,
listenForFollow, listenForFollow,
listenForFollowedFolds,
} from '../lib/firebase/folds' } from '../lib/firebase/folds'
export const useFold = (foldId: string | undefined) => { export const useFold = (foldId: string | undefined) => {
@ -62,7 +62,7 @@ export const useFollowedFolds = (user: User | null | undefined) => {
setFollowedFoldIds(JSON.parse(followedFoldJson)) setFollowedFoldIds(JSON.parse(followedFoldJson))
} }
getFollowedFolds(user.id).then((foldIds) => { return listenForFollowedFolds(user.id, (foldIds) => {
setFollowedFoldIds(foldIds) setFollowedFoldIds(foldIds)
localStorage.setItem(key, JSON.stringify(foldIds)) localStorage.setItem(key, JSON.stringify(foldIds))
}) })

View File

@ -4,6 +4,7 @@ import {
deleteDoc, deleteDoc,
doc, doc,
getDocs, getDocs,
onSnapshot,
query, query,
setDoc, setDoc,
updateDoc, updateDoc,
@ -201,3 +202,20 @@ export async function getFollowedFolds(userId: string) {
) )
return foldIds return foldIds
} }
export function listenForFollowedFolds(
userId: string,
setFoldIds: (foldIds: string[]) => void
) {
return onSnapshot(
query(collectionGroup(db, 'followers'), where('userId', '==', userId)),
(snapshot) => {
if (snapshot.metadata.fromCache) return
const foldIds = snapshot.docs.map(
(doc) => doc.ref.parent.parent?.id as string
)
setFoldIds(foldIds)
}
)
}

View File

@ -37,8 +37,12 @@ const Home = (props: {
}) => { }) => {
const user = useUser() const user = useUser()
const { activeContracts, activeBets, activeComments, followedFoldSlugs } = const {
useActiveContracts(props, user) activeContracts,
activeBets,
activeComments,
initialFollowedFoldSlugs,
} = useActiveContracts(props, user)
if (user === null) { if (user === null) {
Router.replace('/') Router.replace('/')
@ -52,11 +56,11 @@ const Home = (props: {
<FeedCreate user={user ?? undefined} /> <FeedCreate user={user ?? undefined} />
<Spacer h={6} /> <Spacer h={6} />
{followedFoldSlugs !== undefined && {initialFollowedFoldSlugs !== undefined &&
followedFoldSlugs.length === 0 && ( initialFollowedFoldSlugs.length === 0 && (
<FastFoldFollowing <FastFoldFollowing
user={user} user={user}
followedFoldSlugs={followedFoldSlugs} followedFoldSlugs={initialFollowedFoldSlugs}
/> />
)} )}