manifold/web/hooks/use-follows.ts
Ian Philips f50b4775a1
Allow to follow/unfollow markets, backfill as well (#794)
* Allow to follow/unfollow markets, backfill as well

* remove yarn script edit

* add decrement comment

* Lint

* Decrement follow count on unfollow

* Follow/unfollow button logic

* Unfollow/follow => heart

* Add user to followers in place-bet and sell-shares

* Add tracking

* Show contract follow modal for first time following

* Increment follower count as well

* Remove add follow from bet trigger

* restore on-create-bet

* Add pubsub to dev.sh, show heart on FR, remove from answer trigger
2022-08-24 10:49:53 -06:00

43 lines
1.2 KiB
TypeScript

import { useEffect, useState } from 'react'
import { listenForFollowers, listenForFollows } from 'web/lib/firebase/users'
import { contracts, listenForContractFollows } from 'web/lib/firebase/contracts'
export const useFollows = (userId: string | null | undefined) => {
const [followIds, setFollowIds] = useState<string[] | undefined>()
useEffect(() => {
if (userId) {
const key = `follows:${userId}`
const follows = localStorage.getItem(key)
if (follows) setFollowIds(JSON.parse(follows))
return listenForFollows(userId, (follows) => {
setFollowIds(follows)
localStorage.setItem(key, JSON.stringify(follows))
})
}
}, [userId])
return followIds
}
export const useFollowers = (userId: string | undefined) => {
const [followerIds, setFollowerIds] = useState<string[] | undefined>()
useEffect(() => {
if (userId) return listenForFollowers(userId, setFollowerIds)
}, [userId])
return followerIds
}
export const useContractFollows = (contractId: string) => {
const [followIds, setFollowIds] = useState<string[] | undefined>()
useEffect(() => {
return listenForContractFollows(contractId, setFollowIds)
}, [contractId])
return followIds
}