Fix comment inputs on mobile

This commit is contained in:
Ian Philips 2022-06-09 10:15:34 -06:00
parent bdb3bbd960
commit 64f04185d4
2 changed files with 15 additions and 27 deletions

View File

@ -2,7 +2,7 @@ import { Answer } from 'common/answer'
import { Bet } from 'common/bet' import { Bet } from 'common/bet'
import { Comment } from 'common/comment' import { Comment } from 'common/comment'
import { formatPercent } from 'common/util/format' import { formatPercent } from 'common/util/format'
import React, { useEffect, useState } from 'react' import React, { useEffect, useMemo, useState } from 'react'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Modal } from 'web/components/layout/modal' import { Modal } from 'web/components/layout/modal'
import { AnswerBetPanel } from 'web/components/answers/answer-bet-panel' import { AnswerBetPanel } from 'web/components/answers/answer-bet-panel'
@ -113,14 +113,16 @@ export function FeedAnswerCommentGroup(props: {
// Only show one comment input for a bet at a time // Only show one comment input for a bet at a time
const usersMostRecentBet = bets const usersMostRecentBet = bets
.filter((b) => b.userId === user?.id) .filter((b) => b.userId === user?.id)
.sort((a, b) => b.createdTime - a.createdTime)[0] .sort((a, b) => b.createdTime - a.createdTime)
if ( if (
usersMostRecentBet && usersMostRecentBet.length > 1 &&
usersMostRecentBet.outcome !== answer.number.toString() usersMostRecentBet[0].outcome !== answer.number.toString()
) { ) {
setShowReply(false) setShowReply(false)
} }
}, [answer.number, bets, user]) // if we pass memoized bets this still runs on every render, which we don't want
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [bets.length, user, answer.number])
useEffect(() => { useEffect(() => {
if (showReply && inputRef) inputRef.focus() if (showReply && inputRef) inputRef.focus()

View File

@ -336,7 +336,6 @@ export function CommentInput(props: {
} = props } = props
const user = useUser() const user = useUser()
const [comment, setComment] = useState('') const [comment, setComment] = useState('')
const [focused, setFocused] = useState(false)
const [isSubmitting, setIsSubmitting] = useState(false) const [isSubmitting, setIsSubmitting] = useState(false)
const mostRecentCommentableBet = getMostRecentCommentableBet( const mostRecentCommentableBet = getMostRecentCommentableBet(
@ -369,7 +368,6 @@ export function CommentInput(props: {
) )
onSubmitComment?.() onSubmitComment?.()
setComment('') setComment('')
setFocused(false)
setIsSubmitting(false) setIsSubmitting(false)
} }
@ -379,8 +377,6 @@ export function CommentInput(props: {
betsByCurrentUser betsByCurrentUser
) )
const shouldCollapseAfterClickOutside = !comment
const isNumeric = contract.outcomeType === 'NUMERIC' const isNumeric = contract.outcomeType === 'NUMERIC'
return ( return (
@ -441,11 +437,7 @@ export function CommentInput(props: {
? 'Write a reply... ' ? 'Write a reply... '
: 'Write a comment...' : 'Write a comment...'
} }
autoFocus={focused} autoFocus={false}
onFocus={() => setFocused(true)}
onBlur={() =>
shouldCollapseAfterClickOutside && setFocused(false)
}
maxLength={MAX_COMMENT_LENGTH} maxLength={MAX_COMMENT_LENGTH}
disabled={isSubmitting} disabled={isSubmitting}
onKeyDown={(e) => { onKeyDown={(e) => {
@ -457,7 +449,7 @@ export function CommentInput(props: {
}} }}
/> />
<Col className={clsx(focused ? 'justify-end' : 'justify-center')}> <Col className={clsx('justify-end')}>
{user && !isSubmitting && ( {user && !isSubmitting && (
<button <button
className={clsx( className={clsx(
@ -465,22 +457,16 @@ export function CommentInput(props: {
parentCommentId || parentAnswerOutcome parentCommentId || parentAnswerOutcome
? ' bottom-4' ? ' bottom-4'
: ' bottom-2', : ' bottom-2',
(!focused || !comment) && !comment && 'pointer-events-none text-gray-500'
'pointer-events-none text-gray-500'
)} )}
onClick={() => { onClick={() => {
if (!focused) return submitComment(id)
else {
submitComment(id)
}
}} }}
> >
{focused && ( <PaperAirplaneIcon
<PaperAirplaneIcon className={'m-0 min-w-[22px] rotate-90 p-0 '}
className={'m-0 min-w-[22px] rotate-90 p-0 '} height={25}
height={25} />
/>
)}
</button> </button>
)} )}
{isSubmitting && ( {isSubmitting && (