Whalebird-desktop-client-ma.../renderer/components/timelines/status/Poll.tsx

134 lines
4.2 KiB
TypeScript
Raw Normal View History

import { Button, Checkbox, Progress, Radio } from '@material-tailwind/react'
2023-11-04 05:03:56 +01:00
import dayjs from 'dayjs'
import { Entity, MegalodonInterface } from 'megalodon'
2023-11-04 10:14:00 +01:00
import { HTMLAttributes } from 'react'
2023-11-04 07:32:37 +01:00
import { FormattedMessage } from 'react-intl'
2023-11-04 05:03:56 +01:00
type Props = {
poll: Entity.Poll
client: MegalodonInterface
onRefresh: () => void
2023-11-04 10:14:00 +01:00
} & HTMLAttributes<HTMLElement>
2023-11-04 05:03:56 +01:00
export default function Poll(props: Props) {
if (props.poll.voted || props.poll.expired) {
return <PollResult {...props} />
} else if (props.poll.multiple) {
return <MultiplePoll {...props} />
} else {
return <SimplePoll {...props} />
}
}
function SimplePoll(props: Props) {
const vote = async () => {
const elements = document.getElementsByName(props.poll.id)
let checked: number | null = null
elements.forEach((element, index) => {
if ((element as HTMLInputElement).checked) {
checked = index
}
})
if (checked !== null) {
await props.client.votePoll(props.poll.id, [checked])
props.onRefresh()
}
}
return (
2023-11-04 10:14:00 +01:00
<div className={props.className + ' my-2'}>
2023-11-04 05:03:56 +01:00
{props.poll.options.map((option, index) => (
<div key={index} className="flex items-center gap-2 my-2 pl-1">
<Radio id={option.title} name={props.poll.id} value={option.title} label={option.title} />
2023-11-04 05:03:56 +01:00
</div>
))}
<div className="flex gap-2 items-center mt-2">
<Button size="sm" onClick={vote} variant="outlined">
2023-11-04 07:32:37 +01:00
<FormattedMessage id="timeline.status.poll.vote" />
2023-11-04 05:03:56 +01:00
</Button>
2023-11-04 07:32:37 +01:00
<div>
<FormattedMessage id="timeline.status.poll.people" values={{ num: props.poll.votes_count }} />
</div>
2023-11-04 05:03:56 +01:00
<div>
<time dateTime={props.poll.expires_at}>{dayjs(props.poll.expires_at).format('YYYY-MM-DD HH:mm:ss')}</time>
</div>
</div>
</div>
)
}
function MultiplePoll(props: Props) {
const vote = async () => {
let checked: Array<number> = []
props.poll.options.forEach((value, index) => {
const element = document.getElementById(value.title) as HTMLInputElement
if (element.checked) {
checked = [...checked, index]
}
})
if (checked.length > 0) {
await props.client.votePoll(props.poll.id, checked)
props.onRefresh()
}
}
return (
2023-11-04 10:14:00 +01:00
<div className={props.className + ' my-2'}>
2023-11-04 05:03:56 +01:00
{props.poll.options.map((option, index) => (
<div key={index} className="flex items-center gap-2 my-2 pl-1">
<Checkbox id={option.title} label={option.title} />
2023-11-04 05:03:56 +01:00
</div>
))}
<div className="flex gap-2 items-center mt-2">
<Button size="sm" onClick={vote} variant="outlined">
2023-11-04 07:32:37 +01:00
<FormattedMessage id="timeline.status.poll.vote" />
2023-11-04 05:03:56 +01:00
</Button>
2023-11-04 07:32:37 +01:00
<div>
<FormattedMessage id="timeline.status.poll.people" values={{ num: props.poll.votes_count }} />
</div>
2023-11-04 05:03:56 +01:00
<div>
<time dateTime={props.poll.expires_at}>{dayjs(props.poll.expires_at).format('YYYY-MM-DD HH:mm:ss')}</time>
</div>
</div>
</div>
)
}
function PollResult(props: Props) {
return (
2023-11-04 10:14:00 +01:00
<div className={props.className + ' my-2'}>
2023-11-04 05:03:56 +01:00
{props.poll.options.map((option, index) => (
<div key={index}>
<span className="pr-2">{percent(option.votes_count ?? 0, props.poll.votes_count)}%</span>
<span>{option.title}</span>
<Progress value={percent(option.votes_count ?? 0, props.poll.votes_count)} color="blue" />
2023-11-04 05:03:56 +01:00
</div>
))}
<div className="flex gap-2 items-center mt-2">
<Button size="sm" onClick={props.onRefresh} variant="outlined">
2023-11-04 07:32:37 +01:00
<FormattedMessage id="timeline.status.poll.refresh" />
2023-11-04 05:03:56 +01:00
</Button>
2023-11-04 07:32:37 +01:00
<div>
<FormattedMessage id="timeline.status.poll.people" values={{ num: props.poll.votes_count }} />
</div>
2023-11-04 05:03:56 +01:00
{props.poll.expired ? (
2023-11-04 07:32:37 +01:00
<div>
<FormattedMessage id="timeline.status.poll.closed" />
</div>
2023-11-04 05:03:56 +01:00
) : (
<div>
<time dateTime={props.poll.expires_at}>{dayjs(props.poll.expires_at).format('YYYY-MM-DD HH:mm:ss')}</time>
</div>
)}
</div>
</div>
)
}
const percent = (votes: number, all: number) => {
if (all > 0) {
return Math.round((votes * 100) / all)
} else {
return 0
}
}