|
| 1 | +import { FC } from 'react'; |
| 2 | +import { BadgeIcon } from '@/components/badge-icon/badge-icon'; |
| 3 | +import { figmaVariables } from '@/badge/utils/figma-variables-mapping'; |
| 4 | +import { BadgeMediumProps } from './medium.types'; |
| 5 | +import { getRankByType } from '@/badge/utils/get-rank-by-type'; |
| 6 | +import { getTitleByType } from '@/badge/utils/get-title-by-type'; |
| 7 | +import { BadgeType } from '@/badge/badge.types'; |
| 8 | +import { containerStyles, metaItemStyles, rankDeltaStyles, rankStyles, subtitleStyles } from './medium.styles'; |
| 9 | +import { getUsersBehindMe } from '@/badge/utils/users-behind-me'; |
| 10 | + |
| 11 | +const getSubtitleByType = (type: BadgeType) => { |
| 12 | + switch (type) { |
| 13 | + case 'stars': |
| 14 | + return 'based on stars from repos owned by the user'; |
| 15 | + case 'contributions': |
| 16 | + return 'based on stars from repos the user contributed to'; |
| 17 | + case 'followers': |
| 18 | + return 'based on the number of followers the user has'; |
| 19 | + } |
| 20 | +}; |
| 21 | + |
| 22 | +export const BadgeMedium: FC<BadgeMediumProps> = ({ theme, type, data }) => { |
| 23 | + const { colors } = figmaVariables[theme]; |
| 24 | + |
| 25 | + const { rank, delta = 0, value, sentiment } = getRankByType(data, type); |
| 26 | + const title = getTitleByType(type); |
| 27 | + const subtitle = getSubtitleByType(type); |
| 28 | + |
| 29 | + if (!rank || !sentiment) { |
| 30 | + return null; |
| 31 | + } |
| 32 | + |
| 33 | + const entityName = type === 'followers' ? 'followers' : 'stars'; |
| 34 | + |
| 35 | + return ( |
| 36 | + <div |
| 37 | + style={{ |
| 38 | + ...containerStyles, |
| 39 | + color: colors.text.primary, |
| 40 | + backgroundColor: colors.surface.primary, |
| 41 | + }} |
| 42 | + > |
| 43 | + <div style={{ display: 'flex', gap: 12 }}> |
| 44 | + <BadgeIcon type={type} size={32} /> |
| 45 | + <div style={{ display: 'flex', flexDirection: 'column', flexGrow: 1, justifyContent: 'center' }}> |
| 46 | + <div style={{ fontSize: 16, fontWeight: 600 }}>{title}</div> |
| 47 | + <div style={subtitleStyles}>{subtitle}</div> |
| 48 | + </div> |
| 49 | + </div> |
| 50 | + |
| 51 | + <div style={rankStyles}>{rank.toLocaleString('en-US')}</div> |
| 52 | + |
| 53 | + <div style={{ display: 'flex', width: '100%' }}> |
| 54 | + <div style={{ ...metaItemStyles, borderRight: `1px solid ${colors.text.primary}` }}> |
| 55 | + <div style={subtitleStyles}>better than</div> |
| 56 | + <div style={{ fontSize: 14, fontWeight: 600 }}>{getUsersBehindMe(rank)}</div> |
| 57 | + </div> |
| 58 | + <div style={{ ...metaItemStyles, borderRight: `1px solid ${colors.text.primary}` }}> |
| 59 | + <div style={subtitleStyles}>this month</div> |
| 60 | + <div style={{ ...rankDeltaStyles, color: delta ? colors.text[sentiment] : colors.text.primary }}> |
| 61 | + {delta > 0 ? '+' : ''} |
| 62 | + {delta.toLocaleString('en-US')} |
| 63 | + </div> |
| 64 | + </div> |
| 65 | + <div style={metaItemStyles}> |
| 66 | + <div style={subtitleStyles}>total {entityName}</div> |
| 67 | + <div style={{ display: 'flex', fontSize: 14, fontWeight: 600 }}> |
| 68 | + {(!value ? 0 : value).toLocaleString('en-US')} |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + </div> |
| 73 | + ); |
| 74 | +}; |
0 commit comments