From 5a9d8e3f5d4627fc11490866b6ab8ae47b3a902c Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 31 Aug 2022 09:27:37 -0600 Subject: [PATCH] Show how much you've tipped a market --- web/components/contract/like-market-button.tsx | 15 +++++++++++---- web/hooks/use-tip-txns.ts | 12 ++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/web/components/contract/like-market-button.tsx b/web/components/contract/like-market-button.tsx index 0fed0518..6ea6996d 100644 --- a/web/components/contract/like-market-button.tsx +++ b/web/components/contract/like-market-button.tsx @@ -1,6 +1,6 @@ import { HeartIcon } from '@heroicons/react/outline' import { Button } from 'web/components/button' -import React from 'react' +import React, { useMemo } from 'react' import { Contract } from 'common/contract' import { User } from 'common/user' import { useUserLikes } from 'web/hooks/use-likes' @@ -11,13 +11,20 @@ import { LIKE_TIP_AMOUNT } from 'common/like' import clsx from 'clsx' import { Col } from 'web/components/layout/col' import { firebaseLogin } from 'web/lib/firebase/users' +import { useMarketTipTxns } from 'web/hooks/use-tip-txns' +import { sum } from 'lodash' export function LikeMarketButton(props: { contract: Contract user: User | null | undefined }) { const { contract, user } = props - + const tips = useMarketTipTxns(contract.id).filter( + (txn) => txn.fromId === user?.id + ) + const totalTipped = useMemo(() => { + return sum(tips.map((tip) => tip.amount)) + }, [tips]) const likes = useUserLikes(user?.id) const userLikedContractIds = likes ?.filter((l) => l.type === 'contract') @@ -36,7 +43,7 @@ export function LikeMarketButton(props: { color={'gray-white'} onClick={onLike} > - + - Tip + Tip {totalTipped > 0 ? `(${formatMoney(totalTipped)})` : ''} ) diff --git a/web/hooks/use-tip-txns.ts b/web/hooks/use-tip-txns.ts index 50542402..8d26176f 100644 --- a/web/hooks/use-tip-txns.ts +++ b/web/hooks/use-tip-txns.ts @@ -29,3 +29,15 @@ export function useTipTxns(on: { }) }, [txns]) } + +export function useMarketTipTxns(contractId: string): TipTxn[] { + const [txns, setTxns] = useState([]) + + useEffect(() => { + return listenForTipTxns(contractId, (txns) => { + setTxns(txns.filter((txn) => !txn.data.commentId)) + }) + }, [contractId]) + + return txns +}