changing answers to be colors

This commit is contained in:
ingawei 2022-10-12 14:36:12 -07:00
parent 1d618ba337
commit e14f3b69a4
4 changed files with 64 additions and 33 deletions

View File

@ -27,6 +27,13 @@ import { CHOICE_ANSWER_COLORS } from '../charts/contract/choice'
import { useChartAnswers } from '../charts/contract/choice' import { useChartAnswers } from '../charts/contract/choice'
import { ChatIcon } from '@heroicons/react/outline' import { ChatIcon } from '@heroicons/react/outline'
export function getAnswerColor(answer: Answer, answersArray: Answer[]) {
const colorIndex = answersArray.indexOf(answer)
return colorIndex != undefined && colorIndex < CHOICE_ANSWER_COLORS.length
? CHOICE_ANSWER_COLORS[colorIndex] + '90' // semi-transparent
: '#B1B1C755'
}
export function AnswersPanel(props: { export function AnswersPanel(props: {
contract: FreeResponseContract | MultipleChoiceContract contract: FreeResponseContract | MultipleChoiceContract
onAnswerCommentClick: (answer: Answer) => void onAnswerCommentClick: (answer: Answer) => void
@ -107,10 +114,9 @@ export function AnswersPanel(props: {
? 'checkbox' ? 'checkbox'
: undefined : undefined
const colorSortedAnswer = useChartAnswers(contract).map( const answersArray = useChartAnswers(contract)
(value, _index) => value.text
)
console.log(answersArray)
return ( return (
<Col className="gap-3"> <Col className="gap-3">
{(resolveOption || resolution) && {(resolveOption || resolution) &&
@ -139,8 +145,8 @@ export function AnswersPanel(props: {
key={item.id} key={item.id}
answer={item} answer={item}
contract={contract} contract={contract}
colorIndex={colorSortedAnswer.indexOf(item.text)}
onAnswerCommentClick={onAnswerCommentClick} onAnswerCommentClick={onAnswerCommentClick}
color={getAnswerColor(item, answersArray)}
/> />
))} ))}
{hasZeroBetAnswers && !showAllAnswers && ( {hasZeroBetAnswers && !showAllAnswers && (
@ -185,18 +191,14 @@ export function AnswersPanel(props: {
function OpenAnswer(props: { function OpenAnswer(props: {
contract: FreeResponseContract | MultipleChoiceContract contract: FreeResponseContract | MultipleChoiceContract
answer: Answer answer: Answer
colorIndex: number | undefined color: string
onAnswerCommentClick: (answer: Answer) => void onAnswerCommentClick: (answer: Answer) => void
}) { }) {
const { answer, contract, colorIndex, onAnswerCommentClick } = props const { answer, contract, onAnswerCommentClick, color } = props
const { username, avatarUrl, text } = answer const { username, avatarUrl, text } = answer
const prob = getDpmOutcomeProbability(contract.totalShares, answer.id) const prob = getDpmOutcomeProbability(contract.totalShares, answer.id)
const probPercent = formatPercent(prob) const probPercent = formatPercent(prob)
const [open, setOpen] = useState(false) const [open, setOpen] = useState(false)
const color =
colorIndex != undefined && colorIndex < CHOICE_ANSWER_COLORS.length
? CHOICE_ANSWER_COLORS[colorIndex] + '55' // semi-transparent
: '#B1B1C755'
const colorWidth = 100 * Math.max(prob, 0.01) const colorWidth = 100 * Math.max(prob, 0.01)
return ( return (

View File

@ -8,6 +8,7 @@ import { useEffect, useState } from 'react'
import { useUser } from 'web/hooks/use-user' import { useUser } from 'web/hooks/use-user'
import { MAX_COMMENT_LENGTH } from 'web/lib/firebase/comments' import { MAX_COMMENT_LENGTH } from 'web/lib/firebase/comments'
import Curve from 'web/public/custom-components/curve' import Curve from 'web/public/custom-components/curve'
import { getAnswerColor } from './answers/answers-panel'
import { Avatar } from './avatar' import { Avatar } from './avatar'
import { TextEditor, useTextEditor } from './editor' import { TextEditor, useTextEditor } from './editor'
import { CommentsAnswer } from './feed/feed-answer-comment-group' import { CommentsAnswer } from './feed/feed-answer-comment-group'
@ -87,7 +88,6 @@ export function AnswerCommentInput(props: {
id: answerResponse.id, id: answerResponse.id,
username: answerResponse.username, username: answerResponse.username,
} }
return ( return (
<> <>
<CommentsAnswer answer={answerResponse} contract={contract} /> <CommentsAnswer answer={answerResponse} contract={contract} />

View File

@ -38,6 +38,8 @@ import TriangleDownFillIcon from 'web/lib/icons/triangle-down-fill-icon'
import Curve from 'web/public/custom-components/curve' import Curve from 'web/public/custom-components/curve'
import { Answer } from 'common/answer' import { Answer } from 'common/answer'
import { AnswerCommentInput } from '../comment-input' import { AnswerCommentInput } from '../comment-input'
import { getAnswerColor } from '../answers/answers-panel'
import { useChartAnswers } from '../charts/contract/choice'
export function ContractTabs(props: { export function ContractTabs(props: {
contract: Contract contract: Contract
@ -161,6 +163,8 @@ const CommentsTabContent = memo(function CommentsTabContent(props: {
</Row> </Row>
) )
if (contract.outcomeType === 'FREE_RESPONSE') { if (contract.outcomeType === 'FREE_RESPONSE') {
const answersArray = useChartAnswers(contract)
return ( return (
<> <>
<ContractCommentInput className="mb-5" contract={contract} /> <ContractCommentInput className="mb-5" contract={contract} />
@ -194,10 +198,15 @@ const CommentsTabContent = memo(function CommentsTabContent(props: {
console.error('Could not find answer that matches ID') console.error('Could not find answer that matches ID')
return <></> return <></>
} }
const color = getAnswerColor(answer, answersArray)
return ( return (
<> <>
<Row className="gap-2"> <Row className="gap-2">
<CommentsAnswer answer={answer} contract={contract} /> <CommentsAnswer
answer={answer}
contract={contract}
color={color}
/>
</Row> </Row>
<Row> <Row>
<div className="ml-1"> <div className="ml-1">

View File

@ -1,6 +1,6 @@
import { Answer } from 'common/answer' import { Answer } from 'common/answer'
import { Contract } from 'common/contract' import { Contract } from 'common/contract'
import React, { useEffect, useRef } from 'react' import React, { useEffect, useRef, useState } from 'react'
import { Col } from 'web/components/layout/col' import { Col } from 'web/components/layout/col'
import { Row } from 'web/components/layout/row' import { Row } from 'web/components/layout/row'
import { Avatar } from 'web/components/avatar' import { Avatar } from 'web/components/avatar'
@ -8,22 +8,41 @@ import { CopyLinkDateTimeComponent } from 'web/components/feed/copy-link-date-ti
import { useRouter } from 'next/router' import { useRouter } from 'next/router'
import { UserLink } from 'web/components/user-link' import { UserLink } from 'web/components/user-link'
export function CommentsAnswer(props: { answer: Answer; contract: Contract }) { export function CommentsAnswer(props: {
const { answer, contract } = props answer: Answer
contract: Contract
color: string
}) {
const { answer, contract, color } = props
const { username, avatarUrl, name, text } = answer const { username, avatarUrl, name, text } = answer
const answerElementId = `answer-${answer.id}` const answerElementId = `answer-${answer.id}`
const router = useRouter()
const highlighted = router.asPath.endsWith(`#${answerElementId}`) const { isReady, asPath } = useRouter()
const [highlighted, setHighlighted] = useState(false)
const answerRef = useRef<HTMLDivElement>(null) const answerRef = useRef<HTMLDivElement>(null)
useEffect(() => { useEffect(() => {
if (highlighted && answerRef.current != null) { if (isReady && asPath.endsWith(`#${answerElementId}`)) {
setHighlighted(true)
}
}, [isReady, asPath, answerElementId])
useEffect(() => {
if (highlighted && answerRef.current) {
answerRef.current.scrollIntoView(true) answerRef.current.scrollIntoView(true)
} }
}, [highlighted]) }, [highlighted])
return ( return (
<Col className="bg-greyscale-2 w-fit gap-1 rounded-t-xl rounded-bl-xl py-2 px-4"> <Row>
{/* TODO: known bug, doesn't grab color in time and it is gray */}
<div
className="w-2"
style={{
background: color ? color : '#B1B1C755',
}}
/>
<Col className="w-fit gap-1 bg-gray-100 py-2 pl-2 pr-4">
<Row className="gap-2"> <Row className="gap-2">
<Avatar username={username} avatarUrl={avatarUrl} size="xxs" /> <Avatar username={username} avatarUrl={avatarUrl} size="xxs" />
<div className="text-greyscale-6 text-xs"> <div className="text-greyscale-6 text-xs">
@ -38,5 +57,6 @@ export function CommentsAnswer(props: { answer: Answer; contract: Contract }) {
</Row> </Row>
<div className="text-sm">{text}</div> <div className="text-sm">{text}</div>
</Col> </Col>
</Row>
) )
} }