|
| 1 | +/* eslint-disable complexity */import { Dispatch, FC, SetStateAction, useMemo, useState } from 'react' |
| 2 | +import { bind, isEmpty, keys } from 'lodash' |
| 3 | +import Highcharts from 'highcharts' |
| 4 | +import HighchartsReact from 'highcharts-react-official' |
| 5 | + |
| 6 | +import { BaseModal, Button, LoadingSpinner } from '~/libs/ui' |
| 7 | +import { |
| 8 | + MemberStats, |
| 9 | + ratingToCSScolor, |
| 10 | + StatsHistory, |
| 11 | + UserProfile, |
| 12 | + UserStatsDistributionResponse, |
| 13 | + UserStatsHistory, |
| 14 | + useStatsDistribution, |
| 15 | + useStatsHistory, |
| 16 | +} from '~/libs/core' |
| 17 | + |
| 18 | +import { numberToFixed } from '../../lib' |
| 19 | + |
| 20 | +import { RATING_CHART_CONFIG, RATING_DISTRO_CHART_CONFIG } from './chart-configs' |
| 21 | +import styles from './AssemblyDetailsModal.module.scss' |
| 22 | + |
| 23 | +type SRMViewTypes = 'STATISTICS' | 'CHALLENGES DETAILS' |
| 24 | + |
| 25 | +interface AssemblyDetailsModalProps { |
| 26 | + isAssemblyDetailsOpen: boolean |
| 27 | + onClose: () => void |
| 28 | + assemblyStats: MemberStats | undefined |
| 29 | + profile: UserProfile | undefined |
| 30 | +} |
| 31 | + |
| 32 | +const AssemblyDetailsModal: FC<AssemblyDetailsModalProps> = (props: AssemblyDetailsModalProps) => { |
| 33 | + const [viewType, setviewType]: [SRMViewTypes, Dispatch<SetStateAction<SRMViewTypes>>] |
| 34 | + = useState<SRMViewTypes>('STATISTICS') |
| 35 | + |
| 36 | + const statsHistory: UserStatsHistory | undefined = useStatsHistory(props.profile?.handle) |
| 37 | + |
| 38 | + const ratingHistoryOptions: Highcharts.Options | undefined = useMemo(() => { |
| 39 | + const assemblyHistory: Array<StatsHistory> | undefined |
| 40 | + = statsHistory?.DEVELOP?.subTracks?.find(subTrack => subTrack.name === 'ASSEMBLY_COMPETITION')?.history |
| 41 | + || [] |
| 42 | + const options: Highcharts.Options = RATING_CHART_CONFIG |
| 43 | + |
| 44 | + if (!assemblyHistory.length) return undefined |
| 45 | + |
| 46 | + options.series = [{ |
| 47 | + data: assemblyHistory.sort((a, b) => b.date - a.date) |
| 48 | + .map((assemblyChallenge: StatsHistory) => ({ |
| 49 | + name: assemblyChallenge.challengeName, |
| 50 | + x: assemblyChallenge.ratingDate, |
| 51 | + y: assemblyChallenge.newRating, |
| 52 | + })), |
| 53 | + name: 'Assembly Competition Rating', |
| 54 | + type: 'spline', |
| 55 | + }] |
| 56 | + |
| 57 | + return options |
| 58 | + }, [statsHistory]) |
| 59 | + |
| 60 | + const memberStatsDist: UserStatsDistributionResponse | undefined = useStatsDistribution({ |
| 61 | + filter: 'track=DEVELOP&subTrack=ASSEMBLY_COMPETITION', |
| 62 | + }) |
| 63 | + |
| 64 | + const ratingDistributionOptions: Highcharts.Options | undefined = useMemo(() => { |
| 65 | + const ratingDistro: { [key: string]: number } = memberStatsDist?.distribution || {} |
| 66 | + const options: Highcharts.Options = RATING_DISTRO_CHART_CONFIG |
| 67 | + |
| 68 | + if (isEmpty(ratingDistro)) return undefined |
| 69 | + |
| 70 | + options.series = keys(ratingDistro) |
| 71 | + .map((key: string) => ({ |
| 72 | + data: [ratingDistro[key]], |
| 73 | + name: key.split('ratingRange')[1], |
| 74 | + type: 'column', |
| 75 | + })) |
| 76 | + |
| 77 | + return options |
| 78 | + }, [memberStatsDist]) |
| 79 | + |
| 80 | + function toggleViewType(newViewType: SRMViewTypes): void { |
| 81 | + setviewType(newViewType) |
| 82 | + } |
| 83 | + |
| 84 | + return ( |
| 85 | + <BaseModal |
| 86 | + onClose={props.onClose} |
| 87 | + open={props.isAssemblyDetailsOpen} |
| 88 | + size='body' |
| 89 | + title='ASSEMBLY COMPETITION' |
| 90 | + > |
| 91 | + <LoadingSpinner hide={!!statsHistory && !!memberStatsDist} /> |
| 92 | + |
| 93 | + {!!statsHistory && !!memberStatsDist && ( |
| 94 | + <div className={styles.container}> |
| 95 | + <div className='member-stat-header'> |
| 96 | + <div> |
| 97 | + <span |
| 98 | + className='member-stat-value' |
| 99 | + style={ratingToCSScolor(props.assemblyStats?.rank.rating || 0)} |
| 100 | + > |
| 101 | + {props.assemblyStats?.rank.rating} |
| 102 | + </span> |
| 103 | + Rating |
| 104 | + </div> |
| 105 | + <div> |
| 106 | + <span className='member-stat-value'>{props.assemblyStats?.rank.overallRank}</span> |
| 107 | + Rank |
| 108 | + </div> |
| 109 | + <div> |
| 110 | + <span className='member-stat-value'> |
| 111 | + {numberToFixed(props.assemblyStats?.rank.overallPercentile || 0)} |
| 112 | + % |
| 113 | + </span> |
| 114 | + Percentile |
| 115 | + </div> |
| 116 | + <div> |
| 117 | + <span className='member-stat-value'>{props.assemblyStats?.wins}</span> |
| 118 | + Wins |
| 119 | + </div> |
| 120 | + <div> |
| 121 | + <span className='member-stat-value'>{props.assemblyStats?.challenges}</span> |
| 122 | + Challenges |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + |
| 126 | + <div className={styles.content}> |
| 127 | + <div className={styles.contentHeader}> |
| 128 | + <h4>{viewType}</h4> |
| 129 | + <div className={styles.contentHeaderActions}> |
| 130 | + <Button |
| 131 | + primary |
| 132 | + onClick={bind( |
| 133 | + toggleViewType, |
| 134 | + this, |
| 135 | + viewType !== 'CHALLENGES DETAILS' ? 'CHALLENGES DETAILS' : 'STATISTICS', |
| 136 | + )} |
| 137 | + > |
| 138 | + See |
| 139 | + {' '} |
| 140 | + {viewType !== 'CHALLENGES DETAILS' ? 'CHALLENGES DETAILS' : 'STATISTICS'} |
| 141 | + </Button> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + |
| 145 | + <div className={styles.contentBody}> |
| 146 | + { |
| 147 | + viewType === 'STATISTICS' && ( |
| 148 | + <div> |
| 149 | + { |
| 150 | + ratingHistoryOptions && ( |
| 151 | + <HighchartsReact |
| 152 | + highcharts={Highcharts} |
| 153 | + options={ratingHistoryOptions} |
| 154 | + /> |
| 155 | + ) |
| 156 | + } |
| 157 | + { |
| 158 | + ratingDistributionOptions && ( |
| 159 | + <HighchartsReact |
| 160 | + highcharts={Highcharts} |
| 161 | + options={ratingDistributionOptions} |
| 162 | + /> |
| 163 | + ) |
| 164 | + } |
| 165 | + </div> |
| 166 | + ) |
| 167 | + |
| 168 | + } |
| 169 | + { |
| 170 | + viewType === 'CHALLENGES DETAILS' && ( |
| 171 | + <div /> |
| 172 | + ) |
| 173 | + |
| 174 | + } |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + </div> |
| 178 | + )} |
| 179 | + </BaseModal> |
| 180 | + ) |
| 181 | +} |
| 182 | + |
| 183 | +export default AssemblyDetailsModal |
0 commit comments