2022-05-22 08:36:05 +00:00
|
|
|
import { isEqual, sortBy } from 'lodash'
|
2022-01-23 06:51:19 +00:00
|
|
|
import { useEffect, useState } from 'react'
|
2022-05-09 13:04:36 +00:00
|
|
|
import { Fold } from 'common/fold'
|
|
|
|
import { User } from 'common/user'
|
2022-01-26 20:03:32 +00:00
|
|
|
import {
|
2022-03-31 05:35:20 +00:00
|
|
|
listAllFolds,
|
2022-01-26 20:03:32 +00:00
|
|
|
listenForFold,
|
|
|
|
listenForFolds,
|
2022-02-06 22:55:14 +00:00
|
|
|
listenForFoldsWithTags,
|
2022-01-26 20:03:32 +00:00
|
|
|
listenForFollow,
|
2022-02-21 05:12:35 +00:00
|
|
|
listenForFollowedFolds,
|
2022-05-09 13:04:36 +00:00
|
|
|
} from 'web/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
|
|
|
|
2022-02-06 22:55:14 +00:00
|
|
|
export const useFoldsWithTags = (tags: string[] | undefined) => {
|
|
|
|
const [folds, setFolds] = useState<Fold[] | undefined>()
|
|
|
|
|
2022-04-06 16:53:48 +00:00
|
|
|
const tagsKey = tags?.join(',')
|
|
|
|
|
2022-02-06 22:55:14 +00:00
|
|
|
useEffect(() => {
|
|
|
|
if (tags && tags.length > 0) return listenForFoldsWithTags(tags, setFolds)
|
2022-04-06 16:53:48 +00:00
|
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
|
|
}, [tagsKey])
|
2022-02-06 22:55:14 +00:00
|
|
|
|
|
|
|
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>()
|
|
|
|
|
2022-04-06 16:53:48 +00:00
|
|
|
const foldId = fold?.id
|
|
|
|
const userId = user?.id
|
|
|
|
|
2022-01-26 20:03:32 +00:00
|
|
|
useEffect(() => {
|
2022-04-06 16:53:48 +00:00
|
|
|
if (userId) return listenForFollow(foldId, userId, setFollowing)
|
|
|
|
}, [foldId, userId])
|
2022-01-26 20:03:32 +00:00
|
|
|
|
|
|
|
return following
|
|
|
|
}
|
2022-02-04 23:24:54 +00:00
|
|
|
|
2022-03-31 05:35:20 +00:00
|
|
|
// Note: We cache followedFoldIds in localstorage to speed up the initial load
|
|
|
|
export const useFollowedFoldIds = (user: User | null | undefined) => {
|
2022-02-04 23:24:54 +00:00
|
|
|
const [followedFoldIds, setFollowedFoldIds] = useState<string[] | undefined>(
|
|
|
|
undefined
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user) {
|
|
|
|
const key = `followed-folds-${user.id}`
|
|
|
|
const followedFoldJson = localStorage.getItem(key)
|
|
|
|
if (followedFoldJson) {
|
|
|
|
setFollowedFoldIds(JSON.parse(followedFoldJson))
|
|
|
|
}
|
|
|
|
|
2022-02-21 05:12:35 +00:00
|
|
|
return listenForFollowedFolds(user.id, (foldIds) => {
|
2022-02-04 23:24:54 +00:00
|
|
|
setFollowedFoldIds(foldIds)
|
|
|
|
localStorage.setItem(key, JSON.stringify(foldIds))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [user])
|
|
|
|
|
|
|
|
return followedFoldIds
|
|
|
|
}
|
2022-03-31 05:35:20 +00:00
|
|
|
|
|
|
|
// We also cache followedFolds directly in JSON.
|
|
|
|
// TODO: Extract out localStorage caches to a utility
|
|
|
|
export const useFollowedFolds = (user: User | null | undefined) => {
|
|
|
|
const [followedFolds, setFollowedFolds] = useState<Fold[] | undefined>()
|
|
|
|
const ids = useFollowedFoldIds(user)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (user && ids) {
|
|
|
|
const key = `followed-full-folds-${user.id}`
|
|
|
|
const followedFoldJson = localStorage.getItem(key)
|
|
|
|
if (followedFoldJson) {
|
|
|
|
setFollowedFolds(JSON.parse(followedFoldJson))
|
|
|
|
// Exit early if ids and followedFoldIds have all the same elements.
|
|
|
|
if (
|
2022-05-22 08:36:05 +00:00
|
|
|
isEqual(
|
|
|
|
sortBy(ids),
|
|
|
|
sortBy(JSON.parse(followedFoldJson).map((f: Fold) => f.id))
|
2022-03-31 05:35:20 +00:00
|
|
|
)
|
|
|
|
) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, fetch the full contents of all folds
|
|
|
|
listAllFolds().then((folds) => {
|
|
|
|
const followedFolds = folds.filter((fold) => ids.includes(fold.id))
|
|
|
|
setFollowedFolds(followedFolds)
|
|
|
|
localStorage.setItem(key, JSON.stringify(followedFolds))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}, [user, ids])
|
|
|
|
|
|
|
|
return followedFolds
|
|
|
|
}
|