-
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #43
- Loading branch information
Showing
5 changed files
with
159 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { PollStructure } from '../../types'; | ||
import * as S from './style'; | ||
|
||
interface IPoll { | ||
pollData: PollStructure; | ||
} | ||
|
||
function Poll({ pollData }: IPoll) { | ||
return ( | ||
<S.Poll> | ||
<S.Title>{pollData.title}</S.Title> | ||
{pollData.options.map(option => { | ||
return ( | ||
<S.Option key={option.text}> | ||
<div>{option.text}</div> | ||
<S.Flex> | ||
<S.Progress max={pollData.maxVotes} value={option.votes} /> | ||
<div>{option.votes}</div> | ||
</S.Flex> | ||
</S.Option> | ||
); | ||
})} | ||
</S.Poll> | ||
); | ||
} | ||
|
||
export default Poll; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import styled from 'styled-components'; | ||
import { | ||
activeUserDarkBackgroundColor, | ||
viewerDarkBackgroundColor, | ||
whatsappThemeColor, | ||
} from '../../utils/colors'; | ||
|
||
const Poll = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.6rem; | ||
`; | ||
|
||
const Title = styled.div` | ||
font-weight: bolder; | ||
`; | ||
|
||
const Option = styled.div` | ||
display: flex; | ||
flex-direction: column; | ||
gap: 0.2rem; | ||
`; | ||
|
||
const Flex = styled.div` | ||
display: flex; | ||
gap: 0.5rem; | ||
align-items: center; | ||
font-size: 0.8rem; | ||
`; | ||
|
||
const Progress = styled.progress` | ||
display: block; | ||
box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); | ||
width: 100%; | ||
max-width: 500px; | ||
height: 10px; | ||
appearance: none; | ||
border-radius: 99px; | ||
padding: 1px; | ||
&::-webkit-progress-bar { | ||
background-color: white; | ||
border-radius: 99px; | ||
} | ||
&::-webkit-progress-value { | ||
background-color: ${whatsappThemeColor}; | ||
border-radius: 99px; | ||
} | ||
@media (prefers-color-scheme: dark) { | ||
&::-webkit-progress-bar { | ||
background-color: color-mix( | ||
in srgb, | ||
${activeUserDarkBackgroundColor}, | ||
${viewerDarkBackgroundColor} | ||
); | ||
} | ||
} | ||
`; | ||
|
||
export { Poll, Title, Option, Flex, Progress }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { PollOption, PollStructure } from '../types'; | ||
|
||
function parsePollMessage(message: string): PollStructure | null { | ||
const lines = message | ||
.trim() | ||
.split('\n') | ||
.map(line => line.trim()); | ||
|
||
if (!lines[0].includes('POLL:') || !lines[2]?.includes('OPTION:')) | ||
return null; | ||
|
||
const optionRegex = /OPTION: (?<text>.+) \((?<votes>\d+).*\)/; | ||
|
||
const options = lines.slice(2).reduce<PollOption[]>((acc, line) => { | ||
const match = optionRegex.exec(line); | ||
|
||
if (!match?.groups) return acc; | ||
|
||
const { text, votes } = match.groups; | ||
|
||
return acc.concat({ text, votes: Number(votes) }); | ||
}, []); | ||
|
||
return { | ||
title: lines[1], | ||
options, | ||
maxVotes: Math.max(...options.map(o => o.votes)), | ||
}; | ||
} | ||
|
||
export { parsePollMessage }; |