condense comment logic in one component (#119)
This commit is contained in:
parent
ab4dbc798c
commit
8da36298e5
|
@ -227,35 +227,27 @@ export function CommentInput(props: {
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const [comment, setComment] = useState('')
|
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?
|
// Should this be oldest bet or most recent bet?
|
||||||
const mostRecentCommentableBet = betsByCurrentUser
|
const mostRecentCommentableBet = betsByCurrentUser
|
||||||
.filter(
|
.filter(
|
||||||
(bet) =>
|
(bet) =>
|
||||||
canCommentOnBet(bet.userId, bet.createdTime, user) &&
|
canCommentOnBet(bet, bet.createdTime, user) &&
|
||||||
!comments.some((comment) => comment.betId == bet.id)
|
!comments.some((comment) => comment.betId == bet.id)
|
||||||
)
|
)
|
||||||
.sort((b1, b2) => b1.createdTime - b2.createdTime)
|
.sort((b1, b2) => b1.createdTime - b2.createdTime)
|
||||||
.pop()
|
.pop()
|
||||||
|
|
||||||
if (mostRecentCommentableBet) {
|
const { id } = mostRecentCommentableBet || { id: undefined }
|
||||||
return (
|
|
||||||
<FeedBet
|
async function submitComment(id: string | undefined) {
|
||||||
contract={contract}
|
if (!comment) return
|
||||||
bet={mostRecentCommentableBet}
|
if (!user) {
|
||||||
hideOutcome={false}
|
return await firebaseLogin()
|
||||||
smallAvatar={false}
|
|
||||||
/>
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
await createComment(contract.id, comment, user, id)
|
||||||
|
setComment('')
|
||||||
|
}
|
||||||
|
|
||||||
const { userPosition, userPositionMoney, yesFloorShares, noFloorShares } =
|
const { userPosition, userPositionMoney, yesFloorShares, noFloorShares } =
|
||||||
getBettorsPosition(contract, Date.now(), betsByCurrentUser)
|
getBettorsPosition(contract, Date.now(), betsByCurrentUser)
|
||||||
|
|
||||||
|
@ -267,7 +259,14 @@ export function CommentInput(props: {
|
||||||
</div>
|
</div>
|
||||||
<div className={'min-w-0 flex-1 py-1.5'}>
|
<div className={'min-w-0 flex-1 py-1.5'}>
|
||||||
<div className="text-sm text-gray-500">
|
<div className="text-sm text-gray-500">
|
||||||
{user && userPosition > 0 && (
|
{mostRecentCommentableBet && (
|
||||||
|
<BetStatusText
|
||||||
|
contract={contract}
|
||||||
|
bet={mostRecentCommentableBet}
|
||||||
|
isSelf={true}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{!mostRecentCommentableBet && user && userPosition > 0 && (
|
||||||
<>
|
<>
|
||||||
{'You have ' + userPositionMoney + ' '}
|
{'You have ' + userPositionMoney + ' '}
|
||||||
<>
|
<>
|
||||||
|
@ -290,7 +289,7 @@ export function CommentInput(props: {
|
||||||
maxLength={MAX_COMMENT_LENGTH}
|
maxLength={MAX_COMMENT_LENGTH}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
||||||
submitComment()
|
submitComment(id)
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
@ -298,7 +297,7 @@ export function CommentInput(props: {
|
||||||
className={
|
className={
|
||||||
'btn btn-outline btn-sm text-transform: mt-1 capitalize'
|
'btn btn-outline btn-sm text-transform: mt-1 capitalize'
|
||||||
}
|
}
|
||||||
onClick={submitComment}
|
onClick={() => submitComment(id)}
|
||||||
>
|
>
|
||||||
{user ? 'Comment' : 'Sign in to comment'}
|
{user ? 'Comment' : 'Sign in to comment'}
|
||||||
</button>
|
</button>
|
||||||
|
@ -372,24 +371,12 @@ export function FeedBet(props: {
|
||||||
bet: Bet
|
bet: Bet
|
||||||
hideOutcome: boolean
|
hideOutcome: boolean
|
||||||
smallAvatar: boolean
|
smallAvatar: boolean
|
||||||
hideComment?: boolean
|
|
||||||
bettor?: User // If set: reveal bettor identity
|
bettor?: User // If set: reveal bettor identity
|
||||||
}) {
|
}) {
|
||||||
const { contract, bet, hideOutcome, smallAvatar, bettor, hideComment } = props
|
const { contract, bet, hideOutcome, smallAvatar, bettor } = props
|
||||||
const { id, amount, outcome, createdTime, userId } = bet
|
const { userId } = bet
|
||||||
const user = useUser()
|
const user = useUser()
|
||||||
const isSelf = user?.id === userId
|
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 (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -421,9 +408,36 @@ export function FeedBet(props: {
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
<div className={'min-w-0 flex-1 py-1.5'}>
|
<div className={'min-w-0 flex-1 py-1.5'}>
|
||||||
|
<BetStatusText
|
||||||
|
bet={bet}
|
||||||
|
contract={contract}
|
||||||
|
isSelf={isSelf}
|
||||||
|
hideOutcome={hideOutcome}
|
||||||
|
bettor={bettor}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
</Row>
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
function BetStatusText(props: {
|
||||||
|
contract: Contract
|
||||||
|
bet: Bet
|
||||||
|
isSelf: boolean
|
||||||
|
hideOutcome?: boolean
|
||||||
|
bettor?: User
|
||||||
|
}) {
|
||||||
|
const { bet, contract, hideOutcome, bettor, isSelf } = props
|
||||||
|
const { amount, outcome, createdTime } = bet
|
||||||
|
|
||||||
|
const bought = amount >= 0 ? 'bought' : 'sold'
|
||||||
|
const money = formatMoney(Math.abs(amount))
|
||||||
|
|
||||||
|
return (
|
||||||
<div className="text-sm text-gray-500">
|
<div className="text-sm text-gray-500">
|
||||||
<span>{isSelf ? 'You' : bettor ? bettor.name : 'A trader'}</span>{' '}
|
<span>{isSelf ? 'You' : bettor ? bettor.name : 'A trader'}</span> {bought}{' '}
|
||||||
{bought} {money}
|
{money}
|
||||||
{!hideOutcome && (
|
{!hideOutcome && (
|
||||||
<>
|
<>
|
||||||
{' '}
|
{' '}
|
||||||
|
@ -436,34 +450,7 @@ export function FeedBet(props: {
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<RelativeTimestamp time={createdTime} />
|
<RelativeTimestamp time={createdTime} />
|
||||||
{(canComment || comment) && (
|
|
||||||
<div className="mt-2">
|
|
||||||
<Textarea
|
|
||||||
value={comment}
|
|
||||||
onChange={(e) => setComment(e.target.value)}
|
|
||||||
className="textarea textarea-bordered w-full resize-none"
|
|
||||||
placeholder="Add a comment..."
|
|
||||||
rows={3}
|
|
||||||
maxLength={MAX_COMMENT_LENGTH}
|
|
||||||
onKeyDown={(e) => {
|
|
||||||
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
|
|
||||||
submitComment()
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
/>
|
|
||||||
<button
|
|
||||||
className="btn btn-outline btn-sm text-transform: mt-1 capitalize"
|
|
||||||
onClick={submitComment}
|
|
||||||
disabled={!canComment}
|
|
||||||
>
|
|
||||||
Comment
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</Row>
|
|
||||||
</>
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -573,14 +560,12 @@ export function FeedQuestion(props: {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function canCommentOnBet(
|
function canCommentOnBet(bet: Bet, createdTime: number, user?: User | null) {
|
||||||
userId: string,
|
const isSelf = user?.id === bet.userId
|
||||||
createdTime: number,
|
|
||||||
user?: User | null
|
|
||||||
) {
|
|
||||||
const isSelf = user?.id === userId
|
|
||||||
// You can comment if your bet was posted in the last hour
|
// You can comment if your bet was posted in the last hour
|
||||||
return isSelf && Date.now() - createdTime < 60 * 60 * 1000
|
return (
|
||||||
|
!bet.isRedemption && isSelf && Date.now() - createdTime < 60 * 60 * 1000
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
function FeedDescription(props: { contract: Contract }) {
|
function FeedDescription(props: { contract: Contract }) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user