diff --git a/web/components/bet/user-bets-table.tsx b/web/components/bet/user-bets-table.tsx index aef0f98029..2fefec7548 100644 --- a/web/components/bet/user-bets-table.tsx +++ b/web/components/bet/user-bets-table.tsx @@ -1,7 +1,7 @@ 'use client' import { ChevronUpIcon } from '@heroicons/react/solid' import clsx from 'clsx' -import { Bet, LimitBet } from 'common/bet' +import { LimitBet } from 'common/bet' import { getContractBetNullMetrics } from 'common/calculate' import { Contract, contractPath, CPMMContract } from 'common/contract' import { ContractMetric } from 'common/contract-metric' @@ -22,7 +22,7 @@ import { SWEEPIES_MONIKER, } from 'common/util/format' import { searchInAny } from 'common/util/parse' -import { Dictionary, groupBy, max, sortBy, sum, uniqBy } from 'lodash' +import { Dictionary, max, sortBy, sum, uniqBy } from 'lodash' import Link from 'next/link' import { ReactNode, useEffect, useMemo, useState } from 'react' import { BiCaretDown, BiCaretUp } from 'react-icons/bi' @@ -47,7 +47,7 @@ import { usePersistentInMemoryState } from 'web/hooks/use-persistent-in-memory-s import { usePersistentLocalState } from 'web/hooks/use-persistent-local-state' import { usePersistentQueryState } from 'web/hooks/use-persistent-query-state' import { useIsAuthorized, useUser } from 'web/hooks/use-user' -import { api, getUserContractsMetricsWithContracts } from 'web/lib/api/api' +import { getUserContractsMetricsWithContracts } from 'web/lib/api/api' import { User } from 'web/lib/firebase/users' import DropdownMenu from '../comments/dropdown-menu' import { Col } from '../layout/col' @@ -56,6 +56,7 @@ import { LoadingIndicator } from '../widgets/loading-indicator' import { linkClass } from '../widgets/site-link' import { Tooltip } from '../widgets/tooltip' import { SweepiesCoin } from 'web/public/custom-components/sweepiesCoin' +import { useContractBets } from 'web/hooks/use-bets' type BetSort = | 'newest' @@ -630,22 +631,8 @@ function BetsTable(props: { : 'col-span-1' const [expandedIds, setExpandedIds] = useState([]) - const [userBets, setUserBets] = useState>({}) - const hideBetsBefore = areYourBets ? 0 : JUNE_1_2022 const setNewExpandedId = async (id: string) => { - if (!userBets[id]) { - api('bets', { - contractId: id, - userId: user.id, - afterTime: hideBetsBefore, - }).then((newBets) => - setUserBets((oldBets) => ({ - ...oldBets, - ...groupBy(newBets, 'contractId'), - })) - ) - } setExpandedIds((oldIds) => oldIds.includes(id) ? oldIds.filter((oldId) => oldId !== id) @@ -717,18 +704,6 @@ function BetsTable(props: { {contracts .slice(currentSlice, currentSlice + rowsPerSection) .map((contract) => { - const bets: Bet[] | undefined = userBets[contract.id] - const limitBets = (bets ?? []).filter( - (bet) => - bet.limitProb !== undefined && !bet.isCancelled && !bet.isFilled - ) as LimitBet[] - const includeSellButtonForUser = - areYourBets && - !contract.isResolved && - (contract.closeTime ?? 0) > Date.now() && - contract.mechanism === 'cpmm-1' - ? signedInUser - : undefined return ( - {expandedIds.includes(contract.id) && - (bets === undefined ? ( - - - - ) : ( - - - {contract.mechanism === 'cpmm-1' && - limitBets.length > 0 && ( -
-
- Limit orders -
- -
- )} - - - ))} + {expandedIds.includes(contract.id) && ( + + )}
@@ -890,6 +835,70 @@ function BetsTable(props: { ) } +const ExpandedBetRow = (props: { + contract: Contract + user: User + signedInUser: User | null | undefined + contractMetrics: ContractMetric + areYourBets: boolean +}) => { + const { contract, user, signedInUser, contractMetrics, areYourBets } = props + const hideBetsBefore = areYourBets ? 0 : JUNE_1_2022 + const bets = useContractBets(contract.id, { + userId: user.id, + afterTime: hideBetsBefore, + }) + const limitBets = bets?.filter( + (bet) => bet.limitProb !== undefined && !bet.isCancelled && !bet.isFilled + ) as LimitBet[] + + const includeSellButtonForUser = + areYourBets && + !contract.isResolved && + (contract.closeTime ?? 0) > Date.now() && + contract.mechanism === 'cpmm-1' + ? signedInUser + : undefined + if (bets === undefined) { + return ( + + + + ) + } + return ( + + + {contract.mechanism === 'cpmm-1' && limitBets.length > 0 && ( +
+
Limit orders
+ +
+ )} + + + ) +} + const NumberCell = (props: { num: number change?: boolean diff --git a/web/hooks/use-bets.ts b/web/hooks/use-bets.ts index 19ccade665..10041fa2c4 100644 --- a/web/hooks/use-bets.ts +++ b/web/hooks/use-bets.ts @@ -86,6 +86,26 @@ export const useContractBets = ( enabled, }) + // We have to listen to cancels as well, since we don't get them in the `new-bet` topic. + useApiSubscription({ + topics: [`contract/${contractId}/orders`], + onBroadcast: (msg) => { + const betUpdates = msg.data.bets as LimitBet[] + const cancelledBets = betUpdates.filter( + (bet: LimitBet) => bet.isCancelled + ) + setNewBets((currentBets) => { + return currentBets.map((bet) => { + const cancelledBet = cancelledBets.find( + (cancelledBet) => cancelledBet.id === bet.id + ) + return cancelledBet ? { ...bet, isCancelled: true } : bet + }) + }) + }, + enabled, + }) + return newBets }