1 import PropTypes from 'prop-types';
2 import React from 'react';
3 import { Button, Table } from 'react-bootstrap';
4 import { useTranslation } from 'react-i18next';
6 import Icon from '../common/Icon';
7 import Box from '../users/Box';
8 import { comparePlacement } from '../../helpers/Participant';
9 import { getRunners } from '../../helpers/Tournament';
10 import { useUser } from '../../hooks/user';
12 const getRowClassName = (tournament, participant, user) => {
13 const classNames = ['score'];
14 if (participant && user && participant.user_id == user.id) {
15 classNames.push('is-self');
17 return classNames.join(' ');
20 const getPlacementDisplay = participant => {
21 if (participant.placement === 1) {
22 return <Icon.FIRST_PLACE className="text-gold" size="lg" />;
24 if (participant.placement === 2) {
25 return <Icon.SECOND_PLACE className="text-silver" size="lg" />;
27 if (participant.placement === 3) {
28 return <Icon.THIRD_PLACE className="text-bronze" size="lg" />;
30 return participant.placement;
33 const twitchReg = /^https?:\/\/(www\.)?twitch\.tv/;
34 const youtubeReg = /^https?:\/\/(www\.)?youtu(\.be|be\.)/;
36 const getStreamVariant = participant => {
37 if (!participant || !participant.user || !participant.user.stream_link) {
38 return 'outline-secondary';
40 if (twitchReg.test(participant.user.stream_link)) {
41 return 'outline-twitch';
43 if (youtubeReg.test(participant.user.stream_link)) {
44 return 'outline-youtube';
46 return 'outline-secondary';
49 const getStreamIcon = participant => {
50 const variant = getStreamVariant(participant);
51 if (variant === 'outline-twitch') {
52 return <Icon.TWITCH title="" />;
54 if (variant === 'outline-youtube') {
55 return <Icon.YOUTUBE title="" />;
57 return <Icon.VIDEO title="" />;
60 const Scoreboard = ({ tournament }) => {
61 const { t } = useTranslation();
62 const { user } = useUser();
64 return <Table striped className="scoreboard align-middle">
67 <th className="text-center">{t('participants.placementShort')}</th>
68 <th>{t('participants.participant')}</th>
69 <th className="text-end">{t('participants.scoreShort')}</th>
73 {getRunners(tournament).sort(comparePlacement).map(participant =>
74 <tr className={getRowClassName(tournament, participant, user)} key={participant.id}>
75 <td className="text-center">
76 {getPlacementDisplay(participant)}
79 <div className="d-flex align-items-center justify-content-between">
80 <Box user={participant.user} />
81 {participant.user.stream_link ?
83 href={participant.user.stream_link}
86 title={t('users.stream')}
87 variant={getStreamVariant(participant)}
89 {getStreamIcon(participant)}
94 <td className="text-end">{participant.score}</td>
101 Scoreboard.propTypes = {
102 tournament: PropTypes.shape({
106 export default Scoreboard;