From 8da36298e5f6ee2b095351a8e19ac2e291009ede Mon Sep 17 00:00:00 2001 From: Boa Date: Tue, 3 May 2022 07:51:25 -0600 Subject: [PATCH 01/34] condense comment logic in one component (#119) --- web/components/feed/feed-items.tsx | 151 +++++++++++++---------------- 1 file changed, 68 insertions(+), 83 deletions(-) diff --git a/web/components/feed/feed-items.tsx b/web/components/feed/feed-items.tsx index e21db3e5..a3b650c0 100644 --- a/web/components/feed/feed-items.tsx +++ b/web/components/feed/feed-items.tsx @@ -227,35 +227,27 @@ export function CommentInput(props: { const user = useUser() const [comment, setComment] = useState('') - async function submitComment() { - if (!comment) return - if (!user) { - return await firebaseLogin() - } - await createComment(contract.id, comment, user) - setComment('') - } - // Should this be oldest bet or most recent bet? const mostRecentCommentableBet = betsByCurrentUser .filter( (bet) => - canCommentOnBet(bet.userId, bet.createdTime, user) && + canCommentOnBet(bet, bet.createdTime, user) && !comments.some((comment) => comment.betId == bet.id) ) .sort((b1, b2) => b1.createdTime - b2.createdTime) .pop() - if (mostRecentCommentableBet) { - return ( - - ) + const { id } = mostRecentCommentableBet || { id: undefined } + + async function submitComment(id: string | undefined) { + if (!comment) return + if (!user) { + return await firebaseLogin() + } + await createComment(contract.id, comment, user, id) + setComment('') } + const { userPosition, userPositionMoney, yesFloorShares, noFloorShares } = getBettorsPosition(contract, Date.now(), betsByCurrentUser) @@ -267,7 +259,14 @@ export function CommentInput(props: {
- {user && userPosition > 0 && ( + {mostRecentCommentableBet && ( + + )} + {!mostRecentCommentableBet && user && userPosition > 0 && ( <> {'You have ' + userPositionMoney + ' '} <> @@ -290,7 +289,7 @@ export function CommentInput(props: { maxLength={MAX_COMMENT_LENGTH} onKeyDown={(e) => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { - submitComment() + submitComment(id) } }} /> @@ -298,7 +297,7 @@ export function CommentInput(props: { className={ 'btn btn-outline btn-sm text-transform: mt-1 capitalize' } - onClick={submitComment} + onClick={() => submitComment(id)} > {user ? 'Comment' : 'Sign in to comment'} @@ -372,24 +371,12 @@ export function FeedBet(props: { bet: Bet hideOutcome: boolean smallAvatar: boolean - hideComment?: boolean bettor?: User // If set: reveal bettor identity }) { - const { contract, bet, hideOutcome, smallAvatar, bettor, hideComment } = props - const { id, amount, outcome, createdTime, userId } = bet + const { contract, bet, hideOutcome, smallAvatar, bettor } = props + const { userId } = bet const user = useUser() const isSelf = user?.id === userId - const canComment = canCommentOnBet(userId, createdTime, user) && !hideComment - - const [comment, setComment] = useState('') - async function submitComment() { - if (!user || !comment || !canComment) return - await createComment(contract.id, comment, user, id) - setComment('') - } - - const bought = amount >= 0 ? 'bought' : 'sold' - const money = formatMoney(Math.abs(amount)) return ( <> @@ -421,52 +408,52 @@ export function FeedBet(props: { )}
-
- {isSelf ? 'You' : bettor ? bettor.name : 'A trader'}{' '} - {bought} {money} - {!hideOutcome && ( - <> - {' '} - of{' '} - - - )} - - {(canComment || comment) && ( -
-