From 637a76cf34d2e5ff8fa994273de25857c54055ee Mon Sep 17 00:00:00 2001 From: James Grugett Date: Fri, 4 Feb 2022 17:24:54 -0600 Subject: [PATCH] Cache first step of generating feed: folds you follow and contracts you bet on. --- web/hooks/use-fold.ts | 24 ++++++++++++++++++++++++ web/hooks/use-user-bets.ts | 24 ++++++++++++++++++++++++ web/pages/home.tsx | 27 ++++++++++----------------- 3 files changed, 58 insertions(+), 17 deletions(-) diff --git a/web/hooks/use-fold.ts b/web/hooks/use-fold.ts index 310d6c54..010b7217 100644 --- a/web/hooks/use-fold.ts +++ b/web/hooks/use-fold.ts @@ -2,6 +2,7 @@ import { useEffect, useState } from 'react' import { Fold } from '../../common/fold' import { User } from '../../common/user' import { + getFollowedFolds, listenForFold, listenForFolds, listenForFollow, @@ -36,3 +37,26 @@ export const useFollowingFold = (fold: Fold, user: User | null | undefined) => { return following } + +export const useFollowedFolds = (user: User | null | undefined) => { + const [followedFoldIds, setFollowedFoldIds] = useState( + undefined + ) + + useEffect(() => { + if (user) { + const key = `followed-folds-${user.id}` + const followedFoldJson = localStorage.getItem(key) + if (followedFoldJson) { + setFollowedFoldIds(JSON.parse(followedFoldJson)) + } + + getFollowedFolds(user.id).then((foldIds) => { + setFollowedFoldIds(foldIds) + localStorage.setItem(key, JSON.stringify(foldIds)) + }) + } + }, [user]) + + return followedFoldIds +} diff --git a/web/hooks/use-user-bets.ts b/web/hooks/use-user-bets.ts index 6af56902..f4d421fb 100644 --- a/web/hooks/use-user-bets.ts +++ b/web/hooks/use-user-bets.ts @@ -1,3 +1,4 @@ +import _ from 'lodash' import { useEffect, useState } from 'react' import { Bet, listenForUserBets } from '../lib/firebase/bets' @@ -10,3 +11,26 @@ export const useUserBets = (userId: string | undefined) => { return bets } + +export const useUserBetContracts = (userId: string | undefined) => { + const [contractIds, setContractIds] = useState() + + useEffect(() => { + if (userId) { + const key = `user-bet-contractIds-${userId}` + + const userBetContractJson = localStorage.getItem(key) + if (userBetContractJson) { + setContractIds(JSON.parse(userBetContractJson)) + } + + return listenForUserBets(userId, (bets) => { + const contractIds = _.uniq(bets.map((bet) => bet.contractId)) + setContractIds(contractIds) + localStorage.setItem(key, JSON.stringify(contractIds)) + }) + } + }, [userId]) + + return contractIds +} diff --git a/web/pages/home.tsx b/web/pages/home.tsx index b464e6eb..3efd7a20 100644 --- a/web/pages/home.tsx +++ b/web/pages/home.tsx @@ -12,15 +12,16 @@ import { Spacer } from '../components/layout/spacer' import { Col } from '../components/layout/col' import { useUser } from '../hooks/use-user' import { useContracts } from '../hooks/use-contracts' -import { getFollowedFolds, listAllFolds } from '../lib/firebase/folds' +import { listAllFolds } from '../lib/firebase/folds' import { Fold } from '../../common/fold' import { filterDefined } from '../../common/util/array' -import { useUserBets } from '../hooks/use-user-bets' +import { useUserBetContracts } from '../hooks/use-user-bets' import { LoadingIndicator } from '../components/loading-indicator' import { FoldTagList } from '../components/tags-list' import { SearchIcon } from '@heroicons/react/outline' import { Row } from '../components/layout/row' import { SparklesIcon } from '@heroicons/react/solid' +import { useFollowedFolds } from '../hooks/use-fold' export async function getStaticProps() { const [contracts, folds] = await Promise.all([ @@ -45,16 +46,7 @@ const Home = (props: { contracts: Contract[]; folds: Fold[] }) => { const contracts = useContracts() ?? props.contracts - const [followedFoldIds, setFollowedFoldIds] = useState( - undefined - ) - - useEffect(() => { - if (user) { - getFollowedFolds(user.id).then((foldIds) => setFollowedFoldIds(foldIds)) - } - }, [user]) - + const followedFoldIds = useFollowedFolds(user) const followedFolds = filterDefined( (followedFoldIds ?? []).map((id) => folds.find((fold) => fold.id === id)) ) @@ -62,13 +54,13 @@ const Home = (props: { contracts: Contract[]; folds: Fold[] }) => { _.flatten(followedFolds.map((fold) => fold.lowercaseTags)) ) - const yourBets = useUserBets(user?.id) - const yourBetContracts = new Set( - (yourBets ?? []).map((bet) => bet.contractId) - ) + const yourBetContractIds = useUserBetContracts(user?.id) + const yourBetContracts = yourBetContractIds + ? new Set(yourBetContractIds) + : undefined const feedContracts = - followedFoldIds && yourBets + followedFoldIds && yourBetContracts ? contracts.filter( (contract) => contract.lowercaseTags.some((tag) => tagSet.has(tag)) || @@ -86,6 +78,7 @@ const Home = (props: { contracts: Contract[]; folds: Fold[] }) => { Promise.all( feedContracts.map((contract) => listAllBets(contract.id)) ).then(setFeedBets) + Promise.all( feedContracts.map((contract) => listAllComments(contract.id)) ).then(setFeedComments)