-
Notifications
You must be signed in to change notification settings - Fork 292
Expand file tree
/
Copy pathPollMetadata.tsx
More file actions
63 lines (58 loc) · 1.57 KB
/
PollMetadata.tsx
File metadata and controls
63 lines (58 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react';
import isAfter from 'date-fns/isAfter';
import {
Typography,
TypographyColor,
TypographyTag,
TypographyType,
} from '../../typography/Typography';
import { Separator } from './common';
import type { Post } from '../../../graphql/posts';
import { largeNumberFormat } from '../../../lib';
const MIN_VOTES_REQUIRED = 10;
export type PollMetadataProps = Pick<Post, 'endsAt' | 'numPollVotes'> & {
isAuthor: boolean;
};
const PollMetadata = ({
endsAt,
isAuthor,
numPollVotes,
}: PollMetadataProps) => {
const votes = numPollVotes ?? 0;
const shouldShowVotes = votes > MIN_VOTES_REQUIRED || isAuthor;
const pollHasEnded = endsAt && isAfter(new Date(), new Date(endsAt));
return (
<>
{pollHasEnded && (
<>
<Typography tag={TypographyTag.Span} type={TypographyType.Footnote}>
Voting ended
</Typography>
<Separator />
</>
)}
{!pollHasEnded && (
<>
<Typography
tag={TypographyTag.Span}
type={TypographyType.Footnote}
color={TypographyColor.StatusSuccess}
>
{shouldShowVotes ? 'Voting open' : 'New poll'}
</Typography>
<Separator />
</>
)}
{shouldShowVotes && (
<>
<Typography tag={TypographyTag.Span} type={TypographyType.Footnote}>
{largeNumberFormat(votes)}{' '}
{pollHasEnded ? 'total votes' : 'votes'}
</Typography>
<Separator />
</>
)}
</>
);
};
export default PollMetadata;