Fix communites feed to be abbreviated

This commit is contained in:
James Grugett 2022-03-14 15:03:34 -05:00
parent 080bf591e0
commit cb21c7dba2
2 changed files with 33 additions and 27 deletions

View File

@ -88,10 +88,10 @@ function groupBets(
userId: string | undefined, userId: string | undefined,
options: { options: {
hideOutcome: boolean hideOutcome: boolean
truncateComments: boolean abbreviated: boolean
} }
) { ) {
const { hideOutcome, truncateComments } = options const { hideOutcome, abbreviated } = options
const commentsMap = mapCommentsByBetId(comments) const commentsMap = mapCommentsByBetId(comments)
const items: ActivityItem[] = [] const items: ActivityItem[] = []
@ -123,7 +123,7 @@ function groupBets(
bet, bet,
contract, contract,
hideOutcome, hideOutcome,
truncate: truncateComments, truncate: abbreviated,
} }
: { : {
type: 'bet' as const, type: 'bet' as const,
@ -155,7 +155,7 @@ function groupBets(
if (group.length > 0) { if (group.length > 0) {
pushGroup() pushGroup()
} }
return items return abbreviated ? items.slice(-3) : items
} }
function getAnswerGroups( function getAnswerGroups(
@ -164,15 +164,16 @@ function getAnswerGroups(
comments: Comment[], comments: Comment[],
user: User | undefined | null, user: User | undefined | null,
options: { options: {
truncateComments: boolean
sortByProb: boolean sortByProb: boolean
abbreviated: boolean
} }
) { ) {
const { truncateComments, sortByProb } = options const { sortByProb, abbreviated } = options
let outcomes = _.uniq(bets.map((bet) => bet.outcome)).filter( let outcomes = _.uniq(bets.map((bet) => bet.outcome)).filter(
(outcome) => getOutcomeProbability(contract.totalShares, outcome) > 0.01 (outcome) => getOutcomeProbability(contract.totalShares, outcome) > 0.01
) )
if (abbreviated) outcomes = outcomes.slice(-2)
if (sortByProb) { if (sortByProb) {
outcomes = _.sortBy( outcomes = _.sortBy(
outcomes, outcomes,
@ -189,15 +190,17 @@ function getAnswerGroups(
(answer) => answer.id === outcome (answer) => answer.id === outcome
) as Answer ) as Answer
const items = groupBets( let items = groupBets(
answerBets, answerBets,
answerComments, answerComments,
DAY_IN_MS, DAY_IN_MS,
contract, contract,
user?.id, user?.id,
{ hideOutcome: true, truncateComments } { hideOutcome: true, abbreviated }
) )
if (abbreviated) items = items.slice(-2)
return { return {
id: outcome, id: outcome,
type: 'answergroup' as const, type: 'answergroup' as const,
@ -216,8 +219,12 @@ export function getAllContractActivityItems(
bets: Bet[], bets: Bet[],
comments: Comment[], comments: Comment[],
user: User | null | undefined, user: User | null | undefined,
outcome?: string filterToOutcome: string | undefined,
options: {
abbreviated: boolean
}
) { ) {
const { abbreviated } = options
const { outcomeType } = contract const { outcomeType } = contract
bets = bets =
@ -226,25 +233,27 @@ export function getAllContractActivityItems(
: bets.filter((bet) => !(bet.isAnte && (bet.outcome as string) === '0')) : bets.filter((bet) => !(bet.isAnte && (bet.outcome as string) === '0'))
let answer: Answer | undefined let answer: Answer | undefined
if (outcome) { if (filterToOutcome) {
bets = bets.filter((bet) => bet.outcome === outcome) bets = bets.filter((bet) => bet.outcome === filterToOutcome)
answer = contract.answers?.find((answer) => answer.id === outcome) answer = contract.answers?.find((answer) => answer.id === filterToOutcome)
} }
const items: ActivityItem[] = const items: ActivityItem[] =
outcome && answer filterToOutcome && answer
? [{ type: 'createanswer', id: answer.id, contract, answer }] ? [{ type: 'createanswer', id: answer.id, contract, answer }]
: abbreviated
? [{ type: 'question', id: '0', contract, showDescription: false }]
: [{ type: 'description', id: '0', contract }] : [{ type: 'description', id: '0', contract }]
items.push( items.push(
...(outcomeType === 'FREE_RESPONSE' && !outcome ...(outcomeType === 'FREE_RESPONSE' && !filterToOutcome
? getAnswerGroups(contract, bets, comments, user, { ? getAnswerGroups(contract, bets, comments, user, {
truncateComments: false,
sortByProb: true, sortByProb: true,
abbreviated,
}) })
: groupBets(bets, comments, DAY_IN_MS, contract, user?.id, { : groupBets(bets, comments, DAY_IN_MS, contract, user?.id, {
hideOutcome: !!outcome, hideOutcome: !!filterToOutcome,
truncateComments: false, abbreviated,
})) }))
) )
@ -274,7 +283,7 @@ export function getRecentContractActivityItems(
showDescription: false, showDescription: false,
} }
let items: ActivityItem[] = [] const items: ActivityItem[] = []
if (contract.outcomeType === 'FREE_RESPONSE') { if (contract.outcomeType === 'FREE_RESPONSE') {
// Keep last three comments. // Keep last three comments.
@ -307,18 +316,18 @@ export function getRecentContractActivityItems(
items.push( items.push(
...getAnswerGroups(contract, bets, comments, user, { ...getAnswerGroups(contract, bets, comments, user, {
truncateComments: true,
sortByProb: false, sortByProb: false,
abbreviated: true,
}) })
) )
} else { } else {
items.push( items.push(
...groupBets(bets, comments, DAY_IN_MS, contract, user?.id, { ...groupBets(bets, comments, DAY_IN_MS, contract, user?.id, {
hideOutcome: false, hideOutcome: false,
truncateComments: true, abbreviated: true,
}) })
) )
} }
return [questionItem, ...items.slice(-3)] return [questionItem, ...items]
} }

View File

@ -32,7 +32,7 @@ export function ContractActivity(props: {
const updatedBets = mode === 'only-recent' ? undefined : useBets(contract.id) const updatedBets = mode === 'only-recent' ? undefined : useBets(contract.id)
const bets = updatedBets ?? props.bets const bets = updatedBets ?? props.bets
let items = const items =
mode === 'only-recent' mode === 'only-recent'
? getRecentContractActivityItems(contract, bets, comments, user) ? getRecentContractActivityItems(contract, bets, comments, user)
: getAllContractActivityItems( : getAllContractActivityItems(
@ -40,13 +40,10 @@ export function ContractActivity(props: {
bets, bets,
comments, comments,
user, user,
filterToOutcome filterToOutcome,
{ abbreviated: mode === 'abbreviated' }
) )
if (mode === 'abbreviated') {
items = [items[0], ...items.slice(-3)]
}
return ( return (
<FeedItems <FeedItems
contract={contract} contract={contract}