manifold/web/hooks/use-fold.ts

39 lines
898 B
TypeScript
Raw Normal View History

2022-01-23 06:51:19 +00:00
import { useEffect, useState } from 'react'
import { Fold } from '../../common/fold'
2022-01-26 20:03:32 +00:00
import { User } from '../../common/user'
import {
listenForFold,
listenForFolds,
listenForFollow,
} from '../lib/firebase/folds'
2022-01-23 06:51:19 +00:00
2022-01-27 06:38:22 +00:00
export const useFold = (foldId: string | undefined) => {
2022-01-23 06:51:19 +00:00
const [fold, setFold] = useState<Fold | null | undefined>()
useEffect(() => {
2022-01-27 06:38:22 +00:00
if (foldId) return listenForFold(foldId, setFold)
2022-01-23 06:51:19 +00:00
}, [foldId])
return fold
}
2022-01-24 05:47:20 +00:00
export const useFolds = () => {
const [folds, setFolds] = useState<Fold[] | undefined>()
useEffect(() => {
return listenForFolds(setFolds)
}, [])
return folds
}
2022-01-26 20:03:32 +00:00
export const useFollowingFold = (fold: Fold, user: User | null | undefined) => {
const [following, setFollowing] = useState<boolean | undefined>()
useEffect(() => {
if (user) return listenForFollow(fold, user, setFollowing)
}, [fold, user])
return following
}